tamasfe / taplo

A TOML toolkit written in Rust
https://taplo.tamasfe.dev
MIT License
1.41k stars 111 forks source link

Arrays of tables are allowed to extend static arrays when nested inside another array of tables #495

Open benblank opened 1 year ago

benblank commented 1 year ago

When parsing, taplo correctly forbids extending a static array using an array of tables, but only if those arrays are not themselves inside an array of tables.

By my read of the TOML spec, being inside an array of tables as well shouldn't change whether this is valid. The other two TOML parsers I've run this through consider it invalid regardless of context; tomllib in Python's standard library produces the error "Cannot mutate immutable namespace" and https://www.toml-lint.com/ the error "Can't extend an inline array".

In the example below, I've created three invalid TOML files. All three of them contain a static array and an array of tables, both named fruits, but in different contexts:

The result of running those three files through taplo get -o json ``` $ cat invalid1.toml fruits = ["one", "two"] [[fruits]] three = "four" $ npx @taplo/cli get -o json < invalid1.toml error: expected array of tables ┌─ -:1:1 │ 1 │ fruits = ["one", "two"] │ ^^^^^^ expected array of tables 2 │ 3 │ [[fruits]] │ ------ required by this key ERROR operation failed error=semantic errors found $ cat invalid2.toml [food] fruits = ["one", "two"] [[food.fruits]] three = "four" $ npx @taplo/cli get -o json < invalid2.toml error: expected array of tables ┌─ -:2:1 │ 2 │ fruits = ["one", "two"] │ ^^^^^^ expected array of tables 3 │ 4 │ [[food.fruits]] │ ------ required by this key ERROR operation failed error=semantic errors found $ cat invalid3.toml [[food]] fruits = ["one", "two"] [[food.fruits]] three = "four" $ npx @taplo/cli get -o json < invalid3.toml { "food": [ { "fruits": [ "one", "two", { "three": "four" } ] } ] } ```
ia0 commented 1 year ago

Thanks for the bug report! I confirm the issue. I don't have time to fix it though, so PRs are welcome.