lazily create the cache directory (#174)

Don't touch $HOME unless needed.

Fixes #173.
This commit is contained in:
Jonas Chevalier 2022-07-07 17:09:23 +02:00 committed by GitHub
parent db604fd694
commit 3aee5bd5d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 7 deletions

View File

@ -75,4 +75,21 @@ in
# Flake attributes
default = treefmt;
# Test that no HOME is needed when --no-cache is passed
treefmt-no-cache-no-home = nixpkgs.runCommandLocal "format"
{
buildInputs = [ treefmt ];
}
''
cat <<CONFIG > treefmt.toml
[formatter.nix]
command = "${nixpkgs.nixpkgs-fmt}/bin/nixpkgs-fmt"
includes = ["*.nix"]
CONFIG
# uncommenting this makes it work fine
# export HOME=$TMP
treefmt --no-cache --fail-on-change -C ./.
touch $out
'';
}

View File

@ -2,7 +2,6 @@ use crate::engine::run_treefmt;
use anyhow::anyhow;
use directories::ProjectDirs;
use log::debug;
use std::fs;
use std::path::{Path, PathBuf};
pub fn format_cmd(
@ -38,8 +37,6 @@ pub fn format_cmd(
};
let cache_dir = proj_dirs.cache_dir().join("eval-cache");
// Make sure the cache directory exists.
fs::create_dir_all(&cache_dir)?;
debug!(
"tree_root={} work_dir={} cache_dir={} config_file={} paths={:?}",

View File

@ -4,7 +4,6 @@ use crate::expand_path;
use anyhow::anyhow;
use directories::ProjectDirs;
use log::debug;
use std::fs;
use std::path::{Path, PathBuf};
pub fn format_stdin_cmd(
@ -55,8 +54,6 @@ pub fn format_stdin_cmd(
let path = expand_path(paths.first().unwrap(), work_dir);
let cache_dir = proj_dirs.cache_dir().join("eval-cache");
// Make sure the cache directory exists.
fs::create_dir_all(&cache_dir)?;
debug!(
"tree_root={} work_dir={} cache_dir={} config_file={} path={}",

View File

@ -9,7 +9,7 @@ use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
use std::collections::BTreeMap;
use std::fs::{read_to_string, File};
use std::fs::{create_dir_all, read_to_string, File};
use std::io::Write;
use std::path::{Path, PathBuf};
@ -78,6 +78,9 @@ impl CacheManifest {
pub fn try_write(self, cache_dir: &Path, treefmt_toml: &Path) -> Result<()> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml);
debug!("cache: writing to {}", manifest_path.display());
// Make sure the cache directory exists.
create_dir_all(manifest_path.parent().unwrap())?;
// Then write the file.
let mut f = File::create(manifest_path)?;
f.write_all(
format!(