Skip to content

Commit

Permalink
fix: only write prefix file if needed (#1363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts authored May 13, 2024
1 parent cea28f7 commit 4e28230
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down

0 comments on commit 4e28230

Please sign in to comment.