Skip to content

Commit da1223a

Browse files
committed
Eliminate overlap with RUSTUP_TOOLCHAIN
1 parent 92b2ba4 commit da1223a

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

src/cli/proxy_mode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
command::run_command_for_dir,
88
config::ActiveReason,
99
process::Process,
10-
toolchain::ResolvableLocalToolchainName,
10+
toolchain::{ResolvableLocalToolchainName, maybe_set_env_source},
1111
};
1212

1313
#[tracing::instrument(level = "trace", skip(process))]
@@ -37,7 +37,7 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result
3737
let toolchain = cfg.resolve_local_toolchain(toolchain).await?;
3838
let mut cmd = toolchain.command(arg0)?;
3939
if toolchain_specified {
40-
toolchain.maybe_set_env_source(&mut cmd, || Some(ActiveReason::CommandLine));
40+
maybe_set_env_source(&mut cmd, || Some(ActiveReason::CommandLine));
4141
}
4242
run_command_for_dir(cmd, arg0, &cmd_args)
4343
}

src/config.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,13 @@ pub(crate) enum ActiveReason {
104104

105105
impl ActiveReason {
106106
/// Format `ActiveReason` for setting the `RUSTUP_TOOLCHAIN_SOURCE` environment variable.
107-
///
108-
/// The format of the variable's content is `source toolchain`. Neither component should
109-
/// contain spaces.
110-
pub fn to_source(&self, name: &LocalToolchainName) -> String {
107+
pub fn to_source(&self) -> &str {
111108
match self {
112-
Self::Default => format!("default {name}"),
113-
Self::Environment => format!("env {name}"),
114-
Self::CommandLine => format!("cli {name}"),
115-
Self::OverrideDB(_) => format!("override {name}"),
116-
Self::ToolchainFile(_) => format!("config {name}"),
109+
Self::Default => "default",
110+
Self::Environment => "env",
111+
Self::CommandLine => "cli",
112+
Self::OverrideDB(_) => "override",
113+
Self::ToolchainFile(_) => "config",
117114
}
118115
}
119116
}

src/toolchain.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'a> Toolchain<'a> {
181181

182182
env_var::inc("RUST_RECURSION_COUNT", cmd, self.cfg.process);
183183

184-
self.maybe_set_env_source(cmd, || {
184+
maybe_set_env_source(cmd, || {
185185
if let Ok(Some((_, reason))) = self.cfg.active_toolchain() {
186186
Some(reason)
187187
} else {
@@ -193,23 +193,6 @@ impl<'a> Toolchain<'a> {
193193
cmd.env("RUSTUP_HOME", &self.cfg.rustup_dir);
194194
}
195195

196-
/// Set the `RUSTUP_TOOLCHAIN_SOURCE` environment variable if not already set.
197-
///
198-
/// `RUSTUP_TOOLCHAIN_SOURCE` indicates how the toolchain was determined. The environment
199-
/// variable could have been set in proxy_mode.rs, in which case it should not be changed.
200-
pub(crate) fn maybe_set_env_source(
201-
&self,
202-
cmd: &mut Command,
203-
f: impl FnOnce() -> Option<ActiveReason>,
204-
) {
205-
if env::var_os("RUSTUP_TOOLCHAIN_SOURCE").is_some() {
206-
return;
207-
}
208-
if let Some(reason) = f() {
209-
cmd.env("RUSTUP_TOOLCHAIN_SOURCE", reason.to_source(&self.name));
210-
}
211-
}
212-
213196
/// Apply the appropriate LD path for a command being run from a toolchain.
214197
fn set_ldpath(&self, cmd: &mut Command) {
215198
#[cfg_attr(not(target_os = "macos"), allow(unused_mut))]
@@ -630,3 +613,16 @@ impl<'a> Toolchain<'a> {
630613
.collect())
631614
}
632615
}
616+
617+
/// Set the `RUSTUP_TOOLCHAIN_SOURCE` environment variable if not already set.
618+
///
619+
/// `RUSTUP_TOOLCHAIN_SOURCE` indicates how the toolchain was determined. The environment
620+
/// variable could have been set in proxy_mode.rs, in which case it should not be changed.
621+
pub(crate) fn maybe_set_env_source(cmd: &mut Command, f: impl FnOnce() -> Option<ActiveReason>) {
622+
if env::var_os("RUSTUP_TOOLCHAIN_SOURCE").is_some() {
623+
return;
624+
}
625+
if let Some(reason) = f() {
626+
cmd.env("RUSTUP_TOOLCHAIN_SOURCE", reason.to_source());
627+
}
628+
}

tests/suite/cli_rustup.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,7 +3050,7 @@ async fn rustup_toolchain_source_cli() {
30503050
.await
30513051
.with_stderr(snapbox::str![[r#"
30523052
...
3053-
cli nightly-[HOST_TRIPLE]
3053+
cli
30543054
30553055
"#]]);
30563056
}
@@ -3066,7 +3066,7 @@ async fn rustup_toolchain_source_env() {
30663066
.await
30673067
.with_stderr(snapbox::str![[r#"
30683068
...
3069-
env nightly-[HOST_TRIPLE]
3069+
env
30703070
30713071
"#]]);
30723072
}
@@ -3083,7 +3083,7 @@ async fn rustup_toolchain_source_override() {
30833083
.await
30843084
.with_stderr(snapbox::str![[r#"
30853085
...
3086-
override nightly-[HOST_TRIPLE]
3086+
override
30873087
30883088
"#]]);
30893089
}
@@ -3098,7 +3098,7 @@ async fn rustup_toolchain_source_config() {
30983098
.await
30993099
.with_stderr(snapbox::str![[r#"
31003100
...
3101-
config nightly-[HOST_TRIPLE]
3101+
config
31023102
31033103
"#]]);
31043104
}
@@ -3115,7 +3115,7 @@ async fn rustup_toolchain_source_default() {
31153115
.await
31163116
.with_stderr(snapbox::str![[r#"
31173117
...
3118-
default stable-[HOST_TRIPLE]
3118+
default
31193119
31203120
"#]]);
31213121
}

0 commit comments

Comments
 (0)