Skip to content

Support go-toml v2.4.3 - #553

Merged
TomWright merged 2 commits into
masterfrom
issue-550-go-toml-2.4
Aug 1, 2026
Merged

Support go-toml v2.4.3#553
TomWright merged 2 commits into
masterfrom
issue-550-go-toml-2.4

Conversation

@TomWright

Copy link
Copy Markdown
Owner

Fixes #550

Problem

Debian's build of dasel against go-toml v2.4.2 fails every *_to_toml cross-format test with toml: cannot encode a X as a document root.

go-toml v2.4 added a check in encodeRoot that a document root must be a table. Earlier versions encoded whatever they were given. The TOML writer depended on that leniency: when the top level value isn't a map — the result of selecting a scalar or list out of a document — it handed the value straight to encoder.Encode. So dasel -i json -o toml 'hello' printing 'world' only worked because go-toml permitted a bare value at the root.

We were also pinned to a pseudo-version (v2.2.5-0.20250826075308-…), several releases behind and missing the fixes from GHSA-frc4-6h9q-39fq.

Fix

encodeRootValue handles non-map roots by wrapping the value in a generated single-field struct tagged toml:"v,inline", encoding that, and stripping the v = prefix. go-toml still owns all value formatting, so quoting and array style are unchanged.

Verification

Built binaries from master and from this branch and diffed their output across scalars, lists, nested lists, quoted and multiline strings, null, whole documents, and the complex-example round-trip, in both normal and --compact mode.

Output is byte-identical except one case, which improved:

selector before after
root list of tables [[]]a = 1⏎⏎[[]]a = 2 [{a = 1}, {a = 2}]

The old output there was invalid TOML — table headers with an empty key, which dasel itself cannot parse back. The inline tag option fixes it as a side effect. It is a behaviour change, so it is called out in the changelog rather than left silent.

Added TestTomlWriter_RootValue covering 12 root-value shapes plus the null-root error, in both compact and non-compact mode, so a future go-toml bump cannot regress this quietly.

Note on v2

This targets the v3 master branch. Debian packages dasel v2, where the writer code differs, so if their build needs the same fix that is a separate backport — I have not assumed this patch transfers.

go-toml v2.4 rejects encoding a bare value as a document root, which the
TOML writer relied on when the top level value isn't a table - the result
of selecting a scalar or list out of a document. Every *_to_toml case in
the cross format tests failed with "cannot encode a X as a document
root", as reported in the Debian build.

The writer now encodes those values against a placeholder key and strips
the "<key> = " prefix back off, so go-toml still owns value formatting
and the output is unchanged. Using the inline tag option on the
placeholder also fixes a top level list of tables, which previously
emitted table headers with an empty key.

Fixes #550
@TomWright
TomWright merged commit 70fd28f into master Aug 1, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for github.com/pelletier/go-toml v2.4.3+?

1 participant