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

env var for original invocation directory #8148

Open
ijackson opened this issue Apr 23, 2020 · 1 comment
Open

env var for original invocation directory #8148

ijackson opened this issue Apr 23, 2020 · 1 comment
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.

Comments

@ijackson
Copy link
Contributor

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.

@epage
Copy link
Contributor

epage commented Nov 4, 2023

If this is about finding the same cargo as your being run by, wouldn't using CARGO be more appropriate?

@epage epage added the S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. label Nov 4, 2023
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` S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

2 participants