Skip to content

Commit 08d3033

Browse files
committed
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 <[email protected]>
1 parent 8ba2f1b commit 08d3033

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

mesonbuild/cargo/toml.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,27 @@ class TomlImplementationMissing(MesonException):
3131
pass
3232

3333

34+
class CargoTomlError(MesonException):
35+
"""Exception for TOML parsing errors, keeping proper location info."""
36+
37+
3438
def load_toml(filename: str) -> T.Dict[str, object]:
3539
if tomllib:
36-
with open(filename, 'rb') as f:
37-
raw = tomllib.load(f)
40+
try:
41+
with open(filename, 'rb') as f:
42+
raw = tomllib.load(f)
43+
except tomllib.TOMLDecodeError as e:
44+
raise CargoTomlError(e.msg, file=filename, lineno=e.lineno, colno=e.colno) from e
45+
except Exception:
46+
raise
3847
else:
3948
if toml2json is None:
4049
raise TomlImplementationMissing('Could not find an implementation of tomllib, nor toml2json')
4150

4251
p, out, err = Popen_safe([toml2json, filename])
4352
if p.returncode != 0:
44-
raise MesonException('toml2json failed to decode output\n', err)
53+
error_msg = err.strip() or 'toml2json failed to decode TOML'
54+
raise CargoTomlError(error_msg, file=filename)
4555

4656
raw = json.loads(out)
4757

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
project('cargo-toml-error', 'c')
2+
3+
# Check if we have tomllib/tomli (not toml2json)
4+
python = find_program('python3', 'python')
5+
result = run_command(python, '-c', 'import tomllib', check: false)
6+
if result.returncode() != 0
7+
result = run_command(python, '-c', 'import tomli', check: false)
8+
if result.returncode() != 0
9+
# Skip test if using toml2json - error format will be different
10+
error('MESON_SKIP_TEST toml2json in use, skipping test')
11+
endif
12+
endif
13+
14+
# This should trigger a CargoTomlError with proper location info
15+
foo_dep = dependency('foo-0')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[wrap-file]
2+
method = cargo
3+
4+
[provide]
5+
dependency_names = foo-0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "foo"
3+
version = "0.0.1"
4+
edition = "2021"
5+
# This creates a TOML decode error: duplicate key
6+
name = "bar"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"stdout": [
3+
{
4+
"line": "test cases/failing/136 cargo toml error/subprojects/foo-0-rs/Cargo.toml:6:13: ERROR: Cannot overwrite a value"
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)