Skip to content

Commit 8aedaad

Browse files
authored
fix(toml): Prevent non-script fields in Cargo scripts (#16026)
### What does this PR try to resolve? The goal is to make cargo scripts more resilient to new manifest fields by turning them into compilation errors. I didn't think this would work well with how we had things setup, but that was a previous implementation I was remembering. Along the way, I identified some fields that shouldn't be in Cargo scripts including - `package.metabuild` - `build-dependencies` - `default-run` I was unsure about `default-target` and `forced-target` and instead marked that as an unresolved question on #9406. ### How to test and review this PR?
2 parents fa6d686 + d815619 commit 8aedaad

File tree

3 files changed

+83
-69
lines changed

3 files changed

+83
-69
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,70 +1377,90 @@ pub fn to_real_manifest(
13771377
}
13781378

13791379
if is_embedded {
1380-
let invalid_fields = [
1381-
("`workspace`", original_toml.workspace.is_some()),
1382-
("`lib`", original_toml.lib.is_some()),
1383-
("`bin`", original_toml.bin.is_some()),
1384-
("`example`", original_toml.example.is_some()),
1385-
("`test`", original_toml.test.is_some()),
1386-
("`bench`", original_toml.bench.is_some()),
1387-
(
1388-
"`package.workspace`",
1389-
original_toml
1390-
.package()
1391-
.map(|p| p.workspace.is_some())
1392-
.unwrap_or(false),
1393-
),
1394-
(
1395-
"`package.build`",
1396-
original_toml
1397-
.package()
1398-
.map(|p| p.build.is_some())
1399-
.unwrap_or(false),
1400-
),
1401-
(
1402-
"`package.links`",
1403-
original_toml
1404-
.package()
1405-
.map(|p| p.links.is_some())
1406-
.unwrap_or(false),
1407-
),
1408-
(
1409-
"`package.autolib`",
1410-
original_toml
1411-
.package()
1412-
.map(|p| p.autolib.is_some())
1413-
.unwrap_or(false),
1414-
),
1415-
(
1416-
"`package.autobins`",
1417-
original_toml
1418-
.package()
1419-
.map(|p| p.autobins.is_some())
1420-
.unwrap_or(false),
1421-
),
1422-
(
1423-
"`package.autoexamples`",
1424-
original_toml
1425-
.package()
1426-
.map(|p| p.autoexamples.is_some())
1427-
.unwrap_or(false),
1428-
),
1429-
(
1430-
"`package.autotests`",
1431-
original_toml
1432-
.package()
1433-
.map(|p| p.autotests.is_some())
1434-
.unwrap_or(false),
1435-
),
1436-
(
1437-
"`package.autobenches`",
1438-
original_toml
1439-
.package()
1440-
.map(|p| p.autobenches.is_some())
1441-
.unwrap_or(false),
1442-
),
1380+
let manifest::TomlManifest {
1381+
cargo_features: _,
1382+
package: _,
1383+
project: _,
1384+
badges: _,
1385+
features: _,
1386+
lib,
1387+
bin,
1388+
example,
1389+
test,
1390+
bench,
1391+
dependencies: _,
1392+
dev_dependencies: _,
1393+
dev_dependencies2: _,
1394+
build_dependencies,
1395+
build_dependencies2,
1396+
target: _,
1397+
lints: _,
1398+
hints: _,
1399+
workspace,
1400+
profile: _,
1401+
patch: _,
1402+
replace: _,
1403+
_unused_keys: _,
1404+
} = &original_toml;
1405+
let mut invalid_fields = vec![
1406+
("`workspace`", workspace.is_some()),
1407+
("`lib`", lib.is_some()),
1408+
("`bin`", bin.is_some()),
1409+
("`example`", example.is_some()),
1410+
("`test`", test.is_some()),
1411+
("`bench`", bench.is_some()),
1412+
("`build-dependencies`", build_dependencies.is_some()),
1413+
("`build_dependencies`", build_dependencies2.is_some()),
14431414
];
1415+
if let Some(package) = original_toml.package() {
1416+
let manifest::TomlPackage {
1417+
edition: _,
1418+
rust_version: _,
1419+
name: _,
1420+
version: _,
1421+
authors: _,
1422+
build,
1423+
metabuild,
1424+
default_target: _,
1425+
forced_target: _,
1426+
links,
1427+
exclude: _,
1428+
include: _,
1429+
publish: _,
1430+
workspace,
1431+
im_a_teapot: _,
1432+
autolib,
1433+
autobins,
1434+
autoexamples,
1435+
autotests,
1436+
autobenches,
1437+
default_run,
1438+
description: _,
1439+
homepage: _,
1440+
documentation: _,
1441+
readme: _,
1442+
keywords: _,
1443+
categories: _,
1444+
license: _,
1445+
license_file: _,
1446+
repository: _,
1447+
resolver: _,
1448+
metadata: _,
1449+
_invalid_cargo_features: _,
1450+
} = package.as_ref();
1451+
invalid_fields.extend([
1452+
("`package.workspace`", workspace.is_some()),
1453+
("`package.build`", build.is_some()),
1454+
("`package.metabuild`", metabuild.is_some()),
1455+
("`package.links`", links.is_some()),
1456+
("`package.autolib`", autolib.is_some()),
1457+
("`package.autobins`", autobins.is_some()),
1458+
("`package.autoexamples`", autoexamples.is_some()),
1459+
("`package.autotests`", autotests.is_some()),
1460+
("`package.autobenches`", autobenches.is_some()),
1461+
("`package.default-run`", default_run.is_some()),
1462+
]);
1463+
}
14441464
let invalid_fields = invalid_fields
14451465
.into_iter()
14461466
.filter_map(|(name, invalid)| invalid.then_some(name))

tests/testsuite/cargo_remove/script/in/cargo-remove-test-fixture.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
---
22
edition = "2015"
33

4-
[build-dependencies]
5-
semver = "0.1.0"
6-
74
[dependencies]
85
docopt = "0.6"
96
rustc-serialize = "0.4"

tests/testsuite/cargo_remove/script/out/cargo-remove-test-fixture.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
---
22
edition = "2015"
33

4-
[build-dependencies]
5-
semver = "0.1.0"
6-
74
[dependencies]
85
rustc-serialize = "0.4"
96
semver = "0.1"

0 commit comments

Comments
 (0)