Skip to content

Commit

Permalink
Improve rustup show, list toolchains, default
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
majaha committed Dec 5, 2023
1 parent 85788ce commit c0442bb
Show file tree
Hide file tree
Showing 9 changed files with 714 additions and 525 deletions.
11 changes: 2 additions & 9 deletions doc/user-guide/src/overrides.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ the directory tree toward the filesystem root, and a `rust-toolchain.toml` file
that is closer to the current directory will be preferred over a directory
override that is further away.

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

[toolchain]: concepts/toolchains.md
[toolchain override shorthand]: #toolchain-override-shorthand
Expand Down Expand Up @@ -123,16 +120,12 @@ The `channel` setting specifies which [toolchain] to use. The value is a
string in the following form:

```
<channel>[-<date>]
(<channel>[-<date>])|<custom toolchain name>
<channel> = stable|beta|nightly|<major.minor.patch>
<date> = YYYY-MM-DD
```

Note that this is a more restricted form than `rustup` toolchains
generally, and cannot be used to specify custom toolchains or
host-specific toolchains.

[toolchain]: concepts/toolchains.md

#### path
Expand Down
105 changes: 52 additions & 53 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::currentprocess::{
};
use crate::dist::dist::{TargetTriple, ToolchainDesc};
use crate::install::UpdateStatus;
use crate::toolchain::names::{LocalToolchainName, ToolchainName};
use crate::utils::notifications as util_notifications;
use crate::utils::notify::NotificationLevel;
use crate::utils::utils;
Expand Down Expand Up @@ -461,74 +462,72 @@ pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_
Ok(())
}

fn print_toolchain_path(
cfg: &Cfg,
toolchain: &str,
if_default: &str,
if_override: &str,
verbose: bool,
) -> Result<()> {
let toolchain_path = cfg.toolchains_dir.join(toolchain);
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
let toolchain_path = if verbose {
if toolchain_meta.is_dir() {
format!("\t{}", toolchain_path.display())
} else {
format!("\t{}", fs::read_link(toolchain_path)?.display())
}
} else {
String::new()
};
writeln!(
process().stdout().lock(),
"{}{}{}{}",
&toolchain,
if_default,
if_override,
toolchain_path
)?;
Ok(())
}

pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCode> {
// Work with LocalToolchainName to accommodate path based overrides
let toolchains = cfg
.list_toolchains()?
.iter()
.map(Into::into)
.collect::<Vec<_>>();
let toolchains: Vec<ToolchainName> = cfg.list_toolchains()?;
if toolchains.is_empty() {
writeln!(process().stdout().lock(), "no installed toolchains")?;
} else {
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
let default_toolchain_name = cfg.get_default()?;
let cwd = utils::current_dir()?;
let ovr_toolchain_name = if let Ok(Some((toolchain, _reason))) = cfg.find_override(&cwd) {
Some(toolchain)
} else {
None
};
for toolchain in toolchains {
let if_default = if def_toolchain_name.as_ref() == Some(&toolchain) {
" (default)"
let active_toolchain_name: Option<ToolchainName> =
if let Ok(Some((LocalToolchainName::Named(toolchain), _reason))) =
cfg.find_active_toolchain(&cwd)
{
Some(toolchain)
} else {
""
};
let if_override = if ovr_toolchain_name.as_ref() == Some(&toolchain) {
" (override)"
} else {
""
None
};

print_toolchain_path(
for toolchain in toolchains {
let is_default_toolchain = default_toolchain_name.as_ref() == Some(&toolchain);
let is_active_toolchain = active_toolchain_name.as_ref() == Some(&toolchain);

print_toolchain(
cfg,
&toolchain.to_string(),
if_default,
if_override,
is_default_toolchain,
is_active_toolchain,
verbose,
)
.context("Failed to list toolchains' directories")?;
}
}

fn print_toolchain(
cfg: &Cfg,
toolchain: &str,
is_default: bool,
is_active: bool,
verbose: bool,
) -> Result<()> {
let toolchain_path = cfg.toolchains_dir.join(toolchain);
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
let toolchain_path = if verbose {
if toolchain_meta.is_dir() {
format!(" {}", toolchain_path.display())
} else {
format!(" {}", fs::read_link(toolchain_path)?.display())
}
} else {
String::new()
};
let status_str = match (is_default, is_active) {
(true, true) => " (default, active)",
(true, false) => " (default)",
(false, true) => " (active)",
(false, false) => "",
};

writeln!(
process().stdout().lock(),
"{}{}{}",
&toolchain,
status_str,
toolchain_path
)?;
Ok(())
}

Ok(utils::ExitCode(0))
}

Expand Down
Loading

0 comments on commit c0442bb

Please sign in to comment.