Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set OUT_DIR for all units with build scripts #13204

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,8 @@ pub trait TestEnv: Sized {
.env_remove("RUSTFLAGS")
.env_remove("SSH_AUTH_SOCK") // ensure an outer agent is never contacted
.env_remove("USER") // not set on some rust-lang docker images
.env_remove("XDG_CONFIG_HOME"); // see #2345
.env_remove("XDG_CONFIG_HOME") // see #2345
.env_remove("OUT_DIR"); // see #13204
if cfg!(target_os = "macos") {
// Work-around a bug in macOS 10.15, see `link_or_copy` for details.
self = self.env("__CARGO_COPY_DONT_LINK_DO_NOT_USE_THIS", "1");
Expand Down
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));
}
}
}
Comment on lines +189 to +213
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding this, we only really need to set OUT_DIR once per package and the previous code did that be assuming the lib unit was sufficient (but some packages don't have that).

I'm assuming there isn't a more direct way to iterate over packages to find out the necessary information and set it, and instead we have to walk all units and de-dupe down to individual packages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I think your understanding is correct. Before https://github.com/rust-lang/cargo/pull/3310/files, we set it for every unit.
image


// 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
46 changes: 46 additions & 0 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4897,3 +4897,49 @@ fn cargo_test_print_env_verbose() {
)
.run();
}

#[cargo_test]
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
fn cargo_test_set_out_dir_env_var() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2021"
"#,
)
.file(
"src/lib.rs",
r#"
pub fn add(left: usize, right: usize) -> usize {
left + right
}
"#,
)
.file(
"build.rs",
r#"
fn main() {}
"#,
)
.file(
"tests/case.rs",
r#"
#[cfg(test)]
pub mod tests {
#[test]
fn test_add() {
assert!(std::env::var("OUT_DIR").is_ok());
assert_eq!(foo::add(2, 5), 7);
}
}
"#,
)
.build();

p.cargo("test").run();
p.cargo("test --package foo --test case -- tests::test_add --exact --nocapture")
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
.run();
}
Loading