Skip to content

Commit 129f616

Browse files
Merge pull request #21964 from pratham-srivastava-07/pratham/cargo-meta
feat: cargo metadata takes extra args
2 parents 3e5f921 + 9513925 commit 129f616

5 files changed

Lines changed: 35 additions & 0 deletions

File tree

crates/project-model/src/cargo_workspace.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ pub struct CargoConfig {
131131
pub run_build_script_command: Option<Vec<String>>,
132132
/// Extra args to pass to the cargo command.
133133
pub extra_args: Vec<String>,
134+
/// Extra args passed only to `cargo metadata`, not other cargo commands.
135+
pub metadata_extra_args: Vec<String>,
134136
/// Extra env vars to set when invoking the cargo command
135137
pub extra_env: FxHashMap<String, Option<String>>,
136138
pub invocation_strategy: InvocationStrategy,
@@ -320,6 +322,8 @@ pub struct CargoMetadataConfig {
320322
pub targets: Vec<String>,
321323
/// Extra args to pass to the cargo command.
322324
pub extra_args: Vec<String>,
325+
/// Extra args passed directly to `cargo metadata` without filtering.
326+
pub metadata_extra_args: Vec<String>,
323327
/// Extra env vars to set when invoking the cargo command
324328
pub extra_env: FxHashMap<String, Option<String>>,
325329
/// What kind of metadata are we fetching: workspace, rustc, or sysroot.
@@ -679,6 +683,7 @@ impl FetchMetadata {
679683
other_options.push(arg.to_owned());
680684
}
681685
}
686+
other_options.extend(config.metadata_extra_args.iter().cloned());
682687

683688
let mut lockfile_copy = None;
684689
if cargo_toml.is_rust_manifest() {

crates/project-model/src/workspace.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ impl ProjectWorkspace {
216216
features,
217217
rustc_source,
218218
extra_args,
219+
metadata_extra_args,
219220
extra_env,
220221
set_test,
221222
cfg_overrides,
@@ -289,6 +290,7 @@ impl ProjectWorkspace {
289290
features: features.clone(),
290291
targets: targets.clone(),
291292
extra_args: extra_args.clone(),
293+
metadata_extra_args: metadata_extra_args.clone(),
292294
extra_env: extra_env.clone(),
293295
toolchain_version: toolchain.clone(),
294296
kind: "workspace",
@@ -343,6 +345,7 @@ impl ProjectWorkspace {
343345
features: crate::CargoFeatures::default(),
344346
targets: targets.clone(),
345347
extra_args: extra_args.clone(),
348+
metadata_extra_args: metadata_extra_args.clone(),
346349
extra_env: extra_env.clone(),
347350
toolchain_version: toolchain.clone(),
348351
kind: "rustc-dev"
@@ -575,6 +578,7 @@ impl ProjectWorkspace {
575578
features: config.features.clone(),
576579
targets,
577580
extra_args: config.extra_args.clone(),
581+
metadata_extra_args: config.metadata_extra_args.clone(),
578582
extra_env: config.extra_env.clone(),
579583
toolchain_version: toolchain.clone(),
580584
kind: "detached-file",
@@ -1942,6 +1946,7 @@ fn sysroot_metadata_config(
19421946
features: Default::default(),
19431947
targets,
19441948
extra_args: Default::default(),
1949+
metadata_extra_args: config.metadata_extra_args.clone(),
19451950
extra_env: config.extra_env.clone(),
19461951
toolchain_version,
19471952
kind: "sysroot",

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,9 @@ config_data! {
831831
///
832832
/// Set this to `"all"` to pass `--all-features` to cargo.
833833
cargo_features: CargoFeaturesDef = CargoFeaturesDef::Selected(vec![]),
834+
/// Extra arguments passed only to `cargo metadata`, not to other cargo invocations.
835+
/// Useful for flags like `--config` that `cargo metadata` supports.
836+
cargo_metadataExtraArgs: Vec<String> = vec![],
834837
/// Whether to pass `--no-default-features` to cargo.
835838
cargo_noDefaultFeatures: bool = false,
836839
/// Whether to skip fetching dependencies. If set to "true", the analysis is performed
@@ -2444,6 +2447,7 @@ impl Config {
24442447
target_dir_config: self.target_dir_from_config(source_root),
24452448
set_test: *self.cfg_setTest(source_root),
24462449
no_deps: *self.cargo_noDeps(source_root),
2450+
metadata_extra_args: self.cargo_metadataExtraArgs(source_root).clone(),
24472451
}
24482452
}
24492453

docs/book/src/configuration_generated.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ List of features to activate.
166166
Set this to `"all"` to pass `--all-features` to cargo.
167167

168168

169+
## rust-analyzer.cargo.metadataExtraArgs {#cargo.metadataExtraArgs}
170+
171+
Default: `[]`
172+
173+
Extra arguments passed only to `cargo metadata`, not to other cargo invocations.
174+
Useful for flags like `--config` that `cargo metadata` supports.
175+
176+
169177
## rust-analyzer.cargo.noDefaultFeatures {#cargo.noDefaultFeatures}
170178

171179
Default: `false`

editors/code/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,19 @@
993993
}
994994
}
995995
},
996+
{
997+
"title": "Cargo",
998+
"properties": {
999+
"rust-analyzer.cargo.metadataExtraArgs": {
1000+
"markdownDescription": "Extra arguments passed only to `cargo metadata`, not to other cargo invocations.\nUseful for flags like `--config` that `cargo metadata` supports.",
1001+
"default": [],
1002+
"type": "array",
1003+
"items": {
1004+
"type": "string"
1005+
}
1006+
}
1007+
}
1008+
},
9961009
{
9971010
"title": "Cargo",
9981011
"properties": {

0 commit comments

Comments
 (0)