fix(artifact): delete old final artifacts before rebuild

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.
This commit is contained in:
Weihang Lo 2022-09-21 12:59:27 +01:00
parent bf1523b663
commit b47cacbacf
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7
1 changed files with 13 additions and 0 deletions

View File

@ -305,6 +305,19 @@ 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::debug!(
"failed to delete previous output file `{:?}`: {e:?}",
output.path
);
});
}
}
fn verbose_if_simple_exit_code(err: Error) -> Error {