diff --git a/src/cli/completions.rs b/src/cli/completions.rs index 0b494f3254..c5240019ef 100644 --- a/src/cli/completions.rs +++ b/src/cli/completions.rs @@ -37,7 +37,7 @@ pub(super) fn rustup_command(cfg: &Cfg<'_>, args: &[impl AsRef]) -> clap: .ok()? .into_iter() .filter_map(|c| { - (c.component.short_name() == "rust-std" && c.available) + (c.component.is_std() && c.available) .then(|| c.component.target.map(|t| t.to_string()))? }) .collect::>(), diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index f37d6b29c6..69f76b876c 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1367,7 +1367,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> anyhow::Result { .components()? .into_iter() .filter_map(|c| { - (c.installed && c.component.short_name() == "rust-std") + (c.installed && c.component.is_std()) .then(|| c.component.target.expect("rust-std should have a target")) }) .collect(), @@ -1442,7 +1442,7 @@ async fn target_list( if let Ok(distributable) = ChannelToolchain::from_partial(toolchain.clone(), cfg).await { common::list_items( distributable.components()?.into_iter().filter_map(|c| { - if c.component.short_name() == "rust-std" && c.available { + if c.component.is_std() && c.available { c.component.target.map(|target| (target, c.installed)) } else { None @@ -1490,8 +1490,7 @@ async fn target_add( if all { distributable .add_components(distributable.components()?.into_iter().filter_map(|c| { - (c.available && !c.installed && c.component.short_name() == "rust-std") - .then_some(Ok(c.component)) + (c.available && !c.installed && c.component.is_std()).then_some(Ok(c.component)) })) .await?; @@ -1686,31 +1685,39 @@ async fn toolchain_remove(cfg: &Cfg<'_>, opts: UninstallOpts) -> anyhow::Result< fn pin_active_toolchain(qualified: bool, cfg: &Cfg<'_>) -> anyhow::Result { let default_host = cfg.default_host_tuple()?; - let (mut r#override, backfill_components) = + let (mut r#override, backfill_components_and_targets) = match cfg.find_override_config(cfg.toolchain_override.as_ref())? { Some((o, _)) => (o, None), None => { let default = cfg .get_default_resolvable()? .context("no default toolchain to pin")?; - let components = match &default { + let components_and_targets = match &default { PartialToolchainName::Channel(desc) => { let tc = ChannelToolchain::new(cfg, desc.clone().complete(&default_host)?)?; + let host = &tc.desc().target; let manifest = tc.get_manifest()?; - Some( - tc.components()? - .into_iter() - .filter_map(|c| { - (c.available && c.installed) - .then(|| manifest.display_name(&c.component, &default_host)) - }) - .collect(), - ) + let (mut components, mut targets) = (vec![], vec![]); + for c in tc.components()? { + if !(c.available && c.installed) { + continue; + } + + match c.component.target { + Some(t) if c.component.is_std() && t != *host => { + targets.push(t.to_string()) + } + Some(_) | None => { + components.push(manifest.display_name(&c.component, host)); + } + } + } + Some((components, targets)) } PartialToolchainName::Custom(_) => None, }; - (OverrideCfg::from(default), components) + (OverrideCfg::from(default), components_and_targets) } }; if qualified { @@ -1718,8 +1725,9 @@ fn pin_active_toolchain(qualified: bool, cfg: &Cfg<'_>) -> anyhow::Result>, #[serde(skip_serializing_if = "Option::is_none")] - targets: Option>, + pub targets: Option>, #[serde(skip_serializing_if = "Option::is_none")] profile: Option, } diff --git a/src/dist/manifest.rs b/src/dist/manifest.rs index 7d5bfd2af7..e68daf2cbb 100644 --- a/src/dist/manifest.rs +++ b/src/dist/manifest.rs @@ -593,6 +593,11 @@ impl Component { &self.pkg } + /// Returns whether this component is a Rust standard library component. + pub fn is_std(&self) -> bool { + self.short_name() == "rust-std" + } + /// Returns the [`Component`]'s name **after** renaming, including the target if present. /// For the short name before renaming, see [`Manifest::name()`]. pub(crate) fn name(&self) -> String { diff --git a/src/toolchain/channels.rs b/src/toolchain/channels.rs index e6da19cb54..943fe15406 100644 --- a/src/toolchain/channels.rs +++ b/src/toolchain/channels.rs @@ -196,7 +196,7 @@ impl<'a> ChannelToolchain<'a> { let wanted_targets = targets.iter().all(|name| { installed_components .iter() - .filter(|c| c.component.short_name() == "rust-std") + .filter(|c| c.component.is_std()) .any(|status| { let ctarg = status.component.target(); (ctarg == *name) && status.installed diff --git a/tests/suite/cli_rustup.rs b/tests/suite/cli_rustup.rs index 47a0c4f358..9acc7cfafa 100644 --- a/tests/suite/cli_rustup.rs +++ b/tests/suite/cli_rustup.rs @@ -5124,6 +5124,7 @@ async fn pin_default() { [toolchain] channel = "beta" components = ["cargo", "rust-docs", "rust-std", "rustc"] +targets = [] "#]], ); @@ -5140,11 +5141,69 @@ components = ["cargo", "rust-docs", "rust-std", "rustc"] [toolchain] channel = "beta-[HOST_TUPLE]" components = ["cargo", "rust-docs", "rust-std", "rustc"] +targets = [] "#]], ); } +#[tokio::test] +async fn pin_default_with_extra_target() { + let cx = CliTestContext::new(Scenario::SimpleV2).await; + + let mut toml_redactions = snapbox::Redactions::new(); + toml_redactions + .insert("[CROSS_ARCH_I]", CROSS_ARCH1) + .unwrap(); + let toml_assert = snapbox::Assert::new() + .action_env(snapbox::assert::DEFAULT_ACTION_ENV) + .redact_with(toml_redactions); + + cx.config + .expect(["rustup", "default", "beta"]) + .await + .is_ok(); + cx.config + .expect(["rustup", "target", "add", CROSS_ARCH1]) + .await + .is_ok(); + + cx.config + .expect(["rustup", "toolchain", "pin"]) + .await + .is_ok(); + + let toolchain_file = &cx.config.current_dir().join("rust-toolchain.toml"); + toml_assert.eq( + fs::read_to_string(toolchain_file).unwrap(), + snapbox::str![[r#" +... +[toolchain] +channel = "beta" +components = ["cargo", "rust-docs", "rust-std", "rustc"] +targets = ["[CROSS_ARCH_I]"] + +"#]], + ); + + cx.config + .expect(["rustup", "toolchain", "uninstall", "beta"]) + .await + .is_ok(); + cx.config + .expect(["rustup", "toolchain", "install"]) + .await + .is_ok() + .with_stderr(snapbox::str![[r#" +info: syncing channel updates for beta-[HOST_TUPLE] +info: latest update on 2015-01-02 for version 1.2.0 (hash-beta-1.2.0) +info: downloading 5 components +info: the active toolchain `beta-[HOST_TUPLE]` has been installed +info: it's active because: overridden by '[..]/rust-toolchain.toml' + +"#]]); +} + #[tokio::test] async fn pin_dir_override() { let cx = CliTestContext::new(Scenario::SimpleV2).await; @@ -5400,6 +5459,7 @@ error: found existing override file at 'rust-toolchain.toml', refusing to overwr [toolchain] channel = "stable" components = ["cargo", "rust-docs", "rust-std", "rustc"] +targets = [] "#]], );