env var for original invocation directory #8148
Labels
C-feature-request
Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
S-needs-info
Status: Needs more info, such as a reproduction or more background for a feature request.
Describe the problem you are trying to solve
Much crate-supplied code runs within cargo. For example, build.rs, and tests. cargo likes to change to the directory containing the Cargo.toml. I'm sure there are good reasons for that and changing it would probably be a big job. However, this causes some problems in out-of-tree builds, ie where
--manifest-path
is supplied.For example, I was working on cargo_metadata. cargo_metadata's tests run cargo, obviously. The copy of cargo run by cargo_metadata starts in the source directory. I am doing an out of tree build, so that is a completely different directory. The nested copy of cargo then walks up from the directory containing the source code to find a cargo configuration. The cargo configuration it finds is entirely inappropriate and breaks the test.
Ie the nested copy of cargo is getting a completely different cargo configuration to the top-level cargo which was told to
cargo test
.The best fix for this would be to have cargo_metadata's test chdir back to the original directory. It would then find the same cargo configuration as the copy of cargo which was invoked to run the tests, which would be correct. However, the cargo_metadata test does not know the original invocation directory and it doesn't seem that there is any reliable way to find it.
This kind of situation would arise in any crate which wants to run cargo as part of its tests. There may be other kind of circumstances where finding the original invocation directory would be helpful. In general having the build system provide more information makes it more likely that code that runs under it can do the right thing in a wider variety of situations.
Describe the solution you'd like
The simplest answer is to provide an environment variable. cargo already provides several of these containing various other useful information. I suggest
CARGO_INVOCATION_PWD
but obviously other names will do.
Notes
If this seems like it would be useful I will prepare a MR.
The text was updated successfully, but these errors were encountered: