cargo: `$CARGO` passed to external subcommands can point to the wrong `cargo` if already set
Problem
Observed in https://github.com/rust-lang/rust-clippy/issues/14045, if cargo +nightly clippy is ran by a process with CARGO pointing to the stable cargo then nightly clippy-driver will be ran by stable cargo
In this case that resulted in a confusing warning
Steps
-
Put the following into your
PATHascargo-foo#!/usr/bin/env bash echo "$CARGO" -
With stable cargo use
cargo runonuse std::process::Command; fn main() { Command::new("cargo") .args(["+nightly", "foo"]) .status() .unwrap(); } -
The output will be
.../.rustup/toolchains/stable-.../bin/cargo
Possible Solution(s)
Looks to be due to https://github.com/rust-lang/cargo/pull/11285, perhaps there could be an exception for when the current exe really is cargo
Notes
No response
Version
cargo 1.84.0 (66221abde 2024-11-19)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 29 (28 by maintainers)
So I’m working to catch up on this.
It sounds like there are the following care abouts
CARGOCARGO(#10119)CURRENT_EXE(among other things) (#10113)#11285 broke the (1) to fix (2) and (3). It did so by setting the precedence in
GlobalContexttofrom_envfrom_current_exefrom_argvI’m uncomfortable with a rustup solution because this isn’t inherently a rustup problem This can happen in other cases like cargo script invoked via rustup spawning the
debbuild process which spawns Debian-builtcargowhich spawnsCARGO, wanting Debian’s cargo and not rustups.In the Cargo team meeting, someone brought up users manually unsetting
CARGObut I worry about how error prone that is and how difficult to debug.Building off of the idea in https://github.com/rust-lang/cargo/issues/15099#issuecomment-2614462173, my proposed solution is that cargo-the-bin opts-in to different behavior than the default.
default-behavior (when using cargo-the-library):
from_envfrom_current_exefrom_argvopt-in behavior (set by cargo-the-bin):
from_current_exeif itscargo[EXE_SUFFIX]from_argvif itscargo[EXE_SUFFIX]from_envfrom_current_exefrom_argvCharacteristics
CARGO=ldldbut at least we still workcargo@smoelius Yes, I think that’ll be the most appropriate way forward.
Does anyone else have an opinion?
@rami3l I’m going to wait at least 24 hours before modifying the PR.
@rami3l Do you think we should issue a warning but not clear the environment variable (3 above)?
@smoelius Thanks for your contribution! Adding a warning is definitely worthwhile. However, I’m still not sure if we should make
cargo runthat transparent.I don’t expect
cargo runto work identically to a Shell script like you have mentioned in https://github.com/rust-lang/cargo/issues/15099#issuecomment-2622053174 in the first place:… at least you won’t be able to
cargo runwhen developing rustup, for example, exactly because extra env vars are being added to the execution process. A proper comparison of your “Shell script” would be directly running the resulting executable aftercargo build(that’s what we are currently doing with rustup) or something similar 🤔In other words, you could imagine if somehow
cargo runis reprogrammed to run your “Shell script”, say you’ve replaced the resulting executable with that script, then you would probably encounter the same issue.The original case that ran into it was along the lines of
cargo runningWhich seems like a perfectly fine use case, if it were instead written as a shell script it would work as expected. Similarly the equivalent but with
cargo checkwould work as expected@smoelius Thanks a lot for your clarification! This looks like a cargo-calling-rustup issue so indeed it’s specific to rustup.
And, from my perspective, it looks like rustup needs to start following some external convention. If it’s already established somewhere and our not responding (such as sending a warning instead of actually doing anything) is not an option, I guess we would have to add more magic (such as wiping $CARGO) following the current direction, because, as a user, I have no idea why calling
cargoincargo runwould make things difficult forrustup, so I expect everything to “just work” as ifcargo runwere not setting $CARGO at all.@weihanglo This is a bit off-topic, but recursive calls are also exactly why it’s so difficult for me to add a proper lock to rustup in https://github.com/rust-lang/rustup/issues/988, where a common locking scheme can also be broken by considering
rustupcalls incargo run. Given all this, I’m afraid to open a can of worms trying to fix all the edge cases related to recursive calls while keepingrustupmostly transparent to our end users. Maybe we should be less transparent and take @djc’s approach, concluding that it’s up to the user to prevent such complexities?