Skip to content

Commit f36f0d8

Browse files
committed
fix(toml): Warn, rather than fail publish, if a target is excluded
1 parent ec9e601 commit f36f0d8

File tree

9 files changed

+355
-66
lines changed

9 files changed

+355
-66
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn resolve_toml(
263263
manifest_file: &Path,
264264
gctx: &GlobalContext,
265265
warnings: &mut Vec<String>,
266-
_errors: &mut Vec<String>,
266+
errors: &mut Vec<String>,
267267
) -> CargoResult<manifest::TomlManifest> {
268268
let mut resolved_toml = manifest::TomlManifest {
269269
cargo_features: original_toml.cargo_features.clone(),
@@ -324,6 +324,39 @@ fn resolve_toml(
324324
edition,
325325
warnings,
326326
)?;
327+
resolved_toml.bin = targets::resolve_bins(
328+
original_toml.bin.as_ref(),
329+
package_root,
330+
&original_package.name,
331+
edition,
332+
original_package.autobins,
333+
warnings,
334+
resolved_toml.lib.is_some(),
335+
)?;
336+
resolved_toml.example = targets::resolve_examples(
337+
original_toml.example.as_ref(),
338+
package_root,
339+
edition,
340+
original_package.autoexamples,
341+
warnings,
342+
errors,
343+
)?;
344+
resolved_toml.test = targets::resolve_tests(
345+
original_toml.test.as_ref(),
346+
package_root,
347+
edition,
348+
original_package.autotests,
349+
warnings,
350+
errors,
351+
)?;
352+
resolved_toml.bench = targets::resolve_benches(
353+
original_toml.bench.as_ref(),
354+
package_root,
355+
edition,
356+
original_package.autobenches,
357+
warnings,
358+
errors,
359+
)?;
327360

328361
resolved_toml.dependencies = resolve_dependencies(
329362
gctx,
@@ -476,10 +509,10 @@ fn resolve_package_toml<'a>(
476509
.map(manifest::InheritableField::Value),
477510
workspace: original_package.workspace.clone(),
478511
im_a_teapot: original_package.im_a_teapot.clone(),
479-
autobins: original_package.autobins.clone(),
480-
autoexamples: original_package.autoexamples.clone(),
481-
autotests: original_package.autotests.clone(),
482-
autobenches: original_package.autobenches.clone(),
512+
autobins: Some(false),
513+
autoexamples: Some(false),
514+
autotests: Some(false),
515+
autobenches: Some(false),
483516
default_run: original_package.default_run.clone(),
484517
description: original_package
485518
.description
@@ -1118,7 +1151,6 @@ fn to_real_manifest(
11181151
&features,
11191152
&original_toml,
11201153
&resolved_toml,
1121-
package_name,
11221154
package_root,
11231155
edition,
11241156
&resolved_package.metabuild,
@@ -2471,17 +2503,53 @@ fn prepare_toml_for_publish(
24712503
.as_ref()
24722504
.filter(|lib| is_target_included(lib, included, "library", gctx))
24732505
.cloned();
2506+
let bin = me.bin.as_ref().and_then(|targets| {
2507+
targets::non_empty(
2508+
targets
2509+
.iter()
2510+
.filter(|target| is_target_included(target, included, "binary", gctx))
2511+
.cloned()
2512+
.collect::<Vec<_>>(),
2513+
)
2514+
});
2515+
let example = me.example.as_ref().and_then(|targets| {
2516+
targets::non_empty(
2517+
targets
2518+
.iter()
2519+
.filter(|target| is_target_included(target, included, "example", gctx))
2520+
.cloned()
2521+
.collect::<Vec<_>>(),
2522+
)
2523+
});
2524+
let test = me.test.as_ref().and_then(|targets| {
2525+
targets::non_empty(
2526+
targets
2527+
.iter()
2528+
.filter(|target| is_target_included(target, included, "test", gctx))
2529+
.cloned()
2530+
.collect::<Vec<_>>(),
2531+
)
2532+
});
2533+
let bench = me.bench.as_ref().and_then(|targets| {
2534+
targets::non_empty(
2535+
targets
2536+
.iter()
2537+
.filter(|target| is_target_included(target, included, "benchmark", gctx))
2538+
.cloned()
2539+
.collect::<Vec<_>>(),
2540+
)
2541+
});
24742542

24752543
let all = |_d: &manifest::TomlDependency| true;
24762544
let mut manifest = manifest::TomlManifest {
24772545
package: Some(package),
24782546
project: None,
24792547
profile: me.profile.clone(),
24802548
lib,
2481-
bin: me.bin.clone(),
2482-
example: me.example.clone(),
2483-
test: me.test.clone(),
2484-
bench: me.bench.clone(),
2549+
bin,
2550+
example,
2551+
test,
2552+
bench,
24852553
dependencies: map_deps(gctx, me.dependencies.as_ref(), all)?,
24862554
dev_dependencies: map_deps(
24872555
gctx,

src/cargo/util/toml/targets.rs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub(super) fn to_targets(
3636
features: &Features,
3737
original_toml: &TomlManifest,
3838
resolved_toml: &TomlManifest,
39-
package_name: &str,
4039
package_root: &Path,
4140
edition: Edition,
4241
metabuild: &Option<StringOrVec>,
@@ -45,8 +44,6 @@ pub(super) fn to_targets(
4544
) -> CargoResult<Vec<Target>> {
4645
let mut targets = Vec::new();
4746

48-
let has_lib;
49-
5047
if let Some(target) = to_lib_target(
5148
original_toml.lib.as_ref(),
5249
resolved_toml.lib.as_ref(),
@@ -55,72 +52,36 @@ pub(super) fn to_targets(
5552
warnings,
5653
)? {
5754
targets.push(target);
58-
has_lib = true;
59-
} else {
60-
has_lib = false;
6155
}
6256

6357
let package = resolved_toml
6458
.package
6559
.as_ref()
6660
.ok_or_else(|| anyhow::format_err!("manifest has no `package` (or `project`)"))?;
6761

68-
let bins = resolve_bins(
69-
resolved_toml.bin.as_ref(),
70-
package_root,
71-
package_name,
72-
edition,
73-
package.autobins,
74-
warnings,
75-
has_lib,
76-
)?;
7762
targets.extend(to_bin_targets(
7863
features,
79-
bins.as_deref().unwrap_or_default(),
64+
resolved_toml.bin.as_deref().unwrap_or_default(),
8065
package_root,
8166
edition,
8267
errors,
8368
)?);
8469

85-
let toml_examples = resolve_examples(
86-
resolved_toml.example.as_ref(),
87-
package_root,
88-
edition,
89-
package.autoexamples,
90-
warnings,
91-
errors,
92-
)?;
9370
targets.extend(to_example_targets(
94-
toml_examples.as_deref().unwrap_or_default(),
71+
resolved_toml.example.as_deref().unwrap_or_default(),
9572
package_root,
9673
edition,
9774
warnings,
9875
)?);
9976

100-
let toml_tests = resolve_tests(
101-
resolved_toml.test.as_ref(),
102-
package_root,
103-
edition,
104-
package.autotests,
105-
warnings,
106-
errors,
107-
)?;
10877
targets.extend(to_test_targets(
109-
toml_tests.as_deref().unwrap_or_default(),
78+
resolved_toml.test.as_deref().unwrap_or_default(),
11079
package_root,
11180
edition,
11281
)?);
11382

114-
let toml_benches = resolve_benches(
115-
resolved_toml.bench.as_ref(),
116-
package_root,
117-
edition,
118-
package.autobenches,
119-
warnings,
120-
errors,
121-
)?;
12283
targets.extend(to_bench_targets(
123-
toml_benches.as_deref().unwrap_or_default(),
84+
resolved_toml.bench.as_deref().unwrap_or_default(),
12485
package_root,
12586
edition,
12687
)?);
@@ -288,7 +249,7 @@ fn to_lib_target(
288249
Ok(Some(target))
289250
}
290251

291-
fn resolve_bins(
252+
pub fn resolve_bins(
292253
toml_bins: Option<&Vec<TomlBinTarget>>,
293254
package_root: &Path,
294255
package_name: &str,
@@ -414,7 +375,7 @@ fn legacy_bin_path(package_root: &Path, name: &str, has_lib: bool) -> Option<Pat
414375
None
415376
}
416377

417-
fn resolve_examples(
378+
pub fn resolve_examples(
418379
toml_examples: Option<&Vec<TomlExampleTarget>>,
419380
package_root: &Path,
420381
edition: Edition,
@@ -471,7 +432,7 @@ fn to_example_targets(
471432
Ok(result)
472433
}
473434

474-
fn resolve_tests(
435+
pub fn resolve_tests(
475436
toml_tests: Option<&Vec<TomlTestTarget>>,
476437
package_root: &Path,
477438
edition: Edition,
@@ -519,7 +480,7 @@ fn to_test_targets(
519480
Ok(result)
520481
}
521482

522-
fn resolve_benches(
483+
pub fn resolve_benches(
523484
toml_benches: Option<&Vec<TomlBenchTarget>>,
524485
package_root: &Path,
525486
edition: Edition,
@@ -585,7 +546,7 @@ fn to_bench_targets(
585546
Ok(result)
586547
}
587548

588-
fn non_empty<T>(targets: Vec<T>) -> Option<Vec<T>> {
549+
pub fn non_empty<T>(targets: Vec<T>) -> Option<Vec<T>> {
589550
if targets.is_empty() {
590551
None
591552
} else {

tests/testsuite/artifact_dep.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,10 @@ name = "foo"
21652165
version = "0.1.0"
21662166
authors = []
21672167
build = false
2168+
autobins = false
2169+
autoexamples = false
2170+
autotests = false
2171+
autobenches = false
21682172
description = "foo"
21692173
homepage = "foo"
21702174
documentation = "foo"

tests/testsuite/features2.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,10 @@ name = "a"
17041704
version = "0.1.0"
17051705
authors = ["Zzz"]
17061706
build = false
1707+
autobins = false
1708+
autoexamples = false
1709+
autotests = false
1710+
autobenches = false
17071711
description = "foo"
17081712
homepage = "https://example.com/"
17091713
readme = false

tests/testsuite/features_namespaced.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,10 @@ edition = "2015"
986986
name = "foo"
987987
version = "0.1.0"
988988
build = false
989+
autobins = false
990+
autoexamples = false
991+
autotests = false
992+
autobenches = false
989993
description = "foo"
990994
homepage = "https://example.com/"
991995
readme = false
@@ -1110,6 +1114,10 @@ edition = "2015"
11101114
name = "foo"
11111115
version = "0.1.0"
11121116
build = false
1117+
autobins = false
1118+
autoexamples = false
1119+
autotests = false
1120+
autobenches = false
11131121
description = "foo"
11141122
homepage = "https://example.com/"
11151123
readme = false

0 commit comments

Comments
 (0)