Skip to content

Commit 7c77dc6

Browse files
basilclaude
andcommitted
Compile all examples once to avoid cargo build lock contention
Multiple test threads calling compile_example concurrently each spawned a cargo build subprocess that blocked on flock for the build directory. Use compile_examples (plural) in a LazyLock to build all examples with a single cargo invocation at first access, then look them up from the cache. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f49a810 commit 7c77dc6

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

tests/common/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,41 @@
1616

1717
#![allow(dead_code)]
1818

19+
use std::collections::HashMap;
1920
use std::path::PathBuf;
2021
use std::process::Child;
2122
use std::process::Command;
2223
use std::process::Output;
2324
use std::process::Stdio;
25+
use std::sync::LazyLock;
2426
use std::thread;
2527
use std::time::Duration;
2628
use std::time::SystemTime;
2729
use std::time::UNIX_EPOCH;
2830

31+
/// All example targets, compiled once and cached for the lifetime of the test
32+
/// process. This avoids spawning concurrent `cargo build` subprocesses (which
33+
/// deadlock on Cargo's file lock) when tests run in parallel.
34+
static EXAMPLES: LazyLock<HashMap<String, PathBuf>> = LazyLock::new(|| {
35+
snapbox::cmd::compile_examples([])
36+
.expect("failed to compile examples")
37+
.map(|(name, path)| (name, path.expect("failed to compile example")))
38+
.collect()
39+
});
40+
2941
// Find an executable produced by the Cargo build.
3042
//
31-
// For example targets (prefixed with "examples/"), uses snapbox to locate the
32-
// compiled example reliably regardless of build directory layout.
43+
// For example targets (prefixed with "examples/"), looks up the pre-compiled
44+
// example from the EXAMPLES cache.
3345
//
3446
// For bin targets, uses CARGO_BIN_EXE_<name> (Cargo >=1.94) with a fallback
3547
// to locating the binary relative to the test executable.
3648
pub fn find_exec(name: &str) -> PathBuf {
3749
if let Some(example_name) = name.strip_prefix("examples/") {
38-
return snapbox::cmd::compile_example(example_name, []).expect("failed to compile example");
50+
return EXAMPLES
51+
.get(example_name)
52+
.unwrap_or_else(|| panic!("example {example_name:?} not found in compiled examples"))
53+
.clone();
3954
}
4055

4156
let env_var = format!("CARGO_BIN_EXE_{name}");

0 commit comments

Comments
 (0)