Skip to content

Commit 245e220

Browse files
committed
feat(cli/rustup-mode): warn about auto-installation in some subcommands
1 parent d4dc63b commit 245e220

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

src/cli/rustup_mode.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,36 @@ fn update_toolchain_value_parser(s: &str) -> Result<PartialToolchainDesc> {
301301
})
302302
}
303303

304+
impl RustupSubcmd {
305+
fn allow_auto_install(&self) -> bool {
306+
match self {
307+
// These subcommands execute or rely on the active toolchain, so auto-installing it when
308+
// missing may be reasonable depending on the user's decision.
309+
#[cfg(not(windows))]
310+
RustupSubcmd::Man { .. } => true,
311+
RustupSubcmd::Doc { .. } | RustupSubcmd::Run { .. } => true,
312+
313+
// These subcommands don't require the active toolchain, so auto-installing it should be
314+
// disabled to avoid surprises.
315+
RustupSubcmd::Check { .. }
316+
| RustupSubcmd::Completions { .. }
317+
| RustupSubcmd::Component { .. }
318+
| RustupSubcmd::Default { .. }
319+
| RustupSubcmd::DumpTestament
320+
| RustupSubcmd::Install { .. }
321+
| RustupSubcmd::Override { .. }
322+
| RustupSubcmd::Self_ { .. }
323+
| RustupSubcmd::Set { .. }
324+
| RustupSubcmd::Show { .. }
325+
| RustupSubcmd::Target { .. }
326+
| RustupSubcmd::Toolchain { .. }
327+
| RustupSubcmd::Uninstall { .. }
328+
| RustupSubcmd::Update { .. }
329+
| RustupSubcmd::Which { .. } => false,
330+
}
331+
}
332+
}
333+
304334
#[derive(Debug, Subcommand)]
305335
enum ShowSubcmd {
306336
/// Show the active toolchain
@@ -631,15 +661,20 @@ pub async fn main(
631661

632662
update_console_filter(process, &console_filter, matches.quiet, matches.verbose);
633663

634-
let cfg = &mut Cfg::from_env(current_dir, matches.quiet, true, process)?;
635-
cfg.toolchain_override = matches.plus_toolchain;
636-
637664
let Some(subcmd) = matches.subcmd else {
638665
let help = Rustup::command().render_long_help();
639666
writeln!(process.stderr().lock(), "{}", help.ansi())?;
640667
return Ok(ExitCode::FAILURE);
641668
};
642669

670+
let cfg = &mut Cfg::from_env(
671+
current_dir,
672+
matches.quiet,
673+
subcmd.allow_auto_install(),
674+
process,
675+
)?;
676+
cfg.toolchain_override = matches.plus_toolchain;
677+
643678
match subcmd {
644679
RustupSubcmd::DumpTestament => common::dump_testament(process),
645680
RustupSubcmd::Install { opts } => update(cfg, opts, true).await,

src/config.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ impl<T: Display> EnsureInstalled<T> {
154154
"the missing active toolchain `{}` has been auto-installed",
155155
self.inner,
156156
);
157+
158+
if !cfg.allow_auto_install {
159+
// NOTE: Special behavior for rustup v1.29
160+
warn!("auto-installation is deprecated for most `rustup` commands");
161+
warn!("scripts relying on this behavior in `rustup` may stop working in the future");
162+
warn!("to install the active toolchain, use `rustup install` instead");
163+
warn!("see <https://github.com/rust-lang/rustup/issues/4836> for more info");
164+
return;
165+
}
166+
157167
warn!("this might cause rustup commands to take longer time to finish than expected");
158168
info!("you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`");
159169
}
@@ -423,10 +433,6 @@ impl<'a> Cfg<'a> {
423433
}
424434

425435
pub(crate) fn should_auto_install(&self) -> Result<bool> {
426-
if !self.allow_auto_install {
427-
return Ok(false);
428-
}
429-
430436
if let Ok(mode) = self.process.var("RUSTUP_AUTO_INSTALL") {
431437
Ok(mode != "0")
432438
} else {

tests/suite/cli_misc.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,8 +1813,10 @@ rustc-[HOST_TUPLE] (installed)
18131813
.with_stderr(snapbox::str![[r#"
18141814
...
18151815
warn: the missing active toolchain `stable-[HOST_TUPLE]` has been auto-installed
1816-
warn: this might cause rustup commands to take longer time to finish than expected
1817-
info: you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`
1816+
warn: auto-installation is deprecated for most `rustup` commands
1817+
warn: scripts relying on this behavior in `rustup` may stop working in the future
1818+
warn: to install the active toolchain, use `rustup install` instead
1819+
warn: see <https://github.com/rust-lang/rustup/issues/4836> for more info
18181820
18191821
"#]])
18201822
.is_ok();

0 commit comments

Comments
 (0)