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/+from-commit-temp.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A `from-commit` read uses a directory it created for that read. It no longer shares `eb-stack-from-commit-<pid>` with every other read in the process, and it does not remove a directory that was already there.
117 changes: 114 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,11 @@ fn overlay_from_commits(
by_commit.entry(commit).or_default().push(&e.file);
}
}
let dir = std::env::temp_dir().join(format!("eb-stack-from-commit-{}", std::process::id()));
// A directory named only with the process id is shared by every
// from-commit read in this process, and it is a name another user can
// create first in a shared temporary directory. Each read gets a
// directory `create_dir` made, which fails if the path already exists.
let dir = private_temp_dir("eb-stack-from-commit")?;
let mut out = Vec::new();
let result = (|| -> Result<()> {
for (commit, files) in &by_commit {
Expand All @@ -691,7 +695,7 @@ fn overlay_from_commits(
commit,
"easybuild/easyconfigs",
])?;
let root = dir.join(commit);
let root = dir.0.join(commit);
for file in files {
let Some(path) = listing.lines().find(|p| p.ends_with(&format!("/{file}"))) else {
continue;
Expand All @@ -708,10 +712,49 @@ fn overlay_from_commits(
}
Ok(())
})();
let _ = std::fs::remove_dir_all(&dir);
drop(dir);
result.map(|()| out)
}

/// A directory this call created under the temporary directory, removed on drop.
struct PrivateTempDir(std::path::PathBuf);

impl Drop for PrivateTempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}

/// Create a new directory. A path that already exists, including one planted
/// before this call, is not used.
fn private_temp_dir(prefix: &str) -> Result<PrivateTempDir> {
use std::sync::atomic::{AtomicU64, Ordering};
static SEQUENCE: AtomicU64 = AtomicU64::new(0);
let base = std::env::temp_dir();
for _ in 0..32 {
let sequence = SEQUENCE.fetch_add(1, Ordering::Relaxed);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|elapsed| elapsed.as_nanos())
.unwrap_or(0);
let path = base.join(format!(
"{prefix}-{}-{nanos}-{sequence}",
std::process::id()
));
match std::fs::create_dir(&path) {
Ok(()) => return Ok(PrivateTempDir(path)),
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(err) => {
return Err(err).context(format!("create temporary directory {}", path.display()));
}
}
}
bail!(
"could not create a private temporary directory under {}",
base.display()
);
}

/// What a solve compares its result against.
#[derive(Debug, Clone, Copy)]
pub enum BaselineSource<'a> {
Expand Down Expand Up @@ -1283,4 +1326,72 @@ dependencies = []
// Non-overridden upstream still co-selected.
assert_eq!(lock.package("OpenBLAS").unwrap().version, "0.3.27");
}

#[test]
fn from_commit_overlay_does_not_remove_a_precreated_pid_directory() {
let predictable =
std::env::temp_dir().join(format!("eb-stack-from-commit-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&predictable);
std::fs::create_dir(&predictable).expect("predictable directory");
std::fs::write(predictable.join("sentinel"), b"keep").expect("sentinel");

let repo = tempfile::tempdir().expect("repo");
let git = |args: &[&str]| {
let status = std::process::Command::new("git")
.arg("-C")
.arg(repo.path())
.args(args)
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "[email protected]")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "[email protected]")
.status()
.expect("git");
assert!(status.success(), "{args:?}");
};
git(&["init", "-q"]);
let recipe = repo.path().join("easybuild/easyconfigs/f/Foo/Foo-1.0.eb");
std::fs::create_dir_all(recipe.parent().expect("recipe parent")).expect("dirs");
std::fs::write(
&recipe,
"name = 'Foo'\nversion = '1.0'\ntoolchain = SYSTEM\n",
)
.expect("recipe");
git(&["add", "."]);
git(&["commit", "-q", "-m", "foo"]);
let sha = std::process::Command::new("git")
.arg("-C")
.arg(repo.path())
.args(["rev-parse", "HEAD"])
.output()
.expect("rev-parse")
.stdout;
let sha = String::from_utf8(sha).expect("sha").trim().to_string();
let stack_dir = tempfile::tempdir().expect("stack");
let stack = stack_dir.path().join("stack.yml");
std::fs::write(
&stack,
format!("easyconfigs:\n - Foo-1.0.eb:\n options:\n from-commit: {sha}\n"),
)
.expect("easystack");
let family = (
crate::domain::Toolchain {
name: "foss".into(),
version: "2026.1".into(),
},
Vec::new(),
);
let solved = baseline_from_easystacks(&[&stack], &[], Some(repo.path()), &[family]);
assert!(
solved.is_ok(),
"from-commit read failed: {}",
solved.unwrap_err()
);
assert!(
predictable.join("sentinel").is_file(),
"from-commit read reused {}",
predictable.display()
);
let _ = std::fs::remove_dir_all(&predictable);
}
}
Loading