Skip to content

Commit 3aaf4ee

Browse files
committed
fix: set OUT_DIR for all units with build scripts
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 509c75e commit 3aaf4ee

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/cargo/core/compiler/context/mod.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::util::errors::CargoResult;
1212
use crate::util::profile;
1313
use anyhow::{bail, Context as _};
1414
use filetime::FileTime;
15+
use itertools::Itertools;
1516
use jobserver::Client;
1617

1718
use super::build_plan::BuildPlan;
@@ -185,6 +186,32 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
185186
plan.output_plan(self.bcx.config);
186187
}
187188

189+
// Add `OUT_DIR` to env vars if unit has a build script.
190+
let units_with_build_script = &self
191+
.bcx
192+
.roots
193+
.iter()
194+
.filter(|unit| self.build_scripts.contains_key(unit))
195+
.dedup_by(|x, y| x.pkg.package_id() == y.pkg.package_id())
196+
.collect::<Vec<_>>();
197+
for unit in units_with_build_script {
198+
for dep in &self.bcx.unit_graph[unit] {
199+
if dep.unit.mode.is_run_custom_build() {
200+
let out_dir = self
201+
.files()
202+
.build_script_out_dir(&dep.unit)
203+
.display()
204+
.to_string();
205+
let script_meta = self.get_run_build_script_metadata(&dep.unit);
206+
self.compilation
207+
.extra_env
208+
.entry(script_meta)
209+
.or_insert_with(Vec::new)
210+
.push(("OUT_DIR".to_string(), out_dir));
211+
}
212+
}
213+
}
214+
188215
// Collect the result of the build into `self.compilation`.
189216
for unit in &self.bcx.roots {
190217
// Collect tests and executables.
@@ -213,26 +240,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
213240
}
214241
}
215242

216-
// If the unit has a build script, add `OUT_DIR` to the
217-
// environment variables.
218-
if unit.target.is_lib() {
219-
for dep in &self.bcx.unit_graph[unit] {
220-
if dep.unit.mode.is_run_custom_build() {
221-
let out_dir = self
222-
.files()
223-
.build_script_out_dir(&dep.unit)
224-
.display()
225-
.to_string();
226-
let script_meta = self.get_run_build_script_metadata(&dep.unit);
227-
self.compilation
228-
.extra_env
229-
.entry(script_meta)
230-
.or_insert_with(Vec::new)
231-
.push(("OUT_DIR".to_string(), out_dir));
232-
}
233-
}
234-
}
235-
236243
// Collect information for `rustdoc --test`.
237244
if unit.mode.is_doc_test() {
238245
let mut unstable_opts = false;

tests/testsuite/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,7 +4942,5 @@ fn cargo_test_set_out_dir_env_var() {
49424942
p.cargo("test").run();
49434943
p.cargo("test --package foo --test case -- tests::test_add --exact --nocapture")
49444944
.env_remove("OUT_DIR")
4945-
.with_stdout_contains("test tests::test_add ... FAILED")
4946-
.with_status(101)
49474945
.run();
49484946
}

0 commit comments

Comments
 (0)