Skip to content

Commit ff52fda

Browse files
tests: cover a compile that fails on a dist build server
A unit test with the fake dist client (runs in every CI job) and an end to end test in tests/dist.rs with a real scheduler and build server: a compile error found by the compiler on the build server must reach the client with exit code 1. Without the previous commit the end to end test fails with "sccache: Compiler killed by signal 1" and exit code 254, and the unit test sees no exit code at all. The source uses an undeclared identifier rather than `#error`, because preprocessing runs locally and an `#error` never reaches the server. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VszvDmRRPP1UcwozsUMY1L
1 parent 323ccf1 commit ff52fda

2 files changed

Lines changed: 151 additions & 0 deletions

File tree

‎src/compiler/compiler.rs‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,6 +3605,89 @@ LLVM version: 6.0",
36053605
assert!(fs::metadata(&obj).is_err());
36063606
}
36073607

3608+
// A compile that fails on the build server has to reach the client with its exit code; it used
3609+
// to arrive as "killed by signal 1".
3610+
#[test]
3611+
#[cfg(feature = "dist-client")]
3612+
fn test_compiler_get_cached_or_compile_dist_failed_compile() {
3613+
drop(env_logger::try_init());
3614+
let creator = new_creator();
3615+
let f = TestFixture::new();
3616+
let gcc = f.mk_bin("gcc").unwrap();
3617+
let runtime = Runtime::new().unwrap();
3618+
let pool = runtime.handle().clone();
3619+
let storage = DiskCache::new(
3620+
f.tempdir.path().join("cache"),
3621+
u64::MAX,
3622+
&pool,
3623+
PreprocessorCacheModeConfig::default(),
3624+
CacheMode::ReadWrite,
3625+
vec![],
3626+
);
3627+
let storage = Arc::new(storage);
3628+
next_command(
3629+
&creator,
3630+
Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")),
3631+
);
3632+
next_assembler(&creator, "GNU assembler (GNU Binutils) 2.42", "");
3633+
let c = get_compiler_info(
3634+
creator.clone(),
3635+
&gcc,
3636+
f.tempdir.path(),
3637+
&[],
3638+
&[],
3639+
&pool,
3640+
None,
3641+
)
3642+
.wait()
3643+
.unwrap()
3644+
.0;
3645+
next_command(
3646+
&creator,
3647+
Ok(MockChild::new(exit_status(0), "preprocessor output", "")),
3648+
);
3649+
const COMPILER_STDERR: &[u8] = b"foo.c:1: error: expected ';'";
3650+
let dist_client = test_dist::OneshotClient::new(1, vec![], COMPILER_STDERR.to_owned());
3651+
let service = server::SccacheService::mock_with_dist_client(
3652+
dist_client.clone(),
3653+
storage.clone(),
3654+
pool.clone(),
3655+
);
3656+
let arguments = ovec!["-c", "foo.c", "-o", "foo.o"];
3657+
let mut hasher = match c.parse_arguments(&arguments, ".".as_ref(), &[]) {
3658+
CompilerArguments::Ok(h) => h,
3659+
o => panic!("Bad result from parse_arguments: {:?}", o),
3660+
};
3661+
let (cached, res) = runtime
3662+
.block_on(async {
3663+
hasher
3664+
.get_cached_or_compile(
3665+
&service,
3666+
Some(dist_client.clone()),
3667+
creator.clone(),
3668+
storage.clone(),
3669+
arguments.clone(),
3670+
f.tempdir.path().to_path_buf(),
3671+
vec![],
3672+
CacheControl::Default,
3673+
pool.clone(),
3674+
)
3675+
.await
3676+
})
3677+
.unwrap();
3678+
match cached {
3679+
CompileResult::CompileFailed(DistType::Ok(_), _) => {}
3680+
_ => panic!("Unexpected compile result: {:?}", cached),
3681+
}
3682+
assert_eq!(res.status.code(), Some(1));
3683+
#[cfg(unix)]
3684+
{
3685+
use std::os::unix::process::ExitStatusExt;
3686+
assert_eq!(res.status.signal(), None);
3687+
}
3688+
assert!(res.stderr.ends_with(COMPILER_STDERR));
3689+
}
3690+
36083691
#[test_case(true ; "with preprocessor cache")]
36093692
#[test_case(false ; "without preprocessor cache")]
36103693
#[cfg(feature = "dist-client")]

‎tests/dist.rs‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,74 @@ fn test_dist_basic() {
140140
});
141141
}
142142

143+
// A compile error on the build server reached the client as "Compiler killed by signal 1" with exit
144+
// code 254 instead of the compiler's exit code.
145+
#[test]
146+
#[cfg_attr(not(feature = "dist-tests"), ignore)]
147+
fn test_dist_failed_compile() {
148+
let tmpdir = tempfile::Builder::new()
149+
.prefix("sccache_dist_test")
150+
.tempdir()
151+
.unwrap();
152+
let tmpdir = tmpdir.path();
153+
let sccache_dist = harness::sccache_dist_path();
154+
155+
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
156+
system.add_scheduler();
157+
system.add_server();
158+
159+
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
160+
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
161+
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
162+
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
163+
164+
stop_local_daemon();
165+
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
166+
167+
// An error the compiler finds, not the preprocessor: preprocessing runs locally, so an
168+
// `#error` would fail before anything is sent to the build server.
169+
write_source(
170+
tmpdir,
171+
"fail.c",
172+
"int x() { return sccache_dist_test_undeclared; }",
173+
);
174+
let output = sccache_command()
175+
.args([
176+
std::env::var("CC")
177+
.unwrap_or_else(|_| "gcc".to_string())
178+
.as_str(),
179+
"-c",
180+
])
181+
.arg(tmpdir.join("fail.c"))
182+
.arg("-o")
183+
.arg(tmpdir.join("fail.o"))
184+
.envs(vec![
185+
("RUST_BACKTRACE", OsStr::new("1")),
186+
("SCCACHE_CONF", sccache_cfg_path.as_os_str()),
187+
("SCCACHE_CACHED_CONF", sccache_cached_cfg_path.as_os_str()),
188+
])
189+
.output()
190+
.unwrap();
191+
let stdout = String::from_utf8_lossy(&output.stdout);
192+
let stderr = String::from_utf8_lossy(&output.stderr);
193+
assert_eq!(
194+
output.status.code(),
195+
Some(1),
196+
"stdout: {stdout}\nstderr: {stderr}"
197+
);
198+
assert!(!stdout.contains("killed by signal"), "stdout: {stdout}");
199+
assert!(
200+
stderr.contains("sccache_dist_test_undeclared"),
201+
"stderr: {stderr}"
202+
);
203+
204+
get_stats(|info| {
205+
assert_eq!(1, info.stats.dist_compiles.values().sum::<usize>());
206+
assert_eq!(0, info.stats.dist_errors);
207+
assert_eq!(1, info.stats.compile_fails);
208+
});
209+
}
210+
143211
#[test]
144212
#[cfg_attr(not(feature = "dist-tests"), ignore)]
145213
fn test_dist_restartedserver() {

0 commit comments

Comments
 (0)