From 340050e0cb2cafe3ac2da8068d97ba87c6b4ec14 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Apr 2024 11:28:15 -0500 Subject: [PATCH 1/6] test(package): Show current case behavior --- tests/testsuite/package.rs | 54 +++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 5c48f56a534..31333e681ab 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -3224,8 +3224,12 @@ src/main.rs.bak #[cfg(windows)] // windows is the platform that is most consistently configured for case insensitive filesystems fn normalize_case() { let p = project() - .file("src/main.rs", r#"fn main() { println!("hello"); }"#) + .file("Build.rs", r#"fn main() { println!("hello"); }"#) + .file("src/Main.rs", r#"fn main() { println!("hello"); }"#) + .file("src/lib.rs", "") .file("src/bar.txt", "") // should be ignored when packaging + .file("Examples/ExampleFoo.rs", "") + .file("Tests/ExplicitPath.rs", "") .build(); // Workaround `project()` making a `Cargo.toml` on our behalf std::fs::remove_file(p.root().join("Cargo.toml")).unwrap(); @@ -3235,11 +3239,15 @@ fn normalize_case() { [package] name = "foo" version = "0.0.1" - edition = "2015" + edition = "2018" authors = [] exclude = ["*.txt"] license = "MIT" description = "foo" + + [[test]] + name = "explicitpath" + path = "tests/explicitpath.rs" "#, ) .unwrap(); @@ -3253,7 +3261,7 @@ See [..] [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[PACKAGED] 4 files, [..] ([..] compressed) +[PACKAGED] 8 files, [..] ([..] compressed) ", ) .run(); @@ -3261,10 +3269,14 @@ See [..] p.cargo("package -l") .with_stdout( "\ +Build.rs Cargo.lock Cargo.toml Cargo.toml.orig -src/main.rs +Examples/ExampleFoo.rs +Tests/ExplicitPath.rs +src/Main.rs +src/lib.rs ", ) .run(); @@ -3277,7 +3289,7 @@ See [..] [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[PACKAGED] 4 files, [..] ([..] compressed) +[PACKAGED] 8 files, [..] ([..] compressed) ", ) .run(); @@ -3286,8 +3298,36 @@ See [..] validate_crate_contents( f, "foo-0.0.1.crate", - &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], - &[], + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "Build.rs", + "src/Main.rs", + "src/lib.rs", + "Examples/ExampleFoo.rs", + "Tests/ExplicitPath.rs", + ], + &[( + "Cargo.toml", + &format!( + r#"{} +[package] +edition = "2018" +name = "foo" +version = "0.0.1" +authors = [] +exclude = ["*.txt"] +description = "foo" +license = "MIT" + +[[test]] +name = "explicitpath" +path = "tests/explicitpath.rs" +"#, + cargo::core::manifest::MANIFEST_PREAMBLE + ), + )], ); } From 0cf29c57139829d47bad8f119a406f10c2ece285 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Apr 2024 12:58:00 -0500 Subject: [PATCH 2/6] test(package): Show different crate discovery cases I left off the explicit `path` cases because I hope that will become moot --- tests/testsuite/package.rs | 824 +++++++++++++++++++++++++++++++++++++ 1 file changed, 824 insertions(+) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 31333e681ab..835a9cb8e9f 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -3755,3 +3755,827 @@ path = "benches/bench_foo.rs" )], ); } + +#[cargo_test] +fn discovery_inferred_build_rs_included() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs", "build.rs"] + "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs", "build.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = [ + "src/lib.rs", + "build.rs", +] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_inferred_build_rs_excluded() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs"] + "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 3 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = ["src/lib.rs"] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_explicit_build_rs_included() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs", "build.rs"] + build = "build.rs" + "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs", "build.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +build = "build.rs" +include = [ + "src/lib.rs", + "build.rs", +] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_explicit_build_rs_excluded() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs"] + build = "build.rs" + "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_status(101) + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[ERROR] couldn't read build.rs: [..] + +[ERROR] could not compile `foo` (build script) due to 1 previous error +[ERROR] failed to verify package tarball +", + ) + .run(); +} + +#[cargo_test] +fn discovery_inferred_lib_included() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/main.rs", "src/lib.rs"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 5 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "src/main.rs", + "src/lib.rs", + ], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = [ + "src/main.rs", + "src/lib.rs", +] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_inferred_lib_excluded() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/main.rs"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = ["src/main.rs"] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_explicit_lib_included() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/main.rs", "src/lib.rs"] + + [lib] + path = "src/lib.rs" + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 5 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "src/main.rs", + "src/lib.rs", + ], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = [ + "src/main.rs", + "src/lib.rs", +] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" + +[lib] +path = "src/lib.rs" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_explicit_lib_excluded() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/main.rs"] + + [lib] + path = "src/lib.rs" + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "") + .build(); + + p.cargo("package") + .with_status(101) + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[ERROR] couldn't read src/lib.rs: [..] + +[ERROR] could not compile `foo` (lib) due to 1 previous error +[ERROR] failed to verify package tarball +", + ) + .run(); +} + +#[cargo_test] +fn discovery_inferred_other_included() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs", "src/bin/foo/main.rs", "examples/example_foo.rs", "tests/test_foo.rs", "benches/bench_foo.rs"] + "#, + ) + .file("src/lib.rs", "") + .file("src/bin/foo/main.rs", "fn main() {}") + .file("examples/example_foo.rs", "fn main() {}") + .file("tests/test_foo.rs", "fn main() {}") + .file("benches/bench_foo.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 8 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "src/lib.rs", + "src/bin/foo/main.rs", + "examples/example_foo.rs", + "tests/test_foo.rs", + "benches/bench_foo.rs", + ], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = [ + "src/lib.rs", + "src/bin/foo/main.rs", + "examples/example_foo.rs", + "tests/test_foo.rs", + "benches/bench_foo.rs", +] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_inferred_other_excluded() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs"] + "#, + ) + .file("src/lib.rs", "") + .file("src/bin/foo/main.rs", "fn main() {}") + .file("examples/example_foo.rs", "fn main() {}") + .file("tests/test_foo.rs", "fn main() {}") + .file("benches/bench_foo.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = ["src/lib.rs"] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_explicit_other_included() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs", "src/bin/foo/main.rs", "examples/example_foo.rs", "tests/test_foo.rs", "benches/bench_foo.rs"] + + [[bin]] + name = "foo" + + [[example]] + name = "example_foo" + + [[test]] + name = "test_foo" + + [[bench]] + name = "bench_foo" + "#, + ) + .file("src/lib.rs", "") + .file("src/bin/foo/main.rs", "fn main() {}") + .file("examples/example_foo.rs", "fn main() {}") + .file("tests/test_foo.rs", "fn main() {}") + .file("benches/bench_foo.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 8 files, [..] ([..] compressed) +", + ) + .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &[ + "Cargo.lock", + "Cargo.toml", + "Cargo.toml.orig", + "src/lib.rs", + "src/bin/foo/main.rs", + "examples/example_foo.rs", + "tests/test_foo.rs", + "benches/bench_foo.rs", + ], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +include = [ + "src/lib.rs", + "src/bin/foo/main.rs", + "examples/example_foo.rs", + "tests/test_foo.rs", + "benches/bench_foo.rs", +] +description = "foo" +documentation = "docs.rs/foo" +license = "MIT" + +[[bin]] +name = "foo" + +[[example]] +name = "example_foo" + +[[test]] +name = "test_foo" + +[[bench]] +name = "bench_foo" +"#, + )], + ); +} + +#[cargo_test] +fn discovery_explicit_other_excluded() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + license = "MIT" + description = "foo" + documentation = "docs.rs/foo" + authors = [] + include = ["src/lib.rs"] + + [[main]] + name = "foo" + + [[example]] + name = "example_foo" + + [[test]] + name = "test_foo" + + [[bench]] + name = "bench_foo" + "#, + ) + .file("src/lib.rs", "") + .file("src/bin/foo/main.rs", "fn main() {}") + .file("examples/example_foo.rs", "fn main() {}") + .file("tests/test_foo.rs", "fn main() {}") + .file("benches/bench_foo.rs", "fn main() {}") + .build(); + + p.cargo("package") + .with_status(101) + .with_stdout("") + .with_stderr( + "\ +[PACKAGING] foo v0.0.1 ([CWD]) +[VERIFYING] foo v0.0.1 ([CWD]) +[ERROR] failed to verify package tarball + +Caused by: + failed to parse manifest at `[CWD]/target/package/foo-0.0.1/Cargo.toml` + +Caused by: + can't find `example_foo` example at `examples/example_foo.rs` or `examples/example_foo/main.rs`. Please specify example.path if you want to use a non-default path. +", + ) + .run(); +} From 39f1a210b8e6c1784e900721e2f694c2ed990a9e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Apr 2024 13:42:53 -0500 Subject: [PATCH 3/6] perf(toml): Avoid looking up readme on published packages Not much of a performance gain; this is mostly done to be consistent with the target work. --- crates/cargo-util-schemas/src/manifest/mod.rs | 15 ++++++--------- src/cargo/util/toml/mod.rs | 5 ++++- tests/testsuite/artifact_dep.rs | 1 + tests/testsuite/features2.rs | 1 + tests/testsuite/features_namespaced.rs | 2 ++ .../testsuite/inheritable_workspace_fields.rs | 4 ++++ tests/testsuite/package.rs | 19 +++++++++++++++++++ tests/testsuite/publish.rs | 3 +++ tests/testsuite/weak_dep_features.rs | 1 + 9 files changed, 41 insertions(+), 10 deletions(-) diff --git a/crates/cargo-util-schemas/src/manifest/mod.rs b/crates/cargo-util-schemas/src/manifest/mod.rs index 4fbdd39212b..c1d10e4b514 100644 --- a/crates/cargo-util-schemas/src/manifest/mod.rs +++ b/crates/cargo-util-schemas/src/manifest/mod.rs @@ -243,15 +243,12 @@ impl TomlPackage { } pub fn resolved_readme(&self) -> Result, UnresolvedError> { - self.readme - .as_ref() - .map(|v| { - v.resolved().and_then(|sb| match sb { - StringOrBool::Bool(_) => Err(UnresolvedError), - StringOrBool::String(value) => Ok(value), - }) - }) - .transpose() + let readme = self.readme.as_ref().ok_or(UnresolvedError)?; + readme.resolved().and_then(|sb| match sb { + StringOrBool::Bool(false) => Ok(None), + StringOrBool::Bool(true) => Err(UnresolvedError), + StringOrBool::String(value) => Ok(Some(value)), + }) } pub fn resolved_keywords(&self) -> Result>, UnresolvedError> { diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index d89829e0482..9c93f650f15 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -553,7 +553,10 @@ fn resolve_package_toml<'a>( .transpose()? .as_ref(), ) - .map(|s| manifest::InheritableField::Value(StringOrBool::String(s))), + .map(|s| manifest::InheritableField::Value(StringOrBool::String(s))) + .or(Some(manifest::InheritableField::Value(StringOrBool::Bool( + false, + )))), keywords: original_package .keywords .clone() diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index fd36a8386e2..1f7d87fdef3 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -2217,6 +2217,7 @@ authors = [] description = "foo" homepage = "foo" documentation = "foo" +readme = false license = "MIT" repository = "foo" resolver = "2" diff --git a/tests/testsuite/features2.rs b/tests/testsuite/features2.rs index 18a365624c8..c9b824fc0e3 100644 --- a/tests/testsuite/features2.rs +++ b/tests/testsuite/features2.rs @@ -1705,6 +1705,7 @@ version = "0.1.0" authors = ["Zzz"] description = "foo" homepage = "https://example.com/" +readme = false license = "MIT" resolver = "2" "#, diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index b53637547dd..340f60e03dd 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -987,6 +987,7 @@ name = "foo" version = "0.1.0" description = "foo" homepage = "https://example.com/" +readme = false license = "MIT" [dependencies.opt-dep1] @@ -1105,6 +1106,7 @@ name = "foo" version = "0.1.0" description = "foo" homepage = "https://example.com/" +readme = false license = "MIT" [dependencies.bar] diff --git a/tests/testsuite/inheritable_workspace_fields.rs b/tests/testsuite/inheritable_workspace_fields.rs index a84d4cdf4df..0597661065b 100644 --- a/tests/testsuite/inheritable_workspace_fields.rs +++ b/tests/testsuite/inheritable_workspace_fields.rs @@ -227,6 +227,7 @@ publish = true description = "This is a crate" homepage = "https://www.rust-lang.org" documentation = "https://www.rust-lang.org/learn" +readme = false keywords = ["cli"] categories = ["development-tools"] license = "MIT" @@ -383,6 +384,7 @@ edition = "2015" name = "bar" version = "0.2.0" authors = [] +readme = false [dependencies.dep] version = "0.1" @@ -514,6 +516,7 @@ edition = "2015" name = "bar" version = "0.2.0" authors = [] +readme = false [dependencies.dep] version = "0.1.2" @@ -927,6 +930,7 @@ edition = "2015" name = "bar" version = "0.2.0" authors = [] +readme = false [dependencies.dep] version = "0.1" diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 835a9cb8e9f..0908e0ce09c 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1219,6 +1219,7 @@ version = "0.0.1" authors = [] exclude = ["*.txt"] description = "foo" +readme = false license = "MIT" [package.metadata] @@ -1293,6 +1294,7 @@ edition = "2015" name = "bar" version = "0.1.0" authors = [] +readme = false "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -1360,6 +1362,7 @@ fn package_public_dep() { edition = "2015" name = "foo" version = "0.0.1" +readme = false [dependencies.bar] version = "1.0.0" @@ -1378,6 +1381,7 @@ version = "1.0.0" edition = "2015" name = "foo" version = "0.0.1" +readme = false [dependencies.bar] version = "1.0.0" @@ -2855,6 +2859,7 @@ fn workspace_overrides_resolver() { edition = "2021" name = "bar" version = "0.1.0" +readme = false resolver = "1" "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -2874,6 +2879,7 @@ resolver = "1" edition = "2015" name = "baz" version = "0.1.0" +readme = false "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -2938,6 +2944,7 @@ authors = [] exclude = ["*.txt"] description = "foo" homepage = "https://example.com/" +readme = false license = "MIT" "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -3032,6 +3039,7 @@ version = "0.0.1" authors = [] description = "foo" documentation = "https://example.com/" +readme = false license = "MIT" "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -3139,6 +3147,7 @@ version = "0.0.1" authors = [] description = "foo" homepage = "https://example.com/" +readme = false license = "MIT" "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -3319,6 +3328,7 @@ version = "0.0.1" authors = [] exclude = ["*.txt"] description = "foo" +readme = false license = "MIT" [[test]] @@ -3819,6 +3829,7 @@ include = [ ] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -3885,6 +3896,7 @@ authors = [] include = ["src/lib.rs"] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -3956,6 +3968,7 @@ include = [ ] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -4070,6 +4083,7 @@ include = [ ] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -4136,6 +4150,7 @@ authors = [] include = ["src/main.rs"] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -4214,6 +4229,7 @@ include = [ ] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" [lib] @@ -4342,6 +4358,7 @@ include = [ ] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -4411,6 +4428,7 @@ authors = [] include = ["src/lib.rs"] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" "#, )], @@ -4507,6 +4525,7 @@ include = [ ] description = "foo" documentation = "docs.rs/foo" +readme = false license = "MIT" [[bin]] diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index f6181832675..e54e1ec2b4c 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1561,6 +1561,7 @@ You may press ctrl-c [..] version = \"0.1.0\"\n\ authors = []\n\ description = \"foo\"\n\ + readme = false\n\ license = \"MIT\"\n\ \n\ [dependencies.dep1]\n\ @@ -1675,6 +1676,7 @@ authors = [] description = "foo" homepage = "foo" documentation = "foo" +readme = false license = "MIT" repository = "foo" @@ -1934,6 +1936,7 @@ authors = [] description = "foo" homepage = "foo" documentation = "foo" +readme = false license = "MIT" repository = "foo" diff --git a/tests/testsuite/weak_dep_features.rs b/tests/testsuite/weak_dep_features.rs index 0695501935e..ef0c503e1b8 100644 --- a/tests/testsuite/weak_dep_features.rs +++ b/tests/testsuite/weak_dep_features.rs @@ -632,6 +632,7 @@ name = "foo" version = "0.1.0" description = "foo" homepage = "https://example.com/" +readme = false license = "MIT" [dependencies.bar] From 1e6047763d72edf5dfaee52e1c2305b94d0b7ccc Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Apr 2024 14:39:23 -0500 Subject: [PATCH 4/6] fix(toml): Warn, rather than fail publish, if build.rs is excluded This could offer a minor performance gain when reading this manifest since the target doesn't need to be discovered. --- crates/cargo-util-schemas/src/manifest/mod.rs | 9 +++ src/cargo/ops/cargo_package.rs | 6 +- src/cargo/util/toml/mod.rs | 33 +++++++--- src/cargo/util/toml/targets.rs | 22 +++---- tests/testsuite/artifact_dep.rs | 1 + tests/testsuite/features2.rs | 1 + tests/testsuite/features_namespaced.rs | 2 + .../testsuite/inheritable_workspace_fields.rs | 5 ++ tests/testsuite/package.rs | 62 +++++++++++++++++-- tests/testsuite/publish.rs | 3 + tests/testsuite/weak_dep_features.rs | 1 + 11 files changed, 119 insertions(+), 26 deletions(-) diff --git a/crates/cargo-util-schemas/src/manifest/mod.rs b/crates/cargo-util-schemas/src/manifest/mod.rs index c1d10e4b514..80f474a6c65 100644 --- a/crates/cargo-util-schemas/src/manifest/mod.rs +++ b/crates/cargo-util-schemas/src/manifest/mod.rs @@ -215,6 +215,15 @@ impl TomlPackage { self.authors.as_ref().map(|v| v.resolved()).transpose() } + pub fn resolved_build(&self) -> Result, UnresolvedError> { + let readme = self.build.as_ref().ok_or(UnresolvedError)?; + match readme { + StringOrBool::Bool(false) => Ok(None), + StringOrBool::Bool(true) => Err(UnresolvedError), + StringOrBool::String(value) => Ok(Some(value)), + } + } + pub fn resolved_exclude(&self) -> Result>, UnresolvedError> { self.exclude.as_ref().map(|v| v.resolved()).transpose() } diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 7a7474825d1..533852551c2 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -689,7 +689,11 @@ fn tar( let base_name = format!("{}-{}", pkg.name(), pkg.version()); let base_path = Path::new(&base_name); - let publish_pkg = prepare_for_publish(pkg, ws)?; + let included = ar_files + .iter() + .map(|ar_file| ar_file.rel_path.clone()) + .collect::>(); + let publish_pkg = prepare_for_publish(pkg, ws, &included)?; let mut uncompressed_size = 0; for ar_file in ar_files { diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 9c93f650f15..795d8a4e5d3 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -494,7 +494,7 @@ fn resolve_package_toml<'a>( .map(|value| field_inherit_with(value, "authors", || inherit()?.authors())) .transpose()? .map(manifest::InheritableField::Value), - build: original_package.build.clone(), + build: targets::resolve_build(original_package.build.as_ref(), package_root), metabuild: original_package.metabuild.clone(), default_target: original_package.default_target.clone(), forced_target: original_package.forced_target.clone(), @@ -1153,7 +1153,6 @@ fn to_real_manifest( package_name, package_root, edition, - &resolved_package.build, &resolved_package.metabuild, warnings, errors, @@ -2360,10 +2359,15 @@ fn unused_dep_keys( } } -pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult { +pub fn prepare_for_publish( + me: &Package, + ws: &Workspace<'_>, + included: &[PathBuf], +) -> CargoResult { let contents = me.manifest().contents(); let document = me.manifest().document(); - let original_toml = prepare_toml_for_publish(me.manifest().resolved_toml(), ws, me.root())?; + let original_toml = + prepare_toml_for_publish(me.manifest().resolved_toml(), ws, me.root(), included)?; let resolved_toml = original_toml.clone(); let features = me.manifest().unstable_features().clone(); let workspace_config = me.manifest().workspace_config().clone(); @@ -2395,6 +2399,7 @@ fn prepare_toml_for_publish( me: &manifest::TomlManifest, ws: &Workspace<'_>, package_root: &Path, + included: &[PathBuf], ) -> CargoResult { let gctx = ws.gctx(); @@ -2411,11 +2416,21 @@ fn prepare_toml_for_publish( package.workspace = None; if let Some(StringOrBool::String(path)) = &package.build { let path = paths::normalize_path(Path::new(path)); - let path = path - .into_os_string() - .into_string() - .map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?; - package.build = Some(StringOrBool::String(normalize_path_string_sep(path))); + let build = if included.contains(&path) { + let path = path + .into_os_string() + .into_string() + .map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?; + let path = normalize_path_string_sep(path); + StringOrBool::String(path) + } else { + ws.gctx().shell().warn(format!( + "ignoring `package.build` as `{}` is not included in the published package", + path.display() + ))?; + StringOrBool::Bool(false) + }; + package.build = Some(build); } let current_resolver = package .resolver diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 1795cf24752..870ac5d587a 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -38,7 +38,6 @@ pub(super) fn to_targets( package_name: &str, package_root: &Path, edition: Edition, - custom_build: &Option, metabuild: &Option, warnings: &mut Vec, errors: &mut Vec, @@ -120,10 +119,11 @@ pub(super) fn to_targets( targets.extend(to_bench_targets(&toml_benches, package_root, edition)?); // processing the custom build script - if let Some(custom_build) = maybe_custom_build(custom_build, package_root) { + if let Some(custom_build) = package.resolved_build().expect("should be resolved") { if metabuild.is_some() { anyhow::bail!("cannot specify both `metabuild` and `build`"); } + let custom_build = Path::new(custom_build); let name = format!( "build-script-{}", custom_build @@ -1072,22 +1072,22 @@ Cargo doesn't know which to use because multiple target files found at `{}` and } /// Returns the path to the build script if one exists for this crate. -fn maybe_custom_build(build: &Option, package_root: &Path) -> Option { - let build_rs = package_root.join("build.rs"); - match *build { - // Explicitly no build script. - Some(StringOrBool::Bool(false)) => None, - Some(StringOrBool::Bool(true)) => Some(build_rs), - Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)), +pub fn resolve_build(build: Option<&StringOrBool>, package_root: &Path) -> Option { + const BUILD_RS: &str = "build.rs"; + match build { None => { // If there is a `build.rs` file next to the `Cargo.toml`, assume it is // a build script. + let build_rs = package_root.join(BUILD_RS); if build_rs.is_file() { - Some(build_rs) + Some(StringOrBool::String(BUILD_RS.to_owned())) } else { - None + Some(StringOrBool::Bool(false)) } } + // Explicitly no build script. + Some(StringOrBool::Bool(false)) | Some(StringOrBool::String(_)) => build.cloned(), + Some(StringOrBool::Bool(true)) => Some(StringOrBool::String(BUILD_RS.to_owned())), } } diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index 1f7d87fdef3..4bfcfd3bf04 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -2214,6 +2214,7 @@ edition = "2015" name = "foo" version = "0.1.0" authors = [] +build = false description = "foo" homepage = "foo" documentation = "foo" diff --git a/tests/testsuite/features2.rs b/tests/testsuite/features2.rs index c9b824fc0e3..e352f3e7d4e 100644 --- a/tests/testsuite/features2.rs +++ b/tests/testsuite/features2.rs @@ -1703,6 +1703,7 @@ edition = "2015" name = "a" version = "0.1.0" authors = ["Zzz"] +build = false description = "foo" homepage = "https://example.com/" readme = false diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index 340f60e03dd..0fcebbbce74 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -985,6 +985,7 @@ You may press ctrl-c [..] edition = "2015" name = "foo" version = "0.1.0" +build = false description = "foo" homepage = "https://example.com/" readme = false @@ -1104,6 +1105,7 @@ You may press ctrl-c [..] edition = "2015" name = "foo" version = "0.1.0" +build = false description = "foo" homepage = "https://example.com/" readme = false diff --git a/tests/testsuite/inheritable_workspace_fields.rs b/tests/testsuite/inheritable_workspace_fields.rs index 0597661065b..8e15c42a140 100644 --- a/tests/testsuite/inheritable_workspace_fields.rs +++ b/tests/testsuite/inheritable_workspace_fields.rs @@ -217,6 +217,7 @@ rust-version = "1.60" name = "foo" version = "1.2.3" authors = ["Rustaceans"] +build = false exclude = ["foo.txt"] include = [ "bar.txt", @@ -384,6 +385,7 @@ edition = "2015" name = "bar" version = "0.2.0" authors = [] +build = false readme = false [dependencies.dep] @@ -516,6 +518,7 @@ edition = "2015" name = "bar" version = "0.2.0" authors = [] +build = false readme = false [dependencies.dep] @@ -758,6 +761,7 @@ rust-version = "1.60" name = "bar" version = "1.2.3" authors = ["Rustaceans"] +build = false exclude = ["foo.txt"] include = [ "bar.txt", @@ -930,6 +934,7 @@ edition = "2015" name = "bar" version = "0.2.0" authors = [] +build = false readme = false [dependencies.dep] diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 0908e0ce09c..3d88b2c5bcf 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1217,6 +1217,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false exclude = ["*.txt"] description = "foo" readme = false @@ -1294,6 +1295,7 @@ edition = "2015" name = "bar" version = "0.1.0" authors = [] +build = false readme = false "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -1362,6 +1364,7 @@ fn package_public_dep() { edition = "2015" name = "foo" version = "0.0.1" +build = false readme = false [dependencies.bar] @@ -1381,6 +1384,7 @@ version = "1.0.0" edition = "2015" name = "foo" version = "0.0.1" +build = false readme = false [dependencies.bar] @@ -2859,6 +2863,7 @@ fn workspace_overrides_resolver() { edition = "2021" name = "bar" version = "0.1.0" +build = false readme = false resolver = "1" "#, @@ -2879,6 +2884,7 @@ resolver = "1" edition = "2015" name = "baz" version = "0.1.0" +build = false readme = false "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -2941,6 +2947,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false exclude = ["*.txt"] description = "foo" homepage = "https://example.com/" @@ -3037,6 +3044,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false description = "foo" documentation = "https://example.com/" readme = false @@ -3145,6 +3153,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false description = "foo" homepage = "https://example.com/" readme = false @@ -3267,6 +3276,7 @@ fn normalize_case() { [WARNING] manifest has no documentation[..] See [..] [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring `package.build` as `build.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -3295,6 +3305,7 @@ src/lib.rs [WARNING] manifest has no documentation[..] See [..] [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring `package.build` as `build.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -3326,6 +3337,7 @@ edition = "2018" name = "foo" version = "0.0.1" authors = [] +build = false exclude = ["*.txt"] description = "foo" readme = false @@ -3823,6 +3835,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = "build.rs" include = [ "src/lib.rs", "build.rs", @@ -3862,6 +3875,7 @@ fn discovery_inferred_build_rs_excluded() { .with_stderr( "\ [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring `package.build` as `build.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -3893,6 +3907,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = ["src/lib.rs"] description = "foo" documentation = "docs.rs/foo" @@ -3998,20 +4013,51 @@ fn discovery_explicit_build_rs_excluded() { .build(); p.cargo("package") - .with_status(101) .with_stdout("") .with_stderr( "\ [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring `package.build` as `build.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) -[ERROR] couldn't read build.rs: [..] - -[ERROR] could not compile `foo` (build script) due to 1 previous error -[ERROR] failed to verify package tarball +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 3 files, [..] ([..] compressed) ", ) .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +build = false +include = ["src/lib.rs"] +description = "foo" +documentation = "docs.rs/foo" +readme = false +license = "MIT" +"#, + )], + ); } #[cargo_test] @@ -4077,6 +4123,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = [ "src/main.rs", "src/lib.rs", @@ -4147,6 +4194,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = ["src/main.rs"] description = "foo" documentation = "docs.rs/foo" @@ -4223,6 +4271,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = [ "src/main.rs", "src/lib.rs", @@ -4349,6 +4398,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = [ "src/lib.rs", "src/bin/foo/main.rs", @@ -4425,6 +4475,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = ["src/lib.rs"] description = "foo" documentation = "docs.rs/foo" @@ -4516,6 +4567,7 @@ edition = "2015" name = "foo" version = "0.0.1" authors = [] +build = false include = [ "src/lib.rs", "src/bin/foo/main.rs", diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index e54e1ec2b4c..d5b31d3f6b1 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1560,6 +1560,7 @@ You may press ctrl-c [..] name = \"foo\"\n\ version = \"0.1.0\"\n\ authors = []\n\ + build = false\n\ description = \"foo\"\n\ readme = false\n\ license = \"MIT\"\n\ @@ -1673,6 +1674,7 @@ edition = "2015" name = "foo" version = "0.1.0" authors = [] +build = false description = "foo" homepage = "foo" documentation = "foo" @@ -1933,6 +1935,7 @@ edition = "2015" name = "foo" version = "0.1.0" authors = [] +build = false description = "foo" homepage = "foo" documentation = "foo" diff --git a/tests/testsuite/weak_dep_features.rs b/tests/testsuite/weak_dep_features.rs index ef0c503e1b8..09cac833507 100644 --- a/tests/testsuite/weak_dep_features.rs +++ b/tests/testsuite/weak_dep_features.rs @@ -630,6 +630,7 @@ You may press ctrl-c to skip waiting; the crate should be available shortly. edition = "2015" name = "foo" version = "0.1.0" +build = false description = "foo" homepage = "https://example.com/" readme = false From 06a57142f12e56eb78963e0176a8354bff4d6d6a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Apr 2024 16:52:22 -0500 Subject: [PATCH 5/6] fix(toml): Warn, rather than fail publish, if targets are excluded This could offer performance gains when parsing a published manifest since the targets don't need to be discovered. To see this, we'd first need to stop discovering potential targets even when it isn't needed. --- src/cargo/util/toml/mod.rs | 109 ++++-- src/cargo/util/toml/targets.rs | 67 +--- tests/testsuite/artifact_dep.rs | 8 + tests/testsuite/features2.rs | 8 + tests/testsuite/features_namespaced.rs | 16 + .../testsuite/inheritable_workspace_fields.rs | 40 +++ tests/testsuite/package.rs | 310 +++++++++++++++++- tests/testsuite/publish.rs | 24 ++ tests/testsuite/weak_dep_features.rs | 8 + 9 files changed, 500 insertions(+), 90 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 795d8a4e5d3..0eec1700c8a 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -264,7 +264,7 @@ fn resolve_toml( manifest_file: &Path, gctx: &GlobalContext, warnings: &mut Vec, - _errors: &mut Vec, + errors: &mut Vec, ) -> CargoResult { if let Some(workspace) = &original_toml.workspace { if workspace.resolver.as_deref() == Some("3") { @@ -277,11 +277,11 @@ fn resolve_toml( package: None, project: None, profile: original_toml.profile.clone(), - lib: original_toml.lib.clone(), - bin: original_toml.bin.clone(), - example: original_toml.example.clone(), - test: original_toml.test.clone(), - bench: original_toml.bench.clone(), + lib: None, + bin: None, + example: None, + test: None, + bench: None, dependencies: None, dev_dependencies: None, dev_dependencies2: None, @@ -318,6 +318,47 @@ fn resolve_toml( }); resolved_toml.package = Some(resolved_package); + resolved_toml.lib = targets::resolve_lib( + original_toml.lib.as_ref(), + package_root, + &original_package.name, + edition, + warnings, + )?; + resolved_toml.bin = Some(targets::resolve_bins( + original_toml.bin.as_ref(), + package_root, + &original_package.name, + edition, + original_package.autobins, + warnings, + resolved_toml.lib.is_some(), + )?); + resolved_toml.example = Some(targets::resolve_examples( + original_toml.example.as_ref(), + package_root, + edition, + original_package.autoexamples, + warnings, + errors, + )?); + resolved_toml.test = Some(targets::resolve_tests( + original_toml.test.as_ref(), + package_root, + edition, + original_package.autotests, + warnings, + errors, + )?); + resolved_toml.bench = Some(targets::resolve_benches( + original_toml.bench.as_ref(), + package_root, + edition, + original_package.autobenches, + warnings, + errors, + )?); + let activated_opt_deps = resolved_toml .features .as_ref() @@ -519,10 +560,10 @@ fn resolve_package_toml<'a>( .map(manifest::InheritableField::Value), workspace: original_package.workspace.clone(), im_a_teapot: original_package.im_a_teapot.clone(), - autobins: original_package.autobins.clone(), - autoexamples: original_package.autoexamples.clone(), - autotests: original_package.autotests.clone(), - autobenches: original_package.autobenches.clone(), + autobins: Some(false), + autoexamples: Some(false), + autotests: Some(false), + autobenches: Some(false), default_run: original_package.default_run.clone(), description: original_package .description @@ -1149,8 +1190,8 @@ fn to_real_manifest( // If we have a lib with no path, use the inferred lib or else the package name. let targets = to_targets( &features, + &original_toml, &resolved_toml, - package_name, package_root, edition, &resolved_package.metabuild, @@ -2520,14 +2561,14 @@ fn prepare_toml_for_publish( } let lib = if let Some(target) = &me.lib { - Some(prepare_target_for_publish(target, "library")?) + prepare_target_for_publish(target, included, "library", ws.gctx())? } else { None }; - let bin = prepare_targets_for_publish(me.bin.as_ref(), "binary")?; - let example = prepare_targets_for_publish(me.example.as_ref(), "example")?; - let test = prepare_targets_for_publish(me.test.as_ref(), "test")?; - let bench = prepare_targets_for_publish(me.bench.as_ref(), "benchmark")?; + let bin = prepare_targets_for_publish(me.bin.as_ref(), included, "binary", ws.gctx())?; + let example = prepare_targets_for_publish(me.example.as_ref(), included, "example", ws.gctx())?; + let test = prepare_targets_for_publish(me.test.as_ref(), included, "test", ws.gctx())?; + let bench = prepare_targets_for_publish(me.bench.as_ref(), included, "benchmark", ws.gctx())?; let all = |_d: &manifest::TomlDependency| true; let mut manifest = manifest::TomlManifest { @@ -2685,7 +2726,9 @@ fn prepare_toml_for_publish( fn prepare_targets_for_publish( targets: Option<&Vec>, + included: &[PathBuf], context: &str, + gctx: &GlobalContext, ) -> CargoResult>> { let Some(targets) = targets else { return Ok(None); @@ -2693,23 +2736,41 @@ fn prepare_targets_for_publish( let mut prepared = Vec::with_capacity(targets.len()); for target in targets { - let target = prepare_target_for_publish(target, context)?; + let Some(target) = prepare_target_for_publish(target, included, context, gctx)? else { + continue; + }; prepared.push(target); } - Ok(Some(prepared)) + if prepared.is_empty() { + Ok(None) + } else { + Ok(Some(prepared)) + } } fn prepare_target_for_publish( target: &manifest::TomlTarget, + included: &[PathBuf], context: &str, -) -> CargoResult { - let mut target = target.clone(); - if let Some(path) = target.path { - let path = normalize_path(&path.0); - target.path = Some(manifest::PathValue(normalize_path_sep(path, context)?)); + gctx: &GlobalContext, +) -> CargoResult> { + let path = target.path.as_ref().expect("previously resolved"); + let path = normalize_path(&path.0); + if !included.contains(&path) { + let name = target.name.as_ref().expect("previously resolved"); + gctx.shell().warn(format!( + "ignoring {context} `{name}` as `{}` is not included in the published package", + path.display() + ))?; + return Ok(None); } - Ok(target) + + let mut target = target.clone(); + let path = normalize_path_sep(path, context)?; + target.path = Some(manifest::PathValue(path.into())); + + Ok(Some(target)) } fn normalize_path_sep(path: PathBuf, context: &str) -> CargoResult { diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 870ac5d587a..5b7c313d748 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -34,8 +34,8 @@ const DEFAULT_EXAMPLE_DIR_NAME: &'static str = "examples"; #[tracing::instrument(skip_all)] pub(super) fn to_targets( features: &Features, + original_toml: &TomlManifest, resolved_toml: &TomlManifest, - package_name: &str, package_root: &Path, edition: Edition, metabuild: &Option, @@ -44,26 +44,14 @@ pub(super) fn to_targets( ) -> CargoResult> { let mut targets = Vec::new(); - let has_lib; - - let lib = resolve_lib( - resolved_toml.lib.as_ref(), - package_root, - package_name, - edition, - warnings, - )?; if let Some(target) = to_lib_target( + original_toml.lib.as_ref(), resolved_toml.lib.as_ref(), - lib.as_ref(), package_root, edition, warnings, )? { targets.push(target); - has_lib = true; - } else { - has_lib = false; } let package = resolved_toml @@ -71,52 +59,31 @@ pub(super) fn to_targets( .as_ref() .ok_or_else(|| anyhow::format_err!("manifest has no `package` (or `project`)"))?; - let bins = resolve_bins( - resolved_toml.bin.as_ref(), - package_root, - package_name, - edition, - package.autobins, - warnings, - has_lib, - )?; targets.extend(to_bin_targets( features, - &bins, + resolved_toml.bin.as_deref().unwrap_or_default(), package_root, edition, errors, )?); - let toml_examples = resolve_examples( - resolved_toml.example.as_ref(), + targets.extend(to_example_targets( + resolved_toml.example.as_deref().unwrap_or_default(), package_root, edition, - package.autoexamples, - warnings, - errors, - )?; - targets.extend(to_example_targets(&toml_examples, package_root, edition)?); + )?); - let toml_tests = resolve_tests( - resolved_toml.test.as_ref(), + targets.extend(to_test_targets( + resolved_toml.test.as_deref().unwrap_or_default(), package_root, edition, - package.autotests, - warnings, - errors, - )?; - targets.extend(to_test_targets(&toml_tests, package_root, edition)?); + )?); - let toml_benches = resolve_benches( - resolved_toml.bench.as_ref(), + targets.extend(to_bench_targets( + resolved_toml.bench.as_deref().unwrap_or_default(), package_root, edition, - package.autobenches, - warnings, - errors, - )?; - targets.extend(to_bench_targets(&toml_benches, package_root, edition)?); + )?); // processing the custom build script if let Some(custom_build) = package.resolved_build().expect("should be resolved") { @@ -158,7 +125,7 @@ pub(super) fn to_targets( Ok(targets) } -fn resolve_lib( +pub fn resolve_lib( original_lib: Option<&TomlLibTarget>, package_root: &Path, package_name: &str, @@ -283,7 +250,7 @@ fn to_lib_target( Ok(Some(target)) } -fn resolve_bins( +pub fn resolve_bins( toml_bins: Option<&Vec>, package_root: &Path, package_name: &str, @@ -409,7 +376,7 @@ fn legacy_bin_path(package_root: &Path, name: &str, has_lib: bool) -> Option>, package_root: &Path, edition: Edition, @@ -464,7 +431,7 @@ fn to_example_targets( Ok(result) } -fn resolve_tests( +pub fn resolve_tests( toml_tests: Option<&Vec>, package_root: &Path, edition: Edition, @@ -512,7 +479,7 @@ fn to_test_targets( Ok(result) } -fn resolve_benches( +pub fn resolve_benches( toml_benches: Option<&Vec>, package_root: &Path, edition: Edition, diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index 4bfcfd3bf04..e4b6374f40f 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -2215,6 +2215,10 @@ name = "foo" version = "0.1.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "foo" documentation = "foo" @@ -2223,6 +2227,10 @@ license = "MIT" repository = "foo" resolver = "2" +[lib] +name = "foo" +path = "src/lib.rs" + [dependencies.bar] version = "1.0" artifact = ["bin"] diff --git a/tests/testsuite/features2.rs b/tests/testsuite/features2.rs index e352f3e7d4e..9d696978f91 100644 --- a/tests/testsuite/features2.rs +++ b/tests/testsuite/features2.rs @@ -1704,11 +1704,19 @@ name = "a" version = "0.1.0" authors = ["Zzz"] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "https://example.com/" readme = false license = "MIT" resolver = "2" + +[lib] +name = "a" +path = "src/lib.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index 0fcebbbce74..1c9b90ae2c5 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -986,11 +986,19 @@ edition = "2015" name = "foo" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "https://example.com/" readme = false license = "MIT" +[lib] +name = "foo" +path = "src/lib.rs" + [dependencies.opt-dep1] version = "1.0" optional = true @@ -1106,11 +1114,19 @@ edition = "2015" name = "foo" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "https://example.com/" readme = false license = "MIT" +[lib] +name = "foo" +path = "src/lib.rs" + [dependencies.bar] version = "1.0" optional = true diff --git a/tests/testsuite/inheritable_workspace_fields.rs b/tests/testsuite/inheritable_workspace_fields.rs index 8e15c42a140..a91b7abb6f9 100644 --- a/tests/testsuite/inheritable_workspace_fields.rs +++ b/tests/testsuite/inheritable_workspace_fields.rs @@ -225,6 +225,10 @@ include = [ "Cargo.toml", ] publish = true +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "This is a crate" homepage = "https://www.rust-lang.org" documentation = "https://www.rust-lang.org/learn" @@ -233,6 +237,10 @@ keywords = ["cli"] categories = ["development-tools"] license = "MIT" repository = "https://github.com/example/example" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ), @@ -386,8 +394,16 @@ name = "bar" version = "0.2.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false +[[bin]] +name = "bar" +path = "src/main.rs" + [dependencies.dep] version = "0.1" @@ -519,8 +535,16 @@ name = "bar" version = "0.2.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false +[[bin]] +name = "bar" +path = "src/main.rs" + [dependencies.dep] version = "0.1.2" features = ["testing"] @@ -771,6 +795,10 @@ include = [ "README.md", ] publish = true +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "This is a crate" homepage = "https://www.rust-lang.org" documentation = "https://www.rust-lang.org/learn" @@ -780,6 +808,10 @@ categories = ["development-tools"] license = "MIT" license-file = "LICENSE" repository = "https://github.com/example/example" + +[[bin]] +name = "bar" +path = "src/main.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ), @@ -935,8 +967,16 @@ name = "bar" version = "0.2.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false +[[bin]] +name = "bar" +path = "src/main.rs" + [dependencies.dep] version = "0.1" diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 3d88b2c5bcf..850fe9c50b9 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1219,6 +1219,10 @@ version = "0.0.1" authors = [] build = false exclude = ["*.txt"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" readme = false license = "MIT" @@ -1226,6 +1230,10 @@ license = "MIT" [package.metadata] foo = "bar" +[[bin]] +name = "foo" +path = "src/main.rs" + [dependencies.abc] version = "1.0" @@ -1296,7 +1304,15 @@ name = "bar" version = "0.1.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false + +[lib] +name = "bar" +path = "src/lib.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -1365,8 +1381,16 @@ edition = "2015" name = "foo" version = "0.0.1" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false +[[bin]] +name = "foo" +path = "src/main.rs" + [dependencies.bar] version = "1.0.0" @@ -1385,8 +1409,16 @@ edition = "2015" name = "foo" version = "0.0.1" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false +[[bin]] +name = "foo" +path = "src/main.rs" + [dependencies.bar] version = "1.0.0" public = true @@ -2864,8 +2896,16 @@ edition = "2021" name = "bar" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false resolver = "1" + +[lib] +name = "bar" +path = "src/lib.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -2885,7 +2925,15 @@ edition = "2015" name = "baz" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false + +[lib] +name = "baz" +path = "src/lib.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -2949,10 +2997,18 @@ version = "0.0.1" authors = [] build = false exclude = ["*.txt"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "https://example.com/" readme = false license = "MIT" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -3045,10 +3101,18 @@ name = "foo" version = "0.0.1" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "https://example.com/" readme = false license = "MIT" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -3154,10 +3218,18 @@ name = "foo" version = "0.0.1" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "https://example.com/" readme = false license = "MIT" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ); @@ -3277,6 +3349,10 @@ fn normalize_case() { See [..] [PACKAGING] foo v0.0.1 ([CWD]) [WARNING] ignoring `package.build` as `build.rs` is not included in the published package +[WARNING] ignoring binary `foo` as `src/main.rs` is not included in the published package +[WARNING] ignoring example `ExampleFoo` as `examples/ExampleFoo.rs` is not included in the published package +[WARNING] ignoring test `explicitpath` as `tests/explicitpath.rs` is not included in the published package +[WARNING] ignoring test `ExplicitPath` as `tests/ExplicitPath.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -3306,6 +3382,10 @@ src/lib.rs See [..] [PACKAGING] foo v0.0.1 ([CWD]) [WARNING] ignoring `package.build` as `build.rs` is not included in the published package +[WARNING] ignoring binary `foo` as `src/main.rs` is not included in the published package +[WARNING] ignoring example `ExampleFoo` as `examples/ExampleFoo.rs` is not included in the published package +[WARNING] ignoring test `explicitpath` as `tests/explicitpath.rs` is not included in the published package +[WARNING] ignoring test `ExplicitPath` as `tests/ExplicitPath.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -3339,13 +3419,17 @@ version = "0.0.1" authors = [] build = false exclude = ["*.txt"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" readme = false license = "MIT" -[[test]] -name = "explicitpath" -path = "tests/explicitpath.rs" +[lib] +name = "foo" +path = "src/lib.rs" "#, cargo::core::manifest::MANIFEST_PREAMBLE ), @@ -3750,12 +3834,17 @@ name = "foo" version = "0.0.1" authors = [] build = "src/build.rs" +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = "docs/README.md" license-file = "docs/LICENSE" [lib] +name = "foo" path = "src/lib.rs" [[bin]] @@ -3840,10 +3929,18 @@ include = [ "src/lib.rs", "build.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" "#, )], ); @@ -3909,10 +4006,18 @@ version = "0.0.1" authors = [] build = false include = ["src/lib.rs"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" "#, )], ); @@ -3981,10 +4086,18 @@ include = [ "src/lib.rs", "build.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" "#, )], ); @@ -4051,10 +4164,18 @@ version = "0.0.1" authors = [] build = false include = ["src/lib.rs"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" "#, )], ); @@ -4128,10 +4249,22 @@ include = [ "src/main.rs", "src/lib.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, )], ); @@ -4163,6 +4296,7 @@ fn discovery_inferred_lib_excluded() { .with_stderr( "\ [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring library `foo` as `src/lib.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -4196,10 +4330,18 @@ version = "0.0.1" authors = [] build = false include = ["src/main.rs"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, )], ); @@ -4276,13 +4418,22 @@ include = [ "src/main.rs", "src/lib.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" [lib] +name = "foo" path = "src/lib.rs" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, )], ); @@ -4313,20 +4464,59 @@ fn discovery_explicit_lib_excluded() { .build(); p.cargo("package") - .with_status(101) .with_stdout("") .with_stderr( "\ [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring library `foo` as `src/lib.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) -[ERROR] couldn't read src/lib.rs: [..] - -[ERROR] could not compile `foo` (lib) due to 1 previous error -[ERROR] failed to verify package tarball +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +build = false +include = ["src/main.rs"] +autobins = false +autoexamples = false +autotests = false +autobenches = false +description = "foo" +documentation = "docs.rs/foo" +readme = false +license = "MIT" + +[[bin]] +name = "foo" +path = "src/main.rs" +"#, + )], + ); } #[cargo_test] @@ -4406,10 +4596,34 @@ include = [ "tests/test_foo.rs", "benches/bench_foo.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" + +[[bin]] +name = "foo" +path = "src/bin/foo/main.rs" + +[[example]] +name = "example_foo" +path = "examples/example_foo.rs" + +[[test]] +name = "test_foo" +path = "tests/test_foo.rs" + +[[bench]] +name = "bench_foo" +path = "benches/bench_foo.rs" "#, )], ); @@ -4444,6 +4658,10 @@ fn discovery_inferred_other_excluded() { .with_stderr( "\ [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring binary `foo` as `src/bin/foo/main.rs` is not included in the published package +[WARNING] ignoring example `example_foo` as `examples/example_foo.rs` is not included in the published package +[WARNING] ignoring test `test_foo` as `tests/test_foo.rs` is not included in the published package +[WARNING] ignoring benchmark `bench_foo` as `benches/bench_foo.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) [COMPILING] foo v0.0.1 ([CWD][..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] @@ -4477,10 +4695,18 @@ version = "0.0.1" authors = [] build = false include = ["src/lib.rs"] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" "#, )], ); @@ -4575,22 +4801,34 @@ include = [ "tests/test_foo.rs", "benches/bench_foo.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false license = "MIT" +[lib] +name = "foo" +path = "src/lib.rs" + [[bin]] name = "foo" +path = "src/bin/foo/main.rs" [[example]] name = "example_foo" +path = "examples/example_foo.rs" [[test]] name = "test_foo" +path = "tests/test_foo.rs" [[bench]] name = "bench_foo" +path = "benches/bench_foo.rs" "#, )], ); @@ -4633,20 +4871,60 @@ fn discovery_explicit_other_excluded() { .build(); p.cargo("package") - .with_status(101) .with_stdout("") .with_stderr( "\ [PACKAGING] foo v0.0.1 ([CWD]) +[WARNING] ignoring binary `foo` as `src/bin/foo/main.rs` is not included in the published package +[WARNING] ignoring example `example_foo` as `examples/example_foo.rs` is not included in the published package +[WARNING] ignoring test `test_foo` as `tests/test_foo.rs` is not included in the published package +[WARNING] ignoring benchmark `bench_foo` as `benches/bench_foo.rs` is not included in the published package [VERIFYING] foo v0.0.1 ([CWD]) -[ERROR] failed to verify package tarball - -Caused by: - failed to parse manifest at `[CWD]/target/package/foo-0.0.1/Cargo.toml` - -Caused by: - can't find `example_foo` example at `examples/example_foo.rs` or `examples/example_foo/main.rs`. Please specify example.path if you want to use a non-default path. +[COMPILING] foo v0.0.1 ([CWD][..]) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] +[PACKAGED] 4 files, [..] ([..] compressed) ", ) .run(); + + let f = File::open(&p.root().join("target/package/foo-0.0.1.crate")).unwrap(); + validate_crate_contents( + f, + "foo-0.0.1.crate", + &["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], + &[( + "Cargo.toml", + r#"# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2015" +name = "foo" +version = "0.0.1" +authors = [] +build = false +include = ["src/lib.rs"] +autobins = false +autoexamples = false +autotests = false +autobenches = false +description = "foo" +documentation = "docs.rs/foo" +readme = false +license = "MIT" + +[lib] +name = "foo" +path = "src/lib.rs" +"#, + )], + ); } diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index d5b31d3f6b1..38577bbcfdd 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1561,10 +1561,18 @@ You may press ctrl-c [..] version = \"0.1.0\"\n\ authors = []\n\ build = false\n\ + autobins = false\n\ + autoexamples = false\n\ + autotests = false\n\ + autobenches = false\n\ description = \"foo\"\n\ readme = false\n\ license = \"MIT\"\n\ \n\ + [[bin]]\n\ + name = \"foo\"\n\ + path = \"src/main.rs\"\n\ + \n\ [dependencies.dep1]\n\ version = \"1.0\"\n\ ", @@ -1675,6 +1683,10 @@ name = "foo" version = "0.1.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "foo" documentation = "foo" @@ -1682,6 +1694,10 @@ readme = false license = "MIT" repository = "foo" +[lib] +name = "foo" +path = "src/lib.rs" + [dev-dependencies] "#, cargo::core::manifest::MANIFEST_PREAMBLE @@ -1936,6 +1952,10 @@ name = "foo" version = "0.1.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "foo" documentation = "foo" @@ -1943,6 +1963,10 @@ readme = false license = "MIT" repository = "foo" +[[bin]] +name = "foo" +path = "src/main.rs" + [dependencies.normal-and-dev] version = "1.0" features = ["cat"] diff --git a/tests/testsuite/weak_dep_features.rs b/tests/testsuite/weak_dep_features.rs index 09cac833507..bf7b6207585 100644 --- a/tests/testsuite/weak_dep_features.rs +++ b/tests/testsuite/weak_dep_features.rs @@ -631,11 +631,19 @@ edition = "2015" name = "foo" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "https://example.com/" readme = false license = "MIT" +[lib] +name = "foo" +path = "src/lib.rs" + [dependencies.bar] version = "1.0" optional = true From 8cc2f391ba4c72cee9129342e47b1ecb7ba959d3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 22 Apr 2024 14:59:10 -0500 Subject: [PATCH 6/6] docs(toml): Clarify what resolving a TOML means --- src/cargo/core/manifest.rs | 6 ++++++ src/cargo/util/toml/mod.rs | 1 + 2 files changed, 7 insertions(+) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index b2133b204e2..14245c410e8 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -483,6 +483,7 @@ impl Manifest { pub fn contents(&self) -> &str { self.contents.as_str() } + /// See [`Manifest::resolved_toml`] for what "resolved" means pub fn to_resolved_contents(&self) -> CargoResult { let toml = toml::to_string_pretty(self.resolved_toml())?; Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml)) @@ -496,6 +497,11 @@ impl Manifest { &self.original_toml } /// The [`TomlManifest`] with all fields expanded + /// + /// This is the intersection of what fields need resolving for cargo-publish that also are + /// useful for the operation of cargo, including + /// - workspace inheritance + /// - target discovery pub fn resolved_toml(&self) -> &TomlManifest { &self.resolved_toml } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 0eec1700c8a..66bab28157c 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -256,6 +256,7 @@ fn to_workspace_root_config( ws_root_config } +/// See [`Manifest::resolved_toml`] for more details #[tracing::instrument(skip_all)] fn resolve_toml( original_toml: &manifest::TomlManifest,