Skip to content

Commit c0442bb

Browse files
committed
Improve rustup show, list toolchains, default
1. Change the output format of `rustup show`, to be in a more logical order and have more pleasing formatting 2. Update the formatting of `rustup show active-toolchain` and `rustup toolchain list` to match the new `rustup show` format. 3. `rustup +nightly show` will no longer install the nightly toolchain if it isn't already installed. Ditto for `rustup show active-toolchain`. 4. Fix a bug where the RUSTUP_TOOLCHAIN environment variable took priority over the +toolchain command line option, and add a test for override priority. 5. `rustup default` no longer errors when there is no default toolchain configured, it prints a message instead. 6. Update the docs, and fix an issue where they incorrectly stated that `rust-toolchain.toml` config files couldn't specify custom names. (Text last touched here, see commit message: 7bfbd3b)
1 parent 85788ce commit c0442bb

File tree

9 files changed

+714
-525
lines changed

9 files changed

+714
-525
lines changed

doc/user-guide/src/overrides.md

+2-9
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
@@ -123,16 +120,12 @@ The `channel` setting specifies which [toolchain] to use. The value is a
123120
string in the following form:
124121

125122
```
126-
<channel>[-<date>]
123+
(<channel>[-<date>])|<custom toolchain name>
127124
128125
<channel> = stable|beta|nightly|<major.minor.patch>
129126
<date> = YYYY-MM-DD
130127
```
131128

132-
Note that this is a more restricted form than `rustup` toolchains
133-
generally, and cannot be used to specify custom toolchains or
134-
host-specific toolchains.
135-
136129
[toolchain]: concepts/toolchains.md
137130

138131
#### path

src/cli/common.rs

+52-53
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::currentprocess::{
2222
};
2323
use crate::dist::dist::{TargetTriple, ToolchainDesc};
2424
use crate::install::UpdateStatus;
25+
use crate::toolchain::names::{LocalToolchainName, ToolchainName};
2526
use crate::utils::notifications as util_notifications;
2627
use crate::utils::notify::NotificationLevel;
2728
use crate::utils::utils;
@@ -461,74 +462,72 @@ pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_
461462
Ok(())
462463
}
463464

464-
fn print_toolchain_path(
465-
cfg: &Cfg,
466-
toolchain: &str,
467-
if_default: &str,
468-
if_override: &str,
469-
verbose: bool,
470-
) -> Result<()> {
471-
let toolchain_path = cfg.toolchains_dir.join(toolchain);
472-
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
473-
let toolchain_path = if verbose {
474-
if toolchain_meta.is_dir() {
475-
format!("\t{}", toolchain_path.display())
476-
} else {
477-
format!("\t{}", fs::read_link(toolchain_path)?.display())
478-
}
479-
} else {
480-
String::new()
481-
};
482-
writeln!(
483-
process().stdout().lock(),
484-
"{}{}{}{}",
485-
&toolchain,
486-
if_default,
487-
if_override,
488-
toolchain_path
489-
)?;
490-
Ok(())
491-
}
492-
493465
pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCode> {
494-
// Work with LocalToolchainName to accommodate path based overrides
495-
let toolchains = cfg
496-
.list_toolchains()?
497-
.iter()
498-
.map(Into::into)
499-
.collect::<Vec<_>>();
466+
let toolchains: Vec<ToolchainName> = cfg.list_toolchains()?;
500467
if toolchains.is_empty() {
501468
writeln!(process().stdout().lock(), "no installed toolchains")?;
502469
} else {
503-
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
470+
let default_toolchain_name = cfg.get_default()?;
504471
let cwd = utils::current_dir()?;
505-
let ovr_toolchain_name = if let Ok(Some((toolchain, _reason))) = cfg.find_override(&cwd) {
506-
Some(toolchain)
507-
} else {
508-
None
509-
};
510-
for toolchain in toolchains {
511-
let if_default = if def_toolchain_name.as_ref() == Some(&toolchain) {
512-
" (default)"
472+
let active_toolchain_name: Option<ToolchainName> =
473+
if let Ok(Some((LocalToolchainName::Named(toolchain), _reason))) =
474+
cfg.find_active_toolchain(&cwd)
475+
{
476+
Some(toolchain)
513477
} else {
514-
""
515-
};
516-
let if_override = if ovr_toolchain_name.as_ref() == Some(&toolchain) {
517-
" (override)"
518-
} else {
519-
""
478+
None
520479
};
521480

522-
print_toolchain_path(
481+
for toolchain in toolchains {
482+
let is_default_toolchain = default_toolchain_name.as_ref() == Some(&toolchain);
483+
let is_active_toolchain = active_toolchain_name.as_ref() == Some(&toolchain);
484+
485+
print_toolchain(
523486
cfg,
524487
&toolchain.to_string(),
525-
if_default,
526-
if_override,
488+
is_default_toolchain,
489+
is_active_toolchain,
527490
verbose,
528491
)
529492
.context("Failed to list toolchains' directories")?;
530493
}
531494
}
495+
496+
fn print_toolchain(
497+
cfg: &Cfg,
498+
toolchain: &str,
499+
is_default: bool,
500+
is_active: bool,
501+
verbose: bool,
502+
) -> Result<()> {
503+
let toolchain_path = cfg.toolchains_dir.join(toolchain);
504+
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
505+
let toolchain_path = if verbose {
506+
if toolchain_meta.is_dir() {
507+
format!(" {}", toolchain_path.display())
508+
} else {
509+
format!(" {}", fs::read_link(toolchain_path)?.display())
510+
}
511+
} else {
512+
String::new()
513+
};
514+
let status_str = match (is_default, is_active) {
515+
(true, true) => " (default, active)",
516+
(true, false) => " (default)",
517+
(false, true) => " (active)",
518+
(false, false) => "",
519+
};
520+
521+
writeln!(
522+
process().stdout().lock(),
523+
"{}{}{}",
524+
&toolchain,
525+
status_str,
526+
toolchain_path
527+
)?;
528+
Ok(())
529+
}
530+
532531
Ok(utils::ExitCode(0))
533532
}
534533

0 commit comments

Comments
 (0)