Skip to content

Commit 706e972

Browse files
majaharami3l
authored andcommitted
Update format of show and show active-toolchain
This changes the format of `rustup show` to be in a more logical order, and changes the format of `rustup show active-toolchain` to match. Also, as suggested in a comment, these commands will no longer install the active toolchain if it is not already installed, as they now call `cfg.find_active_toolchain()` instead of `cfg.find_or_install_active_toolchain()`. This fixes rust-lang#1397
1 parent 6e569ec commit 706e972

File tree

5 files changed

+274
-212
lines changed

5 files changed

+274
-212
lines changed

doc/user-guide/src/overrides.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ the directory tree toward the filesystem root, and a `rust-toolchain.toml` file
1919
that is closer to the current directory will be preferred over a directory
2020
override that is further away.
2121

22-
To verify which toolchain is active, you can use `rustup show`,
23-
which will also try to install the corresponding
24-
toolchain if the current one has not been installed according to the above rules.
25-
(Please note that this behavior is subject to change, as detailed in issue [#1397].)
22+
To verify which toolchain is active, you can use `rustup show`.
2623

2724
[toolchain]: concepts/toolchains.md
2825
[toolchain override shorthand]: #toolchain-override-shorthand

src/cli/rustup_mode.rs

Lines changed: 103 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
topical_doc,
2121
},
2222
command,
23-
config::ActiveReason,
23+
config::{new_toolchain_with_reason, ActiveReason},
2424
currentprocess::{
2525
argsource::ArgSource,
2626
filesource::{StderrSource, StdoutSource},
@@ -38,8 +38,9 @@ use crate::{
3838
names::{
3939
custom_toolchain_name_parser, maybe_resolvable_toolchainame_parser,
4040
partial_toolchain_desc_parser, resolvable_local_toolchainame_parser,
41-
resolvable_toolchainame_parser, CustomToolchainName, MaybeResolvableToolchainName,
42-
ResolvableLocalToolchainName, ResolvableToolchainName, ToolchainName,
41+
resolvable_toolchainame_parser, CustomToolchainName, LocalToolchainName,
42+
MaybeResolvableToolchainName, ResolvableLocalToolchainName, ResolvableToolchainName,
43+
ToolchainName,
4344
},
4445
toolchain::Toolchain,
4546
},
@@ -1081,120 +1082,106 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
10811082

10821083
let cwd = utils::current_dir()?;
10831084
let installed_toolchains = cfg.list_toolchains()?;
1084-
// XXX: we may want a find_without_install capability for show.
1085-
let active_toolchain = cfg.find_or_install_active_toolchain(&cwd);
1086-
1087-
// active_toolchain will carry the reason we don't have one in its detail.
1088-
let active_targets = if let Ok(ref at) = active_toolchain {
1089-
if let Ok(distributable) = DistributableToolchain::try_from(&at.0) {
1090-
match distributable.components() {
1091-
Ok(cs_vec) => cs_vec
1092-
.into_iter()
1093-
.filter(|c| c.component.short_name_in_manifest() == "rust-std")
1094-
.filter(|c| c.installed)
1095-
.collect(),
1096-
Err(_) => vec![],
1097-
}
1085+
let active_toolchain_and_reason: Option<(ToolchainName, ActiveReason)> =
1086+
if let Ok(Some((LocalToolchainName::Named(toolchain_name), reason))) =
1087+
cfg.find_active_toolchain(&cwd)
1088+
{
1089+
Some((toolchain_name, reason))
10981090
} else {
1099-
// These three vec![] could perhaps be reduced with and_then on active_toolchain.
1100-
vec![]
1101-
}
1102-
} else {
1103-
vec![]
1104-
};
1091+
None
1092+
};
1093+
1094+
let (active_toolchain_name, _active_reason) = active_toolchain_and_reason
1095+
.as_ref()
1096+
.map(|atar| (&atar.0, &atar.1))
1097+
.unzip();
11051098

1106-
let show_installed_toolchains = installed_toolchains.len() > 1;
1107-
let show_active_targets = active_targets.len() > 1;
1108-
let show_active_toolchain = true;
1109-
1110-
// Only need to display headers if we have multiple sections
1111-
let show_headers = [
1112-
show_installed_toolchains,
1113-
show_active_targets,
1114-
show_active_toolchain,
1115-
]
1116-
.iter()
1117-
.filter(|x| **x)
1118-
.count()
1119-
> 1;
1120-
1121-
if show_installed_toolchains {
1099+
let active_toolchain_targets: Vec<TargetTriple> = active_toolchain_name
1100+
.and_then(|atn| match atn {
1101+
ToolchainName::Official(desc) => DistributableToolchain::new(cfg, desc.clone()).ok(),
1102+
ToolchainName::Custom(_) => None,
1103+
})
1104+
.and_then(|distributable| {
1105+
let manifestation = distributable.get_manifestation().ok()?;
1106+
let config = manifestation.read_config().ok()?.unwrap_or_default();
1107+
let manifest = distributable.get_manifest().ok()?;
1108+
manifest
1109+
.query_components(distributable.desc(), &config)
1110+
.ok()
1111+
})
1112+
.map(|cs_vec| {
1113+
cs_vec
1114+
.into_iter()
1115+
.filter(|c| c.component.short_name_in_manifest() == "rust-std")
1116+
.filter(|c| c.installed)
1117+
.map(|c| c.component.target.expect("rust-std should have a target"))
1118+
.collect()
1119+
})
1120+
.unwrap_or_default();
1121+
1122+
// show installed toolchains
1123+
{
11221124
let mut t = process().stdout().terminal();
11231125

1124-
if show_headers {
1125-
print_header::<Error>(&mut t, "installed toolchains")?;
1126-
}
1127-
let default_name = cfg
1128-
.get_default()?
1129-
.ok_or_else(|| anyhow!("no default toolchain configured"))?;
1130-
for it in installed_toolchains {
1131-
if default_name == it {
1132-
writeln!(t.lock(), "{it} (default)")?;
1133-
} else {
1134-
writeln!(t.lock(), "{it}")?;
1135-
}
1126+
print_header::<Error>(&mut t, "installed toolchains")?;
1127+
1128+
let default_toolchain_name = cfg.get_default()?;
1129+
1130+
let last_index = installed_toolchains.len().wrapping_sub(1);
1131+
for (n, toolchain_name) in installed_toolchains.into_iter().enumerate() {
1132+
let is_default_toolchain = default_toolchain_name.as_ref() == Some(&toolchain_name);
1133+
let is_active_toolchain = active_toolchain_name == Some(&toolchain_name);
1134+
1135+
let status_str = match (is_default_toolchain, is_active_toolchain) {
1136+
(true, true) => " (active, default)",
1137+
(true, false) => " (default)",
1138+
(false, true) => " (active)",
1139+
(false, false) => "",
1140+
};
1141+
1142+
writeln!(t.lock(), "{toolchain_name}{status_str}")?;
1143+
11361144
if verbose {
1137-
let toolchain = Toolchain::new(cfg, it.into())?;
1138-
writeln!(process().stdout().lock(), "{}", toolchain.rustc_version())?;
1139-
// To make it easy to see what rustc that belongs to what
1140-
// toolchain we separate each pair with an extra newline
1141-
writeln!(process().stdout().lock())?;
1145+
let toolchain = Toolchain::new(cfg, toolchain_name.into())?;
1146+
writeln!(process().stdout().lock(), " {}", toolchain.rustc_version())?;
1147+
// To make it easy to see which rustc belongs to which
1148+
// toolchain, we separate each pair with an extra newline.
1149+
if n != last_index {
1150+
writeln!(process().stdout().lock())?;
1151+
}
11421152
}
11431153
}
1144-
if show_headers {
1145-
writeln!(t.lock())?
1146-
};
11471154
}
11481155

1149-
if show_active_targets {
1156+
// show active toolchain
1157+
{
11501158
let mut t = process().stdout().terminal();
11511159

1152-
if show_headers {
1153-
print_header::<Error>(&mut t, "installed targets for active toolchain")?;
1154-
}
1155-
for at in active_targets {
1156-
writeln!(
1157-
t.lock(),
1158-
"{}",
1159-
at.component
1160-
.target
1161-
.as_ref()
1162-
.expect("rust-std should have a target")
1163-
)?;
1164-
}
1165-
if show_headers {
1166-
writeln!(t.lock())?;
1167-
};
1168-
}
1160+
writeln!(t.lock())?;
11691161

1170-
if show_active_toolchain {
1171-
let mut t = process().stdout().terminal();
1162+
print_header::<Error>(&mut t, "active toolchain")?;
11721163

1173-
if show_headers {
1174-
print_header::<Error>(&mut t, "active toolchain")?;
1175-
}
1164+
match active_toolchain_and_reason {
1165+
Some((active_toolchain_name, active_reason)) => {
1166+
let active_toolchain = new_toolchain_with_reason(
1167+
cfg,
1168+
active_toolchain_name.clone().into(),
1169+
&active_reason,
1170+
)?;
1171+
writeln!(t.lock(), "name: {}", active_toolchain.name())?;
1172+
writeln!(t.lock(), "compiler: {}", active_toolchain.rustc_version())?;
1173+
writeln!(t.lock(), "active because: {}", active_reason)?;
11761174

1177-
match active_toolchain {
1178-
Ok((ref toolchain, ref reason)) => {
1179-
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
1180-
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1181-
}
1182-
Err(err) => {
1183-
let root_cause = err.root_cause();
1184-
if let Some(RustupError::ToolchainNotSelected) =
1185-
root_cause.downcast_ref::<RustupError>()
1186-
{
1187-
writeln!(t.lock(), "no active toolchain")?;
1188-
} else if let Some(cause) = err.source() {
1189-
writeln!(t.lock(), "(error: {err}, {cause})")?;
1190-
} else {
1191-
writeln!(t.lock(), "(error: {err})")?;
1175+
// show installed targets for the active toolchain
1176+
writeln!(t.lock(), "installed targets:")?;
1177+
1178+
for target in active_toolchain_targets {
1179+
writeln!(t.lock(), " {}", target)?;
11921180
}
11931181
}
1194-
}
1195-
1196-
if show_headers {
1197-
writeln!(t.lock())?
1182+
None => {
1183+
writeln!(t.lock(), "no active toolchain")?;
1184+
}
11981185
}
11991186
}
12001187

@@ -1203,9 +1190,11 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12031190
E: From<std::io::Error>,
12041191
{
12051192
t.attr(terminalsource::Attr::Bold)?;
1206-
writeln!(t.lock(), "{s}")?;
1207-
writeln!(t.lock(), "{}", "-".repeat(s.len()))?;
1208-
writeln!(t.lock())?;
1193+
{
1194+
let mut term_lock = t.lock();
1195+
writeln!(term_lock, "{s}")?;
1196+
writeln!(term_lock, "{}", "-".repeat(s.len()))?;
1197+
} // drop the term_lock
12091198
t.reset()?;
12101199
Ok(())
12111200
}
@@ -1217,27 +1206,24 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12171206
fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12181207
let verbose = m.get_flag("verbose");
12191208
let cwd = utils::current_dir()?;
1220-
match cfg.find_or_install_active_toolchain(&cwd) {
1221-
Err(e) => {
1222-
let root_cause = e.root_cause();
1223-
if let Some(RustupError::ToolchainNotSelected) =
1224-
root_cause.downcast_ref::<RustupError>()
1225-
{
1226-
} else {
1227-
return Err(e);
1228-
}
1229-
}
1230-
Ok((toolchain, reason)) => {
1209+
match cfg.find_active_toolchain(&cwd)? {
1210+
Some((toolchain_name, reason)) => {
1211+
let toolchain = new_toolchain_with_reason(cfg, toolchain_name.clone(), &reason)?;
12311212
writeln!(
12321213
process().stdout().lock(),
1233-
"{} ({})",
1214+
"{}\nactive because: {}",
12341215
toolchain.name(),
12351216
reason
12361217
)?;
12371218
if verbose {
1238-
writeln!(process().stdout().lock(), "{}", toolchain.rustc_version())?;
1219+
writeln!(
1220+
process().stdout().lock(),
1221+
"compiler: {}",
1222+
toolchain.rustc_version()
1223+
)?;
12391224
}
12401225
}
1226+
None => writeln!(process().stdout().lock(), "There isn't an active toolchain")?,
12411227
}
12421228
Ok(utils::ExitCode(0))
12431229
}

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ pub(crate) enum ActiveReason {
108108
impl Display for ActiveReason {
109109
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
110110
match self {
111-
Self::Default => write!(f, "default"),
112-
Self::Environment => write!(f, "environment override by RUSTUP_TOOLCHAIN"),
111+
Self::Default => write!(f, "it's the default toolchain"),
112+
Self::Environment => write!(f, "overriden by environment variable RUSTUP_TOOLCHAIN"),
113113
Self::CommandLine => write!(f, "overridden by +toolchain on the command line"),
114114
Self::OverrideDB(path) => write!(f, "directory override for '{}'", path.display()),
115115
Self::ToolchainFile(path) => write!(f, "overridden by '{}'", path.display()),

tests/suite/cli_misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ fn override_by_toolchain_on_the_command_line() {
10021002
config.expect_stdout_ok(&["rustup", "+nightly", "which", "rustc"], "/bin/rustc");
10031003
config.expect_stdout_ok(
10041004
&["rustup", "+nightly", "show"],
1005-
"(overridden by +toolchain on the command line)",
1005+
"active because: overridden by +toolchain on the command line",
10061006
);
10071007
config.expect_err(
10081008
&["rustup", "+foo", "which", "rustc"],

0 commit comments

Comments
 (0)