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

  1. Put the following into your PATH as cargo-foo

    #!/usr/bin/env bash
    
    echo "$CARGO"
    
  2. With stable cargo use cargo run on

    use std::process::Command;
    
    fn main() {
        Command::new("cargo")
            .args(["+nightly", "foo"])
            .status()
            .unwrap();
    }
    
  3. 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)

Commits related to this issue

Most upvoted comments

So I’m working to catch up on this.

It sounds like there are the following care abouts

  1. Subprocesses of Cargo should inherit the same toolchain as the their parent Cargo process via CARGO
  2. Bins using cargo-as-a-library should not be put in CARGO (#10119)
  3. Ld can mess with CURRENT_EXE (among other things) (#10113)

#11285 broke the (1) to fix (2) and (3). It did so by setting the precedence in GlobalContext to

  1. from_env
  2. from_current_exe
  3. from_argv

I’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 deb build process which spawns Debian-built cargo which spawns CARGO, wanting Debian’s cargo and not rustups.

In the Cargo team meeting, someone brought up users manually unsetting CARGO but 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):

  1. from_env
  2. from_current_exe
  3. from_argv

opt-in behavior (set by cargo-the-bin):

  1. from_current_exe if its cargo[EXE_SUFFIX]
  2. from_argv if its cargo[EXE_SUFFIX]
  3. from_env
  4. from_current_exe
  5. from_argv

Characteristics

  • No change in behavior for cargo-the-library
  • We don’t try set CARGO=ld
  • We don’t fix this issue for ld but at least we still work
  • We don’t fix this issue for when users have an alternative name for the Cargo binary (or on some platforms, an alternative symlink name)
  • We fix this issue for regular use of cargo

@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.

@smoelius Thanks for your contribution! Adding a warning is definitely worthwhile. However, I’m still not sure if we should make cargo run that transparent.

@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 run that transparent.

I don’t expect cargo run to 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:

Which seems like a perfectly fine use case, if it were instead written as a shell script it would work as expected.

… at least you won’t be able to cargo run when 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 after cargo build (that’s what we are currently doing with rustup) or something similar 🤔

In other words, you could imagine if somehow cargo run is reprogrammed to run your “Shell script”, say you’ve replaced the resulting executable with that script, then you would probably encounter the same issue.

I suppose the question is if there are good use cases for having a CARGO set which you want to override using the +toolchain

The original case that ran into it was along the lines of cargo running

fn main() {
    cmd!("cargo clippy");
    cmd!("cargo +nightly clippy");
}

Which 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 check would work as expected

fn main() {
    cmd!("cargo check");
    cmd!("cargo +nightly check");
}

@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 cargo in cargo run would make things difficult for rustup, so I expect everything to “just work” as if cargo run were 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 rustup calls in cargo 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 keeping rustup mostly 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?