Skip to content

Commit

Permalink
fix(artifact): delete old final artifacts before rebuild
Browse files Browse the repository at this point in the history
Some linkers do not remove the executable, but truncate and modify it.
That results in the old hard-link being modified even after renamed.
We delete the old artifact here to prevent this behavior from confusing users.
See rust-lang#8348.
  • Loading branch information
weihanglo committed Sep 21, 2022
1 parent 36258ad commit 58215c3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
paths::remove_file(&dst)?;
}
}

// Some linkers do not remove the executable, but truncate and modify it.
// That results in the old hard-link being modified even after renamed.
// We delete the old artifact here to prevent this behavior from confusing users.
// See rust-lang/cargo#8348.
if output.hardlink.is_some() && output.path.exists() {
_ = paths::remove_file(&output.path).map_err(|e| {
log::warn!("failed to delete previous output file `{:?}`: {e:?}", output.path);
});
}
}

fn verbose_if_simple_exit_code(err: Error) -> Error {
Expand Down

0 comments on commit 58215c3

Please sign in to comment.