Skip to content

Commit cab1cfe

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 #1397
1 parent e1306b3 commit cab1cfe

File tree

5 files changed

+267
-212
lines changed

5 files changed

+267
-212
lines changed

doc/user-guide/src/overrides.md

+1-4
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

+96-117
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,99 @@ 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+
// So far, it is not possible to list targets for a custom toolchain.
1103+
ToolchainName::Custom(_) => None,
1104+
})
1105+
.and_then(|distributable| distributable.components().ok())
1106+
.map(|cs_vec| {
1107+
cs_vec
1108+
.into_iter()
1109+
.filter(|c| c.installed && c.component.short_name_in_manifest() == "rust-std")
1110+
.map(|c| c.component.target.expect("rust-std should have a target"))
1111+
.collect()
1112+
})
1113+
.unwrap_or_default();
1114+
1115+
// show installed toolchains
1116+
{
11221117
let mut t = process().stdout().terminal();
11231118

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-
}
1119+
print_header::<Error>(&mut t, "installed toolchains")?;
1120+
1121+
let default_toolchain_name = cfg.get_default()?;
1122+
1123+
let last_index = installed_toolchains.len().wrapping_sub(1);
1124+
for (n, toolchain_name) in installed_toolchains.into_iter().enumerate() {
1125+
let is_default_toolchain = default_toolchain_name.as_ref() == Some(&toolchain_name);
1126+
let is_active_toolchain = active_toolchain_name == Some(&toolchain_name);
1127+
1128+
let status_str = match (is_active_toolchain, is_default_toolchain) {
1129+
(true, true) => " (active, default)",
1130+
(true, false) => " (active)",
1131+
(false, true) => " (default)",
1132+
(false, false) => "",
1133+
};
1134+
1135+
writeln!(t.lock(), "{toolchain_name}{status_str}")?;
1136+
11361137
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())?;
1138+
let toolchain = Toolchain::new(cfg, toolchain_name.into())?;
1139+
writeln!(process().stdout().lock(), " {}", toolchain.rustc_version())?;
1140+
// To make it easy to see which rustc belongs to which
1141+
// toolchain, we separate each pair with an extra newline.
1142+
if n != last_index {
1143+
writeln!(process().stdout().lock())?;
1144+
}
11421145
}
11431146
}
1144-
if show_headers {
1145-
writeln!(t.lock())?
1146-
};
11471147
}
11481148

1149-
if show_active_targets {
1149+
// show active toolchain
1150+
{
11501151
let mut t = process().stdout().terminal();
11511152

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-
}
1153+
writeln!(t.lock())?;
11691154

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

1173-
if show_headers {
1174-
print_header::<Error>(&mut t, "active toolchain")?;
1175-
}
1157+
match active_toolchain_and_reason {
1158+
Some((active_toolchain_name, active_reason)) => {
1159+
let active_toolchain = new_toolchain_with_reason(
1160+
cfg,
1161+
active_toolchain_name.clone().into(),
1162+
&active_reason,
1163+
)?;
1164+
writeln!(t.lock(), "name: {}", active_toolchain.name())?;
1165+
writeln!(t.lock(), "compiler: {}", active_toolchain.rustc_version())?;
1166+
writeln!(t.lock(), "active because: {}", active_reason)?;
11761167

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})")?;
1168+
// show installed targets for the active toolchain
1169+
writeln!(t.lock(), "installed targets:")?;
1170+
1171+
for target in active_toolchain_targets {
1172+
writeln!(t.lock(), " {}", target)?;
11921173
}
11931174
}
1194-
}
1195-
1196-
if show_headers {
1197-
writeln!(t.lock())?
1175+
None => {
1176+
writeln!(t.lock(), "no active toolchain")?;
1177+
}
11981178
}
11991179
}
12001180

@@ -1203,9 +1183,11 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12031183
E: From<std::io::Error>,
12041184
{
12051185
t.attr(terminalsource::Attr::Bold)?;
1206-
writeln!(t.lock(), "{s}")?;
1207-
writeln!(t.lock(), "{}", "-".repeat(s.len()))?;
1208-
writeln!(t.lock())?;
1186+
{
1187+
let mut term_lock = t.lock();
1188+
writeln!(term_lock, "{s}")?;
1189+
writeln!(term_lock, "{}", "-".repeat(s.len()))?;
1190+
} // drop the term_lock
12091191
t.reset()?;
12101192
Ok(())
12111193
}
@@ -1217,27 +1199,24 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12171199
fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12181200
let verbose = m.get_flag("verbose");
12191201
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)) => {
1202+
match cfg.find_active_toolchain(&cwd)? {
1203+
Some((toolchain_name, reason)) => {
1204+
let toolchain = new_toolchain_with_reason(cfg, toolchain_name.clone(), &reason)?;
12311205
writeln!(
12321206
process().stdout().lock(),
1233-
"{} ({})",
1207+
"{}\nactive because: {}",
12341208
toolchain.name(),
12351209
reason
12361210
)?;
12371211
if verbose {
1238-
writeln!(process().stdout().lock(), "{}", toolchain.rustc_version())?;
1212+
writeln!(
1213+
process().stdout().lock(),
1214+
"compiler: {}",
1215+
toolchain.rustc_version()
1216+
)?;
12391217
}
12401218
}
1219+
None => writeln!(process().stdout().lock(), "There isn't an active toolchain")?,
12411220
}
12421221
Ok(utils::ExitCode(0))
12431222
}

src/config.rs

+2-2
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

+1-1
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)