-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
A-custom-subcommandsArea: custom 3rd party subcommand pluginsArea: custom 3rd party subcommand pluginsA-environment-variablesArea: environment variablesArea: environment variablesC-bugCategory: bugCategory: bugS-needs-team-inputStatus: Needs input from team on whether/how to proceed.Status: Needs input from team on whether/how to proceed.
Description
Problem
Observed in rust-lang/rust-clippy#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
PATH
ascargo-foo
#!/usr/bin/env bash echo "$CARGO"
-
With stable cargo use
cargo run
onuse 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 #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)
BrendanChou
Metadata
Metadata
Assignees
Labels
A-custom-subcommandsArea: custom 3rd party subcommand pluginsArea: custom 3rd party subcommand pluginsA-environment-variablesArea: environment variablesArea: environment variablesC-bugCategory: bugCategory: bugS-needs-team-inputStatus: Needs input from team on whether/how to proceed.Status: Needs input from team on whether/how to proceed.
Activity
weihanglo commentedon Jan 26, 2025
How to achieve that? I mean isn't it be like a revert of #11285?
weihanglo commentedon Jan 26, 2025
Feel like one remote cause is the
cargo-clippy
is, while "official", still an external command, so need to rely on$CARGO
.Alexendoo commentedon Jan 26, 2025
Looking at just #10119 Cargo the binary could set a flag that disables the forwarding, that would keep #11285 working for commands that use Cargo as a library
For #10113 though I'm not sure
smoelius commentedon Jan 26, 2025
If one invokes
cargo +nightly <anything>
, it's pretty clear one wants to run thecargo
proxy because of the+nightly
.Are there cases where one would want the
cargo
proxy to preserve theCARGO
environment variable? I'm wondering if the proxy should clear it.If I'm reading #10119 right, that issue is about using Cargo as a library. So I don't think clearing the
CARGO
environment variable in the proxy would impact that issue's fix.smoelius commentedon Jan 28, 2025
I'm going to boldly assume #15099 (comment) is not crazy and cc some rustup maintainers. @ChrisDenton @djc @rami3l
My question comes down to: should the
cargo
proxy clear theCARGO
environment variable? Or are there legitimate reasons for the proxy to preserve the environment variable?For completeness, @Alexendoo poses a different solution in #15099 (comment).
rami3l commentedon Jan 29, 2025
@smoelius Hmmm there doesn't seem to be any mention of
$CARGO
in our codebase so I might assume it's currently not handled at all. Where is that variable usually read then? Maybe it's better to handle this logic over there if possible? (Otherwise I imagine it'd be difficult for rustup-less setups.)djc commentedon Jan 29, 2025
I think we should fail the invocation if the
+
-specified toolchain and$CARGO
don't agree -- seems like PEBKAC to me.smoelius commentedon Jan 29, 2025
@rami3l
That the variable is not handled at all (by the proxy) is my takeaway as well.
It looks like it is read by many tools: https://github.com/search?q=var%28%22CARGO%22%29&type=code
This specific issue arose because Clippy reads the variable: https://github.com/rust-lang/rust-clippy/blob/e02c8857e83e9113c29e8bd2b429f62dfaba04a7/src/main.rs#L110
Please see my response to djc below.
I may be misunderstanding what you mean. But I think the issue is specifically about
cargo +<toolchain>
invocations, which impliesrustup
setups.@djc
I think failing the invocation would be better than the current situation (i.e., allowing the command to proceed with the wrong
cargo
).In fairness, the problem is subtle. As @Alexendoo explains in rust-lang/rust-clippy#14045 (comment):
cargo
proxy runs, selects the defaultcargo
, and sets theCARGO
environment variable to default toolchain'scargo
.cargo
invocations runs some other code, which invokescargo +<toolchain> <subcommand>
.<subcommand>
reads theCARGO
environment variable and runs the default toolchain'scargo
rather than<toolchain>
'scargo
.@rami3l Your question ("Maybe it's better to handle this logic over there if possible?") is fair, and may be the right answer. But please notice that
<subcommand>
above could be just about anything.Thank you both for your responses. It looks like there are currently two options:
CARGO
environment variable in the proxy.+
-specified toolchain and$CARGO
don't agree.Are there other options? Do folks have strong opinions regarding these two?
djc commentedon Jan 29, 2025
I suppose the question is if there are good use cases for having a
CARGO
set which you want to override using the+toolchain
. Having rustup decide to prioritize the+toolchain
over the environment-specified one feels a little opinionated which I tend to dislike, but on the other hand explicit overrides taking priority over environment-specified ones is usually sane.rami3l commentedon Jan 29, 2025
@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
incargo run
would make things difficult forrustup
, so I expect everything to "just work" as ifcargo 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 rust-lang/rustup#988, where a common locking scheme can also be broken by considering
rustup
calls 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 keepingrustup
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?14 remaining items
smoelius commentedon Feb 5, 2025
What if we only emitted a warning, but the message said something like:
Would that be acceptable to everyone?
smoelius commentedon Feb 6, 2025
I updated the PR, though I softened the language from "will be cleared" to "may be cleared".
epage commentedon Feb 18, 2025
So I'm working to catch up on this.
It sounds like there are the following care abouts
CARGO
CARGO
(CARGO is not Cargo when build script is invoked through cargo used as a library #10119)CURRENT_EXE
(among other things) (CARGO env var set incorrectly when Cargo invoked through ld #10113)#11285 broke the (1) to fix (2) and (3). It did so by setting the precedence in
GlobalContext
tofrom_env
from_current_exe
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-builtcargo
which spawnsCARGO
, 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 #15099 (comment), my proposed solution is that cargo-the-bin opts-in to different behavior than the default.
default-behavior (when using cargo-the-library):
from_env
from_current_exe
from_argv
opt-in behavior (set by cargo-the-bin):
from_current_exe
if itscargo[EXE_SUFFIX]
from_argv
if itscargo[EXE_SUFFIX]
from_env
from_current_exe
from_argv
Characteristics
CARGO=ld
ld
but at least we still workcargo
smoelius commentedon Feb 18, 2025
@epage Thank you very much for your reply.
Just to be sure I understand:
You're saying cargo-the-bin should set the
CARGO
environment variable based on these rules?So, for example, if nightly cargo-the-bin is run and finds that
CARGO
is already set (say, to stable cargo-the-bin), it will nonetheless setCARGO
because the current exe satisfies condition 1?Should a warning be issued in that case, or should the override happen silently?
epage commentedon Feb 18, 2025
imo overriding
CARGO
would be intended behavior and wouldn't be reasonable to warn about.Cargo also tends to avoid warnings that can't be reasonably silenced. We hope to one day have #12235 resolved but that will be project focused and not invocation focused. We do not have plans atm for invocation-focused lint control.
cargo[.exe]
#15208fix: reset $CARGO if the running program is real `cargo[.exe]` (#15208)
weihanglo commentedon Feb 28, 2025
Fixed via #15208
azat commentedon Mar 18, 2025
After #15208 it is not possible to override
CARGO
for build.rs, right?The context is as follow - I was trying to force
cbindgen
(who callscargo metadata
viabuild.rs
) to usecargo --offline
, directly it is not supported yet (mozilla/cbindgen#630), and after #15208 it is not possible, I've tried the following:cargo
and place/usr/bin/cargo --offline "$@"
there and putting it intoPATH
firstPATH=/path/to/wrapper:$PATH CARGO=/path/to/wrapper/cargo cargo
cargo --config build.cargo=/path/to/cargo-wrapper
cargo --config env.CARGO=/path/to/cargo-wrapper
Is it possible to support i.e.
build.cargo
(same asrustc
) ?weihanglo commentedon Mar 18, 2025
@azat, feel free to open a new issue if there is no pre-existing one (I am not aware of any).
CARGO
environment variable, if set to a proxy, should be changed to match the selected toolchain rust-lang/rustup#4274