diff --git a/changelog.d/+lock-source-date-epoch.bugfix.md b/changelog.d/+lock-source-date-epoch.bugfix.md new file mode 100644 index 00000000..c67c8ffa --- /dev/null +++ b/changelog.d/+lock-source-date-epoch.bugfix.md @@ -0,0 +1 @@ +A lock's timestamp is `SOURCE_DATE_EPOCH` when that variable is a number of seconds. Two solves of the same inputs then write the same time. diff --git a/src/domain.rs b/src/domain.rs index 98bde375..9d3e0cf3 100644 --- a/src/domain.rs +++ b/src/domain.rs @@ -384,6 +384,23 @@ pub struct SolverMeta { pub criteria: Vec, } +/// RFC 3339 time written into a lock. +/// +/// `SOURCE_DATE_EPOCH`, when it is a decimal number of seconds since the Unix +/// epoch, is that time in UTC. Two solves of the same inputs then write the +/// same timestamp. Anything else, including an empty or non-numeric value, is +/// the current time. +pub fn solver_timestamp() -> String { + if let Ok(raw) = std::env::var("SOURCE_DATE_EPOCH") { + if let Ok(seconds) = raw.trim().parse::() { + if let Some(stamp) = chrono::DateTime::from_timestamp(seconds, 0) { + return stamp.to_rfc3339_opts(chrono::SecondsFormat::Secs, true); + } + } + } + chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] /// A complete, reproducible selection: every package a stack installs. pub struct StackLock { @@ -408,3 +425,28 @@ impl StackLock { self.packages.iter().find(|p| p.name == name) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn source_date_epoch_is_the_lock_timestamp() { + let previous = std::env::var("SOURCE_DATE_EPOCH").ok(); + std::env::set_var("SOURCE_DATE_EPOCH", "1700000000"); + let direct = solver_timestamp(); + let lock = crate::eb_parse::lock_from_candidates(&[], None, "test"); + let again = crate::eb_parse::lock_from_candidates(&[], None, "test"); + match previous { + Some(value) => std::env::set_var("SOURCE_DATE_EPOCH", value), + None => std::env::remove_var("SOURCE_DATE_EPOCH"), + } + assert_eq!(direct, "2023-11-14T22:13:20Z"); + assert_eq!(lock.solver.timestamp, "2023-11-14T22:13:20Z"); + assert_eq!(again.solver.timestamp, lock.solver.timestamp); + std::env::set_var("SOURCE_DATE_EPOCH", "not-a-time"); + let fallback = solver_timestamp(); + std::env::remove_var("SOURCE_DATE_EPOCH"); + assert_ne!(fallback, "2023-11-14T22:13:20Z"); + } +} diff --git a/src/eb_parse.rs b/src/eb_parse.rs index e8867295..be62c213 100644 --- a/src/eb_parse.rs +++ b/src/eb_parse.rs @@ -4566,7 +4566,7 @@ pub fn lock_from_candidates( }) .collect(); packages.sort_by(|a, b| a.name.cmp(&b.name)); - let ts = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true); + let ts = crate::domain::solver_timestamp(); StackLock { schema_version: 1, toolchain, diff --git a/src/select.rs b/src/select.rs index ccc074bc..17ff32a6 100644 --- a/src/select.rs +++ b/src/select.rs @@ -147,7 +147,7 @@ pub fn select_stack( } } - let ts = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true); + let ts = crate::domain::solver_timestamp(); Ok(StackLock { schema_version: 1, toolchain: policy.toolchain.clone(),