Skip to content
6 changes: 6 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make the comment shorter, no need to document what older binaries do

# 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]
Expand Down
530 changes: 527 additions & 3 deletions src/compiler/compiler.rs

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -791,6 +792,7 @@ impl Default for DistConfig {
toolchains: Default::default(),
toolchain_cache_size: default_toolchain_cache_size(),
rewrite_includes_only: false,
fallback: true,
}
}
}
Expand Down Expand Up @@ -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![],
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions src/dist/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ mod client {
pool: tokio::runtime::Handle,
tc_cache: Arc<cache::ClientToolchains>,
rewrite_includes_only: bool,
fallback: bool,
}

impl Client {
Expand Down Expand Up @@ -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<u8>, Vec<u8>>,
Expand Down Expand Up @@ -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<PathBuf> {
match self.tc_cache.get_custom_toolchain(exe) {
Some(Ok((_, _, path))) => Some(path),
Expand Down
4 changes: 4 additions & 0 deletions src/dist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,5 +753,9 @@ pub trait Client: Send + Sync {
toolchain_packager: Box<dyn pkg::ToolchainPackager>,
) -> 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<PathBuf>;
}
37 changes: 34 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<clap::error::Error>() {
Expand All @@ -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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unrelated to the fallback, please split it into its own PR

exit(match commands::run_command(command) {
Ok(s) => s,
Err(e) => {
Expand All @@ -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);

Expand All @@ -126,6 +128,35 @@ fn init_logging() {
builder.format_timestamp_millis();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the log file can't be opened, we silently drop everything?
commands.rs bails in that case

}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so SCCACHE_LOG=debug in CI or under make now prints nothing at all?
the user explicitly asked for these logs, discarding them silently seems wrong

// When sccache runs as a compiler wrapper (`sccache <compiler> ...`) 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<Box<dyn std::io::Write + Send + 'static>> =
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<dyn std::io::Write + Send + 'static>)
.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),
Expand Down
7 changes: 6 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub struct DistClientConfig {
toolchain_cache_size: u64,
toolchains: Vec<config::DistToolchainConfig>,
rewrite_includes_only: bool,
fallback: bool,
}

#[cfg(feature = "dist-client")]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1165,6 +1168,7 @@ where
toolchain_cache_size: 0,
toolchains: vec![],
rewrite_includes_only: false,
fallback: true,
}),
dist_client,
))),
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion tests/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down
1 change: 1 addition & 0 deletions tests/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down
Loading