Skip to content

Commit

Permalink
Auto merge of #6535 - jethrogb:patch-section-check, r=alexcrichton
Browse files Browse the repository at this point in the history
Having a [patch] section when publishing is not an error

It is not necessary to throw an error when your `Cargo.toml` has a `[patch]` section.

1. Cargo will normalize your `Cargo.toml` while packaging, which will remove the `[patch]` section.
2. Even if the `[patch]` section were to somehow get to crates.io, it would be ignored when the crate is used since only the top-level crate's `[patch]` section is used.

The current behavior is quite annoying for crate maintainers who need a permanent `[patch]` section, for example, [`rand`](rust-random/rand#670 (comment)). This is the situation that leads to rand needing a permanent `[patch]` section:

![image](https://user-images.githubusercontent.com/1132307/50951760-54cf3a00-14d4-11e9-8064-a7def2ed402f.png)

Here, `rand` and `rand_core` are in the same repository, and `rdrand` is in a separate repository. When building `rand` as the top-level crate (e.g. in CI), the `rand->rand_core` path dependency will be used. This is of course a different crate than the crates.io rand_core used by `rdrand`. Therefore, when building `rand` as a top-level crate, you will *always* need to patch `crates-io.rand` to the path dependency. When building `rand` as a dependency, there are no issues, as the crates.io crates are used everywhere.
  • Loading branch information
bors committed Feb 12, 2019
2 parents 865cb70 + d3f7bb0 commit 8fbabe3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
}
}

if !pkg.manifest().patch().is_empty() {
failure::bail!("published crates cannot contain [patch] sections");
}

let (mut registry, reg_id) = registry(
opts.config,
opts.token.clone(),
Expand Down
1 change: 0 additions & 1 deletion src/doc/man/cargo-publish.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ registry. The default registry is https://crates.io. This performs the
following steps:

. Performs a few checks, including:
- No `[patch]` sections are allowed in the manifest.
- Checks the `package.publish` key in the manifest for restrictions on which
registries you are allowed to publish to.
. Create a `.crate` file by following the steps in man:cargo-package[1].
Expand Down
60 changes: 57 additions & 3 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,17 +930,71 @@ fn publish_with_patch() {
bar = { path = "bar" }
"#,
)
.file("src/main.rs", "extern crate bar; fn main() {}")
.file(
"src/main.rs",
"extern crate bar;
fn main() {
bar::newfunc();
}",
)
.file("bar/Cargo.toml", &basic_manifest("bar", "1.0.0"))
.file("bar/src/lib.rs", "")
.file("bar/src/lib.rs", "pub fn newfunc() {}")
.build();

// Check that it works with the patched crate.
p.cargo("build").run();

// Check that verify fails with patched crate which has new functionality.
p.cargo("publish --index")
.arg(registry_url().to_string())
.with_stderr_contains("[..]newfunc[..]")
.with_status(101)
.with_stderr_contains("[ERROR] published crates cannot contain [patch] sections")
.run();

// Remove the usage of new functionality and try again.
p.change_file("src/main.rs", "extern crate bar; pub fn main() {}");

p.cargo("publish --index")
.arg(registry_url().to_string())
.run();

// Note, use of `registry` in the deps here is an artifact that this
// publishes to a fake, local registry that is pretending to be crates.io.
// Normal publishes would set it to null.
publish::validate_upload(
r#"
{
"authors": [],
"badges": {},
"categories": [],
"deps": [
{
"default_features": true,
"features": [],
"kind": "normal",
"name": "bar",
"optional": false,
"registry": "https://github.com/rust-lang/crates.io-index",
"target": null,
"version_req": "^1.0"
}
],
"description": "foo",
"documentation": null,
"features": {},
"homepage": null,
"keywords": [],
"license": "MIT",
"license_file": null,
"links": null,
"name": "foo",
"readme": null,
"readme_file": null,
"repository": null,
"vers": "0.0.1"
}
"#,
"foo-0.0.1.crate",
&["Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
);
}

0 comments on commit 8fbabe3

Please sign in to comment.