Skip to content

Commit a65ecb0

Browse files
feat(custom-toolchains): rustup show now reporting installed targets
1 parent a92432f commit a65ecb0

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

src/cli/rustup_mode.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -987,17 +987,24 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
987987

988988
let active_toolchain_targets: Vec<TargetTriple> = active_toolchain_name
989989
.and_then(|atn| match atn {
990-
ToolchainName::Official(desc) => DistributableToolchain::new(cfg, desc.clone()).ok(),
991-
// So far, it is not possible to list targets for a custom toolchain.
992-
ToolchainName::Custom(_) => None,
993-
})
994-
.and_then(|distributable| distributable.components().ok())
995-
.map(|cs_vec| {
996-
cs_vec
997-
.into_iter()
998-
.filter(|c| c.installed && c.component.short_name_in_manifest() == "rust-std")
999-
.map(|c| c.component.target.expect("rust-std should have a target"))
1000-
.collect()
990+
ToolchainName::Official(desc) => DistributableToolchain::new(cfg, desc.clone())
991+
.ok()
992+
.and_then(|distributable| distributable.components().ok())
993+
.map(|cs_vec| {
994+
cs_vec
995+
.into_iter()
996+
.filter(|c| {
997+
c.installed && c.component.short_name_in_manifest() == "rust-std"
998+
})
999+
.map(|c| c.component.target.expect("rust-std should have a target"))
1000+
.collect()
1001+
}),
1002+
ToolchainName::Custom(name) => {
1003+
Toolchain::new(cfg, LocalToolchainName::Named(name.into()))
1004+
.ok()?
1005+
.list_targets()
1006+
.ok()
1007+
}
10011008
})
10021009
.unwrap_or_default();
10031010

src/dist/component/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod transaction;
1010
// The representation of a package, its components, and installation
1111
mod package;
1212
// The representation of *installed* components, and uninstallation
13-
mod components;
13+
pub mod components;
1414

1515
#[cfg(test)]
1616
mod tests;

src/toolchain.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use wait_timeout::ChildExt;
2222
use crate::{
2323
RustupError,
2424
config::{ActiveReason, Cfg, InstalledPath},
25-
dist::PartialToolchainDesc,
25+
dist::{
26+
PartialToolchainDesc, TargetTriple, component::components::Components,
27+
prefix::InstallPrefix,
28+
},
2629
env_var, install,
2730
notifications::Notification,
2831
utils::{self, raw::open_dir_following_links},
@@ -575,4 +578,27 @@ impl<'a> Toolchain<'a> {
575578
}
576579
Ok(())
577580
}
581+
582+
/// Get the list of installed targets for any toolchain
583+
///
584+
/// NB: An assumption is made that custom toolchains always have a `rustlib/components` file
585+
pub fn list_targets(&self) -> anyhow::Result<Vec<TargetTriple>> {
586+
let prefix = InstallPrefix::from(self.path.clone());
587+
let components = Components::open(prefix)?;
588+
let installed_components = components.list()?;
589+
590+
let targets = installed_components
591+
.into_iter()
592+
.filter_map(|c| {
593+
if c.name().starts_with("rust-std-") {
594+
Some(TargetTriple::new(
595+
c.name().trim_start_matches("rust-std-").to_string(),
596+
))
597+
} else {
598+
None
599+
}
600+
})
601+
.collect();
602+
Ok(targets)
603+
}
578604
}

0 commit comments

Comments
 (0)