Skip to content

Commit faf1b49

Browse files
committed
fix: Fix, clarify and require a value for proc_macro_cwd of CrateData
1 parent 2e1ff25 commit faf1b49

File tree

17 files changed

+143
-74
lines changed

17 files changed

+143
-74
lines changed

.github/workflows/ci.yaml

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ jobs:
2424
pull-requests: read
2525
outputs:
2626
typescript: ${{ steps.filter.outputs.typescript }}
27-
proc_macros: ${{ steps.filter.outputs.proc_macros }}
2827
steps:
2928
- uses: actions/checkout@v4
3029
- uses: dorny/paths-filter@1441771bbfdd59dcd748680ee64ebd8faab1a242
@@ -33,15 +32,10 @@ jobs:
3332
filters: |
3433
typescript:
3534
- 'editors/code/**'
36-
proc_macros:
37-
- 'crates/tt/**'
38-
- 'crates/proc-macro-api/**'
39-
- 'crates/proc-macro-srv/**'
40-
- 'crates/proc-macro-srv-cli/**'
4135
4236
proc-macro-srv:
4337
needs: changes
44-
if: github.repository == 'rust-lang/rust-analyzer' && needs.changes.outputs.proc_macros == 'true'
38+
if: github.repository == 'rust-lang/rust-analyzer'
4539
name: proc-macro-srv
4640
runs-on: ubuntu-latest
4741
env:

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/input.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,11 @@ pub struct CrateData<Id> {
303303
pub dependencies: Vec<Dependency<Id>>,
304304
pub origin: CrateOrigin,
305305
pub is_proc_macro: bool,
306-
/// The working directory to run proc-macros in. This is the workspace root of the cargo workspace
307-
/// for workspace members, the crate manifest dir otherwise.
308-
pub proc_macro_cwd: Option<AbsPathBuf>,
306+
/// The working directory to run proc-macros in invoked in the context of this crate.
307+
/// This is the workspace root of the cargo workspace for workspace members, the crate manifest
308+
/// dir otherwise.
309+
// FIXME: This ought to be a `VfsPath` or something opaque.
310+
pub proc_macro_cwd: Arc<AbsPathBuf>,
309311
}
310312

