Auto merge of #17973 - Veykril:proc-macro-curr-dir, r=Veykril

Expand proc-macros in workspace root, not package root

Should fix https://github.com/rust-lang/rust-analyzer/issues/17748. The approach is generally not perfect though as rust-project.json projects don't benefit from this (still, nothing changes in that regard)
This commit is contained in:
bors 2024-08-27 11:53:04 +00:00
commit 06a40a61b0
7 changed files with 34 additions and 14 deletions

View file

@ -241,6 +241,10 @@ impl TargetKind {
pub fn is_executable(self) -> bool {
matches!(self, TargetKind::Bin | TargetKind::Example)
}
pub fn is_proc_macro(self) -> bool {
matches!(self, TargetKind::Lib { is_proc_macro: true })
}
}
// Deserialize helper for the cargo metadata

View file

@ -3,7 +3,7 @@ use base_db::Env;
use rustc_hash::FxHashMap;
use toolchain::Tool;
use crate::{utf8_stdout, ManifestPath, PackageData, Sysroot, TargetKind};
use crate::{utf8_stdout, CargoWorkspace, ManifestPath, PackageData, Sysroot, TargetKind};
/// Recreates the compile-time environment variables that Cargo sets.
///
@ -50,13 +50,23 @@ pub(crate) fn inject_cargo_env(env: &mut Env) {
env.set("CARGO", Tool::Cargo.path().to_string());
}
pub(crate) fn inject_rustc_tool_env(env: &mut Env, cargo_name: &str, kind: TargetKind) {
pub(crate) fn inject_rustc_tool_env(
env: &mut Env,
cargo: &CargoWorkspace,
cargo_name: &str,
kind: TargetKind,
) {
_ = kind;
// FIXME
// if kind.is_executable() {
// env.set("CARGO_BIN_NAME", cargo_name);
// }
env.set("CARGO_CRATE_NAME", cargo_name.replace('-', "_"));
// NOTE: Technically we should set this for all crates, but that will worsen the deduplication
// logic so for now just keeping it proc-macros ought to be fine.
if kind.is_proc_macro() {
env.set("CARGO_RUSTC_CURRENT_DIR", cargo.manifest_path().to_string());
}
}
pub(crate) fn cargo_config_env(

View file

@ -278,7 +278,7 @@ fn crate_graph_dedup() {
assert_eq!(regex_crate_graph.iter().count(), 60);
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros, |(_, a), (_, b)| a == b);
assert_eq!(crate_graph.iter().count(), 118);
assert_eq!(crate_graph.iter().count(), 119);
}
#[test]

View file

@ -1027,6 +1027,7 @@ fn cargo_to_crate_graph(
let crate_id = add_target_crate_root(
crate_graph,
proc_macros,
cargo,
pkg_data,
build_data,
cfg_options.clone(),
@ -1246,6 +1247,7 @@ fn handle_rustc_crates(
let crate_id = add_target_crate_root(
crate_graph,
proc_macros,
rustc_workspace,
&rustc_workspace[pkg],
build_scripts.get_output(pkg),
cfg_options.clone(),
@ -1305,6 +1307,7 @@ fn handle_rustc_crates(
fn add_target_crate_root(
crate_graph: &mut CrateGraph,
proc_macros: &mut ProcMacroPaths,
cargo: &CargoWorkspace,
pkg: &PackageData,
build_data: Option<&BuildScriptOutput>,
cfg_options: CfgOptions,
@ -1338,7 +1341,7 @@ fn add_target_crate_root(
let mut env = Env::default();
inject_cargo_package_env(&mut env, pkg);
inject_cargo_env(&mut env);
inject_rustc_tool_env(&mut env, cargo_name, kind);
inject_rustc_tool_env(&mut env, cargo, cargo_name, kind);
if let Some(envs) = build_data.map(|it| &it.envs) {
for (k, v) in envs {