Skip to content

Commit 649e4e9

Browse files
committed
cli: propagate ActiveSource from the top
1 parent f028c4f commit 649e4e9

File tree

4 files changed

+62
-40
lines changed

4 files changed

+62
-40
lines changed

src/cli/proxy_mode.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result
2525
.filter(|arg| arg.starts_with('+'))
2626
.map(|name| ResolvableLocalToolchainName::try_from(&name.as_ref()[1..]))
2727
.transpose()?;
28-
let toolchain_specified = toolchain.is_some();
2928

3029
// Build command args now while we know whether or not to skip arg 1.
3130
let cmd_args: Vec<_> = process
@@ -34,21 +33,17 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result
3433
.collect();
3534

3635
let cfg = Cfg::from_env(current_dir, true, process)?;
37-
let toolchain = cfg
36+
let (toolchain, source) = cfg
3837
.local_toolchain(match toolchain {
39-
Some(name) => Some(name.resolve(&cfg.get_default_host_triple()?)?),
38+
Some(name) => Some((
39+
name.resolve(&cfg.get_default_host_triple()?)?,
40+
ActiveSource::CommandLine,
41+
)),
4042
None => None,
4143
})
4244
.await?;
4345

4446
let mut cmd = toolchain.command(arg0)?;
45-
if toolchain_specified {
46-
cmd.env(
47-
"RUSTUP_TOOLCHAIN_SOURCE",
48-
ActiveSource::CommandLine.to_string(),
49-
);
50-
} else if let Ok(Some((_, source))) = cfg.active_toolchain() {
51-
cmd.env("RUSTUP_TOOLCHAIN_SOURCE", source.to_string());
52-
}
47+
cmd.env("RUSTUP_TOOLCHAIN_SOURCE", source.to_string());
5348
run_command_for_dir(cmd, arg0, &cmd_args)
5449
}

