Skip to content

Commit dbc9fd7

Browse files
..
1 parent 88f4e24 commit dbc9fd7

File tree

10 files changed

+52
-29
lines changed

10 files changed

+52
-29
lines changed

crates/base-db/src/input.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ pub struct CrateData {
296296
pub dependencies: Vec<Dependency>,
297297
pub origin: CrateOrigin,
298298
pub is_proc_macro: bool,
299+
pub cwd: Option<AbsPathBuf>,
299300
}
300301

301302
#[derive(Default, Clone, PartialEq, Eq)]
@@ -360,8 +361,9 @@ impl CrateGraph {
360361
cfg_options: Arc<CfgOptions>,
361362
potential_cfg_options: Option<Arc<CfgOptions>>,
362363
mut env: Env,
363-
is_proc_macro: bool,
364364
origin: CrateOrigin,
365+
is_proc_macro: bool,
366+
cwd: Option<AbsPathBuf>,
365367
) -> CrateId {
366368
env.entries.shrink_to_fit();
367369
let data = CrateData {
@@ -375,6 +377,7 @@ impl CrateGraph {
375377
dependencies: Vec::new(),
376378
origin,
377379
is_proc_macro,
380+
cwd,
378381
};
379382
self.arena.alloc(data)
380383
}
@@ -698,8 +701,9 @@ mod tests {
698701
Default::default(),
699702
Default::default(),
700703
Env::default(),
701-
false,
702704
CrateOrigin::Local { repo: None, name: None },
705+
false,
706+
None,
703707
);
704708
let crate2 = graph.add_crate_root(
705709
FileId::from_raw(2u32),
@@ -709,8 +713,9 @@ mod tests {
709713
Default::default(),
710714
Default::default(),
711715
Env::default(),
712-
false,
713716
CrateOrigin::Local { repo: None, name: None },
717+
false,
718+
None,
714719
);
715720
let crate3 = graph.add_crate_root(
716721
FileId::from_raw(3u32),
@@ -720,8 +725,9 @@ mod tests {
720725
Default::default(),
721726
Default::default(),
722727
Env::default(),
723-
false,
724728
CrateOrigin::Local { repo: None, name: None },
729+
false,
730+
None,
725731
);
726732
assert!(graph
727733
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
@@ -745,8 +751,9 @@ mod tests {
745751
Default::default(),
746752
Default::default(),
747753
Env::default(),
748-
false,
749754
CrateOrigin::Local { repo: None, name: None },
755+
false,
756+
None,
750757
);
751758
let crate2 = graph.add_crate_root(
752759
FileId::from_raw(2u32),
@@ -756,8 +763,9 @@ mod tests {
756763
Default::default(),
757764
Default::default(),
758765
Env::default(),
759-
false,
760766
CrateOrigin::Local { repo: None, name: None },
767+
false,
768+
None,
761769
);
762770
assert!(graph
763771
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
@@ -778,8 +786,9 @@ mod tests {
778786
Default::default(),
779787
Default::default(),
780788
Env::default(),
781-
false,
782789
CrateOrigin::Local { repo: None, name: None },
790+
false,
791+
None,
783792
);
784793
let crate2 = graph.add_crate_root(
785794
FileId::from_raw(2u32),
@@ -789,8 +798,9 @@ mod tests {
789798
Default::default(),
790799
Default::default(),
791800
Env::default(),
792-
false,
793801
CrateOrigin::Local { repo: None, name: None },
802+
false,
803+
None,
794804
);
795805
let crate3 = graph.add_crate_root(
796806
FileId::from_raw(3u32),
@@ -800,8 +810,9 @@ mod tests {
800810
Default::default(),
801811
Default::default(),
802812
Env::default(),
803-
false,
804813
CrateOrigin::Local { repo: None, name: None },
814+
false,
815+
None,
805816
);
806817
assert!(graph
807818
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
@@ -822,8 +833,9 @@ mod tests {
822833
Default::default(),
823834
Default::default(),
824835
Env::default(),
825-
false,
826836
CrateOrigin::Local { repo: None, name: None },
837+
false,
838+
None,
827839
);
828840
let crate2 = graph.add_crate_root(
829841
FileId::from_raw(2u32),
@@ -833,8 +845,9 @@ mod tests {
833845
Default::default(),
834846
Default::default(),
835847
Env::default(),
836-
false,
837848
CrateOrigin::Local { repo: None, name: None },
849+
false,
850+
None,
838851
);
839852
assert!(graph
840853
.add_dep(

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.

crates/hir-expand/src/proc_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ 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-
env.get("CARGO_RUSTC_CURRENT_DIR").or_else(|| env.get("CARGO_MANIFEST_DIR"));
241+
let current_dir = krate_graph[calling_crate].cwd.as_ref().map(|it| it.to_string());
242+
243243
match proc_macro.expander.expand(
244244
tt,
245245
attr_arg,

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
})))

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+
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, " Cwd: {:?}\n", cwd);
8890
let deps = dependencies
8991
.iter()
9092
.map(|dep| format!("{}={}", dep.name, dep.crate_id.into_raw()))

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
})))

crates/project-model/src/project_json.rs

Lines changed: 4 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+
cwd: crate_data.cwd.map(absolutize_on_base),
167168
}
168169
})
169170
.collect(),
@@ -240,6 +241,7 @@ pub struct Crate {
240241
pub(crate) include: Vec<AbsPathBuf>,
241242
pub(crate) exclude: Vec<AbsPathBuf>,
242243
pub(crate) is_proc_macro: bool,
244+
pub(crate) cwd: Option<AbsPathBuf>,
243245
pub(crate) repository: Option<String>,
244246
pub build: Option<Build>,
245247
}
@@ -362,6 +364,8 @@ struct CrateData {
362364
repository: Option<String>,
363365
#[serde(default)]
364366
build: Option<BuildData>,
367+
#[serde(default)]
368+
cwd: Option<Utf8PathBuf>,
365369
}
366370

367371
mod cfg_ {

crates/project-model/src/workspace.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@ fn project_json_to_crate_graph(
965965
proc_macro_dylib_path,
966966
is_proc_macro,
967967
repository,
968+
cwd,
968969
..
969970
},
970971
file_id,
@@ -1003,7 +1004,6 @@ fn project_json_to_crate_graph(
10031004
Arc::new(cfg_options),
10041005
None,
10051006
env,
1006-
*is_proc_macro,
10071007
if let Some(name) = display_name.clone() {
10081008
CrateOrigin::Local {
10091009
repo: repository.clone(),
@@ -1012,6 +1012,8 @@ fn project_json_to_crate_graph(
10121012
} else {
10131013
CrateOrigin::Local { repo: None, name: None }
10141014
},
1015+
*is_proc_macro,
1016+
cwd.clone(),
10151017
);
10161018
debug!(
10171019
?crate_graph_crate_id,
@@ -1281,11 +1283,12 @@ fn detached_file_to_crate_graph(
12811283
cfg_options.clone(),
12821284
None,
12831285
Env::default(),
1284-
false,
12851286
CrateOrigin::Local {
12861287
repo: None,
12871288
name: display_name.map(|n| n.canonical_name().to_owned()),
12881289
},
1290+
false,
1291+
Some(detached_file.parent().to_path_buf()),
12891292
);
12901293

12911294
public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
@@ -1446,8 +1449,9 @@ fn add_target_crate_root(
14461449
Arc::new(cfg_options),
14471450
potential_cfg_options.map(Arc::new),
14481451
env,
1449-
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
14501452
origin,
1453+
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
1454+
Some(pkg.manifest.parent().to_path_buf()),
14511455
);
14521456
if let TargetKind::Lib { is_proc_macro: true } = kind {
14531457
let proc_macro = match build_data {
@@ -1585,8 +1589,9 @@ fn sysroot_to_crate_graph(
15851589
cfg_options.clone(),
15861590
None,
15871591
Env::default(),
1588-
false,
15891592
CrateOrigin::Lang(LangCrateOrigin::from(&*stitched[krate].name)),
1593+
false,
1594+
None,
15901595
);
15911596
Some((krate, crate_id))
15921597
})

crates/rust-analyzer/src/reload.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,6 @@ pub fn ws_to_crate_graph(
885885
ws_data.extend(mapping.values().copied().zip(iter::repeat(Arc::new(CrateWorkspaceData {
886886
toolchain: toolchain.clone(),
887887
data_layout: target_layout.clone(),
888-
proc_macro_cwd: Some(ws.workspace_root().to_owned()),
889888
}))));
890889
proc_macro_paths.push(crate_proc_macros);
891890
}

crates/test-fixture/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ impl ChangeFixture {
211211
From::from(meta.cfg.clone()),
212212
Some(From::from(meta.cfg)),
213213
meta.env,
214-
false,
215214
origin,
215+
false,
216+
None,
216217
);
217218
let prev = crates.insert(crate_name.clone(), crate_id);
218219
assert!(prev.is_none(), "multiple crates with same name: {crate_name}");
@@ -249,8 +250,9 @@ impl ChangeFixture {
249250
From::from(default_cfg.clone()),
250251
Some(From::from(default_cfg)),
251252
default_env,
252-
false,
253253
CrateOrigin::Local { repo: None, name: None },
254+
false,
255+
None,
254256
);
255257
} else {
256258
for (from, to, prelude) in crate_deps {
@@ -286,8 +288,9 @@ impl ChangeFixture {
286288
String::from("__ra_is_test_fixture"),
287289
String::from("__ra_is_test_fixture"),
288290
)]),
289-
false,
290291
CrateOrigin::Lang(LangCrateOrigin::Core),
292+
false,
293+
None,
291294
);
292295

293296
for krate in all_crates {
@@ -333,8 +336,9 @@ impl ChangeFixture {
333336
String::from("__ra_is_test_fixture"),
334337
String::from("__ra_is_test_fixture"),
335338
)]),
336-
true,
337339
CrateOrigin::Local { repo: None, name: None },
340+
true,
341+
None,
338342
);
339343
proc_macros.insert(proc_macros_crate, Ok(proc_macro));
340344

@@ -362,7 +366,6 @@ impl ChangeFixture {
362366
crate_graph
363367
.iter()
364368
.zip(iter::repeat(From::from(CrateWorkspaceData {
365-
proc_macro_cwd: None,
366369
data_layout: target_data_layout,
367370
toolchain,
368371
})))

0 commit comments

Comments
 (0)