Skip to content

Commit 0c98d6e

Browse files
committed
Auto merge of #13204 - hi-rustin:rustin-patch-test-out-dir, r=ehuss
fix: set OUT_DIR for all units with build scripts
2 parents 484f0f2 + 6739c7e commit 0c98d6e

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,8 @@ pub trait TestEnv: Sized {
12861286
.env_remove("RUSTFLAGS")
12871287
.env_remove("SSH_AUTH_SOCK") // ensure an outer agent is never contacted
12881288
.env_remove("USER") // not set on some rust-lang docker images
1289-
.env_remove("XDG_CONFIG_HOME"); // see #2345
1289+
.env_remove("XDG_CONFIG_HOME") // see #2345
1290+
.env_remove("OUT_DIR"); // see #13204
12901291
if cfg!(target_os = "macos") {
12911292
// Work-around a bug in macOS 10.15, see `link_or_copy` for details.
12921293
self = self.env("__CARGO_COPY_DONT_LINK_DO_NOT_USE_THIS", "1");

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,3 +4897,49 @@ fn cargo_test_print_env_verbose() {
48974897
)
48984898
.run();
48994899
}
4900+
4901+
#[cargo_test]
4902+
fn cargo_test_set_out_dir_env_var() {
4903+
let p = project()
4904+
.file(
4905+
"Cargo.toml",
4906+
r#"
4907+
[package]
4908+
name = "foo"
4909+
version = "0.0.1"
4910+
edition = "2021"
4911+
"#,
4912+
)
4913+
.file(
4914+
"src/lib.rs",
4915+
r#"
4916+
pub fn add(left: usize, right: usize) -> usize {
4917+
left + right
4918+
}
4919+
"#,
4920+
)
4921+
.file(
4922+
"build.rs",
4923+
r#"
4924+
fn main() {}
4925+
"#,
4926+
)
4927+
.file(
4928+
"tests/case.rs",
4929+
r#"
4930+
#[cfg(test)]
4931+
pub mod tests {
4932+
#[test]
4933+
fn test_add() {
4934+
assert!(std::env::var("OUT_DIR").is_ok());
4935+
assert_eq!(foo::add(2, 5), 7);
4936+
}
4937+
}
4938+
"#,
4939+
)
4940+
.build();
4941+
4942+
p.cargo("test").run();
4943+
p.cargo("test --package foo --test case -- tests::test_add --exact --nocapture")
4944+
.run();
4945+
}

0 commit comments

Comments
 (0)