From 72a181879123bba291b78537174c6783fab16946 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 2 Jan 2024 16:08:12 +0000 Subject: [PATCH] Don't re-write an identical lockfile (#2386) https://github.com/bazelbuild/rules_rust/pull/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. --- crate_universe/src/cli/generate.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/crate_universe/src/cli/generate.rs b/crate_universe/src/cli/generate.rs index fb866f2e8b..6215765c15 100644 --- a/crate_universe/src/cli/generate.rs +++ b/crate_universe/src/cli/generate.rs @@ -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; @@ -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(())