Skip to content

Commit 763ee29

Browse files
committed
Allow unpublishable crates to be packaged
1 parent 3fddcfe commit 763ee29

File tree

3 files changed

+89
-40
lines changed

3 files changed

+89
-40
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ fn get_registry(
198198
if let RegistryOrIndex::Registry(reg_name) = reg {
199199
for pkg in pkgs {
200200
if let Some(allowed) = pkg.publish().as_ref() {
201-
if !allowed.iter().any(|a| a == &reg_name) {
201+
// If allowed is empty (i.e. package.publish is false), we let it slide.
202+
// This allows packaging unpublishable packages (although packaging might
203+
// fail later if the unpublishable package is a dependency of something else).
204+
if !allowed.is_empty() && !allowed.iter().any(|a| a == &reg_name) {
202205
bail!(
203206
"`{}` cannot be packaged.\n\
204207
The registry `{}` is not listed in the `package.publish` value in Cargo.toml.",
@@ -282,7 +285,9 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
282285
} else {
283286
let tarball = create_package(ws, &pkg, ar_files, local_reg.as_ref())?;
284287
if let Some(local_reg) = local_reg.as_mut() {
285-
local_reg.add_package(ws, &pkg, &tarball)?;
288+
if pkg.publish() != &Some(Vec::new()) {
289+
local_reg.add_package(ws, &pkg, &tarball)?;
290+
}
286291
}
287292
outputs.push((pkg, opts, tarball));
288293
}

src/cargo/ops/registry/mod.rs

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -325,39 +325,52 @@ 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() {
331-
Some([unique_pkg_reg]) => {
332-
Ok(Some(RegistryOrIndex::Registry(unique_pkg_reg.to_owned())))
333-
}
334-
None | Some([]) => Ok(None),
335-
Some(regs) => {
336-
let mut regs: Vec<_> = regs.iter().map(|s| format!("\"{}\"", s)).collect();
337-
regs.sort();
338-
regs.dedup();
339-
// unwrap: the match block ensures that there's more than one reg.
340-
let (last_reg, regs) = regs.split_last().unwrap();
341-
bail!(
342-
"--registry is required to disambiguate between {} or {} registries",
343-
regs.join(", "),
344-
last_reg
345-
)
328+
// Ignore "publish = false" packages while inferring the registry.
329+
let pkgs: Vec<_> = pkgs
330+
.iter()
331+
.filter(|p| p.publish() != &Some(Vec::new()))
332+
.collect();
333+
334+
match &pkgs[..] {
335+
[] => Ok(None),
336+
[first, rest @ ..] => {
337+
// If all packages have the same publish settings, we take that as the default.
338+
if rest.iter().all(|p| p.publish() == first.publish()) {
339+
match pkgs[0].publish().as_deref() {
340+
Some([unique_pkg_reg]) => {
341+
Ok(Some(RegistryOrIndex::Registry(unique_pkg_reg.to_owned())))
342+
}
343+
None | Some([]) => Ok(None),
344+
Some(regs) => {
345+
let mut regs: Vec<_> = regs.iter().map(|s| format!("\"{}\"", s)).collect();
346+
regs.sort();
347+
regs.dedup();
348+
// unwrap: the match block ensures that there's more than one reg.
349+
let (last_reg, regs) = regs.split_last().unwrap();
350+
bail!(
351+
"--registry is required to disambiguate between {} or {} registries",
352+
regs.join(", "),
353+
last_reg
354+
)
355+
}
356+
}
357+
} else {
358+
let common_regs = pkgs
359+
.iter()
360+
// `None` means "all registries", so drop them instead of including them
361+
// in the intersection.
362+
.filter_map(|p| p.publish().as_deref())
363+
.map(|p| p.iter().collect::<HashSet<_>>())
364+
.reduce(|xs, ys| xs.intersection(&ys).cloned().collect())
365+
.unwrap_or_default();
366+
if common_regs.is_empty() {
367+
bail!("conflicts between `package.publish` fields in the selected packages");
368+
} else {
369+
bail!(
370+
"--registry is required because not all `package.publish` settings agree",
371+
);
372+
}
346373
}
347374
}
348-
} else {
349-
let common_regs = pkgs
350-
.iter()
351-
// `None` means "all registries", so drop them instead of including them
352-
// in the intersection.
353-
.filter_map(|p| p.publish().as_deref())
354-
.map(|p| p.iter().collect::<HashSet<_>>())
355-
.reduce(|xs, ys| xs.intersection(&ys).cloned().collect())
356-
.unwrap_or_default();
357-
if common_regs.is_empty() {
358-
bail!("conflicts between `package.publish` fields in the selected packages");
359-
} else {
360-
bail!("--registry is required because not all `package.publish` settings agree",);
361-
}
362375
}
363376
}

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)