diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2025-12-02 10:43:37 +0100 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-12-15 13:26:55 -0800 |
| commit | 499015e6ee9b9f47c214b485e5b97f4addb64129 (patch) | |
| tree | 89c0a698406b0ea41ed6ac2ef46b852a8ab66da6 | |
| parent | 5f930d4b53145b9b65e10bfd0d36a9ca91b5297c (diff) | |
| download | meson-499015e6ee9b9f47c214b485e5b97f4addb64129.tar.gz | |
cargo: convert TOMLDecodeError or toml2json errors to a MesonException
Avoid getting a raw exception, instead use a (subclass of) MesonException
that is printed in the usual "FILE:LINE:COLUMN: MESSAGE" format.
Fixes: #15023
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
5 files changed, 52 insertions, 3 deletions
diff --git a/mesonbuild/cargo/toml.py b/mesonbuild/cargo/toml.py index 601510e4e..dda7cfda8 100644 --- a/mesonbuild/cargo/toml.py +++ b/mesonbuild/cargo/toml.py @@ -31,17 +31,28 @@ class TomlImplementationMissing(MesonException): pass +class CargoTomlError(MesonException): + """Exception for TOML parsing errors, keeping proper location info.""" + + def load_toml(filename: str) -> T.Dict[str, object]: if tomllib: - with open(filename, 'rb') as f: - raw = tomllib.load(f) + try: + with open(filename, 'rb') as f: + raw = tomllib.load(f) + except tomllib.TOMLDecodeError as e: + if hasattr(e, 'msg'): + raise CargoTomlError(e.msg, file=filename, lineno=e.lineno, colno=e.colno) from e + else: + raise CargoTomlError(str(e), file=filename) from e else: if toml2json is None: raise TomlImplementationMissing('Could not find an implementation of tomllib, nor toml2json') p, out, err = Popen_safe([toml2json, filename]) if p.returncode != 0: - raise MesonException('toml2json failed to decode output\n', err) + error_msg = err.strip() or 'toml2json failed to decode TOML' + raise CargoTomlError(error_msg, file=filename) raw = json.loads(out) diff --git a/test cases/failing/136 cargo toml error/meson.build b/test cases/failing/136 cargo toml error/meson.build new file mode 100644 index 000000000..6efaab2c5 --- /dev/null +++ b/test cases/failing/136 cargo toml error/meson.build @@ -0,0 +1,19 @@ +project('cargo-toml-error', 'c') + +if not add_languages('rust', required: false) + error('MESON_SKIP_TEST Rust not present, required for Cargo subprojets') +endif + +# Check if we have tomllib/tomli (not toml2json) +python = find_program('python3', 'python') +result = run_command(python, '-c', 'import tomllib', check: false) +if result.returncode() != 0 + result = run_command(python, '-c', 'import tomli', check: false) + if result.returncode() != 0 + # Skip test if using toml2json - error format will be different + error('MESON_SKIP_TEST toml2json in use, skipping test') + endif +endif + +# This should trigger a CargoTomlError with proper location info +foo_dep = dependency('foo-0') diff --git a/test cases/failing/136 cargo toml error/subprojects/foo-0-rs.wrap b/test cases/failing/136 cargo toml error/subprojects/foo-0-rs.wrap new file mode 100644 index 000000000..c39970188 --- /dev/null +++ b/test cases/failing/136 cargo toml error/subprojects/foo-0-rs.wrap @@ -0,0 +1,5 @@ +[wrap-file] +method = cargo + +[provide] +dependency_names = foo-0 diff --git a/test cases/failing/136 cargo toml error/subprojects/foo-0-rs/Cargo.toml b/test cases/failing/136 cargo toml error/subprojects/foo-0-rs/Cargo.toml new file mode 100644 index 000000000..2f2d7a681 --- /dev/null +++ b/test cases/failing/136 cargo toml error/subprojects/foo-0-rs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "foo" +version = "0.0.1" +edition = "2021" +# This creates a TOML decode error: duplicate key +name = "bar"
\ No newline at end of file diff --git a/test cases/failing/136 cargo toml error/test.json b/test cases/failing/136 cargo toml error/test.json new file mode 100644 index 000000000..480cf64d5 --- /dev/null +++ b/test cases/failing/136 cargo toml error/test.json @@ -0,0 +1,8 @@ +{ + "stdout": [ + { + "match": "re", + "line": "test cases/failing/136 cargo toml error/(subprojects/foo-0-rs/Cargo\\.toml:6:13|meson\\.build:19:10): ERROR: Cannot overwrite a value( \\(at.*)?" + } + ] +} |
