diff --git a/docs/Configuration.md b/docs/Configuration.md index c234aa3216..7ea8efe66b 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -46,6 +46,12 @@ scheduler_url = "http://1.2.3.4:10600" toolchains = [] # the maximum size of the toolchain cache in bytes toolchain_cache_size = 5368709120 +# whether a distributed compile that fails on the build server (non-zero exit) +# or leaves out a declared output is recompiled locally. Default: true. +# With false the remote result is reported to the client as is. An sccache +# binary that predates this key rejects a configuration containing it, +# because unknown keys are denied. +fallback = true cache_dir = "/home/user/.cache/sccache-dist-client" [dist.auth] diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index 4d88bd852a..0c93928c6d 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -930,7 +930,10 @@ where let dist_client = match dist_compile_cmd.clone().and(dist_client) { Some(dc) => dc, None => { - debug!("[{}]: Compiling locally", out_pretty); + info!( + "[{}]: Compiling locally (not eligible for distributed compilation)", + out_pretty + ); return compile_cmd .execute(service, &creator) .await @@ -938,8 +941,9 @@ where } }; - debug!("[{}]: Attempting distributed compilation", out_pretty); + info!("[{}]: Attempting distributed compilation", out_pretty); let out_pretty2 = out_pretty.clone(); + let fallback_enabled = dist_client.fallback_enabled(); let local_executable = compile_cmd.get_executable(); let local_executable2 = compile_cmd.get_executable(); @@ -953,6 +957,28 @@ where .map(|output| path_transformer.as_dist_abs(&cwd.join(output.path))) .collect::>() .context("Failed to adapt an output path for distributed compile")?; + // The local paths every declared output must occupy once the compile + // finishes. Captured before `compilation` is moved into the packagers + // below so a remote compile that drops a declared output can be detected + // and salvaged (see the missing-output check after the run completes). + // + // Each entry pairs the output's dist path with its local path. The dist + // path is matched against what the server returned, because a stale file + // from an earlier build at the local path would pass a bare existence + // check. gcc's dependency file (key `d`) is written by the local + // preprocessor and the server never returns it, so it keeps the + // existence check. + let expected_outputs: Vec<(String, PathBuf, bool)> = compilation + .outputs() + .filter(|output| !output.optional) + .map(|output| { + let local = cwd.join(output.path); + path_transformer + .as_dist_abs(&local) + .map(|dist| (dist, local, output.key == "d")) + }) + .collect::>() + .context("Failed to adapt an output path for distributed compile")?; let (inputs_packager, toolchain_packager, outputs_rewriter) = compilation.into_dist_packagers(path_transformer)?; @@ -1055,6 +1081,8 @@ where }}; } + let returned_outputs: std::collections::HashSet = + jc.outputs.iter().map(|(p, _)| p.clone()).collect(); for (path, output_data) in jc.outputs { let len = output_data.lens().actual; let local_path = try_or_cleanup!( @@ -1088,7 +1116,66 @@ where .with_context(|| "failed to rewrite outputs from compile") ); + if jc.output.code != 0 && fallback_enabled { + // The one-line reason above is logged by the caller at warn; the + // remote compiler's own stdout/stderr is the only place the actual + // failure (a missing target std, an unshipped input) is visible, and + // the local fallback discards it. Emit it at debug so SCCACHE_LOG=debug + // surfaces the remote error without a rebuild, while a normal + // SCCACHE_LOG=info run stays quiet. + debug!( + "[{}]: distributed compile on {} exited {}; remote stderr:\n{}\nremote stdout:\n{}", + out_pretty, + server_id.addr(), + jc.output.code, + String::from_utf8_lossy(&jc.output.stderr), + String::from_utf8_lossy(&jc.output.stdout), + ); + // A non-zero remote result is frequently a distribution artifact + // rather than a genuine compiler error: e.g. an object that + // .incbin's a binary the inputs packager did not ship (the kernel's + // vdso/dtb/embedded-config wrappers), which the build-server cannot + // assemble. Fall back to a local recompile via the or_else below - it + // either succeeds (confirming a dist-only artifact) or reproduces the + // real error locally, so a remote failure never breaks a build that + // would compile fine locally. This only affects failing dist + // compiles; successful ones are returned unchanged above. + bail!( + "distributed compile on {} returned exit code {}; recompiling locally", + server_id.addr(), + jc.output.code + ); + } + + // The remote compile returned exit 0 but may have dropped a declared + // output: glibc's ldconfig.o/sprof.o omit their `.o.dt` dep file, which + // the build-server does not return. Zipping the outputs later would then + // fail fatally with no recourse. Treat a missing declared output as a + // distribution artifact and fall back to a local recompile via the + // or_else below, which reproduces the full output set. Compiles whose + // dist output set is complete are unaffected. + if fallback_enabled + && let Some((_, missing, _)) = + expected_outputs + .iter() + .find(|(dist, local, locally_produced)| { + if *locally_produced { + !local.exists() + } else { + !returned_outputs.contains(dist) + } + }) + { + bail!( + "distributed compile on {} did not return expected output {}; recompiling locally", + server_id.addr(), + missing.display() + ); + } + if jc.output.code != 0 { + // Reached only with the fallback switch off, where the remote result is + // reported to the client as release 0.19.0 does. // Add server info to help diagnose host-specific failures, e.g. due to flaky hardware. // Failed builds are not cached so this tampering should not cause too much trouble. let server_info = format!("sccache: Job failed on server {}:\n", server_id.addr()); @@ -1097,6 +1184,11 @@ where .splice(0..0, server_info.as_bytes().to_vec()); } + info!( + "[{}]: Distributed compilation finished on {}", + out_pretty, + server_id.addr() + ); Ok((DistType::Ok(server_id), jc.output.into())) }; @@ -3717,7 +3809,9 @@ LLVM version: 6.0", Ok(MockChild::new(exit_status(0), "preprocessor output", "")), ); const COMPILER_STDERR: &[u8] = b"foo.c:1: error: expected ';'"; - let dist_client = test_dist::OneshotClient::new(1, vec![], COMPILER_STDERR.to_owned()); + // Release behaviour: the remote result reaches the client, no local recompile. + let dist_client = + test_dist::OneshotClient::with_fallback(1, vec![], COMPILER_STDERR.to_owned(), false); let service = server::SccacheService::mock_with_dist_client( dist_client.clone(), storage.clone(), @@ -3758,6 +3852,293 @@ LLVM version: 6.0", assert!(res.stderr.ends_with(COMPILER_STDERR)); } + /// Runs one distributed compile whose remote result is exit 1 and returns + /// the fixture, the runtime (kept alive so the cache write can finish), the + /// compile result and the process output. + #[cfg(feature = "dist-client")] + fn run_dist_failed_compile( + fallback: bool, + local_result: Option<&'static [u8]>, + ) -> (TestFixture, Runtime, CompileResult, process::Output) { + drop(env_logger::try_init()); + let creator = new_creator(); + let f = TestFixture::new(); + let gcc = f.mk_bin("gcc").unwrap(); + let runtime = Runtime::new().unwrap(); + let pool = runtime.handle().clone(); + let storage = DiskCache::new( + f.tempdir.path().join("cache"), + u64::MAX, + &pool, + PreprocessorCacheModeConfig::default(), + CacheMode::ReadWrite, + vec![], + ); + let storage = Arc::new(storage); + next_command( + &creator, + Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")), + ); + next_assembler(&creator, "GNU assembler (GNU Binutils) 2.42", ""); + let c = get_compiler_info( + creator.clone(), + &gcc, + f.tempdir.path(), + &[], + &[], + &pool, + None, + ) + .wait() + .unwrap() + .0; + next_command( + &creator, + Ok(MockChild::new(exit_status(0), "preprocessor output", "")), + ); + if let Some(contents) = local_result { + let obj = f.tempdir.path().join("foo.o"); + next_command_calls(&creator, move |_| { + let mut o = File::create(&obj)?; + o.write_all(contents)?; + Ok(MockChild::new( + exit_status(0), + "local stdout", + "local stderr", + )) + }); + } + let dist_client = + test_dist::OneshotClient::with_fallback(1, vec![], b"remote stderr".to_vec(), fallback); + let service = server::SccacheService::mock_with_dist_client( + dist_client.clone(), + storage.clone(), + pool.clone(), + ); + let arguments = ovec!["-c", "foo.c", "-o", "foo.o"]; + let mut hasher = match c.parse_arguments(&arguments, ".".as_ref(), &[]) { + CompilerArguments::Ok(h) => h, + o => panic!("Bad result from parse_arguments: {:?}", o), + }; + let (cached, res) = runtime + .block_on(async { + hasher + .get_cached_or_compile( + &service, + Some(dist_client.clone()), + creator.clone(), + storage.clone(), + arguments.clone(), + f.tempdir.path().to_path_buf(), + vec![], + CacheControl::Default, + pool.clone(), + ) + .await + }) + .unwrap(); + (f, runtime, cached, res) + } + + #[test] + #[cfg(feature = "dist-client")] + fn test_compiler_get_cached_or_compile_dist_failed_compile_fallback_true() { + let (f, _runtime, cached, res) = run_dist_failed_compile(true, Some(b"local object")); + match cached { + CompileResult::CacheMiss(MissType::Normal, DistType::Error, _, fut) => { + fut.wait().unwrap(); + } + _ => panic!("Unexpected compile result: {:?}", cached), + } + assert_eq!(res.status.code(), Some(0)); + assert_eq!(res.stdout.as_slice(), b"local stdout"); + assert_eq!(res.stderr.as_slice(), b"local stderr"); + assert_eq!( + fs::read(f.tempdir.path().join("foo.o")).unwrap(), + b"local object" + ); + } + + #[test] + #[cfg(feature = "dist-client")] + fn test_compiler_get_cached_or_compile_dist_failed_compile_fallback_false() { + let (_f, _runtime, cached, res) = run_dist_failed_compile(false, None); + match cached { + CompileResult::CompileFailed(DistType::Ok(_), _) => {} + _ => panic!("Unexpected compile result: {:?}", cached), + } + assert_eq!(res.status.code(), Some(1)); + assert!(res.stderr.ends_with(b"remote stderr")); + // As in 0.19.0, a reported remote failure names the server that produced it. + assert!(res.stderr.starts_with(b"sccache: Job failed on server ")); + } + + // A stale object from an earlier build sits at the output path. When the build + // server returns exit 0 but drops that output, the path still exists, so a + // bare existence check would accept the stale file and cache it as the result. + #[test] + #[cfg(feature = "dist-client")] + fn test_compiler_dist_dropped_output_not_masked_by_stale_file() { + drop(env_logger::try_init()); + let creator = new_creator(); + let f = TestFixture::new(); + let gcc = f.mk_bin("gcc").unwrap(); + let runtime = Runtime::new().unwrap(); + let pool = runtime.handle().clone(); + std::fs::write(f.tempdir.path().join("foo.c"), "whatever").unwrap(); + let storage = Arc::new(DiskCache::new( + f.tempdir.path().join("cache"), + u64::MAX, + &pool, + PreprocessorCacheModeConfig::default(), + CacheMode::ReadWrite, + vec![], + )); + next_command( + &creator, + Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")), + ); + next_assembler(&creator, "GNU assembler (GNU Binutils) 2.42", ""); + let c = get_compiler_info( + creator.clone(), + &gcc, + f.tempdir.path(), + &[], + &[], + &pool, + None, + ) + .wait() + .unwrap() + .0; + let obj = f.tempdir.path().join("foo.o"); + std::fs::write(&obj, b"STALE OBJECT").unwrap(); + next_command( + &creator, + Ok(MockChild::new(exit_status(0), "preprocessor output", "")), + ); + let o = obj.clone(); + next_command_calls(&creator, move |_| { + let mut f = File::create(&o)?; + f.write_all(b"FRESH LOCAL")?; + Ok(MockChild::new(exit_status(0), "", "")) + }); + let cwd = f.tempdir.path(); + let arguments = ovec!["-c", "foo.c", "-o", "foo.o"]; + let mut hasher = match c.parse_arguments(&arguments, ".".as_ref(), &[]) { + CompilerArguments::Ok(h) => h, + o => panic!("Bad result from parse_arguments: {:?}", o), + }; + let dist_client = test_dist::IncompleteOutputsClient::new(); + let service = server::SccacheService::mock_with_dist_client( + dist_client.clone(), + storage.clone(), + pool.clone(), + ); + let (_cached, res) = hasher + .get_cached_or_compile( + &service, + Some(dist_client), + creator, + storage, + arguments, + cwd.to_path_buf(), + vec![], + CacheControl::ForceRecache, + pool, + ) + .wait() + .unwrap(); + assert_eq!(exit_status(0), res.status); + assert_eq!( + b"FRESH LOCAL".as_slice(), + fs::read(&obj).unwrap().as_slice() + ); + } + // gcc's dependency file is written by the local preprocessor and the build + // server never returns it. A distributed compile that returns only the object + // must still be accepted, with no local recompile. + #[test] + #[cfg(feature = "dist-client")] + fn test_compiler_dist_locally_written_dep_file_is_not_a_dropped_output() { + drop(env_logger::try_init()); + let creator = new_creator(); + let f = TestFixture::new(); + let gcc = f.mk_bin("gcc").unwrap(); + let runtime = Runtime::new().unwrap(); + let pool = runtime.handle().clone(); + std::fs::write(f.tempdir.path().join("foo.c"), "whatever").unwrap(); + let storage = Arc::new(DiskCache::new( + f.tempdir.path().join("cache"), + u64::MAX, + &pool, + PreprocessorCacheModeConfig::default(), + CacheMode::ReadWrite, + vec![], + )); + next_command( + &creator, + Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")), + ); + next_assembler(&creator, "GNU assembler (GNU Binutils) 2.42", ""); + let c = get_compiler_info( + creator.clone(), + &gcc, + f.tempdir.path(), + &[], + &[], + &pool, + None, + ) + .wait() + .unwrap() + .0; + let dep = f.tempdir.path().join("foo.d"); + let d = dep.clone(); + // The preprocessor invocation writes the dependency file. No compiler + // invocation is queued: a local recompile would find none and fail. + next_command_calls(&creator, move |_| { + std::fs::write(&d, b"foo.o: foo.c\n")?; + Ok(MockChild::new(exit_status(0), "preprocessor output", "")) + }); + let cwd = f.tempdir.path(); + let arguments = ovec!["-c", "foo.c", "-o", "foo.o", "-MD", "-MF", "foo.d"]; + let mut hasher = match c.parse_arguments(&arguments, ".".as_ref(), &[]) { + CompilerArguments::Ok(h) => h, + o => panic!("Bad result from parse_arguments: {:?}", o), + }; + let dist_client = test_dist::IncompleteOutputsClient::dropping(".d"); + let service = server::SccacheService::mock_with_dist_client( + dist_client.clone(), + storage.clone(), + pool.clone(), + ); + let (_cached, res) = hasher + .get_cached_or_compile( + &service, + Some(dist_client), + creator, + storage, + arguments, + cwd.to_path_buf(), + vec![], + CacheControl::ForceRecache, + pool, + ) + .wait() + .unwrap(); + assert_eq!(exit_status(0), res.status); + assert!( + fs::read(f.tempdir.path().join("foo.o")) + .unwrap() + .starts_with(b"some data in ") + ); + assert_eq!( + b"foo.o: foo.c\n".as_slice(), + fs::read(&dep).unwrap().as_slice() + ); + } + #[test_case(true ; "with preprocessor cache")] #[test_case(false ; "without preprocessor cache")] #[cfg(feature = "dist-client")] @@ -3773,6 +4154,7 @@ LLVM version: 6.0", test_dist::ErrorAllocJobClient::new(), test_dist::ErrorSubmitToolchainClient::new(), test_dist::ErrorRunJobClient::new(), + test_dist::IncompleteOutputsClient::new(), ]; // Write a dummy input file so the preprocessor cache mode can work std::fs::write(f.tempdir.path().join("foo.c"), "whatever").unwrap(); @@ -4171,17 +4553,28 @@ mod test_dist { has_started: AtomicBool, tc: Toolchain, output: ProcessOutput, + fallback: bool, } impl OneshotClient { #[allow(clippy::new_ret_no_self)] pub fn new(code: i32, stdout: Vec, stderr: Vec) -> Arc { + Self::with_fallback(code, stdout, stderr, true) + } + + pub fn with_fallback( + code: i32, + stdout: Vec, + stderr: Vec, + fallback: bool, + ) -> Arc { Arc::new(Self { has_started: AtomicBool::default(), tc: Toolchain { archive_id: "somearchiveid".to_owned(), }, output: ProcessOutput::fake_output(code, stdout, stderr), + fallback, }) } } @@ -4261,6 +4654,137 @@ mod test_dist { fn rewrite_includes_only(&self) -> bool { false } + fn fallback_enabled(&self) -> bool { + self.fallback + } + fn get_custom_toolchain(&self, _exe: &Path) -> Option { + None + } + } + + /// A dist client whose remote compile succeeds (exit 0) but whose returned + /// output set drops a declared output - the glibc `.o.dt` failure mode, + /// where the build-server runs the compile fine yet does not return every + /// declared output. The client must detect the missing output and fall back + /// to a local recompile rather than failing the build. + pub struct IncompleteOutputsClient { + has_started: AtomicBool, + tc: Toolchain, + output: ProcessOutput, + /// Name suffix of the one output to drop; the last output when `None`. + drop_suffix: Option<&'static str>, + } + + impl IncompleteOutputsClient { + #[allow(clippy::new_ret_no_self)] + pub fn new() -> Arc { + Arc::new(Self { + has_started: AtomicBool::default(), + tc: Toolchain { + archive_id: "somearchiveid".to_owned(), + }, + output: ProcessOutput::fake_output(0, vec![], vec![]), + drop_suffix: None, + }) + } + + #[allow(clippy::new_ret_no_self)] + pub fn dropping(suffix: &'static str) -> Arc { + Arc::new(Self { + has_started: AtomicBool::default(), + tc: Toolchain { + archive_id: "somearchiveid".to_owned(), + }, + output: ProcessOutput::fake_output(0, vec![], vec![]), + drop_suffix: Some(suffix), + }) + } + } + + #[async_trait] + impl dist::Client for IncompleteOutputsClient { + async fn do_alloc_job(&self, tc: Toolchain) -> Result { + assert!( + !self + .has_started + .swap(true, std::sync::atomic::Ordering::AcqRel) + ); + assert_eq!(self.tc, tc); + + Ok(AllocJobResult::Success { + job_alloc: JobAlloc { + auth: "abcd".to_owned(), + job_id: JobId(0), + server_id: ServerId::new(([0, 0, 0, 0], 1).into()), + }, + need_toolchain: true, + }) + } + async fn do_get_status(&self) -> Result { + unreachable!("fn do_get_status is not used for this test. qed") + } + async fn do_submit_toolchain( + &self, + job_alloc: JobAlloc, + tc: Toolchain, + ) -> Result { + assert_eq!(job_alloc.job_id, JobId(0)); + assert_eq!(self.tc, tc); + + Ok(SubmitToolchainResult::Success) + } + async fn do_run_job( + &self, + job_alloc: JobAlloc, + command: CompileCommand, + outputs: Vec, + inputs_packager: Box, + ) -> Result<(RunJobResult, PathTransformer)> { + assert_eq!(job_alloc.job_id, JobId(0)); + assert_eq!(command.executable, "/overridden/compiler"); + + let mut inputs = vec![]; + let path_transformer = inputs_packager.write_inputs(&mut inputs).unwrap(); + // Drop one declared output to mimic a build-server that returned a + // successful compile with an incomplete output set. + let mut outputs = outputs; + match self.drop_suffix { + Some(suffix) => outputs.retain(|name| !name.ends_with(suffix)), + None => { + outputs.pop(); + } + } + let outputs = outputs + .into_iter() + .map(|name| { + let data = format!("some data in {}", name); + let data = OutputData::try_from_reader(data.as_bytes()).unwrap(); + (name, data) + }) + .collect(); + let result = RunJobResult::Complete(JobComplete { + output: self.output.clone(), + outputs, + }); + Ok((result, path_transformer)) + } + async fn put_toolchain( + &self, + _: PathBuf, + _: String, + _: Box, + ) -> Result<(Toolchain, Option<(String, PathBuf)>)> { + Ok(( + self.tc.clone(), + Some(( + "/overridden/compiler".to_owned(), + PathBuf::from("somearchiveid"), + )), + )) + } + fn rewrite_includes_only(&self) -> bool { + false + } fn get_custom_toolchain(&self, _exe: &Path) -> Option { None } diff --git a/src/config.rs b/src/config.rs index 89ae6413a1..06c5cbf1f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -780,6 +780,7 @@ pub struct DistConfig { #[serde(deserialize_with = "deserialize_size_from_str")] pub toolchain_cache_size: u64, pub rewrite_includes_only: bool, + pub fallback: bool, } impl Default for DistConfig { @@ -791,6 +792,7 @@ impl Default for DistConfig { toolchains: Default::default(), toolchain_cache_size: default_toolchain_cache_size(), rewrite_includes_only: false, + fallback: true, } } } @@ -3109,6 +3111,7 @@ key_prefix = "cosprefix" toolchains: vec![], toolchain_cache_size: 5368709120, rewrite_includes_only: false, + fallback: true, }, server_startup_timeout_ms: Some(10000), basedirs: vec![], @@ -3211,6 +3214,16 @@ size = "7g" ); } +#[test] +fn dist_fallback_toml_parse() { + let config: FileConfig = toml::from_str("[dist]\nfallback = false\n").expect("Is valid toml."); + assert!(!config.dist.fallback); + + let config: FileConfig = toml::from_str("[dist]\nscheduler_url = \"http://1.2.3.4:10600\"\n") + .expect("Is valid toml."); + assert!(config.dist.fallback); +} + #[test] #[serial(config_from_env)] fn test_dist_client_cache_dir_env_overrides_file_config() { diff --git a/src/dist/http.rs b/src/dist/http.rs index bd1562b570..9877ec8826 100644 --- a/src/dist/http.rs +++ b/src/dist/http.rs @@ -1107,6 +1107,7 @@ mod client { pool: tokio::runtime::Handle, tc_cache: Arc, rewrite_includes_only: bool, + fallback: bool, } impl Client { @@ -1140,9 +1141,16 @@ mod client { pool: pool.clone(), tc_cache: Arc::new(client_toolchains), rewrite_includes_only, + fallback: true, }) } + /// Sets whether a failed or incomplete remote compile is recompiled locally. + pub fn with_fallback(mut self, fallback: bool) -> Self { + self.fallback = fallback; + self + } + fn update_certs( client: &mut reqwest::Client, certs: &mut HashMap, Vec>, @@ -1330,6 +1338,9 @@ mod client { fn rewrite_includes_only(&self) -> bool { self.rewrite_includes_only } + fn fallback_enabled(&self) -> bool { + self.fallback + } fn get_custom_toolchain(&self, exe: &Path) -> Option { match self.tc_cache.get_custom_toolchain(exe) { Some(Ok((_, _, path))) => Some(path), diff --git a/src/dist/mod.rs b/src/dist/mod.rs index cf7cbcdaa5..46c1cbd5f5 100644 --- a/src/dist/mod.rs +++ b/src/dist/mod.rs @@ -753,5 +753,9 @@ pub trait Client: Send + Sync { toolchain_packager: Box, ) -> Result<(Toolchain, Option<(String, PathBuf)>)>; fn rewrite_includes_only(&self) -> bool; + /// Whether a failed or incomplete remote compile is recompiled locally. + fn fallback_enabled(&self) -> bool { + true + } fn get_custom_toolchain(&self, exe: &Path) -> Option; } diff --git a/src/lib.rs b/src/lib.rs index af717d6e4d..e98b5f53fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,8 +84,6 @@ fn exit(code: i32) -> ! { } pub fn main() { - init_logging(); - let command = match cmdline::try_parse() { Ok(cmd) => cmd, Err(e) => match e.downcast::() { @@ -105,6 +103,10 @@ pub fn main() { }, }; + // Logging is initialized after parsing so the compile-wrapper case can keep + // sccache's own log records off the wrapped compiler's stderr. + init_logging(&command); + exit(match commands::run_command(command) { Ok(s) => s, Err(e) => { @@ -117,7 +119,7 @@ pub fn main() { }); } -fn init_logging() { +fn init_logging(command: &cmdline::Command) { if env::var(LOGGING_ENV).is_ok() { let mut builder = env_logger::Builder::from_env(LOGGING_ENV); @@ -126,6 +128,35 @@ fn init_logging() { builder.format_timestamp_millis(); } + // When sccache runs as a compiler wrapper (`sccache ...`) it + // shares stderr with the wrapped compiler. Build tools treat any + // unexpected stderr on a feature probe as a compiler failure: libtool's + // `-fPIC` check sets `pic_flag=""` on non-empty stderr, producing non-PIC + // objects that then fail to link into a shared library. sccache's own log + // records must never reach that stderr. Send them to SCCACHE_ERROR_LOG + // when set; otherwise keep stderr only for an interactive terminal (so + // `SCCACHE_LOG=debug sccache gcc ...` at a prompt still shows logs) and + // discard them when stderr is captured by a build. + if matches!(command, cmdline::Command::Compile { .. }) { + use std::io::IsTerminal; + let target: Option> = + match env::var("SCCACHE_ERROR_LOG") { + Ok(path) if !path.is_empty() => Some( + std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&path) + .map(|f| Box::new(f) as Box) + .unwrap_or_else(|_| Box::new(std::io::sink())), + ), + _ if !std::io::stderr().is_terminal() => Some(Box::new(std::io::sink())), + _ => None, + }; + if let Some(target) = target { + builder.target(env_logger::Target::Pipe(target)); + } + } + match builder.try_init() { Ok(_) => (), Err(e) => panic!("Failed to initialize logging: {:?}", e), diff --git a/src/server.rs b/src/server.rs index abcea23e14..2e0ba9698b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -226,6 +226,7 @@ pub struct DistClientConfig { toolchain_cache_size: u64, toolchains: Vec, rewrite_includes_only: bool, + fallback: bool, } #[cfg(feature = "dist-client")] @@ -282,6 +283,7 @@ impl DistClientContainer { toolchain_cache_size: config.dist.toolchain_cache_size, toolchains: config.dist.toolchains.clone(), rewrite_includes_only: config.dist.rewrite_includes_only, + fallback: config.dist.fallback, }; let state = Self::create_state(config); let state = pool.block_on(state); @@ -448,7 +450,8 @@ impl DistClientContainer { &config.toolchains, auth_token, config.rewrite_includes_only, - ); + ) + .map(|client| client.with_fallback(config.fallback)); let dist_client = try_or_retry_later!(dist_client.context("failure during dist client creation")); use crate::dist::Client; @@ -1165,6 +1168,7 @@ where toolchain_cache_size: 0, toolchains: vec![], rewrite_includes_only: false, + fallback: true, }), dist_client, ))), @@ -2774,6 +2778,7 @@ mod tests { toolchain_cache_size: 0, toolchains: vec![], rewrite_includes_only: false, + fallback: true, }; let container = DistClientContainer::new_with_state(DistClientState::RetryCreateAt( Box::new(config), diff --git a/tests/dist.rs b/tests/dist.rs index 45aadd8275..b64da1c8bd 100644 --- a/tests/dist.rs +++ b/tests/dist.rs @@ -156,7 +156,9 @@ fn test_dist_failed_compile() { system.add_scheduler(); system.add_server(); - let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url()); + let mut sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url()); + // This test asserts the remote failure reaches the client, i.e. release behaviour. + sccache_cfg.dist.fallback = false; let sccache_cfg_path = tmpdir.join("sccache-cfg.json"); write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg); let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg"); diff --git a/tests/harness/mod.rs b/tests/harness/mod.rs index 3ce0f642e9..62918e91b1 100644 --- a/tests/harness/mod.rs +++ b/tests/harness/mod.rs @@ -190,6 +190,7 @@ pub fn sccache_client_cfg( toolchains: vec![], toolchain_cache_size: TC_CACHE_SIZE, rewrite_includes_only: false, // TODO + fallback: true, }, server_startup_timeout_ms: None, basedirs: vec![], diff --git a/tests/oauth.rs b/tests/oauth.rs index 764d244dc6..1452a9e54a 100644 --- a/tests/oauth.rs +++ b/tests/oauth.rs @@ -57,6 +57,7 @@ fn config_with_dist_auth( toolchains: vec![], toolchain_cache_size: 0, rewrite_includes_only: true, + fallback: true, }, server_startup_timeout_ms: None, basedirs: vec![],