diff --git a/cargo-insta/src/container.rs b/cargo-insta/src/container.rs index 95fc20d6..062b79c7 100644 --- a/cargo-insta/src/container.rs +++ b/cargo-insta/src/container.rs @@ -164,6 +164,18 @@ impl SnapshotContainer { } pub(crate) fn commit(&mut self) -> Result<(), Box> { + // Try removing the snapshot file. If it fails, it's + // likely because it another process removed it; which + // is fine — print a message and continue. + let try_removing_snapshot = |p| { + fs::remove_file(p).unwrap_or_else(|_| { + eprintln!( + "Pending snapshot file at {:?} couldn't be removed. It was likely removed by another process.", + p + ); + }); + }; + if let Some(ref mut patcher) = self.patcher { let mut new_pending = vec![]; let mut did_accept = false; @@ -193,8 +205,7 @@ impl SnapshotContainer { if did_skip { PendingInlineSnapshot::save_batch(&self.snapshot_path, &new_pending)?; } else { - fs::remove_file(&self.snapshot_path) - .map_err(|e| ContentError::FileIo(e, self.snapshot_path.to_path_buf()))?; + try_removing_snapshot(&self.snapshot_path); } } else { // should only be one or this is weird @@ -203,14 +214,10 @@ impl SnapshotContainer { Operation::Accept => { let snapshot = Snapshot::from_file(&self.snapshot_path)?; snapshot.save(&self.target_path)?; - fs::remove_file(&self.snapshot_path).map_err(|e| { - ContentError::FileIo(e, self.snapshot_path.to_path_buf()) - })?; + try_removing_snapshot(&self.snapshot_path); } Operation::Reject => { - fs::remove_file(&self.snapshot_path).map_err(|e| { - ContentError::FileIo(e, self.snapshot_path.to_path_buf()) - })?; + try_removing_snapshot(&self.snapshot_path); } Operation::Skip => {} }