Skip to content

Commit

Permalink
Use cache dir for temporary files (#2067)
Browse files Browse the repository at this point in the history
Sometimes `/tmp` is mounted with the `noexec` option, which results
causes running shebang recipes to fail.

Instead of `/tmp`, use the cache directory as provided by the
[dirs crate](https://docs.rs/dirs/latest/src/dirs/lib.rs.html#77).
  • Loading branch information
casey committed May 21, 2024
1 parent 63ad7cc commit d3492e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub(crate) enum Error<'src> {
token: Token<'src>,
output_error: OutputError,
},
CacheDirIo {
io_error: io::Error,
path: PathBuf,
},
ChooserInvoke {
shell_binary: String,
shell_arguments: String,
Expand Down Expand Up @@ -279,6 +283,9 @@ impl<'src> ColorDisplay for Error<'src> {
}?,
OutputError::Utf8(utf8_error) => write!(f, "Backtick succeeded but stdout was not utf8: {utf8_error}")?,
}
CacheDirIo { io_error, path } => {
write!(f, "I/O error in cache dir `{}`: {io_error}", path.display())?;
}
ChooserInvoke { shell_binary, shell_arguments, chooser, io_error} => {
let chooser = chooser.to_string_lossy();
write!(f, "Chooser `{shell_binary} {shell_arguments} {chooser}` invocation failed: {io_error}")?;
Expand Down Expand Up @@ -382,8 +389,7 @@ impl<'src> ColorDisplay for Error<'src> {
}?;
}
Load { io_error, path } => {
let path = path.display();
write!(f, "Failed to read justfile at `{path}`: {io_error}")?;
write!(f, "Failed to read justfile at `{}`: {io_error}", path.display())?;
}
MissingImportFile { .. } => write!(f, "Could not find source file for import.")?,
MissingModuleFile { module } => write!(f, "Could not find source file for module `{module}`.")?,
Expand Down
13 changes: 12 additions & 1 deletion src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,18 @@ impl<'src, D> Recipe<'src, D> {
tempdir_builder.prefix("just-");
let tempdir = match &context.settings.tempdir {
Some(tempdir) => tempdir_builder.tempdir_in(context.search.working_directory.join(tempdir)),
None => tempdir_builder.tempdir(),
None => {
if let Some(cache_dir) = dirs::cache_dir() {
let path = cache_dir.join("just");
fs::create_dir_all(&path).map_err(|io_error| Error::CacheDirIo {
io_error,
path: path.clone(),
})?;
tempdir_builder.tempdir_in(path)
} else {
tempdir_builder.tempdir()
}
}
}
.map_err(|error| Error::TempdirIo {
recipe: self.name(),
Expand Down
16 changes: 12 additions & 4 deletions tests/tempdir.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use super::*;

pub(crate) fn tempdir() -> TempDir {
tempfile::Builder::new()
.prefix("just-test-tempdir")
.tempdir()
.expect("failed to create temporary directory")
let mut builder = tempfile::Builder::new();

builder.prefix("just-test-tempdir");

if let Some(cache_dir) = dirs::cache_dir() {
let path = cache_dir.join("just");
fs::create_dir_all(&path).unwrap();
builder.tempdir_in(path)
} else {
builder.tempdir()
}
.expect("failed to create temporary directory")
}

#[test]
Expand Down

0 comments on commit d3492e6

Please sign in to comment.