Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+lock-source-date-epoch.bugfix.md
Original file line number Diff line number Diff line change
@@ -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.
42 changes: 42 additions & 0 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,23 @@ pub struct SolverMeta {
pub criteria: Vec<String>,
}

/// 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::<i64>() {
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 {
Expand All @@ -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");
}
}
2 changes: 1 addition & 1 deletion src/eb_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading