Skip to content

Commit 89c24bf

Browse files
committed
refactor logic
1 parent d6d53ae commit 89c24bf

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

src/cargo/ops/registry.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use anyhow::{bail, format_err, Context as _};
1212
use cargo_util::paths;
1313
use crates_io::{self, NewCrate, NewCrateDependency, Registry};
1414
use curl::easy::{Easy, InfoType, SslOpt, SslVersion};
15-
use itertools::Itertools;
1615
use log::{log, Level};
1716
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
1817
use termcolor::Color::Green;
@@ -93,35 +92,21 @@ pub struct PublishOpts<'cfg> {
9392
pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
9493
let specs = opts.to_publish.to_package_id_specs(ws)?;
9594

96-
let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?;
97-
98-
if let Packages(opt_in) = &opts.to_publish {
99-
if ws.is_virtual() {
100-
bail!("can't use \"--package <SPEC>\" in virtual manifest")
101-
}
102-
let matched_packages = ws
103-
.members()
104-
.filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
105-
.collect::<Vec<_>>();
106-
if matched_packages.is_empty() {
107-
bail!(
108-
"not found `{}` in manifest members. Check in manifest path `{}`",
109-
opt_in.get(0).unwrap(),
110-
ws.root().display()
111-
)
95+
if let Packages(_) = &opts.to_publish {
96+
if specs.len() > 1 {
97+
bail!("the `-p` argument must be specified to select a single package to publish");
11298
}
113-
if matched_packages.len() > 1 {
114-
bail!(
115-
"found multiple `{}` in manifest members. Check in manifest path `{}`",
116-
opt_in.get(0).unwrap(),
117-
ws.root().display()
118-
)
99+
} else {
100+
if ws.is_virtual() {
101+
bail!("must use `-p` argument in virtual manifest")
119102
}
120-
pkgs = pkgs
121-
.into_iter()
122-
.filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id())))
123-
.collect_vec();
124103
}
104+
let member_ids = ws.members().map(|p| p.package_id());
105+
// Check that the spec matches exactly one member.
106+
specs[0].query(member_ids)?;
107+
let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?;
108+
// Double check
109+
assert_eq!(pkgs.len(), 1);
125110

126111
let (pkg, cli_features) = pkgs.pop().unwrap();
127112

tests/testsuite/publish.rs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,7 @@ fn in_package_workspace() {
16751675
[package]
16761676
name = "foo"
16771677
version = "0.1.0"
1678-
edition = "2018"
1678+
edition = "2021"
16791679
[workspace]
16801680
members = ["li"]
16811681
"#,
@@ -1735,9 +1735,9 @@ fn in_virtual_workspace() {
17351735
.file("foo/src/main.rs", "fn main() {}")
17361736
.build();
17371737

1738-
p.cargo("publish --no-verify --token sekrit -p foo")
1738+
p.cargo("publish --no-verify --token sekrit")
17391739
.with_status(101)
1740-
.with_stderr("error: can't use \"--package <SPEC>\" in virtual manifest")
1740+
.with_stderr("error: must use `-p` argument in virtual manifest")
17411741
.run();
17421742
}
17431743

@@ -1752,7 +1752,7 @@ fn in_package_workspace_not_found() {
17521752
[package]
17531753
name = "foo"
17541754
version = "0.1.0"
1755-
edition = "2018"
1755+
edition = "2021"
17561756
[workspace]
17571757
"#,
17581758
)
@@ -1776,14 +1776,16 @@ fn in_package_workspace_not_found() {
17761776
.with_status(101)
17771777
.with_stderr(
17781778
"\
1779-
error: not found `li` in manifest members. Check in manifest path `[CWD]`
1779+
error: package ID specification `li` did not match any packages
1780+
1781+
<tab>Did you mean `foo`?
17801782
",
17811783
)
17821784
.run();
17831785
}
17841786

17851787
#[cargo_test]
1786-
fn in_package_workspace_found_mutilate() {
1788+
fn in_package_workspace_found_multiple() {
17871789
registry::init();
17881790

17891791
let p = project()
@@ -1793,7 +1795,7 @@ fn in_package_workspace_found_mutilate() {
17931795
[package]
17941796
name = "foo"
17951797
version = "0.1.0"
1796-
edition = "2018"
1798+
edition = "2021"
17971799
[workspace]
17981800
members = ["li","lii"]
17991801
"#,
@@ -1831,8 +1833,55 @@ fn in_package_workspace_found_mutilate() {
18311833
.with_status(101)
18321834
.with_stderr(
18331835
"\
1834-
error: found multiple `li*` in manifest members. Check in manifest path `[CWD]`
1836+
error: the `-p` argument must be specified to select a single package to publish
1837+
",
1838+
)
1839+
.run();
1840+
}
1841+
1842+
1843+
#[cargo_test]
1844+
// https://github.com/rust-lang/cargo/issues/10536
1845+
fn publish_path_dependency_without_workspace() {
1846+
registry::init();
1847+
1848+
let p = project()
1849+
.file(
1850+
"Cargo.toml",
1851+
r#"
1852+
[package]
1853+
name = "foo"
1854+
version = "0.1.0"
1855+
edition = "2021"
1856+
[dependencies.bar]
1857+
path = "bar"
1858+
"#,
1859+
)
1860+
.file("src/main.rs", "fn main() {}")
1861+
.file(
1862+
"bar/Cargo.toml",
1863+
r#"
1864+
[package]
1865+
name = "bar"
1866+
version = "0.0.1"
1867+
edition = "2021"
1868+
authors = []
1869+
license = "MIT"
1870+
description = "bar"
1871+
"#,
1872+
)
1873+
.file("bar/src/main.rs", "fn main() {}")
1874+
.build();
1875+
1876+
p.cargo("publish -p bar --no-verify --token sekrit ")
1877+
.with_status(101)
1878+
.with_stderr(
1879+
"\
1880+
error: package ID specification `bar` did not match any packages
1881+
1882+
<tab>Did you mean `foo`?
18351883
",
18361884
)
18371885
.run();
18381886
}
1887+

0 commit comments

Comments
 (0)