Skip to content

Commit

Permalink
fix: set OUT_DIR for all units with build scripts
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Jan 8, 2024
1 parent 8bba4f4 commit 6739c7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
47 changes: 27 additions & 20 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -185,6 +186,32 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
plan.output_plan(self.bcx.config);
}

// Add `OUT_DIR` to env vars if unit has a build script.
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::<Vec<_>>();
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.
Expand Down Expand Up @@ -213,26 +240,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;
Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4941,7 +4941,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")
.with_stdout_contains("test tests::test_add ... FAILED")
.with_status(101)
.run();
}

0 comments on commit 6739c7e

Please sign in to comment.