diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs index cfbfccb30451..34624472e3b6 100644 --- a/src/cargo/core/compiler/context/mod.rs +++ b/src/cargo/core/compiler/context/mod.rs @@ -12,6 +12,7 @@ use crate::util::errors::CargoResult; use crate::util::profile; use anyhow::{bail, Context as _}; use filetime::FileTime; +use itertools::Itertools; use jobserver::Client; use super::build_plan::BuildPlan; @@ -185,6 +186,33 @@ impl<'a, 'cfg> Context<'a, 'cfg> { plan.output_plan(self.bcx.config); } + // If the unit has a build script, add `OUT_DIR` to the + // environment variables. + let units_with_build_script = &self + .bcx + .roots + .iter() + .filter(|unit| self.build_scripts.contains_key(unit)) + .dedup_by(|x, y| x.pkg.package_id() == y.pkg.package_id()) + .collect::>(); + for unit in units_with_build_script { + for dep in &self.bcx.unit_graph[unit] { + if dep.unit.mode.is_run_custom_build() { + let out_dir = self + .files() + .build_script_out_dir(&dep.unit) + .display() + .to_string(); + let script_meta = self.get_run_build_script_metadata(&dep.unit); + self.compilation + .extra_env + .entry(script_meta) + .or_insert_with(Vec::new) + .push(("OUT_DIR".to_string(), out_dir)); + } + } + } + // Collect the result of the build into `self.compilation`. for unit in &self.bcx.roots { // Collect tests and executables. @@ -213,26 +241,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> { } } - // If the unit has a build script, add `OUT_DIR` to the - // environment variables. - if unit.target.is_lib() { - for dep in &self.bcx.unit_graph[unit] { - if dep.unit.mode.is_run_custom_build() { - let out_dir = self - .files() - .build_script_out_dir(&dep.unit) - .display() - .to_string(); - let script_meta = self.get_run_build_script_metadata(&dep.unit); - self.compilation - .extra_env - .entry(script_meta) - .or_insert_with(Vec::new) - .push(("OUT_DIR".to_string(), out_dir)); - } - } - } - // Collect information for `rustdoc --test`. if unit.mode.is_doc_test() { let mut unstable_opts = false; diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index cba2c5f1dccd..d969d32cd707 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -4942,7 +4942,5 @@ fn cargo_test_set_out_dir_env_var() { p.cargo("test").run(); p.cargo("test --package foo --test case -- tests::test_add --exact --nocapture") .env_remove("OUT_DIR") - .with_stdout_contains("test tests::test_add ... FAILED") - .with_status(101) .run(); }