Skip to content

Commit eeeacc4

Browse files
Apply environment set by build scripts
1 parent d6aa1ba commit eeeacc4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

crates/project_model/src/cargo_workspace.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct PackageData {
8888
pub edition: Edition,
8989
pub features: Vec<String>,
9090
pub cfgs: Vec<CfgFlag>,
91+
pub envs: Vec<(String, String)>,
9192
pub out_dir: Option<AbsPathBuf>,
9293
pub proc_macro_dylib_path: Option<AbsPathBuf>,
9394
}
@@ -173,11 +174,13 @@ impl CargoWorkspace {
173174

174175
let mut out_dir_by_id = FxHashMap::default();
175176
let mut cfgs = FxHashMap::default();
177+
let mut envs = FxHashMap::default();
176178
let mut proc_macro_dylib_paths = FxHashMap::default();
177179
if config.load_out_dirs_from_check {
178180
let resources = load_extern_resources(cargo_toml, config)?;
179181
out_dir_by_id = resources.out_dirs;
180182
cfgs = resources.cfgs;
183+
envs = resources.env;
181184
proc_macro_dylib_paths = resources.proc_dylib_paths;
182185
}
183186

@@ -205,6 +208,7 @@ impl CargoWorkspace {
205208
dependencies: Vec::new(),
206209
features: Vec::new(),
207210
cfgs: cfgs.get(&id).cloned().unwrap_or_default(),
211+
envs: envs.get(&id).cloned().unwrap_or_default(),
208212
out_dir: out_dir_by_id.get(&id).cloned(),
209213
proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(),
210214
});
@@ -289,6 +293,7 @@ pub(crate) struct ExternResources {
289293
out_dirs: FxHashMap<PackageId, AbsPathBuf>,
290294
proc_dylib_paths: FxHashMap<PackageId, AbsPathBuf>,
291295
cfgs: FxHashMap<PackageId, Vec<CfgFlag>>,
296+
env: FxHashMap<PackageId, Vec<(String, String)>>,
292297
}
293298

294299
pub(crate) fn load_extern_resources(
@@ -323,7 +328,13 @@ pub(crate) fn load_extern_resources(
323328
for message in cargo_metadata::Message::parse_stream(output.stdout.as_slice()) {
324329
if let Ok(message) = message {
325330
match message {
326-
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => {
331+
Message::BuildScriptExecuted(BuildScript {
332+
package_id,
333+
out_dir,
334+
cfgs,
335+
env,
336+
..
337+
}) => {
327338
let cfgs = {
328339
let mut acc = Vec::new();
329340
for cfg in cfgs {
@@ -341,8 +352,10 @@ pub(crate) fn load_extern_resources(
341352
if out_dir != PathBuf::default() {
342353
let out_dir = AbsPathBuf::assert(out_dir);
343354
res.out_dirs.insert(package_id.clone(), out_dir);
344-
res.cfgs.insert(package_id, cfgs);
355+
res.cfgs.insert(package_id.clone(), cfgs);
345356
}
357+
358+
res.env.insert(package_id, env);
346359
}
347360
Message::CompilerArtifact(message) => {
348361
if message.target.kind.contains(&"proc-macro".to_string()) {

crates/project_model/src/workspace.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,18 @@ fn add_target_crate_root(
453453
opts.extend(pkg.cfgs.iter().cloned());
454454
opts
455455
};
456+
456457
let mut env = Env::default();
458+
for (k, v) in &pkg.envs {
459+
env.set(k, v.clone());
460+
}
457461
if let Some(out_dir) = &pkg.out_dir {
458462
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
459463
if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
460464
env.set("OUT_DIR", out_dir);
461465
}
462466
}
467+
463468
let proc_macro =
464469
pkg.proc_macro_dylib_path.as_ref().map(|it| proc_macro_loader(&it)).unwrap_or_default();
465470

0 commit comments

Comments
 (0)