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

Validate feature names are crates.io friendly. #7256

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option

if opts.check_metadata {
check_metadata(pkg, config)?;
verify_features(pkg)?;
}

verify_dependencies(pkg)?;
Expand Down Expand Up @@ -218,6 +219,34 @@ fn verify_dependencies(pkg: &Package) -> CargoResult<()> {
Ok(())
}

// Checks that the package features are rusty / can be published to crates.io.
// Intended to avoid this suprise after a successful cargo publish --dry-run:
// invalid upload request: invalid value: string "futures-0.1", expected a
// valid feature name containing only letters, numbers, hyphens, or underscores
fn verify_features(pkg: &Package) -> CargoResult<()> {
for (feature, _) in pkg.summary().features() {
if feature.is_empty() || feature.as_str() == "_" {
failure::bail!(
"expected a valid (not empty nor \"_\") feature name containing \
only letters, numbers, hyphens, or underscores.\n\
feature `{}` is not valid.",
feature
)
} else if !feature
.chars()
.all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
{
failure::bail!(
"expected a valid (not empty nor \"_\") feature name containing \
only letters, numbers, hyphens, or underscores.\n\
feature `{}` contains other characters.",
feature
)
}
}
Ok(())
}

// Checks if the package source is in a *git* DVCS repository. If *git*, and
// the source is *dirty* (e.g., has uncommitted changes) and not `allow_dirty`
// then `bail!` with an informative message. Otherwise return the sha1 hash of
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,36 @@ fn publish_with_no_default_features() {
.run();
}

#[cargo_test]
fn publish_with_unrusty_features() {
registry::init();

let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"

[features]
"invalid::characters" = []
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("publish --no-default-features --dry-run --index")
.arg(registry_url().to_string())
.with_stderr_contains("error: expected a valid (not empty nor \"_\") feature name containing only letters, numbers, hyphens, or underscores.")
.with_stderr_contains("feature `invalid::characters` contains other characters.")
.with_status(101)
.run();
}

#[cargo_test]
fn publish_with_patch() {
Package::new("bar", "1.0.0").publish();
Expand Down