mikefarah / yq

yq is a portable command-line YAML, JSON, XML, CSV, TOML and properties processor
https://mikefarah.gitbook.io/yq/
MIT License
12.4k stars 607 forks source link

TOML nested array of tables with arrays breaks v4.35.1 #1758

Open mbigras opened 1 year ago

mbigras commented 1 year ago

Describe the bug

Hi! Thank you for making and maintaining yq; I happily use your tool daily and especially thanks for starting work on TOML support—see https://github.com/mikefarah/yq/issues/1364!

Issue: Yq breaks when parsing TOML nested array of tables with arrays (LOL)—see https://toml.io/en/v1.0.0#array-of-tables. Workaround: You can parse TOML with https://github.com/pelletier/go-toml, then pipe JSON to yq.

Version of yq: v4.35.1,...,v4.42.1 Operating system: Mac and Linux Installed via: Homebrew and Docker

Input TOML data1.toml:

[[fruits]]
name = "apple"
[[fruits.varieties]]  # nested array of tables
name = "red delicious"

Command The command you ran:

yq -ptoml -oyaml data1.toml

Actual behavior

Error: bad file 'data1.toml': cannot index array with 'varieties' (strconv.ParseInt: parsing "varieties": invalid syntax)

Expected behavior

fruits:
  - name: apple
    varieties:
      - name: red delicious

Additional context I suspect this is a problem with yq because the tomljson can handle data1.toml like the following shell session illustrates:

$ tomljson /dev/stdin <<'TOML' | yq -pjson -oyaml
[[fruits]]
name = "apple"
[[fruits.varieties]]  # nested array of tables
name = "red delicious"
TOML
fruits:
  - name: apple
    varieties:
      - name: red delicious

Again, thank you for yq and for considering this sort of obtuse issue.

edmorley commented 1 year ago

We're encountering this too, in: https://github.com/heroku/builder/actions/runs/6303904793/job/17114118993?pr=392#step:5:9

When running yq '.stack.run-image' builder-22/builder.toml against this toml file: https://github.com/heroku/builder/blob/b83a3606a68b882484ee3353a58c2813d321a9f4/builder-22/builder.toml

antoinedeschenes commented 8 months ago

Also seeing the issue with sub-tables, for example:

curl -s https://raw.githubusercontent.com/veertuinc/gitlab-runner/master/config.toml.example | yq -p toml -o yaml .
vindex10 commented 1 month ago

still relevant.

Thank you for supporting TOML!

kleinschmidt commented 1 month ago

Another potentially useful test case:

julia_version = "1.10.5"
manifest_format = "2.0"
project_hash = "6a7f2fbcfb1740b9273397ba607e5438aaea4fa8"

[[deps.AbstractFFTs]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "d92ad398961a3ed262d8bf04a1a2b8340f915fef"
uuid = "621f4979-c628-5d54-868e-fcf4e3e8185c"
version = "1.5.0"
weakdeps = ["ChainRulesCore", "Test"]

    [deps.AbstractFFTs.extensions]
    AbstractFFTsChainRulesCoreExt = "ChainRulesCore"
    AbstractFFTsTestExt = "Test"

(this is from a Julia language Manifest.toml file which I was hoping to use yq to pull some metadata from)

kleinschmidt commented 1 month ago

Huh, it's interesting that tomljson works w/ this syntax but yq doesn't, they're both using the same library under the hood (https://github.com/mikefarah/yq/blob/master/pkg/yqlib/decoder_toml.go#L13)