Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't re-write an identical lockfile #2386

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading