Skip to content

Commit f932701

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 c3fc265 commit f932701

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
@@ -161,11 +161,8 @@ fn create_package(
161161
}
162162

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

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

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

190183
let uncompressed = HumanBytes(uncompressed_size);
@@ -218,22 +211,15 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
218211

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

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

239225
Ok(result)

tests/testsuite/build_dir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ 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
527+
[ROOT]/foo/build-dir/package/tmp-crate/foo-0.0.1.crate
528528
529529
"#]]);
530530

0 commit comments

Comments
 (0)