Skip to content

Commit 5e90849

Browse files
majaharami3l
authored andcommitted
Change find_override to find_active_toolchain
Changes the model from "find an override to the default toolchain" to "find the active toolchain, which may be the default toolchain".
1 parent 3470d0c commit 5e90849

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

src/cli/common.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use once_cell::sync::Lazy;
1414

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
17+
use crate::config::ActiveReason;
1718
use crate::currentprocess::{
1819
argsource::ArgSource,
1920
filesource::{StdinSource, StdoutSource},
@@ -466,11 +467,15 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCod
466467
} else {
467468
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
468469
let cwd = utils::current_dir()?;
469-
let ovr_toolchain_name = if let Ok(Some((toolchain, _reason))) = cfg.find_override(&cwd) {
470-
Some(toolchain)
471-
} else {
472-
None
473-
};
470+
let ovr_toolchain_name =
471+
if let Ok(Some((toolchain, reason))) = cfg.find_active_toolchain(&cwd) {
472+
match reason {
473+
ActiveReason::Default => None,
474+
_ => Some(toolchain),
475+
}
476+
} else {
477+
None
478+
};
474479
for toolchain in toolchains {
475480
let if_default = if def_toolchain_name.as_ref() == Some(&toolchain) {
476481
" (default)"

src/cli/rustup_mode.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
topical_doc,
2121
},
2222
command,
23+
config::ActiveReason,
2324
currentprocess::{
2425
argsource::ArgSource,
2526
filesource::{StderrSource, StdoutSource},
@@ -864,8 +865,10 @@ fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
864865
};
865866

866867
let cwd = utils::current_dir()?;
867-
if let Some((toolchain, reason)) = cfg.find_override(&cwd)? {
868-
info!("note that the toolchain '{toolchain}' is currently in use ({reason})");
868+
if let Some((toolchain, reason)) = cfg.find_active_toolchain(&cwd)? {
869+
if !matches!(reason, ActiveReason::Default) {
870+
info!("note that the toolchain '{toolchain}' is currently in use ({reason})");
871+
}
869872
}
870873
} else {
871874
let default_toolchain = cfg

src/config.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,20 @@ impl<T: Into<String>> From<T> for OverrideFile {
9595
}
9696
}
9797

98+
// Represents the reason why the active toolchain is active.
9899
#[derive(Debug)]
99-
pub(crate) enum OverrideReason {
100+
pub(crate) enum ActiveReason {
101+
Default,
100102
Environment,
101103
CommandLine,
102104
OverrideDB(PathBuf),
103105
ToolchainFile(PathBuf),
104106
}
105107

106-
impl Display for OverrideReason {
108+
impl Display for ActiveReason {
107109
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
108110
match self {
111+
Self::Default => write!(f, "default"),
109112
Self::Environment => write!(f, "environment override by RUSTUP_TOOLCHAIN"),
110113
Self::CommandLine => write!(f, "overridden by +toolchain on the command line"),
111114
Self::OverrideDB(path) => write!(f, "directory override for '{}'", path.display()),
@@ -115,11 +118,11 @@ impl Display for OverrideReason {
115118
}
116119

117120
/// Calls Toolchain::new(), but augments the error message with more context
118-
/// from the OverrideReason if the toolchain isn't installed.
121+
/// from the ActiveReason if the toolchain isn't installed.
119122
pub(crate) fn new_toolchain_with_reason<'a>(
120123
cfg: &'a Cfg,
121124
name: LocalToolchainName,
122-
reason: &OverrideReason,
125+
reason: &ActiveReason,
123126
) -> Result<Toolchain<'a>> {
124127
match Toolchain::new(cfg, name.clone()) {
125128
Err(RustupError::ToolchainNotInstalled(_)) => (),
@@ -129,21 +132,24 @@ pub(crate) fn new_toolchain_with_reason<'a>(
129132
}
130133

131134
let reason_err = match reason {
132-
OverrideReason::Environment => {
135+
ActiveReason::Environment => {
133136
"the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain"
134137
.to_string()
135138
}
136-
OverrideReason::CommandLine => {
139+
ActiveReason::CommandLine => {
137140
"the +toolchain on the command line specifies an uninstalled toolchain".to_string()
138141
}
139-
OverrideReason::OverrideDB(ref path) => format!(
142+
ActiveReason::OverrideDB(ref path) => format!(
140143
"the directory override for '{}' specifies an uninstalled toolchain",
141144
utils::canonicalize_path(path, cfg.notify_handler.as_ref()).display(),
142145
),
143-
OverrideReason::ToolchainFile(ref path) => format!(
146+
ActiveReason::ToolchainFile(ref path) => format!(
144147
"the toolchain file at '{}' specifies an uninstalled toolchain",
145148
utils::canonicalize_path(path, cfg.notify_handler.as_ref()).display(),
146149
),
150+
ActiveReason::Default => {
151+
"the default toolchain does not describe an installed toolchain".to_string()
152+
}
147153
};
148154

149155
Err(anyhow!(reason_err).context(format!("override toolchain '{name}' is not installed")))
@@ -478,21 +484,26 @@ impl Cfg {
478484
.transpose()?)
479485
}
480486

481-
pub(crate) fn find_override(
487+
pub(crate) fn find_active_toolchain(
482488
&self,
483489
path: &Path,
484-
) -> Result<Option<(LocalToolchainName, OverrideReason)>> {
485-
Ok(self
486-
.find_override_config(path)?
487-
.and_then(|(override_cfg, reason)| override_cfg.toolchain.map(|t| (t, reason))))
490+
) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
491+
Ok(
492+
if let Some((override_config, reason)) = self.find_override_config(path)? {
493+
override_config.toolchain.map(|t| (t, reason))
494+
} else {
495+
self.get_default()?
496+
.map(|x| (x.into(), ActiveReason::Default))
497+
},
498+
)
488499
}
489500

490-
fn find_override_config(&self, path: &Path) -> Result<Option<(OverrideCfg, OverrideReason)>> {
501+
fn find_override_config(&self, path: &Path) -> Result<Option<(OverrideCfg, ActiveReason)>> {
491502
let mut override_ = None;
492503

493504
// First check toolchain override from command
494505
if let Some(ref name) = self.toolchain_override {
495-
override_ = Some((name.to_string().into(), OverrideReason::CommandLine));
506+
override_ = Some((name.to_string().into(), ActiveReason::CommandLine));
496507
}
497508

498509
// Check RUSTUP_TOOLCHAIN
@@ -501,7 +512,7 @@ impl Cfg {
501512
// custom, distributable, and absolute path toolchains otherwise
502513
// rustup's export of a RUSTUP_TOOLCHAIN when running a process will
503514
// error when a nested rustup invocation occurs
504-
override_ = Some((name.to_string().into(), OverrideReason::Environment));
515+
override_ = Some((name.to_string().into(), ActiveReason::Environment));
505516
}
506517

507518
// Then walk up the directory tree from 'path' looking for either the
@@ -539,14 +550,14 @@ impl Cfg {
539550
&self,
540551
dir: &Path,
541552
settings: &Settings,
542-
) -> Result<Option<(OverrideFile, OverrideReason)>> {
553+
) -> Result<Option<(OverrideFile, ActiveReason)>> {
543554
let notify = self.notify_handler.as_ref();
544555
let mut dir = Some(dir);
545556

546557
while let Some(d) = dir {
547558
// First check the override database
548559
if let Some(name) = settings.dir_override(d, notify) {
549-
let reason = OverrideReason::OverrideDB(d.to_owned());
560+
let reason = ActiveReason::OverrideDB(d.to_owned());
550561
return Ok(Some((name.into(), reason)));
551562
}
552563

@@ -619,7 +630,7 @@ impl Cfg {
619630
}
620631
}
621632

622-
let reason = OverrideReason::ToolchainFile(toolchain_file);
633+
let reason = ActiveReason::ToolchainFile(toolchain_file);
623634
return Ok(Some((override_file, reason)));
624635
}
625636

@@ -663,7 +674,7 @@ impl Cfg {
663674
pub(crate) fn find_or_install_override_toolchain_or_default(
664675
&self,
665676
path: &Path,
666-
) -> Result<(Toolchain<'_>, Option<OverrideReason>)> {
677+
) -> Result<(Toolchain<'_>, Option<ActiveReason>)> {
667678
let (toolchain, components, targets, reason, profile) =
668679
match self.find_override_config(path)? {
669680
Some((

0 commit comments

Comments
 (0)