Skip to content

Commit ff45d84

Browse files
fix: fix packaging behavior
1 parent 83475b1 commit ff45d84

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
221221
for (pkg, _, src) in packaged {
222222
let filename = pkg.package_id().tarball_name();
223223
let dst = artifact_dir.open_rw_exclusive_create(filename, ws.gctx(), "uplifted package")?;
224+
dst.file().set_len(0)?;
224225
src.file().seek(SeekFrom::Start(0))?;
225226
std::io::copy(&mut src.file(), &mut dst.file())?;
226227
result.push(dst);

tests/testsuite/package.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7898,11 +7898,8 @@ fn package_dir_not_excluded_from_backups() {
78987898

78997899
#[cargo_test]
79007900
fn repackage_smaller_crate_has_trailing_garbage() {
7901-
// When a package is re-packaged and the
7902-
// new .crate file is smaller than the previous one, the artifact on disk has
7903-
// trailing garbage bytes because the destination file is not truncated before
7904-
// writing.
7905-
// See https://github.com/rust-lang/cargo/issues/16683.
7901+
// When a package is re-packaged and the new .crate file is smaller than
7902+
// the previous one, the artifact on disk should be smaller without trailing garbage bytes.
79067903
let big_file_contents = "x".repeat(100_000);
79077904
let p = project()
79087905
.file(
@@ -7945,11 +7942,9 @@ fn repackage_smaller_crate_has_trailing_garbage() {
79457942

79467943
let second_size = fs::metadata(&crate_path).unwrap().len();
79477944

7948-
// Without truncating dst before writing, the file on disk retains the
7949-
// old (larger) size. The second size should be much less than the first, but
7950-
// instead it equals the first size because trailing garbage was left behind.
7951-
assert_eq!(
7952-
first_size, second_size,
7953-
"the .crate file should have the same size after removing big.txt"
7945+
// The target .crate file *is* smaller.
7946+
assert!(
7947+
first_size > second_size,
7948+
"the .crate file should be smaller after removing big.txt"
79547949
);
79557950
}

0 commit comments

Comments
 (0)