src/cli/rustup_mode.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,12 @@ async fn which(
10361036
binary: &str,
10371037
toolchain: Option<ResolvableToolchainName>,
10381038
) -> Result<ExitCode> {
1039-
let toolchain = cfg
1039+
let (toolchain, _) = cfg
10401040
.local_toolchain(match toolchain {
1041-
Some(name) => Some(name.resolve(&cfg.get_default_host_triple()?)?.into()),
1041+
Some(name) => Some((
1042+
name.resolve(&cfg.get_default_host_triple()?)?.into(),
1043+
ActiveSource::CommandLine, // From --toolchain option
1044+
)),
10421045
None => None,
10431046
})
10441047
.await?;
@@ -1251,6 +1254,8 @@ async fn target_list(
12511254
installed_only: bool,
12521255
quiet: bool,
12531256
) -> Result<ExitCode> {
1257+
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
1258+
12541259
// If a toolchain is Distributable, we can assume it has a manifest and thus print all possible targets and the installed ones.
12551260
// However, if it is a custom toolchain, we can only print the installed targets.
12561261
// NB: this decision is made based on the absence of a manifest in custom toolchains.
@@ -1268,7 +1273,7 @@ async fn target_list(
12681273
cfg.process,
12691274
)
12701275
} else {
1271-
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
1276+
let toolchain = cfg.toolchain_from_partial(toolchain).await?.0;
12721277
common::list_items(
12731278
toolchain.installed_targets()?.iter().map(|s| (s, true)),
12741279
installed_only,
@@ -1288,9 +1293,13 @@ async fn target_add(
12881293
// isn't a feature yet.
12891294
// list_components *and* add_component would both be inappropriate for
12901295
// custom toolchains.
1291-
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
1292-
let components = distributable.components()?;
1296+
let distributable = DistributableToolchain::from_partial(
1297+
toolchain.map(|desc| (desc, ActiveSource::CommandLine)),
1298+
cfg,
1299+
)
1300+
.await?;
12931301

1302+
let components = distributable.components()?;
12941303
if targets.contains(&"all".to_string()) {
12951304
if targets.len() != 1 {
12961305
return Err(anyhow!(
@@ -1332,7 +1341,11 @@ async fn target_remove(
13321341
targets: Vec<String>,
13331342
toolchain: Option<PartialToolchainDesc>,
13341343
) -> Result<ExitCode> {
1335-
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
1344+
let distributable = DistributableToolchain::from_partial(
1345+
toolchain.map(|desc| (desc, ActiveSource::CommandLine)),
1346+
cfg,
1347+
)
1348+
.await?;
13361349

13371350
for target in targets {
13381351
let target = TargetTriple::new(target);
@@ -1369,6 +1382,8 @@ async fn component_list(
13691382
installed_only: bool,
13701383
quiet: bool,
13711384
) -> Result<ExitCode> {
1385+
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
1386+
13721387
// downcasting required because the toolchain files can name any toolchain
13731388
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
13741389
common::list_items(
@@ -1381,7 +1396,7 @@ async fn component_list(
13811396
cfg.process,
13821397
)
13831398
} else {
1384-
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
1399+
let toolchain = cfg.toolchain_from_partial(toolchain).await?.0;
13851400
common::list_items(
13861401
toolchain
13871402
.installed_components()?
@@ -1400,9 +1415,13 @@ async fn component_add(
14001415
toolchain: Option<PartialToolchainDesc>,
14011416
target: Option<String>,
14021417
) -> Result<ExitCode> {
1403-
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
1404-
let target = get_target(target, &distributable);
1418+
let distributable = DistributableToolchain::from_partial(
1419+
toolchain.map(|desc| (desc, ActiveSource::CommandLine)),
1420+
cfg,
1421+
)
1422+
.await?;
14051423

1424+
let target = get_target(target, &distributable);
14061425
for component in &components {
14071426
let new_component = Component::try_new(component, &distributable, target.as_ref())?;
14081427
distributable.add_component(new_component).await?;
@@ -1426,6 +1445,7 @@ async fn component_remove(
14261445
toolchain: Option<PartialToolchainDesc>,
14271446
target: Option<String>,
14281447
) -> Result<ExitCode> {
1448+
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
14291449
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
14301450
let target = get_target(target, &distributable);
14311451

@@ -1707,7 +1727,8 @@ async fn doc(
17071727
mut topic: Option<&str>,
17081728
doc_page: &DocPage,
17091729
) -> Result<ExitCode> {
1710-
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
1730+
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
1731+
let toolchain = cfg.toolchain_from_partial(toolchain).await?.0;
17111732

17121733
if let Ok(distributable) = DistributableToolchain::try_from(&toolchain)
17131734
&& let [_] = distributable
@@ -1772,7 +1793,8 @@ async fn man(
17721793
command: &str,
17731794
toolchain: Option<PartialToolchainDesc>,
17741795
) -> Result<ExitCode> {
1775-
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
1796+
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
1797+
let toolchain = cfg.toolchain_from_partial(toolchain).await?.0;
17761798
let path = toolchain.man_path();
17771799
utils::assert_is_directory(&path)?;
17781800

src/config.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<T: Into<String>> From<T> for OverrideFile {
8888
}
8989

9090
// Represents the source that determined the current active toolchain.
91-
#[derive(Debug)]
91+
#[derive(Clone, Debug)]
9292
pub(crate) enum ActiveSource {
9393
Default,
9494
Environment,
@@ -477,13 +477,16 @@ impl<'a> Cfg<'a> {
477477

478478
pub(crate) async fn toolchain_from_partial(
479479
&self,
480-
toolchain: Option<PartialToolchainDesc>,
481-
) -> Result<Toolchain<'_>> {
480+
toolchain: Option<(PartialToolchainDesc, ActiveSource)>,
481+
) -> Result<(Toolchain<'_>, ActiveSource)> {
482482
let toolchain = toolchain
483-
.map(|desc| {
484-
anyhow::Ok(LocalToolchainName::Named(ToolchainName::Official(
485-
desc.resolve(&self.get_default_host_triple()?)?,
486-
)))
483+
.map(|(desc, source)| {
484+
anyhow::Ok((
485+
LocalToolchainName::Named(ToolchainName::Official(
486+
desc.resolve(&self.get_default_host_triple()?)?,
487+
)),
488+
source,
489+
))
487490
})
488491
.transpose()?;
489492
self.local_toolchain(toolchain).await
@@ -700,20 +703,22 @@ impl<'a> Cfg<'a> {
700703

701704
pub(crate) async fn local_toolchain(
702705
&self,
703-
name: Option<LocalToolchainName>,
704-
) -> Result<Toolchain<'_>> {
706+
name: Option<(LocalToolchainName, ActiveSource)>,
707+
) -> Result<(Toolchain<'_>, ActiveSource)> {
705708
match name {
706-
Some(tc) => {
709+
Some((tc, source)) => {
707710
let install_if_missing = self.should_auto_install()?;
708-
Toolchain::from_local(tc, install_if_missing, self).await
711+
Ok((
712+
Toolchain::from_local(tc, install_if_missing, self).await?,
713+
source,
714+
))
709715
}
710716
None => {
711-
let tc = self
717+
let (tc, source) = self
712718
.maybe_ensure_active_toolchain(None)
713719
.await?
714-
.ok_or_else(|| no_toolchain_error(self.process))?
715-
.0;
716-
Ok(Toolchain::new(self, tc)?)
720+
.ok_or_else(|| no_toolchain_error(self.process))?;
721+
Ok((Toolchain::new(self, tc)?, source))
717722
}
718723
}
719724
}

src/toolchain/distributable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use platforms::Platform;
99

1010
use crate::{
1111
RustupError, component_for_bin,
12-
config::Cfg,
12+
config::{ActiveSource, Cfg},
1313
dist::{
1414
DistOptions, PartialToolchainDesc, Profile, ToolchainDesc,
1515
config::Config,
@@ -35,11 +35,11 @@ pub(crate) struct DistributableToolchain<'a> {
3535

3636
impl<'a> DistributableToolchain<'a> {
3737
pub(crate) async fn from_partial(
38-
toolchain: Option<PartialToolchainDesc>,
38+
toolchain: Option<(PartialToolchainDesc, ActiveSource)>,
3939
cfg: &'a Cfg<'a>,
4040
) -> anyhow::Result<Self> {
4141
Ok(Self::try_from(
42-
&cfg.toolchain_from_partial(toolchain).await?,
42+
&cfg.toolchain_from_partial(toolchain).await?.0,
4343
)?)
4444
}
4545

0 commit comments

Comments
 (0)