Proposal: special toolchain names referring to particular override levels
Part of https://github.com/rust-lang/rustup/issues/4059.
Since https://github.com/rust-lang/rustup/pull/4932 it has been decided that only characters satisfying GeneralSecurityProfile::identifier_allowed() should be allowed in rustup toolchain names.
This has opened up new possibilities for us to assemble our special/reserved toolchain names as needed with special characters such as @ or =.
Following up the above efforts, noting the five levels of rustup toolchain overrides:
- The
+<toolchain>override.- The
RUSTUP_TOOLCHAINenvironment variable.- The
rustup override setoverride.- The
rust-toolchain.tomloverride.- The default toolchain.
... aiming to address https://github.com/rust-lang/rustup/issues/4391, I propose the following changes:
- Make
envanddefaultnew reserved words in addition to the existingnone. - Introduce the following toolchain syntax:
env: Refers to the toolchain that should be activated by the env override.- This will not be useful per se but sometime people want to be explicit, e.g.
rustup run env cargoinstead ofcargo.
- This will not be useful per se but sometime people want to be explicit, e.g.
dir=<dir>: Refers to the toolchain that should be activated by the dir override on<dir>or its fallback dirs.- e.g.
cargo +dir=./crates/download
- e.g.
file=<file>: Refers to the toolchain that should be activated by the TOML override in<file>which follows therust-toolchain.tomlschema.- e.g.
cargo +file=windows.toml, for a Windows-specificrust-toolchain.tomlsetup.
- e.g.
default: Refers to the default toolchain. Bails out if none is set.- e.g.
cargo +default
- e.g.
Concerns
- How should we handle the error for
dir=andfile=when the dir/TOML override doesn't exist? Should we bail out or warn and fall back to the next level?- My guess is that we should warn and fall back.
- Consider the relation between this proposal and #4945. Should we resolve the toolchain name when the user issues
rustup default file=windows.toml? If so, should we fully resolve or partially resolve it?- My guess is that we should partially resolve to an official or a custom toolchain name (that isn't a reserved word and doesn't have special characters like
=).
- My guess is that we should partially resolve to an official or a custom toolchain name (that isn't a reserved word and doesn't have special characters like
- Can we use this in
RUSTUP_TOOLCHAIN(isRUSTUP_TOOLCHAINnot guaranteed to be a parsable toolchain name by downstreams other than rustup)? If we can, we will also resolve #2793 at the same time by settingRUST_TOOLCHAIN=default.- I think we probably can.
Note
This also simplifies https://github.com/rust-lang/cargo/issues/10098 because this involves changing the override lookup logic to work with arbitrary base paths. In that case, we'd just need to alter the path for our dir/TOML override source by prefixing it with the path passed via cargo -C.
I'll think about this some more later but my initial impressions is good. I do like the general idea.
is
RUST_TOOLCHAINnot guaranteed to be a parsable toolchain name by downstreams other than rustup
Hm, I do somewhat suspect others do parse this (and the toolchain file). On the other hand we already support things like stable (iirc) so I don't think adding default to the mix really makes things any less rustup specific. However, the more complex syntax (e.g. dir=) might possibly be more of a problem. Then again it's unlikely to match any real-world toolchain if it's not parsed.
@ChrisDenton In that case we can probably partially resolve and replace it in place until no reserved words or special characters remain, upon the first encounter.
IIRC we already need to convert any toolchain override to a RUSTUP_TOOLCHAIN value anyway.
It seems to me like we have a clear use case for file= but I don't think it's as obvious for the other ones? I don't think we should try to overgeneralize/implement a more complex solution than what we have a need for.
It seems to me like we have a clear use case for
file=but I don't think it's as obvious for the other ones? I don't think we should try to overgeneralize/implement a more complex solution than what we have a need for.
@djc Of course I can do this in separate patches.
As for the concrete use case, as previously mentioned:
- Adding the
file=level will resolve https://github.com/rust-lang/rustup/issues/4391 - Adding the
defaultlevel will resolve https://github.com/rust-lang/rustup/issues/2793
I can remember others being mentioned in previous discussions, but it might be just me. I'll try to post the link here next time I encounter that thread/post.
I do think default and file= are useful.
The justification for env does seem quite weak though. And it would indeed be useful to find some discussion of where dir= would be useful.
The justification for
envdoes seem quite weak though.
@ChrisDenton FYI I am reminded of why I have the impression that env is mentioned somewhere:
Our old guidelines of running rustup with cargo run used to be RUSTUP_FORCE_ARG0=rustup rustup run <active-toolchain> cargo run -- (where <active-toolchain> will be env here), it became cargo run --config env.RUSTUP_FORCE_ARG0='rustup' -- only a bit later.
The reason why rustup run <active-toolchain> was used instead of cargo is that the former isn't a proxy mode call so RUSTUP_FORCE_ARG0=rustup is a no-op for this call and this call only.
I do agree that this was a very particular use case for which we now have a pretty good alternative, and I'd be glad to leave this unimplemented unless there are some other real use cases. Same thing for dir= although I'm still searching about the references of that in my memory... I believe it has something to do with workspaces though. 🤔
@rustbot claim
@fulldecent's https://github.com/rust-lang/rustup/issues/5114#issuecomment-6030761140 seems to be the missing justification for the env case: the user may want to use rustup run env cargo when they have no direct access to their rustup proxies (maybe because they are shadowed).
@fulldecent's #5114 (comment) seems to be the missing justification for the
envcase: the user may want to userustup run env cargowhen they have no direct access to their rustup proxies (maybe because they are shadowed).
It's not obvious to me how you get from that (longish) comment to this conclusion, can you explain your thinking a bit?
It's not obvious to me how you get from that (longish) comment to this conclusion, can you explain your thinking a bit?
@djc rustup run env cargo is often equivalent to just cargo, but this is true only if the user has followed rustup's PATH setup. There are cases where this setup is undesirable or complicated, and currently we already have an option --no-modify-path to skip that step.
In particular, it seems from that thread that, for example, brew's current default behavior is exposing rustup in the PATH so that cargo can be reserved for the rust package.
For this to be a valid setup, we would normally need to configure PATH as we have previously mentioned, or provide an option for the user to explicitly invoke cargo from rustup. rustup run env cargo provides exactly such a choice.
Alternatively we can of course declare this case as unsupported and that the user should absolutely control the details of their PATH, but I think the env case looks closer to npm run oxfmt or uv run pytest etc. which might be a viable alternative option in terms of UX for people from other ecosystems.
Still, the env item looks relatively messy compared to npm or uv. Can we make our run do the right thing more directly?
Still, the
envitem looks relatively messy compared tonpmoruv. Can we make ourrundo the right thing more directly?
Yes, but that would need a new subcommand for backwards compatibility, say rustup exec, and I'd personally be in favor of such an addition. See also #4564.
Based on @rami3l notes I understand that rustup run env cargo behaves in these ways:
- If
$PATHprefers the rustup packagecargocommand, then run thecargocommand in$PATH. - If
$PATHprefers the rustc packagecargocommand, then run thecargocommand in$PATH.
Or more simply, it always runs the cargo command in $PATH.
I think the more reasonable behavior, and perhaps from the perspective of this rustup project, the defined behavior is/should be:
- Regardless of which command is preferred in
$PATH, run thecargocommand from rustup.
This results in well defined, predictable behavior.
Also IMHO, I believe the brew package manager approach of NOT adding rustc shims when installing rustup is prudent. And I think it is unreasonable for the rustup project to expect package managers to install shims that override rustc. But more than MHO, as an actual bug report, we should be resilient in such situations, and ultimately not care. Hence the notes here above the ---.
Based on @rami3l notes I understand that
rustup run env cargobehaves in these ways:
- If
$PATHprefers the rustup packagecargocommand, then run thecargocommand in$PATH.- If
$PATHprefers the rustc packagecargocommand, then run thecargocommand in$PATH.Or more simply, it always runs the
cargocommand in$PATH.
@fulldecent That is incorrect; rustup run always run the cargo command from rustup, that is to say, you would always get the latter behavior:
- Regardless of which command is preferred in
$PATH, run thecargocommand from rustup.This results in well defined, predictable behavior.
Whether cargo is available from $PATH is irrelevant in this case.
@rami3l Got it, thank you. Yes, the behavior you describe is useful. But I'd like to propose a different name.
PATH="$(dirname "$(rustup which cargo)"):$PATH" DYLD_LIBRARY_PATH="$(cd "$(dirname "$(rustup which cargo)")/../lib" && pwd)" cargo build --locked
would be equivalent to:
rustup run active cargo build --locked
Here, active is already the established term in several places:
rustup show active-toolchain
rustup show
rustup toolchain list
And if we support active, we should also support default, which is referenced no less prominently in the same commands.
The active and default may be in scope for this issue and/or they may be a sub-issue that could land before the rest of this issue.