Skip to content

Commit b16c26b

Browse files
authored
Fix proc_macro_dylib_path when they are built in both opt and debug (#1089)
* Fix proc_macro_dylib_path in cases where they are built in both opt and debug * Resolve rustfmt issues * Update test comment
1 parent 8205991 commit b16c26b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tools/rust_analyzer/aquery.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ fn consolidate_crate_specs(crate_specs: Vec<CrateSpec>) -> anyhow::Result<BTreeS
177177
existing.display_name = spec.display_name;
178178
existing.crate_type = "rlib".into();
179179
}
180+
181+
// For proc-macro crates that exist within the workspace, there will be a
182+
// generated crate-spec in both the fastbuild and opt-exec configuration.
183+
// Prefer proc macro paths with an opt-exec component in the path.
184+
if let Some(dylib_path) = spec.proc_macro_dylib_path.as_ref() {
185+
const OPT_PATH_COMPONENT: &str = "-opt-exec-";
186+
if dylib_path.contains(OPT_PATH_COMPONENT) {
187+
existing.proc_macro_dylib_path.replace(dylib_path.clone());
188+
}
189+
}
180190
} else {
181191
consolidated_specs.insert(spec.crate_id.clone(), spec);
182192
}
@@ -512,4 +522,69 @@ mod test {
512522
);
513523
}
514524
}
525+
526+
#[test]
527+
fn consolidate_proc_macro_prefer_exec() {
528+
// proc macro crates should prefer the -opt-exec- path which is always generated
529+
// during builds where it is used, while the fastbuild version would only be built
530+
// when explicitly building that target.
531+
let crate_specs = vec![
532+
CrateSpec {
533+
crate_id: "ID-myproc_macro.rs".into(),
534+
display_name: "myproc_macro".into(),
535+
edition: "2018".into(),
536+
root_module: "myproc_macro.rs".into(),
537+
is_workspace_member: true,
538+
deps: BTreeSet::new(),
539+
proc_macro_dylib_path: Some(
540+
"bazel-out/k8-opt-exec-F005BA11/bin/myproc_macro/libmyproc_macro-12345.so"
541+
.into(),
542+
),
543+
source: None,
544+
cfg: vec!["test".into(), "debug_assertions".into()],
545+
env: BTreeMap::new(),
546+
target: "x86_64-unknown-linux-gnu".into(),
547+
crate_type: "proc_macro".into(),
548+
},
549+
CrateSpec {
550+
crate_id: "ID-myproc_macro.rs".into(),
551+
display_name: "myproc_macro".into(),
552+
edition: "2018".into(),
553+
root_module: "myproc_macro.rs".into(),
554+
is_workspace_member: true,
555+
deps: BTreeSet::new(),
556+
proc_macro_dylib_path: Some(
557+
"bazel-out/k8-fastbuild/bin/myproc_macro/libmyproc_macro-12345.so".into(),
558+
),
559+
source: None,
560+
cfg: vec!["test".into(), "debug_assertions".into()],
561+
env: BTreeMap::new(),
562+
target: "x86_64-unknown-linux-gnu".into(),
563+
crate_type: "proc_macro".into(),
564+
},
565+
];
566+
567+
for perm in crate_specs.into_iter().permutations(2) {
568+
assert_eq!(
569+
consolidate_crate_specs(perm).unwrap(),
570+
BTreeSet::from([CrateSpec {
571+
crate_id: "ID-myproc_macro.rs".into(),
572+
display_name: "myproc_macro".into(),
573+
edition: "2018".into(),
574+
root_module: "myproc_macro.rs".into(),
575+
is_workspace_member: true,
576+
deps: BTreeSet::new(),
577+
proc_macro_dylib_path: Some(
578+
"bazel-out/k8-opt-exec-F005BA11/bin/myproc_macro/libmyproc_macro-12345.so"
579+
.into()
580+
),
581+
source: None,
582+
cfg: vec!["test".into(), "debug_assertions".into()],
583+
env: BTreeMap::new(),
584+
target: "x86_64-unknown-linux-gnu".into(),
585+
crate_type: "proc_macro".into(),
586+
},])
587+
);
588+
}
589+
}
515590
}

0 commit comments

Comments
 (0)