From 95cb21d6a105885dca88b95e58f9f39218e174bc Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Wed, 18 May 2022 22:40:55 +0800 Subject: [PATCH 01/12] fix cargo -p --- src/cargo/core/workspace.rs | 1 + src/cargo/ops/cargo_compile.rs | 34 +++++-- tests/testsuite/publish.rs | 159 +++++++++++++++++++++++++++------ 3 files changed, 162 insertions(+), 32 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index cf5aeb5161e..98c833878b7 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1449,6 +1449,7 @@ impl<'cfg> Workspace<'cfg> { let ms: Vec<_> = self .members() + .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) .filter_map(|member| { let member_id = member.package_id(); match self.current_opt() { diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 07dabce6d08..dd655ae6461 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -146,10 +146,15 @@ impl Packages { emit_pattern_not_found(ws, patterns, true).or_else(warn)?; specs } - Packages::Packages(packages) if packages.is_empty() => { - vec![PackageIdSpec::from_package_id(ws.current()?.package_id())] - } Packages::Packages(opt_in) => { + if ws.is_virtual() { + bail!("can't use \"--package \" in virtual manifest") + } + if opt_in.is_empty() { + return Ok(vec![PackageIdSpec::from_package_id( + ws.current()?.package_id(), + )]); + } let (mut patterns, packages) = opt_patterns_and_names(opt_in)?; let mut specs = packages .iter() @@ -164,6 +169,25 @@ impl Packages { specs.extend(matched_pkgs); } emit_pattern_not_found(ws, patterns, false)?; + let matched_packages = ws + .members() + .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) + .collect::>(); + if matched_packages.is_empty() { + bail!( + "not found `{}` in manifest members. Check in manifest path `{}`", + opt_in.get(0).unwrap(), + ws.root().display() + ) + } + if matched_packages.len() > 1 { + bail!( + "found mutilate `{}` in manifest members. Check in manifest path `{}`", + opt_in.get(0).unwrap(), + ws.root().display() + ) + } + specs } Packages::Default => ws @@ -174,13 +198,13 @@ impl Packages { }; if specs.is_empty() { if ws.is_virtual() { - anyhow::bail!( + bail!( "manifest path `{}` contains no package: The manifest is virtual, \ and the workspace has no members.", ws.root().display() ) } - anyhow::bail!("no packages to compile") + bail!("no packages to compile") } Ok(specs) } diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 88f9fee8e86..257d204dbcb 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -56,7 +56,7 @@ fn validate_upload_foo() { ); } -fn validate_upload_bar() { +fn validate_upload_li() { publish::validate_upload( r#" { @@ -64,7 +64,7 @@ fn validate_upload_bar() { "badges": {}, "categories": [], "deps": [], - "description": "bar", + "description": "li", "documentation": null, "features": {}, "homepage": null, @@ -72,14 +72,14 @@ fn validate_upload_bar() { "license": "MIT", "license_file": null, "links": null, - "name": "bar", + "name": "li", "readme": null, "readme_file": null, "repository": null, "vers": "0.0.1" } "#, - "bar-0.0.1.crate", + "li-0.0.1.crate", &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], ); } @@ -1665,7 +1665,52 @@ Caused by: } #[cargo_test] -fn in_workspace() { +fn in_package_workspace() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2018" + [workspace] + members = ["li"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "li/Cargo.toml", + r#" + [package] + name = "li" + version = "0.0.1" + description = "li" + license = "MIT" + "#, + ) + .file("li/src/main.rs", "fn main() {}") + .build(); + + p.cargo("publish -p li --no-verify --token sekrit") + .with_stderr( + "\ +[UPDATING] [..] +[WARNING] manifest has no documentation, homepage or repository. +See [..] +[PACKAGING] li v0.0.1 ([CWD]/li) +[UPLOADING] li v0.0.1 ([CWD]/li) +", + ) + .run(); + + validate_upload_li(); +} + +#[cargo_test] +fn in_virtual_workspace() { registry::init(); let p = project() @@ -1673,7 +1718,7 @@ fn in_workspace() { "Cargo.toml", r#" [workspace] - members = ["foo", "bar"] + members = ["foo"] "#, ) .file( @@ -1688,46 +1733,106 @@ fn in_workspace() { "#, ) .file("foo/src/main.rs", "fn main() {}") + .build(); + + p.cargo("publish --no-verify --token sekrit -p foo") + .with_status(101) + .with_stderr("error: can't use \"--package \" in virtual manifest") + .run(); +} + +#[cargo_test] +fn in_package_workspace_not_found() { + registry::init(); + + let p = project() .file( - "bar/Cargo.toml", + "Cargo.toml", r#" - [project] - name = "bar" + [package] + name = "foo" + version = "0.1.0" + edition = "2018" + [workspace] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "li/Cargo.toml", + r#" + [package] + name = "li" version = "0.0.1" + edition = "2021" authors = [] license = "MIT" - description = "bar" - workspace = ".." + description = "li" "#, ) - .file("bar/src/main.rs", "fn main() {}") + .file("li/src/main.rs", "fn main() {}") .build(); - p.cargo("publish --no-verify --token sekrit -p foo") + p.cargo("publish -p li --no-verify --token sekrit ") + .with_status(101) .with_stderr( "\ -[UPDATING] [..] -[WARNING] manifest has no documentation, [..] -See [..] -[PACKAGING] foo v0.0.1 ([CWD]/foo) -[UPLOADING] foo v0.0.1 ([CWD]/foo) +error: not found `li` in manifest members. Check in manifest path `[CWD]` ", ) .run(); +} - validate_upload_foo(); +#[cargo_test] +fn in_package_workspace_found_mutilate() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2018" + [workspace] + members = ["li","lii"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "li/Cargo.toml", + r#" + [package] + name = "li" + version = "0.0.1" + edition = "2021" + authors = [] + license = "MIT" + description = "li" + "#, + ) + .file("li/src/main.rs", "fn main() {}") + .file( + "lii/Cargo.toml", + r#" + [package] + name = "lii" + version = "0.0.1" + edition = "2021" + authors = [] + license = "MIT" + description = "lii" + "#, + ) + .file("lii/src/main.rs", "fn main() {}") + .build(); - p.cargo("publish --no-verify --token sekrit -p bar") + p.cargo("publish -p li* --no-verify --token sekrit ") + .with_status(101) .with_stderr( "\ -[UPDATING] [..] -[WARNING] manifest has no documentation, [..] -See [..] -[PACKAGING] bar v0.0.1 ([CWD]/bar) -[UPLOADING] bar v0.0.1 ([CWD]/bar) +error: found mutilate `li*` in manifest members. Check in manifest path `[CWD]` ", ) .run(); - - validate_upload_bar(); } From ef7d9b7f8effcd9bea8576ba3084abc9b3e6d855 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Wed, 18 May 2022 23:40:07 +0800 Subject: [PATCH 02/12] extract to registry --- src/cargo/ops/cargo_compile.rs | 30 +++--------------------------- src/cargo/ops/registry.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index dd655ae6461..5d273295ae3 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -146,15 +146,10 @@ impl Packages { emit_pattern_not_found(ws, patterns, true).or_else(warn)?; specs } + Packages::Packages(packages) if packages.is_empty() => { + vec![PackageIdSpec::from_package_id(ws.current()?.package_id())] + } Packages::Packages(opt_in) => { - if ws.is_virtual() { - bail!("can't use \"--package \" in virtual manifest") - } - if opt_in.is_empty() { - return Ok(vec![PackageIdSpec::from_package_id( - ws.current()?.package_id(), - )]); - } let (mut patterns, packages) = opt_patterns_and_names(opt_in)?; let mut specs = packages .iter() @@ -169,25 +164,6 @@ impl Packages { specs.extend(matched_pkgs); } emit_pattern_not_found(ws, patterns, false)?; - let matched_packages = ws - .members() - .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) - .collect::>(); - if matched_packages.is_empty() { - bail!( - "not found `{}` in manifest members. Check in manifest path `{}`", - opt_in.get(0).unwrap(), - ws.root().display() - ) - } - if matched_packages.len() > 1 { - bail!( - "found mutilate `{}` in manifest members. Check in manifest path `{}`", - opt_in.get(0).unwrap(), - ws.root().display() - ) - } - specs } Packages::Default => ws diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 68f19392e0c..72d511cd78a 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -23,6 +23,7 @@ use crate::core::resolver::CliFeatures; use crate::core::source::Source; use crate::core::{Package, SourceId, Workspace}; use crate::ops; +use crate::ops::Packages::Packages; use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_IO_REGISTRY}; use crate::util::config::{self, Config, SslVersionConfig, SslVersionConfigRange}; use crate::util::errors::CargoResult; @@ -90,6 +91,31 @@ pub struct PublishOpts<'cfg> { pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { let specs = opts.to_publish.to_package_id_specs(ws)?; + + if let Packages(opt_in) = &opts.to_publish { + if ws.is_virtual() { + bail!("can't use \"--package \" in virtual manifest") + } + let matched_packages = ws + .members() + .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) + .collect::>(); + if matched_packages.is_empty() { + bail!( + "not found `{}` in manifest members. Check in manifest path `{}`", + opt_in.get(0).unwrap(), + ws.root().display() + ) + } + if matched_packages.len() > 1 { + bail!( + "found mutilate `{}` in manifest members. Check in manifest path `{}`", + opt_in.get(0).unwrap(), + ws.root().display() + ) + } + } + let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; let (pkg, cli_features) = pkgs.pop().unwrap(); From a5a0f6809d8ee6bfa6fea52c9d8ec9a3bbc109a8 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Wed, 18 May 2022 23:42:42 +0800 Subject: [PATCH 03/12] fix typo --- src/cargo/ops/registry.rs | 2 +- tests/testsuite/publish.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 72d511cd78a..7b5300583cc 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -109,7 +109,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { } if matched_packages.len() > 1 { bail!( - "found mutilate `{}` in manifest members. Check in manifest path `{}`", + "found multiple `{}` in manifest members. Check in manifest path `{}`", opt_in.get(0).unwrap(), ws.root().display() ) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 257d204dbcb..f5ec3fc3fe9 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1831,7 +1831,7 @@ fn in_package_workspace_found_mutilate() { .with_status(101) .with_stderr( "\ -error: found mutilate `li*` in manifest members. Check in manifest path `[CWD]` +error: found multiple `li*` in manifest members. Check in manifest path `[CWD]` ", ) .run(); From d6d53ae514fb7b8765de327166a3d96c95895124 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 19 May 2022 00:25:59 +0800 Subject: [PATCH 04/12] fix test --- src/cargo/core/workspace.rs | 1 - src/cargo/ops/registry.rs | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 98c833878b7..cf5aeb5161e 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1449,7 +1449,6 @@ impl<'cfg> Workspace<'cfg> { let ms: Vec<_> = self .members() - .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) .filter_map(|member| { let member_id = member.package_id(); match self.current_opt() { diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 7b5300583cc..21e7bbf67d2 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -12,6 +12,7 @@ use anyhow::{bail, format_err, Context as _}; use cargo_util::paths; use crates_io::{self, NewCrate, NewCrateDependency, Registry}; use curl::easy::{Easy, InfoType, SslOpt, SslVersion}; +use itertools::Itertools; use log::{log, Level}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use termcolor::Color::Green; @@ -92,6 +93,8 @@ pub struct PublishOpts<'cfg> { pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { let specs = opts.to_publish.to_package_id_specs(ws)?; + let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; + if let Packages(opt_in) = &opts.to_publish { if ws.is_virtual() { bail!("can't use \"--package \" in virtual manifest") @@ -114,10 +117,12 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { ws.root().display() ) } + pkgs = pkgs + .into_iter() + .filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id()))) + .collect_vec(); } - let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; - let (pkg, cli_features) = pkgs.pop().unwrap(); let mut publish_registry = opts.registry.clone(); From 89c24bf811866f79f43bba840d005a30703f24e4 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Mon, 23 May 2022 21:01:55 +0800 Subject: [PATCH 05/12] refactor logic --- src/cargo/ops/registry.rs | 39 +++++++---------------- tests/testsuite/publish.rs | 65 +++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 35 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 21e7bbf67d2..034835c40cf 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -12,7 +12,6 @@ use anyhow::{bail, format_err, Context as _}; use cargo_util::paths; use crates_io::{self, NewCrate, NewCrateDependency, Registry}; use curl::easy::{Easy, InfoType, SslOpt, SslVersion}; -use itertools::Itertools; use log::{log, Level}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use termcolor::Color::Green; @@ -93,35 +92,21 @@ pub struct PublishOpts<'cfg> { pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { let specs = opts.to_publish.to_package_id_specs(ws)?; - let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; - - if let Packages(opt_in) = &opts.to_publish { - if ws.is_virtual() { - bail!("can't use \"--package \" in virtual manifest") - } - let matched_packages = ws - .members() - .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id()))) - .collect::>(); - if matched_packages.is_empty() { - bail!( - "not found `{}` in manifest members. Check in manifest path `{}`", - opt_in.get(0).unwrap(), - ws.root().display() - ) + if let Packages(_) = &opts.to_publish { + if specs.len() > 1 { + bail!("the `-p` argument must be specified to select a single package to publish"); } - if matched_packages.len() > 1 { - bail!( - "found multiple `{}` in manifest members. Check in manifest path `{}`", - opt_in.get(0).unwrap(), - ws.root().display() - ) + } else { + if ws.is_virtual() { + bail!("must use `-p` argument in virtual manifest") } - pkgs = pkgs - .into_iter() - .filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id()))) - .collect_vec(); } + let member_ids = ws.members().map(|p| p.package_id()); + // Check that the spec matches exactly one member. + specs[0].query(member_ids)?; + let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; + // Double check + assert_eq!(pkgs.len(), 1); let (pkg, cli_features) = pkgs.pop().unwrap(); diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index f5ec3fc3fe9..e68b54a38c8 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1675,7 +1675,7 @@ fn in_package_workspace() { [package] name = "foo" version = "0.1.0" - edition = "2018" + edition = "2021" [workspace] members = ["li"] "#, @@ -1735,9 +1735,9 @@ fn in_virtual_workspace() { .file("foo/src/main.rs", "fn main() {}") .build(); - p.cargo("publish --no-verify --token sekrit -p foo") + p.cargo("publish --no-verify --token sekrit") .with_status(101) - .with_stderr("error: can't use \"--package \" in virtual manifest") + .with_stderr("error: must use `-p` argument in virtual manifest") .run(); } @@ -1752,7 +1752,7 @@ fn in_package_workspace_not_found() { [package] name = "foo" version = "0.1.0" - edition = "2018" + edition = "2021" [workspace] "#, ) @@ -1776,14 +1776,16 @@ fn in_package_workspace_not_found() { .with_status(101) .with_stderr( "\ -error: not found `li` in manifest members. Check in manifest path `[CWD]` +error: package ID specification `li` did not match any packages + +Did you mean `foo`? ", ) .run(); } #[cargo_test] -fn in_package_workspace_found_mutilate() { +fn in_package_workspace_found_multiple() { registry::init(); let p = project() @@ -1793,7 +1795,7 @@ fn in_package_workspace_found_mutilate() { [package] name = "foo" version = "0.1.0" - edition = "2018" + edition = "2021" [workspace] members = ["li","lii"] "#, @@ -1831,8 +1833,55 @@ fn in_package_workspace_found_mutilate() { .with_status(101) .with_stderr( "\ -error: found multiple `li*` in manifest members. Check in manifest path `[CWD]` +error: the `-p` argument must be specified to select a single package to publish +", + ) + .run(); +} + + +#[cargo_test] +// https://github.com/rust-lang/cargo/issues/10536 +fn publish_path_dependency_without_workspace() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2021" + [dependencies.bar] + path = "bar" + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.0.1" + edition = "2021" + authors = [] + license = "MIT" + description = "bar" + "#, + ) + .file("bar/src/main.rs", "fn main() {}") + .build(); + + p.cargo("publish -p bar --no-verify --token sekrit ") + .with_status(101) + .with_stderr( + "\ +error: package ID specification `bar` did not match any packages + +Did you mean `foo`? ", ) .run(); } + From 1179e7ef6b88a6940168cbc2e806d48ed20d92ef Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Mon, 23 May 2022 21:06:34 +0800 Subject: [PATCH 06/12] cargo fmt --- tests/testsuite/publish.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index e68b54a38c8..99f06804286 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1839,7 +1839,6 @@ error: the `-p` argument must be specified to select a single package to publish .run(); } - #[cargo_test] // https://github.com/rust-lang/cargo/issues/10536 fn publish_path_dependency_without_workspace() { @@ -1884,4 +1883,3 @@ error: package ID specification `bar` did not match any packages ) .run(); } - From d8280951eb01b3cdfb37bbab956ca64299bd0563 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Tue, 24 May 2022 00:58:33 +0800 Subject: [PATCH 07/12] Update src/cargo/ops/registry.rs with err msg Co-authored-by: Eric Huss --- src/cargo/ops/registry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 034835c40cf..e874d1a13cd 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -98,7 +98,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { } } else { if ws.is_virtual() { - bail!("must use `-p` argument in virtual manifest") + bail!("the `-p` argument must be specified in the root of a virtual workspace") } } let member_ids = ws.members().map(|p| p.package_id()); From 72457ef462ea40aa5d97b774a79d741a904c0219 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Tue, 24 May 2022 09:09:57 +0800 Subject: [PATCH 08/12] fix test stderr --- tests/testsuite/publish.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 99f06804286..850c4567c6b 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1737,7 +1737,7 @@ fn in_virtual_workspace() { p.cargo("publish --no-verify --token sekrit") .with_status(101) - .with_stderr("error: must use `-p` argument in virtual manifest") + .with_stderr("error: the `-p` argument must be specified in the root of a virtual workspace") .run(); } From ddd5c83b8cf63458bf67ae504bb289ed4e987d38 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Tue, 24 May 2022 09:15:02 +0800 Subject: [PATCH 09/12] rust fmt --- src/cargo/ops/registry.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index e874d1a13cd..3a063a3f75a 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -168,7 +168,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { cli_features: cli_features, }, )? - .unwrap(); + .unwrap(); opts.config .shell() @@ -262,7 +262,7 @@ fn transmit( DepKind::Build => "build", DepKind::Development => "dev", } - .to_string(), + .to_string(), registry: dep_registry, explicit_name_in_toml: dep.explicit_name_in_toml().map(|s| s.to_string()), }) From 72d6c5411a215ce5ab0ff22b1e1e1525883b7c33 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Tue, 24 May 2022 09:15:44 +0800 Subject: [PATCH 10/12] cargo fmt --- src/cargo/ops/registry.rs | 4 ++-- tests/testsuite/publish.rs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 3a063a3f75a..e874d1a13cd 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -168,7 +168,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { cli_features: cli_features, }, )? - .unwrap(); + .unwrap(); opts.config .shell() @@ -262,7 +262,7 @@ fn transmit( DepKind::Build => "build", DepKind::Development => "dev", } - .to_string(), + .to_string(), registry: dep_registry, explicit_name_in_toml: dep.explicit_name_in_toml().map(|s| s.to_string()), }) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 850c4567c6b..195c7eb926c 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1737,7 +1737,9 @@ fn in_virtual_workspace() { p.cargo("publish --no-verify --token sekrit") .with_status(101) - .with_stderr("error: the `-p` argument must be specified in the root of a virtual workspace") + .with_stderr( + "error: the `-p` argument must be specified in the root of a virtual workspace", + ) .run(); } From 52165e8c2def43618f37af03d4b60d787beff294 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Wed, 25 May 2022 18:17:08 +0800 Subject: [PATCH 11/12] update --- src/cargo/ops/registry.rs | 30 ++++++++---- tests/testsuite/publish.rs | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 10 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index e874d1a13cd..32a8217fe10 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -23,7 +23,7 @@ use crate::core::resolver::CliFeatures; use crate::core::source::Source; use crate::core::{Package, SourceId, Workspace}; use crate::ops; -use crate::ops::Packages::Packages; +use crate::ops::Packages; use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_IO_REGISTRY}; use crate::util::config::{self, Config, SslVersionConfig, SslVersionConfigRange}; use crate::util::errors::CargoResult; @@ -91,21 +91,31 @@ pub struct PublishOpts<'cfg> { pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { let specs = opts.to_publish.to_package_id_specs(ws)?; - - if let Packages(_) = &opts.to_publish { - if specs.len() > 1 { - bail!("the `-p` argument must be specified to select a single package to publish"); - } - } else { - if ws.is_virtual() { - bail!("the `-p` argument must be specified in the root of a virtual workspace") + if specs.len() > 1 { + match opts.to_publish { + Packages::Default => { + bail!("must be specified to select a single package to publish. Check `default-members` or using `-p` argument") + } + Packages::Packages(_) => { + bail!("the `-p` argument must be specified to select a single package to publish") + } + _ => {} } } + if Packages::Packages(vec![]) != opts.to_publish && ws.is_virtual() { + bail!("the `-p` argument must be specified in the root of a virtual workspace") + } let member_ids = ws.members().map(|p| p.package_id()); // Check that the spec matches exactly one member. specs[0].query(member_ids)?; let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; - // Double check + // In `members_with_features_old`, it will add "current" package(determined by the cwd). Line:1455 in workspace.rs. + // So we need filter + pkgs = pkgs + .into_iter() + .filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id()))) + .collect(); + // Double check. It is safe theoretically, unless logic has updated. assert_eq!(pkgs.len(), 1); let (pkg, cli_features) = pkgs.pop().unwrap(); diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 195c7eb926c..b1af6eb5785 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1709,6 +1709,100 @@ See [..] validate_upload_li(); } +#[cargo_test] +fn with_duplicate_spec_in_members() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + [workspace] + resolver = "2" + members = ["li","bar"] + default-members = ["li","bar"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "li/Cargo.toml", + r#" + [package] + name = "li" + version = "0.0.1" + description = "li" + license = "MIT" + "#, + ) + .file("li/src/main.rs", "fn main() {}") + .file( + "bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.0.1" + description = "bar" + license = "MIT" + "#, + ) + .file("bar/src/main.rs", "fn main() {}") + .build(); + + p.cargo("publish --no-verify --token sekrit") + .with_status(101) + .with_stderr( + "error: must be specified to select a single package to publish. Check `default-members` or using `-p` argument", + ) + .run(); +} + +#[cargo_test] +fn in_package_workspace_with_members_with_features_old() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + [workspace] + members = ["li"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "li/Cargo.toml", + r#" + [package] + name = "li" + version = "0.0.1" + description = "li" + license = "MIT" + "#, + ) + .file("li/src/main.rs", "fn main() {}") + .build(); + + p.cargo("publish -p li --no-verify --token sekrit") + .with_stderr( + "\ +[UPDATING] [..] +[WARNING] manifest has no documentation, homepage or repository. +See [..] +[PACKAGING] li v0.0.1 ([CWD]/li) +[UPLOADING] li v0.0.1 ([CWD]/li) +", + ) + .run(); + + validate_upload_li(); +} + #[cargo_test] fn in_virtual_workspace() { registry::init(); From b486ada19ff9b065520b4aa0da4408fab7b1746f Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 26 May 2022 08:49:06 +0800 Subject: [PATCH 12/12] clean err msg --- src/cargo/ops/registry.rs | 14 +++------- tests/testsuite/publish.rs | 52 +++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 32a8217fe10..5bd8decb1b8 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -92,24 +92,16 @@ pub struct PublishOpts<'cfg> { pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { let specs = opts.to_publish.to_package_id_specs(ws)?; if specs.len() > 1 { - match opts.to_publish { - Packages::Default => { - bail!("must be specified to select a single package to publish. Check `default-members` or using `-p` argument") - } - Packages::Packages(_) => { - bail!("the `-p` argument must be specified to select a single package to publish") - } - _ => {} - } + bail!("the `-p` argument must be specified to select a single package to publish") } - if Packages::Packages(vec![]) != opts.to_publish && ws.is_virtual() { + if Packages::Default == opts.to_publish && ws.is_virtual() { bail!("the `-p` argument must be specified in the root of a virtual workspace") } let member_ids = ws.members().map(|p| p.package_id()); // Check that the spec matches exactly one member. specs[0].query(member_ids)?; let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?; - // In `members_with_features_old`, it will add "current" package(determined by the cwd). Line:1455 in workspace.rs. + // In `members_with_features_old`, it will add "current" package (determined by the cwd) // So we need filter pkgs = pkgs .into_iter() diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index b1af6eb5785..1977aba7007 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1754,7 +1754,7 @@ fn with_duplicate_spec_in_members() { p.cargo("publish --no-verify --token sekrit") .with_status(101) .with_stderr( - "error: must be specified to select a single package to publish. Check `default-members` or using `-p` argument", + "error: the `-p` argument must be specified to select a single package to publish", ) .run(); } @@ -1837,6 +1837,56 @@ fn in_virtual_workspace() { .run(); } +#[cargo_test] +fn in_virtual_workspace_with_p() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["foo","li"] + "#, + ) + .file( + "foo/Cargo.toml", + r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + "#, + ) + .file("foo/src/main.rs", "fn main() {}") + .file( + "li/Cargo.toml", + r#" + [package] + name = "li" + version = "0.0.1" + description = "li" + license = "MIT" + "#, + ) + .file("li/src/main.rs", "fn main() {}") + .build(); + + p.cargo("publish -p li --no-verify --token sekrit") + .with_stderr( + "\ +[UPDATING] [..] +[WARNING] manifest has no documentation, homepage or repository. +See [..] +[PACKAGING] li v0.0.1 ([CWD]/li) +[UPLOADING] li v0.0.1 ([CWD]/li) +", + ) + .run(); +} + #[cargo_test] fn in_package_workspace_not_found() { registry::init();