Skip to content

fix!(config): re-enable implicit toolchain installation in Cfg::local_toolchain() with optional opt-out #4227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,16 +784,23 @@ impl<'a> Cfg<'a> {
}

async fn local_toolchain(&self, name: Option<LocalToolchainName>) -> Result<Toolchain<'_>> {
let toolchain = match name {
Some(tc) => tc,
match name {
Some(tc) => {
let install_if_missing = self
.process
.var("RUSTUP_AUTO_INSTALL")
.map_or(true, |it| it != "0");
Toolchain::from_local(tc, install_if_missing, self).await
}
None => {
self.find_active_toolchain(None)
let tc = self
.find_active_toolchain(None)
.await?
.ok_or_else(|| no_toolchain_error(self.process))?
.0
.0;
Ok(Toolchain::new(self, tc)?)
}
};
Ok(Toolchain::new(self, toolchain)?)
}
}

#[tracing::instrument(level = "trace", skip_all)]
Expand Down
7 changes: 6 additions & 1 deletion src/test/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ impl Config {

/// Expect an err status and a string in stderr
pub async fn expect_err(&self, args: &[&str], expected: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
self.expect_err_env(args, &[], expected).await
}

/// Expect an err status and a string in stderr, with extra environment variables
pub async fn expect_err_env(&self, args: &[&str], env: &[(&str, &str)], expected: &str) {
let out = self.run(args[0], &args[1..], env).await;
if out.ok || !out.stderr.contains(expected) {
print_command(args, &out);
println!("expected.ok: false");
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/cli_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,9 @@ info: installing component 'rust-std' for '{0}'
async fn show_suggestion_for_missing_toolchain() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_err_ex(
.expect_err_env(
&["cargo", "+nightly", "fmt"],
r"",
&[("RUSTUP_AUTO_INSTALL", "0")],
for_host!(
r"error: toolchain 'nightly-{0}' is not installed
help: run `rustup toolchain install nightly-{0}` to install it
Expand Down
13 changes: 3 additions & 10 deletions tests/suite/cli_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,24 +1073,17 @@ async fn which_asking_uninstalled_toolchain() {
cx.config
.expect_ok(&["rustup", "default", "custom-1"])
.await;
#[cfg(windows)]
cx.config
.expect_stdout_ok(
&["rustup", "which", "rustc"],
"\\toolchains\\custom-1\\bin\\rustc",
&["", "toolchains", "custom-1", "bin", "rustc"].join(std::path::MAIN_SEPARATOR_STR),
)
.await;
#[cfg(not(windows))]
cx.config
.expect_stdout_ok(
&["rustup", "which", "rustc"],
"/toolchains/custom-1/bin/rustc",
)
.await;
cx.config
.expect_err(
&["rustup", "which", "--toolchain=nightly", "rustc"],
for_host!("toolchain 'nightly-{}' is not installed"),
&["", "toolchains", for_host!("nightly-{0}"), "bin", "rustc"]
.join(std::path::MAIN_SEPARATOR_STR),
)
.await;
}
Expand Down
22 changes: 17 additions & 5 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,19 @@ async fn file_override_toolchain_err_handling() {
async fn plus_override_toolchain_err_handling() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_err(
&["rustc", "+beta"],
.expect_err_env(
&["rustc", "+beta", "--version"],
&[("RUSTUP_AUTO_INSTALL", "0")],
for_host!("toolchain 'beta-{0}' is not installed"),
)
.await;
cx.config
.expect_ok_contains(
&["rustc", "+beta", "--version"],
"1.2.0 (hash-beta-1.2.0)",
"",
)
.await;
}

#[tokio::test]
Expand Down Expand Up @@ -776,8 +784,9 @@ async fn upgrade_v2_to_v1() {
async fn list_targets_no_toolchain() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_err(
.expect_err_env(
&["rustup", "target", "list", "--toolchain=nightly"],
&[("RUSTUP_AUTO_INSTALL", "0")],
for_host!("toolchain 'nightly-{0}' is not installed"),
)
.await;
Expand Down Expand Up @@ -962,18 +971,20 @@ async fn remove_target_by_component_remove() {
async fn add_target_no_toolchain() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_err(
.expect_err_env(
&[
"rustup",
"target",
"add",
CROSS_ARCH1,
"--toolchain=nightly",
],
&[("RUSTUP_AUTO_INSTALL", "0")],
for_host!("toolchain 'nightly-{0}' is not installed"),
)
.await;
}

#[tokio::test]
async fn add_target_bogus() {
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
Expand Down Expand Up @@ -1120,14 +1131,15 @@ async fn remove_target_not_installed() {
async fn remove_target_no_toolchain() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_err(
.expect_err_env(
&[
"rustup",
"target",
"remove",
CROSS_ARCH1,
"--toolchain=nightly",
],
&[("RUSTUP_AUTO_INSTALL", "0")],
for_host!("toolchain 'nightly-{0}' is not installed"),
)
.await;
Expand Down