311313
pub type CrateDataBuilder = CrateData<CrateBuilderId>;
@@ -425,7 +427,7 @@ impl CrateGraphBuilder {
425427
mut env: Env,
426428
origin: CrateOrigin,
427429
is_proc_macro: bool,
428-
proc_macro_cwd: Option<AbsPathBuf>,
430+
proc_macro_cwd: Arc<AbsPathBuf>,
429431
ws_data: Arc<CrateWorkspaceData>,
430432
) -> CrateBuilderId {
431433
env.entries.shrink_to_fit();
@@ -861,6 +863,7 @@ impl fmt::Display for CyclicDependenciesError {
861863
#[cfg(test)]
862864
mod tests {
863865
use triomphe::Arc;
866+
use vfs::AbsPathBuf;
864867

865868
use crate::{CrateWorkspaceData, DependencyBuilder};
866869

@@ -883,7 +886,7 @@ mod tests {
883886
Env::default(),
884887
CrateOrigin::Local { repo: None, name: None },
885888
false,
886-
None,
889+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
887890
empty_ws_data(),
888891
);
889892
let crate2 = graph.add_crate_root(
@@ -896,7 +899,7 @@ mod tests {
896899
Env::default(),
897900
CrateOrigin::Local { repo: None, name: None },
898901
false,
899-
None,
902+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
900903
empty_ws_data(),
901904
);
902905
let crate3 = graph.add_crate_root(
@@ -909,7 +912,7 @@ mod tests {
909912
Env::default(),
910913
CrateOrigin::Local { repo: None, name: None },
911914
false,
912-
None,
915+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
913916
empty_ws_data(),
914917
);
915918
assert!(
@@ -942,7 +945,7 @@ mod tests {
942945
Env::default(),
943946
CrateOrigin::Local { repo: None, name: None },
944947
false,
945-
None,
948+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
946949
empty_ws_data(),
947950
);
948951
let crate2 = graph.add_crate_root(
@@ -955,7 +958,7 @@ mod tests {
955958
Env::default(),
956959
CrateOrigin::Local { repo: None, name: None },
957960
false,
958-
None,
961+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
959962
empty_ws_data(),
960963
);
961964
assert!(
@@ -983,7 +986,7 @@ mod tests {
983986
Env::default(),
984987
CrateOrigin::Local { repo: None, name: None },
985988
false,
986-
None,
989+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
987990
empty_ws_data(),
988991
);
989992
let crate2 = graph.add_crate_root(
@@ -996,7 +999,7 @@ mod tests {
996999
Env::default(),
9971000
CrateOrigin::Local { repo: None, name: None },
9981001
false,
999-
None,
1002+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
10001003
empty_ws_data(),
10011004
);
10021005
let crate3 = graph.add_crate_root(
@@ -1009,7 +1012,7 @@ mod tests {
10091012
Env::default(),
10101013
CrateOrigin::Local { repo: None, name: None },
10111014
false,
1012-
None,
1015+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
10131016
empty_ws_data(),
10141017
);
10151018
assert!(
@@ -1037,7 +1040,7 @@ mod tests {
10371040
Env::default(),
10381041
CrateOrigin::Local { repo: None, name: None },
10391042
false,
1040-
None,
1043+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
10411044
empty_ws_data(),
10421045
);
10431046
let crate2 = graph.add_crate_root(
@@ -1050,7 +1053,7 @@ mod tests {
10501053
Env::default(),
10511054
CrateOrigin::Local { repo: None, name: None },
10521055
false,
1053-
None,
1056+
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
10541057
empty_ws_data(),
10551058
);
10561059
assert!(

crates/hir-def/src/macro_expansion_tests/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
362362
_: Span,
363363
_: Span,
364364
_: Span,
365-
_: Option<String>,
365+
_: String,
366366
) -> Result<TopSubtree, ProcMacroExpansionError> {
367367
let (parse, _) = syntax_bridge::token_tree_to_syntax_node(
368368
subtree,

crates/hir-def/src/nameres/tests/incremental.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub const BAZ: u32 = 0;
5252
{
5353
// Add a dependency a -> b.
5454
let mut new_crate_graph = CrateGraphBuilder::default();
55+
5556
let mut add_crate = |crate_name, root_file_idx: usize| {
5657
new_crate_graph.add_crate_root(
5758
files[root_file_idx].file_id(),
@@ -63,7 +64,13 @@ pub const BAZ: u32 = 0;
6364
Env::default(),
6465
CrateOrigin::Local { repo: None, name: Some(Symbol::intern(crate_name)) },
6566
false,
66-
None,
67+
Arc::new(
68+
// FIXME: This is less than ideal
69+
TryFrom::try_from(
70+
&*std::env::current_dir().unwrap().as_path().to_string_lossy(),
71+
)
72+
.unwrap(),
73+
),
6774
Arc::new(CrateWorkspaceData { data_layout: Err("".into()), toolchain: None }),
6875
)
6976
};

crates/hir-expand/src/proc_macro.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe + AsAny {
4141
def_site: Span,
4242
call_site: Span,
4343
mixed_site: Span,
44-
current_dir: Option<String>,
44+
current_dir: String,
4545
) -> Result<tt::TopSubtree, ProcMacroExpansionError>;
4646

4747
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool;
@@ -318,8 +318,8 @@ impl CustomProcMacroExpander {
318318

319319
// Proc macros have access to the environment variables of the invoking crate.
320320
let env = calling_crate.env(db);
321-
let current_dir =
322-
calling_crate.data(db).proc_macro_cwd.as_deref().map(ToString::to_string);
321+
// FIXME: Can we avoid the string allocation here?
322+
let current_dir = calling_crate.data(db).proc_macro_cwd.to_string();
323323

324324
match proc_macro.expander.expand(
325325
tt,

crates/ide/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ impl Analysis {
244244
// FIXME: cfg options
245245
// Default to enable test for single file.
246246
let mut cfg_options = CfgOptions::default();
247+
248+
// FIXME: This is less than ideal
249+
let proc_macro_cwd = Arc::new(
250+
TryFrom::try_from(&*std::env::current_dir().unwrap().as_path().to_string_lossy())
251+
.unwrap(),
252+
);
247253
cfg_options.insert_atom(sym::test.clone());
248254
crate_graph.add_crate_root(
249255
file_id,
@@ -255,7 +261,7 @@ impl Analysis {
255261
Env::default(),
256262
CrateOrigin::Local { repo: None, name: None },
257263
false,
258-
None,
264+
proc_macro_cwd,
259265
Arc::new(CrateWorkspaceData {
260266
data_layout: Err("fixture has no layout".into()),
261267
toolchain: None,

crates/load-cargo/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl ProcMacroExpander for Expander {
496496
def_site: Span,
497497
call_site: Span,
498498
mixed_site: Span,
499-
current_dir: Option<String>,
499+
current_dir: String,
500500
) -> Result<tt::TopSubtree<Span>, ProcMacroExpansionError> {
501501
match self.0.expand(
502502
subtree.view(),

crates/proc-macro-api/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl ProcMacro {
170170
def_site: Span,
171171
call_site: Span,
172172
mixed_site: Span,
173-
current_dir: Option<String>,
173+
current_dir: String,
174174
) -> Result<Result<tt::TopSubtree<Span>, PanicMessage>, ServerError> {
175175
let version = self.process.version();
176176

@@ -198,7 +198,7 @@ impl ProcMacro {
198198
},
199199
lib: self.dylib_path.to_path_buf().into(),
200200
env,
201-
current_dir,
201+
current_dir: Some(current_dir),
202202
};
203203

204204
let response = self.process.send_task(Request::ExpandMacro(Box::new(task)))?;

0 commit comments

Comments
 (0)