Skip to content

Commit 52165e8

Browse files
committed
update
1 parent 72d6c54 commit 52165e8

File tree

2 files changed

+114
-10
lines changed

2 files changed

+114
-10
lines changed

src/cargo/ops/registry.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::core::resolver::CliFeatures;
2323
use crate::core::source::Source;
2424
use crate::core::{Package, SourceId, Workspace};
2525
use crate::ops;
26-
use crate::ops::Packages::Packages;
26+
use crate::ops::Packages;
2727
use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_IO_REGISTRY};
2828
use crate::util::config::{self, Config, SslVersionConfig, SslVersionConfigRange};
2929
use crate::util::errors::CargoResult;
@@ -91,21 +91,31 @@ pub struct PublishOpts<'cfg> {
9191

9292
pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
9393
let specs = opts.to_publish.to_package_id_specs(ws)?;
94-
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");
98-
}
99-
} else {
100-
if ws.is_virtual() {
101-
bail!("the `-p` argument must be specified in the root of a virtual workspace")
94+
if specs.len() > 1 {
95+
match opts.to_publish {
96+
Packages::Default => {
97+
bail!("must be specified to select a single package to publish. Check `default-members` or using `-p` argument")
98+
}
99+
Packages::Packages(_) => {
100+
bail!("the `-p` argument must be specified to select a single package to publish")
101+
}
102+
_ => {}
102103
}
103104
}
105+
if Packages::Packages(vec![]) != opts.to_publish && ws.is_virtual() {
106+
bail!("the `-p` argument must be specified in the root of a virtual workspace")
107+
}
104108
let member_ids = ws.members().map(|p| p.package_id());
105109
// Check that the spec matches exactly one member.
106110
specs[0].query(member_ids)?;
107111
let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?;
108-
// Double check
112+
// In `members_with_features_old`, it will add "current" package(determined by the cwd). Line:1455 in workspace.rs.
113+
// So we need filter
114+
pkgs = pkgs
115+
.into_iter()
116+
.filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id())))
117+
.collect();
118+
// Double check. It is safe theoretically, unless logic has updated.
109119
assert_eq!(pkgs.len(), 1);
110120

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

tests/testsuite/publish.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,100 @@ See [..]
17091709
validate_upload_li();
17101710
}
17111711

1712+
#[cargo_test]
1713+
fn with_duplicate_spec_in_members() {
1714+
registry::init();
1715+
1716+
let p = project()
1717+
.file(
1718+
"Cargo.toml",
1719+
r#"
1720+
[package]
1721+
name = "foo"
1722+
version = "0.1.0"
1723+
[workspace]
1724+
resolver = "2"
1725+
members = ["li","bar"]
1726+
default-members = ["li","bar"]
1727+
"#,
1728+
)
1729+
.file("src/main.rs", "fn main() {}")
1730+
.file(
1731+
"li/Cargo.toml",
1732+
r#"
1733+
[package]
1734+
name = "li"
1735+
version = "0.0.1"
1736+
description = "li"
1737+
license = "MIT"
1738+
"#,
1739+
)
1740+
.file("li/src/main.rs", "fn main() {}")
1741+
.file(
1742+
"bar/Cargo.toml",
1743+
r#"
1744+
[package]
1745+
name = "bar"
1746+
version = "0.0.1"
1747+
description = "bar"
1748+
license = "MIT"
1749+
"#,
1750+
)
1751+
.file("bar/src/main.rs", "fn main() {}")
1752+
.build();
1753+
1754+
p.cargo("publish --no-verify --token sekrit")
1755+
.with_status(101)
1756+
.with_stderr(
1757+
"error: must be specified to select a single package to publish. Check `default-members` or using `-p` argument",
1758+
)
1759+
.run();
1760+
}
1761+
1762+
#[cargo_test]
1763+
fn in_package_workspace_with_members_with_features_old() {
1764+
registry::init();
1765+
1766+
let p = project()
1767+
.file(
1768+
"Cargo.toml",
1769+
r#"
1770+
[package]
1771+
name = "foo"
1772+
version = "0.1.0"
1773+
[workspace]
1774+
members = ["li"]
1775+
"#,
1776+
)
1777+
.file("src/main.rs", "fn main() {}")
1778+
.file(
1779+
"li/Cargo.toml",
1780+
r#"
1781+
[package]
1782+
name = "li"
1783+
version = "0.0.1"
1784+
description = "li"
1785+
license = "MIT"
1786+
"#,
1787+
)
1788+
.file("li/src/main.rs", "fn main() {}")
1789+
.build();
1790+
1791+
p.cargo("publish -p li --no-verify --token sekrit")
1792+
.with_stderr(
1793+
"\
1794+
[UPDATING] [..]
1795+
[WARNING] manifest has no documentation, homepage or repository.
1796+
See [..]
1797+
[PACKAGING] li v0.0.1 ([CWD]/li)
1798+
[UPLOADING] li v0.0.1 ([CWD]/li)
1799+
",
1800+
)
1801+
.run();
1802+
1803+
validate_upload_li();
1804+
}
1805+
17121806
#[cargo_test]
17131807
fn in_virtual_workspace() {
17141808
registry::init();

0 commit comments

Comments
 (0)