Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(super) fn rustup_command(cfg: &Cfg<'_>, args: &[impl AsRef<OsStr>]) -> 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::<Vec<_>>(),
Expand Down
44 changes: 26 additions & 18 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> anyhow::Result<ExitCode> {
.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(),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -1686,40 +1685,49 @@ async fn toolchain_remove(cfg: &Cfg<'_>, opts: UninstallOpts) -> anyhow::Result<
fn pin_active_toolchain(qualified: bool, cfg: &Cfg<'_>) -> anyhow::Result<ExitCode> {
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 {
r#override.qualify(&default_host);
}

let mut r#override = OverrideFile::from(r#override);
if backfill_components.is_some() {
r#override.toolchain.components = backfill_components;
if let Some((components, targets)) = backfill_components_and_targets {
r#override.toolchain.components = Some(components);
r#override.toolchain.targets = Some(targets);
}

let rel_path = "rust-toolchain.toml";
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) struct ToolchainSection {
#[serde(skip_serializing_if = "Option::is_none")]
pub components: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
targets: Option<Vec<String>>,
pub targets: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
profile: Option<String>,
}
Expand Down
5 changes: 5 additions & 0 deletions src/dist/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/toolchain/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions tests/suite/cli_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5124,6 +5124,7 @@ async fn pin_default() {
[toolchain]
channel = "beta"
components = ["cargo", "rust-docs", "rust-std", "rustc"]
targets = []

"#]],
);
Expand All @@ -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;
Expand Down Expand Up @@ -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 = []

"#]],
);
Expand Down
Loading