From 4217b7b2e18d88ee724c78c8c7fd0c0527714d76 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Apr 2024 21:06:17 -0500 Subject: [PATCH] fix(toml): Warn, rather than fail publish, if a target is excluded --- src/cargo/util/toml/mod.rs | 88 +++++++- src/cargo/util/toml/targets.rs | 57 +---- tests/testsuite/artifact_dep.rs | 4 + tests/testsuite/features2.rs | 4 + tests/testsuite/features_namespaced.rs | 8 + .../testsuite/inheritable_workspace_fields.rs | 40 ++++ tests/testsuite/package.rs | 200 +++++++++++++++++- tests/testsuite/publish.rs | 20 ++ tests/testsuite/weak_dep_features.rs | 4 + 9 files changed, 359 insertions(+), 66 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index a151c59fac02..a4fcb0e0f881 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -263,7 +263,7 @@ fn resolve_toml( manifest_file: &Path, gctx: &GlobalContext, warnings: &mut Vec, - _errors: &mut Vec, + errors: &mut Vec, ) -> CargoResult { let mut resolved_toml = manifest::TomlManifest { cargo_features: original_toml.cargo_features.clone(), @@ -324,6 +324,39 @@ fn resolve_toml( edition, warnings, )?; + resolved_toml.bin = 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 = targets::resolve_examples( + original_toml.example.as_ref(), + package_root, + edition, + original_package.autoexamples, + warnings, + errors, + )?; + resolved_toml.test = targets::resolve_tests( + original_toml.test.as_ref(), + package_root, + edition, + original_package.autotests, + warnings, + errors, + )?; + resolved_toml.bench = targets::resolve_benches( + original_toml.bench.as_ref(), + package_root, + edition, + original_package.autobenches, + warnings, + errors, + )?; resolved_toml.dependencies = resolve_dependencies( gctx, @@ -476,10 +509,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 @@ -1118,7 +1151,6 @@ fn to_real_manifest( &features, &original_toml, &resolved_toml, - package_name, package_root, edition, &resolved_package.metabuild, @@ -2471,6 +2503,42 @@ fn prepare_toml_for_publish( .as_ref() .filter(|lib| is_target_included(lib, included, "library", gctx)) .cloned(); + let bin = me.bin.as_ref().and_then(|targets| { + targets::non_empty( + targets + .iter() + .filter(|target| is_target_included(target, included, "binary", gctx)) + .cloned() + .collect::>(), + ) + }); + let example = me.example.as_ref().and_then(|targets| { + targets::non_empty( + targets + .iter() + .filter(|target| is_target_included(target, included, "example", gctx)) + .cloned() + .collect::>(), + ) + }); + let test = me.test.as_ref().and_then(|targets| { + targets::non_empty( + targets + .iter() + .filter(|target| is_target_included(target, included, "test", gctx)) + .cloned() + .collect::>(), + ) + }); + let bench = me.bench.as_ref().and_then(|targets| { + targets::non_empty( + targets + .iter() + .filter(|target| is_target_included(target, included, "benchmark", gctx)) + .cloned() + .collect::>(), + ) + }); let all = |_d: &manifest::TomlDependency| true; let mut manifest = manifest::TomlManifest { @@ -2478,10 +2546,10 @@ fn prepare_toml_for_publish( project: None, profile: me.profile.clone(), lib, - bin: me.bin.clone(), - example: me.example.clone(), - test: me.test.clone(), - bench: me.bench.clone(), + bin, + example, + test, + bench, dependencies: map_deps(gctx, me.dependencies.as_ref(), all)?, dev_dependencies: map_deps( gctx, diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 84d90ac95076..853745eb5e55 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -36,7 +36,6 @@ pub(super) fn to_targets( features: &Features, original_toml: &TomlManifest, resolved_toml: &TomlManifest, - package_name: &str, package_root: &Path, edition: Edition, metabuild: &Option, @@ -45,8 +44,6 @@ pub(super) fn to_targets( ) -> CargoResult> { let mut targets = Vec::new(); - let has_lib; - if let Some(target) = to_lib_target( original_toml.lib.as_ref(), resolved_toml.lib.as_ref(), @@ -55,9 +52,6 @@ pub(super) fn to_targets( warnings, )? { targets.push(target); - has_lib = true; - } else { - has_lib = false; } let package = resolved_toml @@ -65,62 +59,29 @@ 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.as_deref().unwrap_or_default(), + resolved_toml.bin.as_deref().unwrap_or_default(), package_root, edition, errors, )?); - let toml_examples = resolve_examples( - resolved_toml.example.as_ref(), - package_root, - edition, - package.autoexamples, - warnings, - errors, - )?; targets.extend(to_example_targets( - toml_examples.as_deref().unwrap_or_default(), + resolved_toml.example.as_deref().unwrap_or_default(), package_root, edition, warnings, )?); - let toml_tests = resolve_tests( - resolved_toml.test.as_ref(), - package_root, - edition, - package.autotests, - warnings, - errors, - )?; targets.extend(to_test_targets( - toml_tests.as_deref().unwrap_or_default(), + resolved_toml.test.as_deref().unwrap_or_default(), package_root, edition, )?); - let toml_benches = resolve_benches( - resolved_toml.bench.as_ref(), - package_root, - edition, - package.autobenches, - warnings, - errors, - )?; targets.extend(to_bench_targets( - toml_benches.as_deref().unwrap_or_default(), + resolved_toml.bench.as_deref().unwrap_or_default(), package_root, edition, )?); @@ -288,7 +249,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, @@ -414,7 +375,7 @@ fn legacy_bin_path(package_root: &Path, name: &str, has_lib: bool) -> Option>, package_root: &Path, edition: Edition, @@ -471,7 +432,7 @@ fn to_example_targets( Ok(result) } -fn resolve_tests( +pub fn resolve_tests( toml_tests: Option<&Vec>, package_root: &Path, edition: Edition, @@ -519,7 +480,7 @@ fn to_test_targets( Ok(result) } -fn resolve_benches( +pub fn resolve_benches( toml_benches: Option<&Vec>, package_root: &Path, edition: Edition, @@ -585,7 +546,7 @@ fn to_bench_targets( Ok(result) } -fn non_empty(targets: Vec) -> Option> { +pub fn non_empty(targets: Vec) -> Option> { if targets.is_empty() { None } else { diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index 26d9de63872a..8bae889681b3 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -2165,6 +2165,10 @@ name = "foo" version = "0.1.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" homepage = "foo" documentation = "foo" diff --git a/tests/testsuite/features2.rs b/tests/testsuite/features2.rs index d59f331e9a9f..937c844dbf7e 100644 --- a/tests/testsuite/features2.rs +++ b/tests/testsuite/features2.rs @@ -1704,6 +1704,10 @@ 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 diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index ffd47bcf4d1c..0283fc2555dd 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -986,6 +986,10 @@ 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 @@ -1110,6 +1114,10 @@ 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 diff --git a/tests/testsuite/inheritable_workspace_fields.rs b/tests/testsuite/inheritable_workspace_fields.rs index 64304026eea0..ee166249dac1 100644 --- a/tests/testsuite/inheritable_workspace_fields.rs +++ b/tests/testsuite/inheritable_workspace_fields.rs @@ -234,6 +234,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" @@ -243,6 +247,10 @@ categories = ["development-tools"] license = "MIT" repository = "https://github.com/example/example" +[[bin]] +name = "foo" +path = "src/main.rs" + [badges.gitlab] branch = "master" repository = "https://gitlab.com/rust-lang/rust" @@ -399,8 +407,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" @@ -532,8 +548,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"] @@ -789,6 +813,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" @@ -799,6 +827,10 @@ license = "MIT" license-file = "LICENSE" repository = "https://github.com/example/example" +[[bin]] +name = "bar" +path = "src/main.rs" + [badges.gitlab] branch = "master" repository = "https://gitlab.com/rust-lang/rust" @@ -957,8 +989,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 363ff9c71f57..a9bde7a820a1 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,6 +1304,10 @@ name = "bar" version = "0.1.0" authors = [] build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false [lib] @@ -1369,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" @@ -1389,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 @@ -2819,6 +2847,10 @@ edition = "2021" name = "bar" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false resolver = "1" @@ -2844,6 +2876,10 @@ edition = "2015" name = "baz" version = "0.1.0" build = false +autobins = false +autoexamples = false +autotests = false +autobenches = false readme = false [lib] @@ -2912,10 +2948,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 ); @@ -3008,10 +3052,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 ); @@ -3117,10 +3169,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 ); @@ -3634,6 +3694,10 @@ include = [ "src/lib.rs", "build.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false @@ -3707,6 +3771,10 @@ 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 @@ -3783,6 +3851,10 @@ include = [ "src/lib.rs", "build.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false @@ -3857,6 +3929,10 @@ 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 @@ -3938,6 +4014,10 @@ include = [ "src/main.rs", "src/lib.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false @@ -3946,6 +4026,10 @@ license = "MIT" [lib] name = "foo" path = "src/lib.rs" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, )], ); @@ -4011,10 +4095,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" "#, )], ); @@ -4091,6 +4183,10 @@ include = [ "src/main.rs", "src/lib.rs", ] +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "foo" documentation = "docs.rs/foo" readme = false @@ -4099,6 +4195,10 @@ license = "MIT" [lib] name = "foo" path = "src/lib.rs" + +[[bin]] +name = "foo" +path = "src/main.rs" "#, )], ); @@ -4167,10 +4267,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" "#, )], ); @@ -4253,6 +4361,10 @@ 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 @@ -4261,6 +4373,22 @@ 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" "#, )], ); @@ -4295,6 +4423,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 [..] @@ -4328,6 +4460,10 @@ 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 @@ -4430,6 +4566,10 @@ 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 @@ -4441,15 +4581,19 @@ 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" "#, )], ); @@ -4492,20 +4636,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 88c062b93575..ac600b401805 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" @@ -1940,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" @@ -1947,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 ddf229d08af8..c6e6b6b03ed3 100644 --- a/tests/testsuite/weak_dep_features.rs +++ b/tests/testsuite/weak_dep_features.rs @@ -631,6 +631,10 @@ 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