From a20e0646808b8b08403e1646e3c2f285c477cd8c Mon Sep 17 00:00:00 2001 From: Phoebus Date: Sun, 20 Sep 2026 05:00:45 -0700 Subject: [PATCH] fix(update): keep fetched distfiles when binary prebuild is skipped emerge --buildpkgonly refuses while dependencies still need merging, which is normal for an out-of-date system. Record that as Ready with the fetched distfiles and the Portage reason instead of a hard Failure, and include the redacted stderr tail in the state note. The VM harness check timeout is now configurable. --- .../crates/phoebus-executor/src/update.rs | 129 +++++++++++++++--- installer/tests/vm/serial-driver.py | 3 +- 2 files changed, 114 insertions(+), 18 deletions(-) diff --git a/installer-rs/crates/phoebus-executor/src/update.rs b/installer-rs/crates/phoebus-executor/src/update.rs index d8cd7ce..cf673ea 100644 --- a/installer-rs/crates/phoebus-executor/src/update.rs +++ b/installer-rs/crates/phoebus-executor/src/update.rs @@ -13,6 +13,7 @@ use crate::error::{ExecError, Result}; use crate::exec::Exec; use crate::portage; +#[derive(Debug)] pub struct PrepareOutcome { pub started: bool, pub note: String, @@ -100,34 +101,56 @@ pub fn prepare(exec: &Exec, root: &Path, cores: u32, force: bool) -> Result Result<()> { - portage::chroot(exec, root, "emerge", &fetch_args())?; - state.note = "building binary packages".into(); + // Phase 1: distfiles. A failure here is a real failure. + if let Err(e) = portage::chroot(exec, root, "emerge", &fetch_args()) { + state.status = PrepareStatus::Failed; + state.note = describe_failure(&e); let _ = save_state(exec, root, &state); - portage::chroot(exec, root, "emerge", &build_args())?; - Ok(()) - })(); + return Err(e); + } - match result { + // Phase 2: reusable binary packages. `--buildpkgonly` refuses while + // dependencies still need merging, which is the common case for a system + // that is behind on updates. The distfiles are already fetched, so record + // that honestly as Ready with the reason rather than a hard failure that + // would hide the successful fetch. Activation stays explicit. + state.note = "building binary packages".into(); + let _ = save_state(exec, root, &state); + match portage::chroot(exec, root, "emerge", &build_args()) { Ok(()) => { state.status = PrepareStatus::Ready; state.note = "updates prepared (not activated)".into(); - state.last_prepared_unix = now_unix(); - save_state(exec, root, &state)?; exec.logger .info("background update preparation complete (not activated)"); - Ok(PrepareOutcome { - started: true, - note: state.note.clone(), - }) } Err(e) => { - state.status = PrepareStatus::Failed; - state.note = format!("{e}"); - let _ = save_state(exec, root, &state); - Err(e) + state.status = PrepareStatus::Ready; + state.note = format!( + "distfiles fetched; binary prebuild skipped: {}", + describe_failure(&e) + ); + exec.logger.warn(format!("binary prebuild skipped: {e}")); } } + state.last_prepared_unix = now_unix(); + save_state(exec, root, &state)?; + Ok(PrepareOutcome { + started: true, + note: state.note.clone(), + }) +} + +/// Include the (already redacted) stderr tail of a failed command so the state +/// explains *why* preparation failed instead of only the exit status. +fn describe_failure(error: &ExecError) -> String { + match error { + ExecError::Command { stderr, .. } if !stderr.trim().is_empty() => { + let lines: Vec<&str> = stderr.lines().collect(); + let tail = lines[lines.len().saturating_sub(5)..].join("\n"); + format!("{error}: {tail}") + } + _ => format!("{error}"), + } } fn now_unix() -> u64 { @@ -172,6 +195,78 @@ mod tests { assert_eq!(state.status, PrepareStatus::Ready); } + #[test] + fn failed_prepare_records_stderr_detail() { + let runner = RecordingRunner::new(); + runner.fail("chroot", "!!! emerge failed: unsatisfied dependency"); + let exec = make_exec(runner); + let root = Path::new(&exec.root); + let err = prepare(&exec, root, 8, true).unwrap_err(); + assert!(matches!(err, ExecError::Command { .. })); + let state = load_state(root); + assert_eq!(state.status, PrepareStatus::Failed); + assert!( + state.note.contains("unsatisfied dependency"), + "note was {}", + state.note + ); + } + + #[test] + fn binary_prebuild_skipped_after_a_successful_fetch_is_ready() { + use crate::command::{CommandOutput, ProcessRunner}; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::time::Duration; + + // First emerge (fetch) succeeds; second (buildpkgonly) fails with the + // real Portage "dependencies must be merged" condition. + struct FetchOkBuildFail { + calls: AtomicUsize, + } + impl ProcessRunner for FetchOkBuildFail { + fn execute(&self, _spec: &crate::command::CommandSpec) -> Result { + let n = self.calls.fetch_add(1, Ordering::SeqCst); + if n == 0 { + Ok(CommandOutput { + status: 0, + success: true, + stdout: Vec::new(), + stderr: Vec::new(), + duration: Duration::from_millis(1), + }) + } else { + Ok(CommandOutput { + status: 1, + success: false, + stdout: Vec::new(), + stderr: b"!!! --buildpkgonly requires all dependencies to be merged." + .to_vec(), + duration: Duration::from_millis(1), + }) + } + } + } + + let path = tempfile::tempdir().unwrap().keep(); + let logger = Logger::open(path.join("l.log"), Redactor::new(), false); + let exec = Exec::new( + Box::new(FetchOkBuildFail { + calls: AtomicUsize::new(0), + }), + logger, + false, + path.join("root"), + path, + ); + let root = Path::new(&exec.root); + let outcome = prepare(&exec, root, 8, true).unwrap(); + assert!(outcome.started); + assert!(outcome.note.contains("binary prebuild skipped")); + let state = load_state(root); + assert_eq!(state.status, PrepareStatus::Ready); + assert!(state.note.contains("dependencies to be merged")); + } + #[test] fn state_roundtrip() { let runner = RecordingRunner::new(); diff --git a/installer/tests/vm/serial-driver.py b/installer/tests/vm/serial-driver.py index 62466e7..f0ec012 100644 --- a/installer/tests/vm/serial-driver.py +++ b/installer/tests/vm/serial-driver.py @@ -119,7 +119,8 @@ def do_boot(): r"phoebus login:"], timeout=60) == 0: break child.sendline(checks) - child.expect("===CHECKS-END===", timeout=180) + checks_timeout = int(os.environ.get("CHECKS_TIMEOUT", "180")) + child.expect("===CHECKS-END===", timeout=checks_timeout) print("checks complete", flush=True) finally: child.close(force=True)