From 4e28230f7e5e4a194a6b445083b90539fc5a8d3f Mon Sep 17 00:00:00 2001 From: Ruben Arts Date: Mon, 13 May 2024 08:56:38 +0200 Subject: [PATCH] fix: only write prefix file if needed (#1363) --- src/environment.rs | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/environment.rs b/src/environment.rs index 29bbeed82..385b6df8a 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -103,21 +103,34 @@ async fn prefix_location_changed( /// Create the prefix location file. /// Give it the environment path to place it. fn create_prefix_location_file(environment_dir: &Path) -> miette::Result<()> { - let prefix_file = environment_dir + let prefix_file_path = environment_dir .join("conda-meta") .join(consts::PREFIX_FILE_NAME); + tracing::info!("Creating prefix file at: {}", prefix_file_path.display()); - tracing::info!("create prefix file: {}", prefix_file.display()); - let binding = prefix_file.clone(); - let parent = binding - .parent() - .ok_or_else(|| miette::miette!("cannot find parent of '{}'", binding.display()))?; + let parent_dir = prefix_file_path.parent().ok_or_else(|| { + miette::miette!( + "Cannot find parent directory of '{}'", + prefix_file_path.display() + ) + })?; + + if parent_dir.exists() { + let contents = parent_dir.to_string_lossy(); + + let path = Path::new(&prefix_file_path); + // Read existing contents to determine if an update is necessary + if path.exists() { + let existing_contents = std::fs::read_to_string(path).into_diagnostic()?; + if existing_contents == contents { + tracing::info!("No update needed for the prefix file."); + return Ok(()); + } + } - if parent.exists() { - let contents = parent.to_str().ok_or_else(|| { - miette::miette!("failed to convert path to str: '{}'", parent.display()) - })?; - std::fs::write(prefix_file, contents).into_diagnostic()?; + // Write new contents to the prefix file + std::fs::write(path, contents.as_ref()).into_diagnostic()?; + tracing::info!("Prefix file updated with: '{}'.", contents); } Ok(()) }