Skip to content

Commit 4d386f7

Browse files
Use correct working directory for non-workspace proc-macro execution
1 parent e08736d commit 4d386f7

File tree

15 files changed

+159
-31
lines changed

15 files changed

+159
-31
lines changed

src/tools/rust-analyzer/crates/base-db/src/input.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ pub struct CrateData {
296296
pub dependencies: Vec<Dependency>,
297297
pub origin: CrateOrigin,
298298
pub is_proc_macro: bool,
299+
/// The working directory to run proc-macros in. This is the workspace root of the cargo workspace
300+
/// for workspace members, the crate manifest dir otherwise.
301+
pub proc_macro_cwd: Option<AbsPathBuf>,
299302
}
300303

301304
#[derive(Default, Clone, PartialEq, Eq)]
@@ -360,8 +363,9 @@ impl CrateGraph {
360363
cfg_options: Arc<CfgOptions>,
361364
potential_cfg_options: Option<Arc<CfgOptions>>,
362365
mut env: Env,
363-
is_proc_macro: bool,
364366
origin: CrateOrigin,
367+
is_proc_macro: bool,
368+
proc_macro_cwd: Option<AbsPathBuf>,
365369
) -> CrateId {
366370
env.entries.shrink_to_fit();
367371
let data = CrateData {
@@ -375,6 +379,7 @@ impl CrateGraph {
375379
dependencies: Vec::new(),
376380
origin,
377381
is_proc_macro,
382+
proc_macro_cwd,
378383
};
379384
self.arena.alloc(data)
380385
}
@@ -698,8 +703,9 @@ mod tests {
698703
Default::default(),
699704
Default::default(),
700705
Env::default(),
701-
false,
702706
CrateOrigin::Local { repo: None, name: None },
707+
false,
708+
None,
703709
);
704710
let crate2 = graph.add_crate_root(
705711
FileId::from_raw(2u32),
@@ -709,8 +715,9 @@ mod tests {
709715
Default::default(),
710716
Default::default(),
711717
Env::default(),
712-
false,
713718
CrateOrigin::Local { repo: None, name: None },
719+
false,
720+
None,
714721
);
715722
let crate3 = graph.add_crate_root(
716723
FileId::from_raw(3u32),
@@ -720,8 +727,9 @@ mod tests {
720727
Default::default(),
721728
Default::default(),
722729
Env::default(),
723-
false,
724730
CrateOrigin::Local { repo: None, name: None },
731+
false,
732+
None,
725733
);
726734
assert!(graph
727735
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
@@ -745,8 +753,9 @@ mod tests {
745753
Default::default(),
746754
Default::default(),
747755
Env::default(),
748-
false,
749756
CrateOrigin::Local { repo: None, name: None },
757+
false,
758+
None,
750759
);
751760
let crate2 = graph.add_crate_root(
752761
FileId::from_raw(2u32),
@@ -756,8 +765,9 @@ mod tests {
756765
Default::default(),
757766
Default::default(),
758767
Env::default(),
759-
false,
760768
CrateOrigin::Local { repo: None, name: None },
769+
false,
770+
None,
761771
);
762772
assert!(graph
763773
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
@@ -778,8 +788,9 @@ mod tests {
778788
Default::default(),
779789
Default::default(),
780790
Env::default(),
781-
false,
782791
CrateOrigin::Local { repo: None, name: None },
792+
false,
793+
None,
783794
);
784795
let crate2 = graph.add_crate_root(
785796
FileId::from_raw(2u32),
@@ -789,8 +800,9 @@ mod tests {
789800
Default::default(),
790801
Default::default(),
791802
Env::default(),
792-
false,
793803
CrateOrigin::Local { repo: None, name: None },
804+
false,
805+
None,
794806
);
795807
let crate3 = graph.add_crate_root(
796808
FileId::from_raw(3u32),
@@ -800,8 +812,9 @@ mod tests {
800812
Default::default(),
801813
Default::default(),
802814
Env::default(),
803-
false,
804815
CrateOrigin::Local { repo: None, name: None },
816+
false,
817+
None,
805818
);
806819
assert!(graph
807820
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
@@ -822,8 +835,9 @@ mod tests {
822835
Default::default(),
823836
Default::default(),
824837
Env::default(),
825-
false,
826838
CrateOrigin::Local { repo: None, name: None },
839+
false,
840+
None,
827841
);
828842
let crate2 = graph.add_crate_root(
829843
FileId::from_raw(2u32),
@@ -833,8 +847,9 @@ mod tests {
833847
Default::default(),
834848
Default::default(),
835849
Env::default(),
836-
false,
837850
CrateOrigin::Local { repo: None, name: None },
851+
false,
852+
None,
838853
);
839854
assert!(graph
840855
.add_dep(

src/tools/rust-analyzer/crates/base-db/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;
1010
use span::EditionedFileId;
1111
use syntax::{ast, Parse, SourceFile, SyntaxError};
1212
use triomphe::Arc;
13-
use vfs::{AbsPathBuf, FileId};
13+
use vfs::FileId;
1414

1515
pub use crate::{
1616
change::FileChange,
@@ -85,8 +85,6 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
8585
/// Crate related data shared by the whole workspace.
8686
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
8787
pub struct CrateWorkspaceData {
88-
/// The working directory to run proc-macros in. This is usually the workspace root of cargo workspaces.
89-
pub proc_macro_cwd: Option<AbsPathBuf>,
9088
// FIXME: Consider removing this, making HirDatabase::target_data_layout an input query
9189
pub data_layout: TargetLayoutLoadResult,
9290
/// Toolchain version used to compile the crate.

src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,17 @@ impl CustomProcMacroExpander {
238238
let krate_graph = db.crate_graph();
239239
// Proc macros have access to the environment variables of the invoking crate.
240240
let env = &krate_graph[calling_crate].env;
241+
let current_dir =
242+
krate_graph[calling_crate].proc_macro_cwd.as_deref().map(ToString::to_string);
243+
241244
match proc_macro.expander.expand(
242245
tt,
243246
attr_arg,
244247
env,
245248
def_site,
246249
call_site,
247250
mixed_site,
248-
db.crate_workspace_data()[&calling_crate]
249-
.proc_macro_cwd
250-
.as_ref()
251-
.map(ToString::to_string),
251+
current_dir,
252252
) {
253253
Ok(t) => ExpandResult::ok(t),
254254
Err(err) => match err {

src/tools/rust-analyzer/crates/ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ impl Analysis {
252252
Arc::new(cfg_options),
253253
None,
254254
Env::default(),
255-
false,
256255
CrateOrigin::Local { repo: None, name: None },
256+
false,
257+
None,
257258
);
258259
change.change_file(file_id, Some(text));
259260
let ws_data = crate_graph
260261
.iter()
261262
.zip(iter::repeat(Arc::new(CrateWorkspaceData {
262-
proc_macro_cwd: None,
263263
data_layout: Err("fixture has no layout".into()),
264264
toolchain: None,
265265
})))

src/tools/rust-analyzer/crates/ide/src/status.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
6868
dependencies,
6969
origin,
7070
is_proc_macro,
71+
proc_macro_cwd,
7172
} = &crate_graph[crate_id];
7273
format_to!(
7374
buf,
@@ -85,6 +86,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
8586
format_to!(buf, " Env: {:?}\n", env);
8687
format_to!(buf, " Origin: {:?}\n", origin);
8788
format_to!(buf, " Is a proc macro crate: {}\n", is_proc_macro);
89+
format_to!(buf, " Proc macro cwd: {:?}\n", proc_macro_cwd);
8890
let deps = dependencies
8991
.iter()
9092
.map(|dep| format!("{}={}", dep.name, dep.crate_id.into_raw()))

src/tools/rust-analyzer/crates/load-cargo/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ fn load_crate_graph(
456456
let ws_data = crate_graph
457457
.iter()
458458
.zip(iter::repeat(From::from(CrateWorkspaceData {
459-
proc_macro_cwd: None,
460459
data_layout: target_layout.clone(),
461460
toolchain: toolchain.clone(),
462461
})))

src/tools/rust-analyzer/crates/project-model/src/project_json.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl ProjectJson {
164164
is_proc_macro: crate_data.is_proc_macro,
165165
repository: crate_data.repository,
166166
build,
167+
proc_macro_cwd: crate_data.proc_macro_cwd.map(absolutize_on_base),
167168
}
168169
})
169170
.collect(),
@@ -240,6 +241,8 @@ pub struct Crate {
240241
pub(crate) include: Vec<AbsPathBuf>,
241242
pub(crate) exclude: Vec<AbsPathBuf>,
242243
pub(crate) is_proc_macro: bool,
244+
/// The working directory to run proc-macros in. This is usually the workspace root of cargo workspaces.
245+
pub(crate) proc_macro_cwd: Option<AbsPathBuf>,
243246
pub(crate) repository: Option<String>,
244247
pub build: Option<Build>,
245248
}
@@ -362,6 +365,8 @@ struct CrateData {
362365
repository: Option<String>,
363366
#[serde(default)]
364367
build: Option<BuildData>,
368+
#[serde(default)]
369+
proc_macro_cwd: Option<Utf8PathBuf>,
365370
}
366371

367372
mod cfg_ {

src/tools/rust-analyzer/crates/project-model/src/workspace.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ fn project_json_to_crate_graph(
958958
is_proc_macro,
959959
repository,
960960
is_workspace_member,
961+
proc_macro_cwd,
961962
..
962963
},
963964
file_id,
@@ -1005,7 +1006,6 @@ fn project_json_to_crate_graph(
10051006
Arc::new(cfg_options),
10061007
None,
10071008
env,
1008-
*is_proc_macro,
10091009
if let Some(name) = display_name.clone() {
10101010
CrateOrigin::Local {
10111011
repo: repository.clone(),
@@ -1014,6 +1014,8 @@ fn project_json_to_crate_graph(
10141014
} else {
10151015
CrateOrigin::Local { repo: None, name: None }
10161016
},
1017+
*is_proc_macro,
1018+
proc_macro_cwd.clone(),
10171019
);
10181020
debug!(
10191021
?crate_graph_crate_id,
@@ -1283,11 +1285,12 @@ fn detached_file_to_crate_graph(
12831285
cfg_options.clone(),
12841286
None,
12851287
Env::default(),
1286-
false,
12871288
CrateOrigin::Local {
12881289
repo: None,
12891290
name: display_name.map(|n| n.canonical_name().to_owned()),
12901291
},
1292+
false,
1293+
None,
12911294
);
12921295

12931296
public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
@@ -1448,8 +1451,13 @@ fn add_target_crate_root(
14481451
Arc::new(cfg_options),
14491452
potential_cfg_options.map(Arc::new),
14501453
env,
1451-
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
14521454
origin,
1455+
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
1456+
Some(if pkg.is_member {
1457+
cargo.workspace_root().to_path_buf()
1458+
} else {
1459+
pkg.manifest.parent().to_path_buf()
1460+
}),
14531461
);
14541462
if let TargetKind::Lib { is_proc_macro: true } = kind {
14551463
let proc_macro = match build_data {
@@ -1587,8 +1595,9 @@ fn sysroot_to_crate_graph(
15871595
cfg_options.clone(),
15881596
None,
15891597
Env::default(),
1590-
false,
15911598
CrateOrigin::Lang(LangCrateOrigin::from(&*stitched[krate].name)),
1599+
false,
1600+
None,
15921601
);
15931602
Some((krate, crate_id))
15941603
})

src/tools/rust-analyzer/crates/project-model/test_data/output/cargo_hello_world_project_model.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
),
6262
},
6363
is_proc_macro: false,
64+
proc_macro_cwd: Some(
65+
AbsPathBuf(
66+
"$ROOT$hello-world",
67+
),
68+
),
6469
},
6570
1: CrateData {
6671
root_file_id: FileId(
@@ -132,6 +137,11 @@
132137
),
133138
},
134139
is_proc_macro: false,
140+
proc_macro_cwd: Some(
141+
AbsPathBuf(
142+
"$ROOT$hello-world",
143+
),
144+
),
135145
},
136146
2: CrateData {
137147
root_file_id: FileId(
@@ -203,6 +213,11 @@
203213
),
204214
},
205215
is_proc_macro: false,
216+
proc_macro_cwd: Some(
217+
AbsPathBuf(
218+
"$ROOT$hello-world",
219+
),
220+
),
206221
},
207222
3: CrateData {
208223
root_file_id: FileId(
@@ -274,6 +289,11 @@
274289
),
275290
},
276291
is_proc_macro: false,
292+
proc_macro_cwd: Some(
293+
AbsPathBuf(
294+
"$ROOT$hello-world",
295+
),
296+
),
277297
},
278298
4: CrateData {
279299
root_file_id: FileId(
@@ -341,5 +361,10 @@
341361
name: "libc",
342362
},
343363
is_proc_macro: false,
364+
proc_macro_cwd: Some(
365+
AbsPathBuf(
366+
"$ROOT$.cargo/registry/src/i.8713187.xyz-1ecc6299db9ec823/libc-0.2.98",
367+
),
368+
),
344369
},
345370
}

0 commit comments

Comments
 (0)