-
Notifications
You must be signed in to change notification settings - Fork 591
Remove non-deterministic files from OUT_DIR to fix TreeArtifact cache misses #3973
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
cargo/tests/cargo_build_script/nondeterministic_out_dir/BUILD.bazel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| load("//cargo:defs.bzl", "cargo_build_script") | ||
| load("//rust:defs.bzl", "rust_test") | ||
|
|
||
| cargo_build_script( | ||
| name = "build_script", | ||
| srcs = ["build.rs"], | ||
| edition = "2021", | ||
| ) | ||
|
|
||
| rust_test( | ||
| name = "test", | ||
| srcs = ["test.rs"], | ||
| edition = "2021", | ||
| deps = [":build_script"], | ||
| ) |
34 changes: 34 additions & 0 deletions
34
cargo/tests/cargo_build_script/nondeterministic_out_dir/build.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| fn main() { | ||
| let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); | ||
|
|
||
| // Simulate files written by autoconf/cmake/mklove that embed sandbox-specific | ||
| // paths and must be stripped by the build script runner before Bazel captures | ||
| // OUT_DIR as a TreeArtifact. | ||
| std::fs::write( | ||
| out_dir.join("config.log"), | ||
| "configure log with /sandbox/path", | ||
| ) | ||
| .unwrap(); | ||
| std::fs::write(out_dir.join("config.status"), "configure status").unwrap(); | ||
| std::fs::write(out_dir.join("Makefile"), "all:\n\t@echo sandbox path here").unwrap(); | ||
| std::fs::write(out_dir.join("Makefile.config"), "CFLAGS=-I/sandbox/include").unwrap(); | ||
| std::fs::write( | ||
| out_dir.join("config.cache"), | ||
| "# generated at Mon Jan 1 00:00:00 UTC 2024", | ||
| ) | ||
| .unwrap(); | ||
| std::fs::write(out_dir.join("foo.d"), "foo.o: foo.c /sandbox/include/bar.h").unwrap(); | ||
| std::fs::write(out_dir.join("baz.d"), "baz.o: baz.c /sandbox/include/qux.h").unwrap(); | ||
| std::fs::write( | ||
| out_dir.join("foo.pc"), | ||
| "prefix=/sandbox/out\nexec_prefix=${prefix}", | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| // Write a legitimate output that downstream consumers must be able to read. | ||
| std::fs::write(out_dir.join("output.txt"), "legitimate output").unwrap(); | ||
|
|
||
| println!("cargo:rerun-if-changed=build.rs"); | ||
| } |
75 changes: 75 additions & 0 deletions
75
cargo/tests/cargo_build_script/nondeterministic_out_dir/test.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| //! include_str! resolves at compile time against the TreeArtifact captured by Bazel. | ||
| //! If the runner failed to strip config.log / *.d / *.pc files, the TreeArtifact hash | ||
| //! would change on every run, causing unnecessary rebuilds for all downstream crates. | ||
|
|
||
| const OUTPUT: &str = include_str!(concat!(env!("OUT_DIR"), "/output.txt")); | ||
|
|
||
| #[test] | ||
| fn legitimate_output_survives_nondeterministic_file_removal() { | ||
| assert_eq!(OUTPUT, "legitimate output"); | ||
| } | ||
|
|
||
| // Verify that volatile files written by the build script are absent from the | ||
| // captured OUT_DIR TreeArtifact. The build script wrote each of these; the | ||
| // cargo_build_script_runner must have removed them before Bazel snapshotted | ||
| // the directory. | ||
|
|
||
| #[test] | ||
| fn config_log_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/config.log")).exists(), | ||
| "config.log should have been removed from OUT_DIR" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn config_status_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/config.status")).exists(), | ||
| "config.status should have been removed from OUT_DIR" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn makefile_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/Makefile")).exists(), | ||
| "Makefile should have been removed from OUT_DIR" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn makefile_config_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/Makefile.config")).exists(), | ||
| "Makefile.config should have been removed from OUT_DIR" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn config_cache_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/config.cache")).exists(), | ||
| "config.cache should have been removed from OUT_DIR" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dot_d_files_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/foo.d")).exists(), | ||
| "foo.d should have been removed from OUT_DIR" | ||
| ); | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/baz.d")).exists(), | ||
| "baz.d should have been removed from OUT_DIR" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dot_pc_file_removed() { | ||
| assert!( | ||
| !std::path::Path::new(concat!(env!("OUT_DIR"), "/foo.pc")).exists(), | ||
| "foo.pc should have been removed from OUT_DIR" | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.