Skip to content

Commit c3043c5

Browse files
committed
Allow unpublishable crates to be packaged
1 parent e7073ea commit c3043c5

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
249249
} else {
250250
let tarball = create_package(ws, &pkg, ar_files, local_reg.as_ref())?;
251251
if let Some(local_reg) = local_reg.as_mut() {
252-
local_reg.add_package(ws, &pkg, &tarball)?;
252+
if pkg.publish() != &Some(Vec::new()) {
253+
local_reg.add_package(ws, &pkg, &tarball)?;
254+
}
253255
}
254256
outputs.push((pkg, opts, tarball));
255257
}
@@ -290,7 +292,10 @@ fn get_registry(
290292
if let RegistryOrIndex::Registry(reg_name) = reg {
291293
for pkg in pkgs {
292294
if let Some(allowed) = pkg.publish().as_ref() {
293-
if !allowed.iter().any(|a| a == &reg_name) {
295+
// If allowed is empty (i.e. package.publish is false), we let it slide.
296+
// This allows packaging unpublishable packages (although packaging might
297+
// fail later if the unpublishable package is a dependency of something else).
298+
if !allowed.is_empty() && !allowed.iter().any(|a| a == &reg_name) {
294299
bail!(
295300
"`{}` cannot be packaged.\n\
296301
The registry `{}` is not listed in the `package.publish` value in Cargo.toml.",

src/cargo/ops/registry/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,19 @@ pub(crate) struct RegistrySourceIds {
325325

326326
/// If this set of packages has an unambiguous publish registry, find it.
327327
pub(crate) fn infer_registry(pkgs: &[&Package]) -> CargoResult<Option<RegistryOrIndex>> {
328-
if pkgs[1..].iter().all(|p| p.publish() == pkgs[0].publish()) {
329-
// If all packages have the same publish settings, we take that as the default.
330-
match pkgs[0].publish().as_deref() {
328+
// Ignore "publish = false" packages while inferring the registry.
329+
let publishable_pkgs: Vec<_> = pkgs
330+
.iter()
331+
.filter(|p| p.publish() != &Some(Vec::new()))
332+
.collect();
333+
334+
let Some((first, rest)) = publishable_pkgs.split_first() else {
335+
return Ok(None);
336+
};
337+
338+
// If all packages have the same publish settings, we take that as the default.
339+
if rest.iter().all(|p| p.publish() == first.publish()) {
340+
match publishable_pkgs[0].publish().as_deref() {
331341
Some([unique_pkg_reg]) => {
332342
Ok(Some(RegistryOrIndex::Registry(unique_pkg_reg.to_owned())))
333343
}
@@ -346,7 +356,7 @@ pub(crate) fn infer_registry(pkgs: &[&Package]) -> CargoResult<Option<RegistryOr
346356
}
347357
}
348358
} else {
349-
let common_regs = pkgs
359+
let common_regs = publishable_pkgs
350360
.iter()
351361
// `None` means "all registries", so drop them instead of including them
352362
// in the intersection.

tests/testsuite/package.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6227,19 +6227,41 @@ fn registry_inference_ignores_unpublishable() {
62276227

62286228
p.cargo("package -Zpackage-workspace")
62296229
.masquerade_as_nightly_cargo(&["package-workspace"])
6230-
.with_status(101)
62316230
.with_stderr_data(str![[r#"
6232-
[ERROR] conflicts between `package.publish` fields in the selected packages
6231+
[PACKAGING] dep v0.1.0 ([ROOT]/foo/dep)
6232+
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
6233+
[PACKAGING] main v0.0.1 ([ROOT]/foo/main)
6234+
[UPDATING] `alternative` index
6235+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
6236+
[VERIFYING] dep v0.1.0 ([ROOT]/foo/dep)
6237+
[COMPILING] dep v0.1.0 ([ROOT]/foo/target/package/dep-0.1.0)
6238+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
6239+
[VERIFYING] main v0.0.1 ([ROOT]/foo/main)
6240+
[UPDATING] `alternative` index
6241+
[UNPACKING] dep v0.1.0 (registry `[ROOT]/foo/target/package/tmp-registry`)
6242+
[COMPILING] dep v0.1.0 (registry `alternative`)
6243+
[COMPILING] main v0.0.1 ([ROOT]/foo/target/package/main-0.0.1)
6244+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
62336245
62346246
"#]])
62356247
.run();
62366248

62376249
p.cargo("package -Zpackage-workspace --registry=alternative")
62386250
.masquerade_as_nightly_cargo(&["package-workspace"])
6239-
.with_status(101)
62406251
.with_stderr_data(str![[r#"
6241-
[ERROR] `main` cannot be packaged.
6242-
The registry `alternative` is not listed in the `package.publish` value in Cargo.toml.
6252+
[PACKAGING] dep v0.1.0 ([ROOT]/foo/dep)
6253+
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
6254+
[PACKAGING] main v0.0.1 ([ROOT]/foo/main)
6255+
[UPDATING] `alternative` index
6256+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
6257+
[VERIFYING] dep v0.1.0 ([ROOT]/foo/dep)
6258+
[COMPILING] dep v0.1.0 ([ROOT]/foo/target/package/dep-0.1.0)
6259+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
6260+
[VERIFYING] main v0.0.1 ([ROOT]/foo/main)
6261+
[UPDATING] `alternative` index
6262+
[COMPILING] dep v0.1.0 (registry `alternative`)
6263+
[COMPILING] main v0.0.1 ([ROOT]/foo/target/package/main-0.0.1)
6264+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
62436265
62446266
"#]])
62456267
.run();
@@ -6464,7 +6486,16 @@ fn unpublishable_dependency() {
64646486
.masquerade_as_nightly_cargo(&["package-workspace"])
64656487
.with_status(101)
64666488
.with_stderr_data(str![[r#"
6467-
[ERROR] conflicts between `package.publish` fields in the selected packages
6489+
[PACKAGING] dep v0.1.0 ([ROOT]/foo/dep)
6490+
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
6491+
[PACKAGING] main v0.0.1 ([ROOT]/foo/main)
6492+
[UPDATING] `alternative` index
6493+
[ERROR] failed to prepare local package for uploading
6494+
6495+
Caused by:
6496+
no matching package named `dep` found
6497+
location searched: registry `alternative`
6498+
required by package `main v0.0.1 ([ROOT]/foo/main)`
64686499
64696500
"#]])
64706501
.run();

0 commit comments

Comments
 (0)