Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(publish): add more check when use publish -p <SPEC> #10677

Merged
merged 13 commits into from May 27, 2022
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Expand Up @@ -174,13 +174,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)
}
Expand Down
26 changes: 26 additions & 0 deletions src/cargo/ops/registry.rs
Expand Up @@ -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;
use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_IO_REGISTRY};
use crate::util::config::{self, Config, SslVersionConfig, SslVersionConfigRange};
use crate::util::errors::CargoResult;
Expand Down Expand Up @@ -90,7 +91,32 @@ 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")
}
_ => {}
}
}
likzn marked this conversation as resolved.
Show resolved Hide resolved
if Packages::Packages(vec![]) != opts.to_publish && ws.is_virtual() {
likzn marked this conversation as resolved.
Show resolved Hide resolved
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.
likzn marked this conversation as resolved.
Show resolved Hide resolved
// 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();

Expand Down