Skip to content

Commit 5bb7eeb

Browse files
committed
devenv: further reduce file modifications on assemble
This helps reduce accidental direnv triggers.
1 parent b292bc9 commit 5bb7eeb

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

devenv/src/devenv.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,20 +1002,18 @@ impl Devenv {
10021002
}
10031003
util::write_file_with_lock(
10041004
self.devenv_dotfile.join("flake.json"),
1005-
&serde_json::to_string(&flake_inputs).unwrap(),
1005+
serde_json::to_string(&flake_inputs).unwrap(),
10061006
)?;
1007-
fs::write(
1007+
util::write_file_with_lock(
10081008
self.devenv_dotfile.join("devenv.json"),
10091009
serde_json::to_string(&self.config).unwrap(),
1010-
)
1011-
.expect("Failed to write devenv.json");
1010+
)?;
10121011
// TODO: superceded by eval caching.
10131012
// Remove once direnvrc migration is implemented.
1014-
fs::write(
1013+
util::write_file_with_lock(
10151014
self.devenv_dotfile.join("imports.txt"),
10161015
self.config.imports.join("\n"),
1017-
)
1018-
.expect("Failed to write imports.txt");
1016+
)?;
10191017

10201018
fs::create_dir_all(&self.devenv_runtime).map_err(|e| {
10211019
miette::miette!("Failed to create {}: {}", self.devenv_runtime.display(), e)
@@ -1066,8 +1064,7 @@ impl Devenv {
10661064

10671065
cli_options.push_str("}\n");
10681066

1069-
fs::write(self.devenv_dotfile.join("cli-options.nix"), cli_options)
1070-
.expect("Failed to write cli-options.nix");
1067+
util::write_file_with_lock(self.devenv_dotfile.join("cli-options.nix"), &cli_options)?;
10711068
} else {
10721069
// Remove the file if it exists but there are no CLI options
10731070
let cli_options_path = self.devenv_dotfile.join("cli-options.nix");
@@ -1120,7 +1117,7 @@ impl Devenv {
11201117
let env = self.nix.dev_env(json, &gc_root).instrument(span).await?;
11211118

11221119
use devenv_eval_cache::command::{FileInputDesc, Input};
1123-
fs::write(
1120+
util::write_file_with_lock(
11241121
self.devenv_dotfile.join("input-paths.txt"),
11251122
env.inputs
11261123
.iter()
@@ -1139,8 +1136,7 @@ impl Devenv {
11391136
})
11401137
.collect::<Vec<_>>()
11411138
.join("\n"),
1142-
)
1143-
.expect("Failed to write input-paths.txt");
1139+
)?;
11441140

11451141
Ok(DevEnv { output: env.stdout })
11461142
}

devenv/src/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use std::path::Path;
77
/// Safely write a file with locking, avoiding writing if the content hasn't changed.
88
///
99
/// Returns Ok(true) if the file was written, Ok(false) if no write was needed.
10-
pub fn write_file_with_lock<P: AsRef<Path>>(path: P, content: &str) -> Result<bool> {
10+
pub fn write_file_with_lock<P: AsRef<Path>, S: AsRef<str>>(path: P, content: S) -> Result<bool> {
1111
let path = path.as_ref();
12+
let content = content.as_ref();
1213

1314
// Create parent directories if they don't exist
1415
if let Some(parent) = path.parent() {

0 commit comments

Comments
 (0)