Skip to content

Commit 86f6344

Browse files
committed
fix(publish): Move .crate out of final artifact location
When `target_dir == build_dir`, ensure `cargo publish` doesn't put intermediate artifacts in the final artifact location of `cargo package`. If anyone was relying on this behavior of `cargo publish`, it will break them. We could avoid this and instead consider the location change to be part of the opt-in of using `build-dir` (until we make it opt-out). Note that we expect to be able to change the layouf of content written to `build-dir` even if users aren't opting in. On the other hand, this will help identify people relying on intermediate artifacts. While there aren't any performance benefits to this, it consolidates all of the uplifting logic and avoids dealing with overlapping `target_dir` and `build_dir`. We could optimize this further by doing a `rename` and only doing a copy if that fails.
1 parent 8e43074 commit 86f6344

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,8 @@ fn create_package(
163163
let filename = pkg.package_id().tarball_name();
164164
let build_dir = ws.build_dir();
165165
paths::create_dir_all_excluded_from_backups_atomic(build_dir.as_path_unlocked())?;
166-
let dir = build_dir.join("package");
167-
let mut dst = {
168-
let tmp = format!(".{}", filename);
169-
dir.open_rw_exclusive_create(&tmp, gctx, "package scratch space")?
170-
};
166+
let dir = build_dir.join("package").join("tmp-crate");
167+
let dst = dir.open_rw_exclusive_create(&filename, gctx, "package scratch space")?;
171168

172169
// Package up and test a temporary tarball and only move it to the final
173170
// location if it actually passes all our tests. Any previously existing
@@ -179,14 +176,10 @@ fn create_package(
179176
let uncompressed_size = tar(ws, opts, pkg, local_reg, ar_files, dst.file(), &filename)
180177
.context("failed to prepare local package for uploading")?;
181178

182-
dst.seek(SeekFrom::Start(0))?;
183-
let dst_path = dst.parent().join(&filename);
184-
dst.rename(&dst_path)?;
185-
186179
let dst_metadata = dst
187180
.file()
188181
.metadata()
189-
.with_context(|| format!("could not learn metadata for: `{}`", dst_path.display()))?;
182+
.with_context(|| format!("could not learn metadata for: `{}`", dst.path().display()))?;
190183
let compressed_size = dst_metadata.len();
191184

192185
let uncompressed = HumanBytes(uncompressed_size);
@@ -220,23 +213,17 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
220213

221214
let packaged = do_package(ws, opts, pkgs)?;
222215

216+
// Uplifting artifacts
223217
let mut result = Vec::new();
224218
let target_dir = ws.target_dir();
225-
let build_dir = ws.build_dir();
226-
if target_dir == build_dir {
227-
result.extend(packaged.into_iter().map(|(_, _, src)| src));
228-
} else {
229-
// Uplifting artifacts
230-
paths::create_dir_all_excluded_from_backups_atomic(target_dir.as_path_unlocked())?;
231-
let artifact_dir = target_dir.join("package");
232-
for (pkg, _, src) in packaged {
233-
let filename = pkg.package_id().tarball_name();
234-
let dst =
235-
artifact_dir.open_rw_exclusive_create(filename, ws.gctx(), "uplifted package")?;
236-
src.file().seek(SeekFrom::Start(0))?;
237-
std::io::copy(&mut src.file(), &mut dst.file())?;
238-
result.push(dst);
239-
}
219+
paths::create_dir_all_excluded_from_backups_atomic(target_dir.as_path_unlocked())?;
220+
let artifact_dir = target_dir.join("package");
221+
for (pkg, _, src) in packaged {
222+
let filename = pkg.package_id().tarball_name();
223+
let dst = artifact_dir.open_rw_exclusive_create(filename, ws.gctx(), "uplifted package")?;
224+
src.file().seek(SeekFrom::Start(0))?;
225+
std::io::copy(&mut src.file(), &mut dst.file())?;
226+
result.push(dst);
240227
}
241228

242229
Ok(result)

tests/testsuite/build_dir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,8 @@ fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
524524
[ROOT]/foo/build-dir/package/foo-0.0.1/Cargo.toml
525525
[ROOT]/foo/build-dir/package/foo-0.0.1/Cargo.toml.orig
526526
[ROOT]/foo/build-dir/package/foo-0.0.1/src/main.rs
527-
[ROOT]/foo/build-dir/package/foo-0.0.1.crate
528527
[ROOT]/foo/build-dir/CACHEDIR.TAG
528+
[ROOT]/foo/build-dir/package/tmp-crate/foo-0.0.1.crate
529529
530530
"#]]);
531531

tests/testsuite/build_dir_legacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
493493
[ROOT]/foo/build-dir/package/foo-0.0.1/Cargo.toml
494494
[ROOT]/foo/build-dir/package/foo-0.0.1/Cargo.toml.orig
495495
[ROOT]/foo/build-dir/package/foo-0.0.1/src/main.rs
496-
[ROOT]/foo/build-dir/package/foo-0.0.1.crate
497496
[ROOT]/foo/build-dir/CACHEDIR.TAG
497+
[ROOT]/foo/build-dir/package/tmp-crate/foo-0.0.1.crate
498498
499499
"#]]);
500500

0 commit comments

Comments
 (0)