Skip to content

Commit 2bffb85

Browse files
Prevent rustup from automatically installing toolchains
By setting RUSTUP_AUTO_INSTALL=0.
1 parent e7d7cb4 commit 2bffb85

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

crates/project-model/src/cargo_workspace.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use serde_derive::Deserialize;
1313
use serde_json::from_value;
1414
use span::Edition;
1515
use stdx::process::spawn_with_streaming_output;
16-
use toolchain::Tool;
16+
use toolchain::{NO_RUSTUP_AUTO_INSTALL_ENV, Tool};
1717
use triomphe::Arc;
1818

19-
use crate::cargo_config_file::make_lockfile_copy;
20-
use crate::{CfgOverrides, InvocationStrategy};
21-
use crate::{ManifestPath, Sysroot};
19+
use crate::{
20+
CfgOverrides, InvocationStrategy, ManifestPath, Sysroot, cargo_config_file::make_lockfile_copy,
21+
};
2222

2323
pub(crate) const MINIMUM_TOOLCHAIN_VERSION_SUPPORTING_LOCKFILE_PATH: semver::Version =
2424
semver::Version {
@@ -632,6 +632,8 @@ impl FetchMetadata {
632632
) -> Self {
633633
let cargo = sysroot.tool(Tool::Cargo, current_dir, &config.extra_env);
634634
let mut command = MetadataCommand::new();
635+
// Prevent rustup from automatically installing toolchains, see https://github.com/rust-lang/rust-analyzer/issues/20719.
636+
command.env(NO_RUSTUP_AUTO_INSTALL_ENV.0, NO_RUSTUP_AUTO_INSTALL_ENV.1);
635637
command.cargo_path(cargo.get_program());
636638
cargo.get_envs().for_each(|(var, val)| _ = command.env(var, val.unwrap_or_default()));
637639
command.manifest_path(cargo_toml.to_path_buf());

crates/project-model/src/workspace.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
1616
use rustc_hash::{FxHashMap, FxHashSet};
1717
use semver::Version;
1818
use span::{Edition, FileId};
19-
use toolchain::Tool;
19+
use toolchain::{NO_RUSTUP_AUTO_INSTALL_ENV, Tool};
2020
use tracing::instrument;
21+
use tracing::{debug, error, info};
2122
use triomphe::Arc;
2223

2324
use crate::{
@@ -33,7 +34,6 @@ use crate::{
3334
toolchain_info::{QueryConfig, rustc_cfg, target_data, target_tuple, version},
3435
utf8_stdout,
3536
};
36-
use tracing::{debug, error, info};
3737

3838
pub type FileLoader<'a> = &'a mut dyn for<'b> FnMut(&'b AbsPath) -> Option<FileId>;
3939

@@ -1907,6 +1907,7 @@ fn cargo_target_dir(
19071907
) -> Option<Utf8PathBuf> {
19081908
let cargo = sysroot.tool(Tool::Cargo, manifest.parent(), extra_env);
19091909
let mut meta = cargo_metadata::MetadataCommand::new();
1910+
meta.env(NO_RUSTUP_AUTO_INSTALL_ENV.0, NO_RUSTUP_AUTO_INSTALL_ENV.1);
19101911
meta.cargo_path(cargo.get_program());
19111912
meta.manifest_path(manifest);
19121913
// `--no-deps` doesn't (over)write lockfiles as it doesn't do any package resolve.

crates/toolchain/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ impl Tool {
7171
}
7272
}
7373

74+
pub const NO_RUSTUP_AUTO_INSTALL_ENV: (&str, &str) = ("RUSTUP_AUTO_INSTALL", "0");
75+
7476
#[allow(clippy::disallowed_types)] /* generic parameter allows for FxHashMap */
7577
pub fn command<H>(
7678
cmd: impl AsRef<OsStr>,
@@ -81,6 +83,8 @@ pub fn command<H>(
8183
#[allow(clippy::disallowed_methods)]
8284
let mut cmd = Command::new(cmd);
8385
cmd.current_dir(working_directory);
86+
// Prevent rustup from automatically installing toolchains, see https://github.com/rust-lang/rust-analyzer/issues/20719.
87+
cmd.env(NO_RUSTUP_AUTO_INSTALL_ENV.0, NO_RUSTUP_AUTO_INSTALL_ENV.1);
8488
for env in extra_env {
8589
match env {
8690
(key, Some(val)) => cmd.env(key, val),

0 commit comments

Comments
 (0)