Skip to content

Commit 8cc523d

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 2394ea6 commit 8cc523d

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,8 @@ fn create_package(
160160
}
161161

162162
let filename = pkg.package_id().tarball_name();
163-
let dir = ws.build_dir().join("package");
164-
let mut dst = {
165-
let tmp = format!(".{}", filename);
166-
dir.open_rw_exclusive_create(&tmp, gctx, "package scratch space")?
167-
};
163+
let dir = ws.build_dir().join("package").join("tmp-crate");
164+
let dst = dir.open_rw_exclusive_create(&filename, gctx, "package scratch space")?;
168165

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

179-
dst.seek(SeekFrom::Start(0))?;
180-
let dst_path = dst.parent().join(&filename);
181-
dst.rename(&dst_path)?;
182-
183176
let dst_metadata = dst
184177
.file()
185178
.metadata()
186-
.with_context(|| format!("could not learn metadata for: `{}`", dst_path.display()))?;
179+
.with_context(|| format!("could not learn metadata for: `{}`", dst.path().display()))?;
187180
let compressed_size = dst_metadata.len();
188181

189182
let uncompressed = HumanBytes(uncompressed_size);
@@ -217,22 +210,15 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
217210

218211
let packaged = do_package(ws, opts, pkgs)?;
219212

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

238224
Ok(result)

tests/testsuite/build_dir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ 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
496+
[ROOT]/foo/build-dir/package/tmp-crate/foo-0.0.1.crate
497497
498498
"#]]);
499499

0 commit comments

Comments
 (0)