Skip to content

Commit ca11ee8

Browse files
authored
Refactor: optimize cargo clean and improve test reliability (#230)
* Improves test reliability and cargo clean behavior Adds file-based locking to prevent parallel test execution conflicts that could cause race conditions when multiple tests run simultaneously on the same project directories. This race conditions problem have already caused several test failures in github CI. Optimizes cargo clean to use --workspace flag, avoiding unnecessary removal of dependency artifacts which are time-consuming to rebuild. Runs clean both before and after cargo check to prevent output pollution. Refactors test assertions to use helper functions with clearer error reporting that shows both the expected pattern and full output when assertions fail. Adds debug logging to distinguish between rapx analysis runs and standard rustc compilations for better troubleshooting. * fix copilot review advise * fix
1 parent 8a3798d commit ca11ee8

6 files changed

Lines changed: 197 additions & 130 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ UPG/
66
tests/**/Cargo.lock
77
**/*.dot
88
**/*.png
9+
.rapx-test.lock

rapx/Cargo.lock

Lines changed: 42 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rapx/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ itertools = "0.14.0"
4747
safety-parser = "0.4.1"
4848
syn = { version = "2", features = ["extra-traits", "full"] }
4949
rust_intervals = "0.3.0"
50-
clap = { version = "4.5.60"}
50+
clap = { version = "4.5.60" }
5151
clap-cargo = "0.18.3"
5252
shlex = "1.3.0"
5353
color-print = "0.3.7"
54+
fs4 = "0.13.1"
5455
[features]
5556
backtraces = ["snafu/backtraces", "snafu/backtraces-impl-backtrace-crate"]
5657

rapx/src/bin/cargo-rapx/cargo_check.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,21 @@ fn cargo_check(dir: &Utf8Path) {
6868
} else if !child.wait().unwrap().success() {
6969
rap_error_and_exit("Finished with non-zero exit code.");
7070
}
71+
72+
// Run `cargo clean` again to avoid polluting
73+
// the output of `cargo check`
74+
cargo_clean(dir, args::rap_clean());
7175
}
7276

7377
fn cargo_clean(dir: &Utf8Path, really: bool) {
7478
if really {
7579
rap_trace!("cargo clean in package folder {dir}");
76-
if let Err(err) = Command::new("cargo").arg("clean").current_dir(dir).output() {
80+
if let Err(err) = Command::new("cargo")
81+
.arg("clean")
82+
.arg("--workspace") // use --workspace to clean all members of the workspace, which may be more thorough but also more time-consuming to rebuild
83+
.current_dir(dir)
84+
.output()
85+
{
7786
rap_error_and_exit(format!("`cargo clean` exits unexpectedly:\n{err}"));
7887
}
7988
}

rapx/src/bin/cargo-rapx/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ fn phase_rustc_wrapper() {
2727
rap_trace!("Launch cargo-rapx again triggered by cargo check.");
2828

2929
let is_primary = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
30+
let package_name = env::var("CARGO_PKG_NAME").unwrap_or_default();
3031

3132
// check `CARGO_PRIMARY_PACKAGE` to make sure we only run
3233
// rapx for the local crate, but not dependencies.
3334
// rapx only checks local crates
3435
if is_primary {
36+
rap_debug!("run rapx for package {}", package_name);
3537
run_rap();
3638
return;
3739
}
3840

41+
rap_debug!("run rustc for package {}", package_name);
3942
// for dependencies and some special crate types, run rustc as usual
4043
run_rustc();
4144
}

0 commit comments

Comments
 (0)