Skip to content

Commit c503caa

Browse files
committed
Properly set the working directory for proc-macro execution
1 parent aa883b4 commit c503caa

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
320320
_: Span,
321321
_: Span,
322322
_: Span,
323+
_: Option<String>,
323324
) -> Result<Subtree, ProcMacroExpansionError> {
324325
let (parse, _) = syntax_bridge::token_tree_to_syntax_node(
325326
subtree,

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
2929
def_site: Span,
3030
call_site: Span,
3131
mixed_site: Span,
32+
current_dir: Option<String>,
3233
) -> Result<tt::Subtree, ProcMacroExpansionError>;
3334
}
3435

@@ -234,8 +235,18 @@ impl CustomProcMacroExpander {
234235
let krate_graph = db.crate_graph();
235236
// Proc macros have access to the environment variables of the invoking crate.
236237
let env = &krate_graph[calling_crate].env;
237-
match proc_macro.expander.expand(tt, attr_arg, env, def_site, call_site, mixed_site)
238-
{
238+
match proc_macro.expander.expand(
239+
tt,
240+
attr_arg,
241+
env,
242+
def_site,
243+
call_site,
244+
mixed_site,
245+
db.crate_workspace_data()[&calling_crate]
246+
.proc_macro_cwd
247+
.as_ref()
248+
.map(ToString::to_string),
249+
) {
239250
Ok(t) => ExpandResult::ok(t),
240251
Err(err) => match err {
241252
// Don't discard the item in case something unexpected happened while expanding attributes

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,17 @@ impl ProcMacroExpander for Expander {
493493
def_site: Span,
494494
call_site: Span,
495495
mixed_site: Span,
496+
current_dir: Option<String>,
496497
) -> Result<tt::Subtree<Span>, ProcMacroExpansionError> {
497-
match self.0.expand(subtree, attrs, env.clone(), def_site, call_site, mixed_site) {
498+
match self.0.expand(
499+
subtree,
500+
attrs,
501+
env.clone(),
502+
def_site,
503+
call_site,
504+
mixed_site,
505+
current_dir,
506+
) {
498507
Ok(Ok(subtree)) => Ok(subtree),
499508
Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
500509
Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),

src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,9 @@ impl ProcMacro {
152152
def_site: Span,
153153
call_site: Span,
154154
mixed_site: Span,
155+
current_dir: Option<String>,
155156
) -> Result<Result<tt::Subtree<Span>, PanicMessage>, ServerError> {
156157
let version = self.process.version();
157-
let current_dir =
158-
env.get("CARGO_RUSTC_CURRENT_DIR").or_else(|| env.get("CARGO_MANIFEST_DIR"));
159158

160159
let mut span_data_table = SpanDataIndexMap::default();
161160
let def_site = span_data_table.insert_full(def_site).0;

src/tools/rust-analyzer/crates/test-fixture/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ impl ProcMacroExpander for IdentityProcMacroExpander {
571571
_: Span,
572572
_: Span,
573573
_: Span,
574+
_: Option<String>,
574575
) -> Result<Subtree<Span>, ProcMacroExpansionError> {
575576
Ok(subtree.clone())
576577
}
@@ -588,6 +589,7 @@ impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
588589
_: Span,
589590
_: Span,
590591
_: Span,
592+
_: Option<String>,
591593
) -> Result<Subtree<Span>, ProcMacroExpansionError> {
592594
attrs
593595
.cloned()
@@ -606,6 +608,7 @@ impl ProcMacroExpander for MirrorProcMacroExpander {
606608
_: Span,
607609
_: Span,
608610
_: Span,
611+
_: Option<String>,
609612
) -> Result<Subtree<Span>, ProcMacroExpansionError> {
610613
fn traverse(input: &Subtree<Span>) -> Subtree<Span> {
611614
let mut token_trees = vec![];
@@ -636,6 +639,7 @@ impl ProcMacroExpander for ShortenProcMacroExpander {
636639
_: Span,
637640
_: Span,
638641
_: Span,
642+
_: Option<String>,
639643
) -> Result<Subtree<Span>, ProcMacroExpansionError> {
640644
return Ok(traverse(input));
641645

0 commit comments

Comments
 (0)