Skip to content

Commit

Permalink
fix(manifest): Provide more context on sematic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 24, 2023
1 parent 8927ea9 commit 53dcd2f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
11 changes: 2 additions & 9 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use cargo_util::paths;
use itertools::Itertools;
use lazycell::LazyCell;
use semver::{self, VersionReq};
use serde::de::IntoDeserializer as _;
use serde::de::{self, Unexpected};
use serde::ser;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -99,15 +98,9 @@ fn read_manifest_from_str(
) -> CargoResult<(EitherManifest, Vec<PathBuf>)> {
let package_root = manifest_file.parent().unwrap();

let toml = {
let pretty_filename = manifest_file
.strip_prefix(config.cwd())
.unwrap_or(manifest_file);
parse_document(contents, pretty_filename, config)?
};

let mut unused = BTreeSet::new();
let manifest: TomlManifest = serde_ignored::deserialize(toml.into_deserializer(), |path| {
let deserializer = toml::de::Deserializer::new(contents);
let manifest: TomlManifest = serde_ignored::deserialize(deserializer, |path| {
let mut key = String::new();
stringify(&mut key, &path);
unused.insert(key);
Expand Down
20 changes: 16 additions & 4 deletions tests/testsuite/bad_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,11 @@ fn bad_dependency() {
error: failed to parse manifest at `[..]`
Caused by:
TOML parse error at line 8, column 23
|
8 | bar = 3
| ^
invalid type: integer `3`, expected a version string like [..]
in `dependencies.bar`
",
)
.run();
Expand Down Expand Up @@ -1317,8 +1320,11 @@ fn bad_debuginfo() {
error: failed to parse manifest [..]
Caused by:
TOML parse error at line 8, column 25
|
8 | debug = 'a'
| ^^^
invalid value: string \"a\", expected a boolean, 0, 1, 2, \"line-tables-only\", or \"line-directives-only\"
in `profile.dev.debug`
",
)
.run();
Expand Down Expand Up @@ -1349,8 +1355,11 @@ fn bad_debuginfo2() {
error: failed to parse manifest at `[..]`
Caused by:
TOML parse error at line 8, column 25
|
8 | debug = 3.6
| ^^^
invalid type: floating point `3.6`, expected a boolean, 0, 1, 2, \"line-tables-only\", or \"line-directives-only\"
in `profile.dev.debug`
",
)
.run();
Expand Down Expand Up @@ -1379,8 +1388,11 @@ fn bad_opt_level() {
error: failed to parse manifest at `[..]`
Caused by:
TOML parse error at line 6, column 25
|
6 | build = 3
| ^
expected a boolean or a string
in `package.build`
",
)
.run();
Expand Down
5 changes: 4 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,11 @@ fn cargo_compile_with_invalid_version() {
[ERROR] failed to parse manifest at `[..]`
Caused by:
TOML parse error at line 4, column 19
|
4 | version = \"1.0\"
| ^^^^^
unexpected end of input while parsing minor version number
in `package.version`
",
)
.run();
Expand Down
5 changes: 4 additions & 1 deletion tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,11 @@ fn wrong_position() {
error: failed to parse manifest at [..]
Caused by:
TOML parse error at line 5, column 34
|
5 | cargo-features = [\"test-dummy-unstable\"]
| ^^^^^^^^^^^^^^^^^^^^^^^
the field `cargo-features` should be set at the top of Cargo.toml before any tables
in `package.cargo-features`
",
)
.run();
Expand Down
5 changes: 4 additions & 1 deletion tests/testsuite/inheritable_workspace_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,11 @@ fn error_workspace_false() {
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
Caused by:
TOML parse error at line 7, column 41
|
7 | description = { workspace = false }
| ^^^^^
`workspace` cannot be false
in `package.description.workspace`
",
)
.run();
Expand Down
21 changes: 15 additions & 6 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,8 +1821,11 @@ fn cargo_metadata_with_invalid_authors_field() {
r#"[ERROR] failed to parse manifest at `[..]`
Caused by:
invalid type: string "", expected a vector of strings or workspace
in `package.authors`"#,
TOML parse error at line 3, column 27
|
3 | authors = ""
| ^^
invalid type: string "", expected a vector of strings or workspace"#,
)
.run();
}
Expand All @@ -1846,8 +1849,11 @@ fn cargo_metadata_with_invalid_version_field() {
r#"[ERROR] failed to parse manifest at `[..]`
Caused by:
invalid type: integer `1`, expected SemVer version
in `package.version`"#,
TOML parse error at line 3, column 27
|
3 | version = 1
| ^
invalid type: integer `1`, expected SemVer version"#,
)
.run();
}
Expand All @@ -1871,8 +1877,11 @@ fn cargo_metadata_with_invalid_publish_field() {
r#"[ERROR] failed to parse manifest at `[..]`
Caused by:
invalid type: string "foo", expected a boolean, a vector of strings, or workspace
in `package.publish`"#,
TOML parse error at line 3, column 27
|
3 | publish = "foo"
| ^^^^^
invalid type: string "foo", expected a boolean, a vector of strings, or workspace"#,
)
.run();
}
Expand Down

0 comments on commit 53dcd2f

Please sign in to comment.