Skip to content

Commit

Permalink
Merge pull request #18807 from Veykril/push-vxopsoummyzx
Browse files Browse the repository at this point in the history
fix: Populate cargo config env vars for crates
  • Loading branch information
Veykril authored Dec 31, 2024
2 parents c86dd17 + 4284637 commit dcee5fd
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 96 deletions.
9 changes: 9 additions & 0 deletions crates/project-model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops;
use std::str::from_utf8;

use anyhow::Context;
use base_db::Env;
use cargo_metadata::{CargoOpt, MetadataCommand};
use la_arena::{Arena, Idx};
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
Expand Down Expand Up @@ -34,6 +35,8 @@ pub struct CargoWorkspace {
target_directory: AbsPathBuf,
manifest_path: ManifestPath,
is_virtual_workspace: bool,
/// Environment variables set in the `.cargo/config` file.
config_env: Env,
}

impl ops::Index<Package> for CargoWorkspace {
Expand Down Expand Up @@ -395,6 +398,7 @@ impl CargoWorkspace {
pub fn new(
mut meta: cargo_metadata::Metadata,
ws_manifest_path: ManifestPath,
cargo_config_env: Env,
) -> CargoWorkspace {
let mut pkg_by_id = FxHashMap::default();
let mut packages = Arena::default();
Expand Down Expand Up @@ -516,6 +520,7 @@ impl CargoWorkspace {
target_directory,
manifest_path: ws_manifest_path,
is_virtual_workspace,
config_env: cargo_config_env,
}
}

Expand Down Expand Up @@ -602,4 +607,8 @@ impl CargoWorkspace {
pub fn is_virtual_workspace(&self) -> bool {
self.is_virtual_workspace
}

pub fn env(&self) -> &Env {
&self.config_env
}
}
51 changes: 36 additions & 15 deletions crates/project-model/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Cargo-like environment variables injection.
use base_db::Env;
use paths::Utf8Path;
use rustc_hash::FxHashMap;
use toolchain::Tool;

Expand Down Expand Up @@ -73,7 +74,7 @@ pub(crate) fn cargo_config_env(
manifest: &ManifestPath,
extra_env: &FxHashMap<String, String>,
sysroot: &Sysroot,
) -> FxHashMap<String, String> {
) -> Env {
let mut cargo_config = sysroot.tool(Tool::Cargo, manifest.parent());
cargo_config.envs(extra_env);
cargo_config
Expand All @@ -85,7 +86,7 @@ pub(crate) fn cargo_config_env(
// if successful we receive `env.key.value = "value" per entry
tracing::debug!("Discovering cargo config env by {:?}", cargo_config);
utf8_stdout(&mut cargo_config)
.map(parse_output_cargo_config_env)
.map(|stdout| parse_output_cargo_config_env(manifest, stdout))
.inspect(|env| {
tracing::debug!("Discovered cargo config env: {:?}", env);
})
Expand All @@ -95,18 +96,38 @@ pub(crate) fn cargo_config_env(
.unwrap_or_default()
}

fn parse_output_cargo_config_env(stdout: String) -> FxHashMap<String, String> {
stdout
.lines()
.filter_map(|l| l.strip_prefix("env."))
.filter_map(|l| l.split_once(" = "))
.filter_map(|(k, v)| {
if k.contains('.') {
k.strip_suffix(".value").zip(Some(v))
} else {
Some((k, v))
fn parse_output_cargo_config_env(manifest: &ManifestPath, stdout: String) -> Env {
let mut env = Env::default();
let mut relatives = vec![];
for (key, val) in
stdout.lines().filter_map(|l| l.strip_prefix("env.")).filter_map(|l| l.split_once(" = "))
{
let val = val.trim_matches('"').to_owned();
if let Some((key, modifier)) = key.split_once('.') {
match modifier {
"relative" => relatives.push((key, val)),
"value" => _ = env.insert(key, val),
_ => {
tracing::warn!(
"Unknown modifier in cargo config env: {}, expected `relative` or `value`",
modifier
);
continue;
}
}
})
.map(|(key, value)| (key.to_owned(), value.trim_matches('"').to_owned()))
.collect()
} else {
env.insert(key, val);
}
}
// FIXME: The base here should be the parent of the `.cargo/config` file, not the manifest.
// But cargo does not provide this information.
let base = <_ as AsRef<Utf8Path>>::as_ref(manifest.parent());
for (key, val) in relatives {
if let Some(val) = env.get(&val) {
env.insert(key, base.join(val).to_string());
} else {
env.insert(key, base.to_string());
}
}
env
}
2 changes: 1 addition & 1 deletion crates/project-model/src/sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl Sysroot {
res.packages.remove(idx);
});

let cargo_workspace = CargoWorkspace::new(res, library_manifest);
let cargo_workspace = CargoWorkspace::new(res, library_manifest, Default::default());
Some(Sysroot {
root: sysroot_dir.clone(),
src_root: Some(sysroot_src_dir.clone()),
Expand Down
6 changes: 2 additions & 4 deletions crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ fn load_cargo_with_overrides(
let meta: Metadata = get_test_json_file(file);
let manifest_path =
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
let cargo_workspace = CargoWorkspace::new(meta, manifest_path, Default::default());
let project_workspace = ProjectWorkspace {
kind: ProjectWorkspaceKind::Cargo {
cargo: cargo_workspace,
build_scripts: WorkspaceBuildScripts::default(),
rustc: Err(None),
cargo_config_extra_env: Default::default(),
error: None,
set_test: true,
},
Expand Down Expand Up @@ -228,7 +227,7 @@ fn smoke_test_real_sysroot_cargo() {
let meta: Metadata = get_test_json_file("hello-world-metadata.json");
let manifest_path =
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
let cargo_workspace = CargoWorkspace::new(meta, manifest_path, Default::default());
let sysroot = Sysroot::discover(
AbsPath::assert(Utf8Path::new(env!("CARGO_MANIFEST_DIR"))),
&Default::default(),
Expand All @@ -240,7 +239,6 @@ fn smoke_test_real_sysroot_cargo() {
cargo: cargo_workspace,
build_scripts: WorkspaceBuildScripts::default(),
rustc: Err(None),
cargo_config_extra_env: Default::default(),
error: None,
set_test: true,
},
Expand Down
93 changes: 25 additions & 68 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ pub enum ProjectWorkspaceKind {
/// The rustc workspace loaded for this workspace. An `Err(None)` means loading has been
/// disabled or was otherwise not requested.
rustc: Result<Box<(CargoWorkspace, WorkspaceBuildScripts)>, Option<String>>,
/// Environment variables set in the `.cargo/config` file.
cargo_config_extra_env: FxHashMap<String, String>,
set_test: bool,
},
/// Project workspace was specified using a `rust-project.json` file.
Expand All @@ -99,8 +97,6 @@ pub enum ProjectWorkspaceKind {
file: ManifestPath,
/// Is this file a cargo script file?
cargo: Option<(CargoWorkspace, WorkspaceBuildScripts, Option<Arc<anyhow::Error>>)>,
/// Environment variables set in the `.cargo/config` file.
cargo_config_extra_env: FxHashMap<String, String>,
set_test: bool,
},
}
Expand All @@ -110,14 +106,7 @@ impl fmt::Debug for ProjectWorkspace {
// Make sure this isn't too verbose.
let Self { kind, sysroot, rustc_cfg, toolchain, target_layout, cfg_overrides } = self;
match kind {
ProjectWorkspaceKind::Cargo {
cargo,
error: _,
build_scripts,
rustc,
cargo_config_extra_env,
set_test,
} => f
ProjectWorkspaceKind::Cargo { cargo, error: _, build_scripts, rustc, set_test } => f
.debug_struct("Cargo")
.field("root", &cargo.workspace_root().file_name())
.field("n_packages", &cargo.packages().len())
Expand All @@ -130,7 +119,6 @@ impl fmt::Debug for ProjectWorkspace {
.field("n_cfg_overrides", &cfg_overrides.len())
.field("toolchain", &toolchain)
.field("data_layout", &target_layout)
.field("cargo_config_extra_env", &cargo_config_extra_env)
.field("set_test", set_test)
.field("build_scripts", &build_scripts.error().unwrap_or("ok"))
.finish(),
Expand All @@ -146,12 +134,7 @@ impl fmt::Debug for ProjectWorkspace {

debug_struct.finish()
}
ProjectWorkspaceKind::DetachedFile {
file,
cargo: cargo_script,
cargo_config_extra_env,
set_test,
} => f
ProjectWorkspaceKind::DetachedFile { file, cargo: cargo_script, set_test } => f
.debug_struct("DetachedFiles")
.field("file", &file)
.field("cargo_script", &cargo_script.is_some())
Expand All @@ -161,7 +144,6 @@ impl fmt::Debug for ProjectWorkspace {
.field("toolchain", &toolchain)
.field("data_layout", &target_layout)
.field("n_cfg_overrides", &cfg_overrides.len())
.field("cargo_config_extra_env", &cargo_config_extra_env)
.field("set_test", set_test)
.finish(),
}
Expand Down Expand Up @@ -289,14 +271,14 @@ impl ProjectWorkspace {
progress,
) {
Ok((meta, _error)) => {
let workspace = CargoWorkspace::new(meta, cargo_toml.clone());
let buildscripts = WorkspaceBuildScripts::rustc_crates(
let workspace = CargoWorkspace::new(meta, cargo_toml.clone(), Env::default());
let build_scripts = WorkspaceBuildScripts::rustc_crates(
&workspace,
cargo_toml.parent(),
&config.extra_env,
&sysroot,
);
Ok(Box::new((workspace, buildscripts)))
Ok(Box::new((workspace, build_scripts)))
}
Err(e) => {
tracing::error!(
Expand Down Expand Up @@ -348,14 +330,13 @@ impl ProjectWorkspace {
"Failed to read Cargo metadata from Cargo.toml file {cargo_toml}, {toolchain:?}",
)
})?;
let cargo = CargoWorkspace::new(meta, cargo_toml.clone());
let cargo_config_extra_env = cargo_config_env(cargo_toml, &config.extra_env, &sysroot);
let cargo = CargoWorkspace::new(meta, cargo_toml.clone(), cargo_config_extra_env);
Ok(ProjectWorkspace {
kind: ProjectWorkspaceKind::Cargo {
cargo,
build_scripts: WorkspaceBuildScripts::default(),
rustc,
cargo_config_extra_env,
error: error.map(Arc::new),
set_test: config.set_test,
},
Expand Down Expand Up @@ -450,19 +431,19 @@ impl ProjectWorkspace {
)
.ok()
.map(|(ws, error)| {
let cargo_config_extra_env =
cargo_config_env(detached_file, &config.extra_env, &sysroot);
(
CargoWorkspace::new(ws, detached_file.clone()),
CargoWorkspace::new(ws, detached_file.clone(), cargo_config_extra_env),
WorkspaceBuildScripts::default(),
error.map(Arc::new),
)
});

let cargo_config_extra_env = cargo_config_env(detached_file, &config.extra_env, &sysroot);
Ok(ProjectWorkspace {
kind: ProjectWorkspaceKind::DetachedFile {
file: detached_file.to_owned(),
cargo: cargo_script,
cargo_config_extra_env,
set_test: config.set_test,
},
sysroot,
Expand Down Expand Up @@ -643,14 +624,7 @@ impl ProjectWorkspace {
.chain(mk_sysroot())
.unique()
.collect(),
ProjectWorkspaceKind::Cargo {
cargo,
rustc,
build_scripts,
cargo_config_extra_env: _,
error: _,
set_test: _,
} => {
ProjectWorkspaceKind::Cargo { cargo, rustc, build_scripts, error: _, set_test: _ } => {
cargo
.packages()
.map(|pkg| {
Expand Down Expand Up @@ -787,23 +761,18 @@ impl ProjectWorkspace {
extra_env,
cfg_overrides,
),
ProjectWorkspaceKind::Cargo {
cargo,
rustc,
build_scripts,
cargo_config_extra_env: _,
error: _,
set_test,
} => cargo_to_crate_graph(
load,
rustc.as_ref().map(|a| a.as_ref()).ok(),
cargo,
sysroot,
rustc_cfg.clone(),
cfg_overrides,
build_scripts,
*set_test,
),
ProjectWorkspaceKind::Cargo { cargo, rustc, build_scripts, error: _, set_test } => {
cargo_to_crate_graph(
load,
rustc.as_ref().map(|a| a.as_ref()).ok(),
cargo,
sysroot,
rustc_cfg.clone(),
cfg_overrides,
build_scripts,
*set_test,
)
}
ProjectWorkspaceKind::DetachedFile { file, cargo: cargo_script, set_test, .. } => {
if let Some((cargo, build_scripts, _)) = cargo_script {
cargo_to_crate_graph(
Expand Down Expand Up @@ -848,45 +817,33 @@ impl ProjectWorkspace {
ProjectWorkspaceKind::Cargo {
cargo,
rustc,
cargo_config_extra_env,
build_scripts: _,
error: _,
set_test: _,
},
ProjectWorkspaceKind::Cargo {
cargo: o_cargo,
rustc: o_rustc,
cargo_config_extra_env: o_cargo_config_extra_env,
build_scripts: _,
error: _,
set_test: _,
},
) => {
cargo == o_cargo
&& rustc == o_rustc
&& cargo_config_extra_env == o_cargo_config_extra_env
}
) => cargo == o_cargo && rustc == o_rustc,
(ProjectWorkspaceKind::Json(project), ProjectWorkspaceKind::Json(o_project)) => {
project == o_project
}
(
ProjectWorkspaceKind::DetachedFile {
file,
cargo: Some((cargo_script, _, _)),
cargo_config_extra_env,
set_test: _,
},
ProjectWorkspaceKind::DetachedFile {
file: o_file,
cargo: Some((o_cargo_script, _, _)),
cargo_config_extra_env: o_cargo_config_extra_env,
set_test: _,
},
) => {
file == o_file
&& cargo_script == o_cargo_script
&& cargo_config_extra_env == o_cargo_config_extra_env
}
) => file == o_file && cargo_script == o_cargo_script,
_ => return false,
}) && sysroot == o_sysroot
&& rustc_cfg == o_rustc_cfg
Expand Down Expand Up @@ -1402,7 +1359,7 @@ fn add_target_crate_root(
opts
};

let mut env = Env::default();
let mut env = cargo.env().clone();
inject_cargo_package_env(&mut env, pkg);
inject_cargo_env(&mut env);
inject_rustc_tool_env(&mut env, cargo, cargo_name, kind);
Expand Down
1 change: 0 additions & 1 deletion crates/rust-analyzer/src/cli/rustc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl Tester {
kind: ProjectWorkspaceKind::DetachedFile {
file: ManifestPath::try_from(tmp_file).unwrap(),
cargo: None,
cargo_config_extra_env: Default::default(),
set_test: true,
},
sysroot,
Expand Down
Loading

0 comments on commit dcee5fd

Please sign in to comment.