Skip to content

feat(custom-toolchains): targets and components are now reported for custom toolchains #4347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
33 changes: 14 additions & 19 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ use super::self_update;
use crate::{
cli::download_tracker::DownloadTracker,
config::Cfg,
dist::{
TargetTriple, ToolchainDesc, manifest::ComponentStatus, notifications as dist_notifications,
},
dist::{TargetTriple, ToolchainDesc, notifications as dist_notifications},
errors::RustupError,
install::UpdateStatus,
notifications::Notification,
process::{Process, terminalsource},
toolchain::{DistributableToolchain, LocalToolchainName, Toolchain, ToolchainName},
toolchain::{LocalToolchainName, Toolchain, ToolchainName},
utils::{self, notifications as util_notifications, notify::NotificationLevel},
};

Expand Down Expand Up @@ -380,29 +378,26 @@ where
Ok(utils::ExitCode(0))
}

/// Iterates over pairs representing the name of a target or component and a
/// boolean value indicating whether it is installed or not.
/// The boolean value is needed to determine whether to print "(installed)"
/// next to the target/component name."
pub(super) fn list_items(
distributable: DistributableToolchain<'_>,
f: impl Fn(&ComponentStatus) -> Option<&str>,
items: impl Iterator<Item = (String, bool)>,
installed_only: bool,
quiet: bool,
process: &Process,
) -> Result<utils::ExitCode> {
let mut t = process.stdout().terminal(process);
for component in distributable.components()? {
let Some(name) = f(&component) else { continue };
match (component.available, component.installed, installed_only) {
(false, _, _) | (_, false, true) => continue,
(true, true, false) if !quiet => {
t.attr(terminalsource::Attr::Bold)?;
writeln!(t.lock(), "{name} (installed)")?;
t.reset()?;
}
(true, _, false) | (_, true, true) => {
writeln!(t.lock(), "{name}")?;
}
for (name, installed) in items {
if installed && !installed_only && !quiet {
t.attr(terminalsource::Attr::Bold)?;
writeln!(t.lock(), "{name} (installed)")?;
t.reset()?;
} else if installed || !installed_only {
writeln!(t.lock(), "{name}")?;
}
}

Ok(utils::ExitCode(0))
}

Expand Down
80 changes: 56 additions & 24 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,22 +1170,37 @@ async fn target_list(
installed_only: bool,
quiet: bool,
) -> Result<utils::ExitCode> {
// downcasting required because the toolchain files can name any toolchain
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
common::list_items(
distributable,
|c| {
(c.component.short_name_in_manifest() == "rust-std").then(|| {
c.component
.target
.as_deref()
.expect("rust-std should have a target")
})
},
installed_only,
quiet,
cfg.process,
)
// If a toolchain is Distributable, we can assume it has a manifest and thus print all possible targets and the installed ones.
// However, if it is a custom toolchain, we can only print the installed targets.
// NB: this decision is made based on the absence of a manifest in custom toolchains.
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
common::list_items(
distributable.components()?.into_iter().filter_map(|c| {
if c.component.short_name_in_manifest() == "rust-std" && c.available {
c.component
.target
.as_deref()
.map(|target| (target.to_string(), c.installed))
} else {
None
}
}),
installed_only,
quiet,
cfg.process,
)
} else {
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
common::list_items(
toolchain
.installed_targets()?
.iter()
.map(|s| (s.to_string(), true)),
installed_only,
quiet,
cfg.process,
)
}
}

async fn target_add(
Expand Down Expand Up @@ -1280,14 +1295,31 @@ async fn component_list(
quiet: bool,
) -> Result<utils::ExitCode> {
// downcasting required because the toolchain files can name any toolchain
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
common::list_items(
distributable,
|c| Some(&c.name),
installed_only,
quiet,
cfg.process,
)
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
common::list_items(
distributable.components()?.into_iter().filter_map(|c| {
if c.available {
Some((c.name, c.installed))
} else {
None
}
}),
installed_only,
quiet,
cfg.process,
)
} else {
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
common::list_items(
toolchain
.installed_components()?
.iter()
.map(|s| (s.name().to_string(), true)),
installed_only,
quiet,
cfg.process,
)
}
}

async fn component_add(
Expand Down
45 changes: 33 additions & 12 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,21 +836,42 @@ async fn list_targets_v1_toolchain() {

#[tokio::test]
async fn list_targets_custom_toolchain() {
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
let path = cx.config.customdir.join("custom-1");
let path = path.to_string_lossy();
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_ok(&["rustup", "toolchain", "link", "default-from-path", &path])
.await;
.expect(&["rustup", "default", "stable"])
.await
.is_ok();
let stable_path = cx
.config
.rustupdir
.join("toolchains")
.join(format!("stable-{}", this_host_triple()));
cx.config
.expect_ok(&["rustup", "default", "default-from-path"])
.await;
.expect([
"rustup",
"toolchain",
"link",
"stuff",
&stable_path.to_string_lossy(),
])
.await
.is_ok();
cx.config
.expect_err(
&["rustup", "target", "list"],
"toolchain 'default-from-path' does not support components",
)
.await;
.expect(["rustup", "+stuff", "target", "list", "--installed"])
.await
.with_stdout(snapbox::str![[r#"
[HOST_TRIPLE]

"#]])
.is_ok();
cx.config
.expect(["rustup", "+stuff", "target", "list"])
.await
.with_stdout(snapbox::str![[r#"
[HOST_TRIPLE] (installed)

"#]])
.is_ok();
}

#[tokio::test]
Expand Down
Loading