-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
CARGO is not Cargo when build script is invoked through cargo used as a library #10119
Comments
It could be that one significant improvement here is to make the logic that sets |
With #10115 closed (which I think is reasonable), I think it's even more important to figure out a solution to this, ideally using a single mechanism to address both cases. |
To leave breadcrumbs to related issues, a very specific (but also slightly different) instantiation of this problem is #10113. Ideally a solution addresses both use-cases. |
Also, just for convenience for readers of this issue, the place where Cargo discovers the current Cargo executable is cargo/src/cargo/util/config/mod.rs Line 406 in 3ff0443
It's used here for external authentication commands: cargo/src/cargo/ops/registry/auth.rs Line 126 in f955ddc
Here for build scripts: cargo/src/cargo/core/compiler/compilation.rs Line 316 in 1ac43cf
And here for fingerprinting: cargo/src/cargo/core/compiler/fingerprint.rs Line 728 in 25c5ced
|
I also just realized that Cargo already sets Lines 193 to 195 in c71f344
So I think there's a reasonable argument for saying that Cargo should prefer re-using an existing value for |
Currently, Cargo will always set `$CARGO` to point to what it detects its own path to be (using `std::env::current_exe`). Unfortunately, this runs into trouble when Cargo is used as a library, or when `current_exe` is not actually the binary itself (e.g., when invoked through Valgrind or `ld.so`), since `$CARGO` will not point at something that can be used as `cargo`. This, in turn, means that users can't currently rely on `$CARGO` to do the right thing, and will sometimes have to invoke `cargo` directly from `$PATH` instead, which may not reflect the `cargo` that's currently in use. This patch makes Cargo re-use the existing value of `$CARGO` if it's already set in the environment. For Cargo subcommands, this will mean that the initial invocation of `cargo` in `cargo foo` will set `$CARGO`, and then Cargo-as-a-library inside of `cargo-foo` will inherit that (correct) value instead of overwriting it with the incorrect value `cargo-foo`. For other execution environments that do not have `cargo` in their call stack, it gives them the opportunity to set a working value for `$CARGO`. One note about the implementation of this is that the test suite now needs to override `$CARGO` explicitly so that the _user's_ `$CARGO` does not interfere with the contents of the tests. It _could_ remove `$CARGO` instead, but overriding it seemed less error-prone. Fixes rust-lang#10119. Fixes rust-lang#10113.
I filed #11285 to fix this by forwarding |
Make cargo forward pre-existing CARGO if set Currently, Cargo will always set `$CARGO` to point to what it detects its own path to be (using `std::env::current_exe`). Unfortunately, this runs into trouble when Cargo is used as a library, or when `current_exe` is not actually the binary itself (e.g., when invoked through Valgrind or `ld.so`), since `$CARGO` will not point at something that can be used as `cargo`. This, in turn, means that users can't currently rely on `$CARGO` to do the right thing, and will sometimes have to invoke `cargo` directly from `$PATH` instead, which may not reflect the `cargo` that's currently in use. This patch makes Cargo re-use the existing value of `$CARGO` if it's already set in the environment. For Cargo subcommands, this will mean that the initial invocation of `cargo` in `cargo foo` will set `$CARGO`, and then Cargo-as-a-library inside of `cargo-foo` will inherit that (correct) value instead of overwriting it with the incorrect value `cargo-foo`. For other execution environments that do not have `cargo` in their call stack, it gives them the opportunity to set a working value for `$CARGO`. One note about the implementation of this is that the test suite now needs to override `$CARGO` explicitly so that the _user's_ `$CARGO` does not interfere with the contents of the tests. It _could_ remove `$CARGO` instead, but overriding it seemed less error-prone. Fixes #10119. Fixes #10113.
Problem
When a program using cargo as a library initiates a build, the
CARGO
environment variable to build scripts is set to be the path to the current executable, even though that command may look nothing like Cargo. This, in turn, means that build scripts invoked as part as such builds may fail if they try to useCARGO
as though it were Cargo, like for examplecbindgen
does:https://github.com/eqrion/cbindgen/blob/93c06c5c9d319f481788c9670700097b4e46d270/src/bindgen/cargo/cargo_metadata.rs#L233-L235
Steps
Yields
Note that a command like
$CARGO metadata
(likecbindgen
executes) would fail in this case, sincecargo-something != cargo
.Possible Solution(s)
This is a tough one. In the particular case where the binary invoking a build through cargo-as-a-library is a cargo external subcommand, perhaps the initiating
cargo
invocation could setCARGO
(by some other name) that then gets picked up transparently by the build logic. But in the more general case, it seems like this needs to be settable throughcargo::Config
somewhere so that library users at least have a way to correctCARGO
if they do invoke a build.Notes
No response
Version
The text was updated successfully, but these errors were encountered: