Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct working directory for non-workspace proc-macro execution #19151

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ pub struct CrateData {
pub dependencies: Vec<Dependency>,
pub origin: CrateOrigin,
pub is_proc_macro: bool,
/// The working directory to run proc-macros in. This is the workspace root of the cargo workspace
/// for workspace members, the crate manifest dir otherwise.
pub proc_macro_cwd: Option<AbsPathBuf>,
}

#[derive(Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -360,8 +363,9 @@ impl CrateGraph {
cfg_options: Arc<CfgOptions>,
potential_cfg_options: Option<Arc<CfgOptions>>,
mut env: Env,
is_proc_macro: bool,
origin: CrateOrigin,
is_proc_macro: bool,
proc_macro_cwd: Option<AbsPathBuf>,
) -> CrateId {
env.entries.shrink_to_fit();
let data = CrateData {
Expand All @@ -375,6 +379,7 @@ impl CrateGraph {
dependencies: Vec::new(),
origin,
is_proc_macro,
proc_macro_cwd,
};
self.arena.alloc(data)
}
Expand Down Expand Up @@ -698,8 +703,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -709,8 +715,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate3 = graph.add_crate_root(
FileId::from_raw(3u32),
Expand All @@ -720,8 +727,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
Expand All @@ -745,8 +753,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -756,8 +765,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
Expand All @@ -778,8 +788,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -789,8 +800,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate3 = graph.add_crate_root(
FileId::from_raw(3u32),
Expand All @@ -800,8 +812,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
Expand All @@ -822,8 +835,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -833,8 +847,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(
Expand Down
4 changes: 1 addition & 3 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;
use span::EditionedFileId;
use syntax::{ast, Parse, SourceFile, SyntaxError};
use triomphe::Arc;
use vfs::{AbsPathBuf, FileId};
use vfs::FileId;

pub use crate::{
change::FileChange,
Expand Down Expand Up @@ -85,8 +85,6 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
/// Crate related data shared by the whole workspace.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct CrateWorkspaceData {
/// The working directory to run proc-macros in. This is usually the workspace root of cargo workspaces.
pub proc_macro_cwd: Option<AbsPathBuf>,
// FIXME: Consider removing this, making HirDatabase::target_data_layout an input query
pub data_layout: TargetLayoutLoadResult,
/// Toolchain version used to compile the crate.
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,17 @@ impl CustomProcMacroExpander {
let krate_graph = db.crate_graph();
// Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env;
let current_dir =
krate_graph[calling_crate].proc_macro_cwd.as_deref().map(ToString::to_string);

match proc_macro.expander.expand(
tt,
attr_arg,
env,
def_site,
call_site,
mixed_site,
db.crate_workspace_data()[&calling_crate]
.proc_macro_cwd
.as_ref()
.map(ToString::to_string),
current_dir,
) {
Ok(t) => ExpandResult::ok(t),
Err(err) => match err {
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ impl Analysis {
Arc::new(cfg_options),
None,
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
change.change_file(file_id, Some(text));
let ws_data = crate_graph
.iter()
.zip(iter::repeat(Arc::new(CrateWorkspaceData {
proc_macro_cwd: None,
data_layout: Err("fixture has no layout".into()),
toolchain: None,
})))
Expand Down
2 changes: 2 additions & 0 deletions crates/ide/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
dependencies,
origin,
is_proc_macro,
proc_macro_cwd,
} = &crate_graph[crate_id];
format_to!(
buf,
Expand All @@ -85,6 +86,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
format_to!(buf, " Env: {:?}\n", env);
format_to!(buf, " Origin: {:?}\n", origin);
format_to!(buf, " Is a proc macro crate: {}\n", is_proc_macro);
format_to!(buf, " Proc macro cwd: {:?}\n", proc_macro_cwd);
let deps = dependencies
.iter()
.map(|dep| format!("{}={}", dep.name, dep.crate_id.into_raw()))
Expand Down
1 change: 0 additions & 1 deletion crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ fn load_crate_graph(
let ws_data = crate_graph
.iter()
.zip(iter::repeat(From::from(CrateWorkspaceData {
proc_macro_cwd: None,
data_layout: target_layout.clone(),
toolchain: toolchain.clone(),
})))
Expand Down
5 changes: 5 additions & 0 deletions crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl ProjectJson {
is_proc_macro: crate_data.is_proc_macro,
repository: crate_data.repository,
build,
proc_macro_cwd: crate_data.proc_macro_cwd.map(absolutize_on_base),
}
})
.collect(),
Expand Down Expand Up @@ -240,6 +241,8 @@ pub struct Crate {
pub(crate) include: Vec<AbsPathBuf>,
pub(crate) exclude: Vec<AbsPathBuf>,
pub(crate) is_proc_macro: bool,
/// The working directory to run proc-macros in. This is usually the workspace root of cargo workspaces.
pub(crate) proc_macro_cwd: Option<AbsPathBuf>,
pub(crate) repository: Option<String>,
pub build: Option<Build>,
}
Expand Down Expand Up @@ -362,6 +365,8 @@ struct CrateData {
repository: Option<String>,
#[serde(default)]
build: Option<BuildData>,
#[serde(default)]
proc_macro_cwd: Option<Utf8PathBuf>,
}

mod cfg_ {
Expand Down
17 changes: 13 additions & 4 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ fn project_json_to_crate_graph(
is_proc_macro,
repository,
is_workspace_member,
proc_macro_cwd,
..
},
file_id,
Expand Down Expand Up @@ -1005,7 +1006,6 @@ fn project_json_to_crate_graph(
Arc::new(cfg_options),
None,
env,
*is_proc_macro,
if let Some(name) = display_name.clone() {
CrateOrigin::Local {
repo: repository.clone(),
Expand All @@ -1014,6 +1014,8 @@ fn project_json_to_crate_graph(
} else {
CrateOrigin::Local { repo: None, name: None }
},
*is_proc_macro,
proc_macro_cwd.clone(),
);
debug!(
?crate_graph_crate_id,
Expand Down Expand Up @@ -1283,11 +1285,12 @@ fn detached_file_to_crate_graph(
cfg_options.clone(),
None,
Env::default(),
false,
CrateOrigin::Local {
repo: None,
name: display_name.map(|n| n.canonical_name().to_owned()),
},
false,
None,
);

public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
Expand Down Expand Up @@ -1448,8 +1451,13 @@ fn add_target_crate_root(
Arc::new(cfg_options),
potential_cfg_options.map(Arc::new),
env,
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
origin,
matches!(kind, TargetKind::Lib { is_proc_macro: true }),
Some(if pkg.is_member {
cargo.workspace_root().to_path_buf()
} else {
pkg.manifest.parent().to_path_buf()
}),
);
if let TargetKind::Lib { is_proc_macro: true } = kind {
let proc_macro = match build_data {
Expand Down Expand Up @@ -1587,8 +1595,9 @@ fn sysroot_to_crate_graph(
cfg_options.clone(),
None,
Env::default(),
false,
CrateOrigin::Lang(LangCrateOrigin::from(&*stitched[krate].name)),
false,
None,
);
Some((krate, crate_id))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
),
},
is_proc_macro: false,
proc_macro_cwd: Some(
AbsPathBuf(
"$ROOT$hello-world",
),
),
},
1: CrateData {
root_file_id: FileId(
Expand Down Expand Up @@ -132,6 +137,11 @@
),
},
is_proc_macro: false,
proc_macro_cwd: Some(
AbsPathBuf(
"$ROOT$hello-world",
),
),
},
2: CrateData {
root_file_id: FileId(
Expand Down Expand Up @@ -203,6 +213,11 @@
),
},
is_proc_macro: false,
proc_macro_cwd: Some(
AbsPathBuf(
"$ROOT$hello-world",
),
),
},
3: CrateData {
root_file_id: FileId(
Expand Down Expand Up @@ -274,6 +289,11 @@
),
},
is_proc_macro: false,
proc_macro_cwd: Some(
AbsPathBuf(
"$ROOT$hello-world",
),
),
},
4: CrateData {
root_file_id: FileId(
Expand Down Expand Up @@ -341,5 +361,10 @@
name: "libc",
},
is_proc_macro: false,
proc_macro_cwd: Some(
AbsPathBuf(
"$ROOT$.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.98",
),
),
},
}
Loading