Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--out-dir but for cargo test #11110

Closed
stevenhansel opened this issue Sep 19, 2022 · 7 comments
Closed

--out-dir but for cargo test #11110

stevenhansel opened this issue Sep 19, 2022 · 7 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@stevenhansel
Copy link

Problem

I'm glad that the --out-dir is out for the cargo build command, but is it possible to also include this feature in cargo test? The use case for me is to run the binary containing the tests under the debugger.

Proposed Solution

Adding--out-dir flag in cargo test CLI

Notes

No response

@stevenhansel stevenhansel added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Sep 19, 2022
@ehuss
Copy link
Contributor

ehuss commented Sep 19, 2022

--out-dir is intended for collecting final artifacts. I wouldn't consider tests to be part of the final artifacts.

Looking at the other issue, it seems like you want to get access to the executables for running under a debugger? The intended path for that is to run cargo -test --no-run, and then launch the debugger with the path to the executable that is printed by cargo.

@stevenhansel
Copy link
Author

stevenhansel commented Sep 20, 2022

--out-dir is intended for collecting final artifacts. I wouldn't consider tests to be part of the final artifacts.

Looking at the other issue, it seems like you want to get access to the executables for running under a debugger? The intended path for that is to run cargo -test --no-run, and then launch the debugger with the path to the executable that is printed by cargo.

❯ cargo test build
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests (target/debug/deps/temp2-8fe0671d643594b3)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s

❯ cargo test --no-run
    Finished test [unoptimized + debuginfo] target(s) in 0.00s

I think the command that you meant is cargo test build. Anyway, I don't know quite the norm yet, but what I want to do is have the executable under a specific folder (i want to put it at ~/.local/nvim/cargo) that contains all the executables for debugging. I already implemented this for cargo +nightly build --out-dir <directory>, so now I need the same command but for the test command.

With all respect, regardless whether tests are final artifacts or not. Cargo already has a command to build tests, cargo test build. Isn't that enough of a reason to be able to specify where do I want to output the executables?

@ehuss
Copy link
Contributor

ehuss commented Sep 20, 2022

cargo test build isn't a specific command. The arguments after cargo test are the names of tests to run. For example, cargo test some_test_name is how that is normally used. If the test name doesn't exist, then it runs the test binary, but then filters all the tests out.

With --no-run, you should see the name of the test executable:

~/Temp/foo> cargo test --no-run
   Compiling foo v0.1.0 (/Users/eric/Temp/foo)
    Finished test [unoptimized + debuginfo] target(s) in 1.02s
  Executable unittests src/main.rs (target/debug/deps/foo-418fa8594e53bda1)

--out-dir is currently not on a track to be stabilized, and its future is uncertain. I suspect that it will be unlikely or difficult to make tests participate in out-dir, since they have various links to executable paths (such as through environment variables) and such to support integration tests. They also don't have a practical naming convention that allows them to be identified.

Can you say more about why you need the executables placed in a separate directory? Are there problems with the workflow of running them via the target directory?

@stevenhansel
Copy link
Author

Thank you for correcting me. I was randomly following some answers on StackOverflow without checking the cargo's documentation. I'm quite sorry for making an argument based on an assumption.

Can you say more about why you need the executables placed in a separate directory? Are there problems with the workflow of running them via the target directory?

I'm planning to write some sort of a neovim plugin to improve my debugging workflow with Rust. Right now my workflow is to compile first to get the executable and run the debugger based on the just-now compiled executable. I want to improve the flow so that I don't have to manually compile the executable myself. I haven't looked up on this, but I don't know whether the path to the compiled executable will always be the same for every cargo / Rust project which is usually <root>/target/debug/<test_file>, but I thought it would way be easier if I can just move every executable to a single directory. Hence my thought process.

Because of the future of --out-dir itself is quite uncertain, I will close this issue if you recommend it. Thanks!

@epage
Copy link
Contributor

epage commented Sep 20, 2022

btw there are crates like escargot that allow you to programmatically call into cargo and get the generated binaries

See example

@weihanglo
Copy link
Member

If you can access programming interface, such like developing a plugin for neovim, I bet that you can leverage JSON format output of Cargo (--message-format=json).

I integrate nvim-dap into my debugging workflow, and here is how I use JSON format. (Warning: I seldom use a debugger for Rust, so this config might be outdated)

@stevenhansel
Copy link
Author

I'm going to close this issue because there are already multiple alternatives to the problem that I was facing and also because the original feature is also still unstable. Thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

4 participants