Skip to content

Commit

Permalink
cargo: Warn when encountering unknown keys
Browse files Browse the repository at this point in the history
Cargo sometimes adds new keys and Meson needs to gracefully handle
those. Currently, an unknown key will trigger an uncaught Python
exception, which is pretty much the worse case. With this change Meson
will instead issue a warning much like the one for unknown cpu
architectures.

See mesonbuild#13826 for the motivation for this change
  • Loading branch information
dcbaker committed Oct 28, 2024
1 parent 3487d1b commit 4ddcd3b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
40 changes: 39 additions & 1 deletion mesonbuild/cargo/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
toml2json = shutil.which('toml2json')


_EXTRA_KEYS_WARNING = (
"This may (unlikely) be an error in the cargo manifest, or may be a missing "
"implementation in Meson. If this issue can be reproduced with the latest "
"version of Meson, please help us by opening an issue at "
"https://github.com/mesonbuild/meson/issues. Please include the crate and "
"version that is generating this warning if possible."
)

class TomlImplementationMissing(MesonException):
pass

Expand Down Expand Up @@ -160,6 +168,12 @@ def __post_init__(self) -> None:
def from_raw(cls, raw: manifest.Package) -> Self:
pkg = T.cast('manifest.FixedPackage',
{fixup_meson_varname(k): v for k, v in raw.items()})
unexpected = set(pkg) - {x.name for x in dataclasses.fields(cls)}
if unexpected:
mlog.warning("Package entry", pkg['name'], "has unexpected keys",
'"{}".'.format(", ".join(sorted(unexpected))), _EXTRA_KEYS_WARNING)
for k in unexpected:
del pkg[k]
return cls(**pkg)

@dataclasses.dataclass
Expand Down Expand Up @@ -240,7 +254,16 @@ def from_raw(cls, name: str, raw: manifest.DependencyV) -> Dependency:
"""Create a dependency from a raw cargo dictionary"""
if isinstance(raw, str):
return cls(name, version.convert(raw))
return cls(name, **_fixup_raw_mappings(raw))
fixed = _fixup_raw_mappings(raw)

unexpected = set(fixed) - {x.name for x in dataclasses.fields(cls)}
if unexpected:
mlog.warning("Dependency entry", name, "has unexpected keys",
'"{}".'.format(", ".join(sorted(unexpected))), _EXTRA_KEYS_WARNING)
for k in unexpected:
del fixed[k]

return cls(name, **fixed)


@dataclasses.dataclass
Expand Down Expand Up @@ -274,6 +297,14 @@ class BuildTarget:
@classmethod
def from_raw(cls, raw: manifest.BuildTarget) -> Self:
build = _fixup_raw_mappings(raw)

unexpected = set(build) - {x.name for x in dataclasses.fields(cls)}
if unexpected:
mlog.warning("Binary entry", build.get('name', '<anonymous>'), "has unexpected keys",
'"{}".'.format(", ".join(sorted(unexpected))), _EXTRA_KEYS_WARNING)
for k in unexpected:
del build[k]

return cls(**build)

@dataclasses.dataclass
Expand All @@ -297,6 +328,13 @@ def from_raw(cls, raw: manifest.LibTarget, fallback_name: str) -> Self:
if 'name' not in fixed:
fixed['name'] = fallback_name

unexpected = set(fixed) - {x.name for x in dataclasses.fields(cls)}
if unexpected:
mlog.warning("Binary entry", fixed['name'], "has unexpected keys",
'"{}".'.format(", ".join(sorted(unexpected))), _EXTRA_KEYS_WARNING)
for k in unexpected:
del fixed[k]

return cls(**fixed)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "bar"
version = "0.1"
flooob = "lolz"

# This dependency does not exist, it is required by default but this subproject
# is called with default-features=false.
Expand Down
8 changes: 8 additions & 0 deletions test cases/rust/22 cargo subproject/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"stdout": [
{
"line": "foo-0-rs| WARNING: Package entry bar has unexpected keys \"flooob\". This may (unlikely) be an error in the cargo manifest, or may be a missing implementation in Meson. If this issue can be reproduced with the latest version of Meson, please help us by opening an issue at https://github.com/mesonbuild/meson/issues. Please include the crate and version that is generating this warning if possible."
}
]
}

0 comments on commit 4ddcd3b

Please sign in to comment.