diff --git a/crates/project-model/src/build_scripts.rs b/crates/project-model/src/build_scripts.rs index 38c1b37f0e3a..6df1273edd65 100644 --- a/crates/project-model/src/build_scripts.rs +++ b/crates/project-model/src/build_scripts.rs @@ -67,6 +67,7 @@ impl WorkspaceBuildScripts { let mut cmd = Command::new(toolchain::cargo()); cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]); + cmd.args(&config.extra_args); // --all-targets includes tests, benches and examples in addition to the // default lib and bins. This is an independent concept from the --target diff --git a/crates/project-model/src/cargo_workspace.rs b/crates/project-model/src/cargo_workspace.rs index fdc7859eb90f..732adc50b500 100644 --- a/crates/project-model/src/cargo_workspace.rs +++ b/crates/project-model/src/cargo_workspace.rs @@ -105,6 +105,8 @@ pub struct CargoConfig { pub wrap_rustc_in_build_scripts: bool, /// The command to run instead of `cargo check` for building build scripts. pub run_build_script_command: Option>, + /// Extra args to pass to the cargo command. + pub extra_args: Vec, /// Extra env vars to set when invoking the cargo command pub extra_env: FxHashMap, pub invocation_strategy: InvocationStrategy, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 0d75607f35d8..75233dbb2abe 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -101,6 +101,8 @@ config_data! { /// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to /// avoid checking unnecessary things. cargo_buildScripts_useRustcWrapper: bool = "true", + /// Extra arguments that are passed to every cargo invocation. + cargo_extraArgs: Vec = "[]", /// Extra environment variables that will be set when running cargo, rustc /// or other commands within the workspace. Useful for setting RUSTFLAGS. cargo_extraEnv: FxHashMap = "{}", @@ -1055,10 +1057,20 @@ impl Config { } } + pub fn extra_args(&self) -> &Vec { + &self.data.cargo_extraArgs + } + pub fn extra_env(&self) -> &FxHashMap { &self.data.cargo_extraEnv } + pub fn check_extra_args(&self) -> Vec { + let mut extra_args = self.extra_args().clone(); + extra_args.extend_from_slice(&self.data.check_extraArgs); + extra_args + } + pub fn check_extra_env(&self) -> FxHashMap { let mut extra_env = self.data.cargo_extraEnv.clone(); extra_env.extend(self.data.check_extraEnv.clone()); @@ -1157,6 +1169,7 @@ impl Config { InvocationLocation::Workspace => project_model::InvocationLocation::Workspace, }, run_build_script_command: self.data.cargo_buildScripts_overrideCommand.clone(), + extra_args: self.data.cargo_extraArgs.clone(), extra_env: self.data.cargo_extraEnv.clone(), } } @@ -1227,7 +1240,7 @@ impl Config { CargoFeaturesDef::All => vec![], CargoFeaturesDef::Selected(it) => it, }, - extra_args: self.data.check_extraArgs.clone(), + extra_args: self.check_extra_args(), extra_env: self.check_extra_env(), ansi_color_output: self.color_diagnostic_output(), }, diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 1bc498c42cec..6937a7ed9a2d 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -71,6 +71,11 @@ cargo check --quiet --workspace --message-format=json --all-targets Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to avoid checking unnecessary things. -- +[[rust-analyzer.cargo.extraArgs]]rust-analyzer.cargo.extraArgs (default: `[]`):: ++ +-- +Extra arguments that are passed to every cargo invocation. +-- [[rust-analyzer.cargo.extraEnv]]rust-analyzer.cargo.extraEnv (default: `{}`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index 90f7b9074c85..a3b1a3107d0c 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -511,6 +511,14 @@ "default": true, "type": "boolean" }, + "rust-analyzer.cargo.extraArgs": { + "markdownDescription": "Extra arguments that are passed to every cargo invocation.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + }, "rust-analyzer.cargo.extraEnv": { "markdownDescription": "Extra environment variables that will be set when running cargo, rustc\nor other commands within the workspace. Useful for setting RUSTFLAGS.", "default": {},