Skip to content

Commit

Permalink
Don't re-write an identical lockfile (#2386)
Browse files Browse the repository at this point in the history
#1910 has a workaround for
this because changing the timestamp may invalidate the repository rule.

Instead, just don't do the write if the contents is identical.
  • Loading branch information
illicitonion authored Jan 2, 2024
1 parent dd944ed commit 72a1818
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions crate_universe/src/cli/generate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! The cli entrypoint for the `generate` subcommand
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::{bail, Context as AnyhowContext, Result};
use cargo_lock::Lockfile;
use clap::Parser;

use crate::config::Config;
Expand Down Expand Up @@ -128,8 +129,21 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
write_lockfile(lock_content, &lockfile, opt.dry_run)?;
}

// Write the updated Cargo.lock file
fs::write(&opt.cargo_lockfile, cargo_lockfile.to_string())
update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?;

Ok(())
}

fn update_cargo_lockfile(path: &Path, cargo_lockfile: Lockfile) -> Result<()> {
let old_contents = fs::read_to_string(path).ok();
let new_contents = cargo_lockfile.to_string();

// Don't overwrite identical contents because timestamp changes may invalidate repo rules.
if old_contents.as_ref() == Some(&new_contents) {
return Ok(());
}

fs::write(path, new_contents)
.context("Failed to write Cargo.lock file back to the workspace.")?;

Ok(())
Expand Down

0 comments on commit 72a1818

Please sign in to comment.