antonmedv/fx · error
<panics with the raw TOML input bytes (in) when toml.Unmarsh
Error message
<panics with the raw TOML input bytes (in) when toml.Unmarshal fails>
What it means
ToJSON converts raw TOML bytes to JSON. When toml.Unmarshal fails to parse the input, the function panics with the raw input bytes rather than returning the parser error. The error path is treated as an internal invariant because the caller is expected to feed it TOML that the unstable.Parser can also consume.
Source
Thrown at internal/toml/toml.go:33
type jnode interface{}
type jobject struct {
fields []jfield
}
type jfield struct {
key string
val jnode
}
type jarray struct {
elems []jnode
}
func ToJSON(in []byte) ([]byte, error) {
var typed any
if err := toml.Unmarshal(in, &typed); err != nil {
panic(in)
}
root := &jobject{}
p := unstable.Parser{}
p.Reset(in)
aotActive := map[string]int{} // path -> current index for that AOT
aotCount := map[string]int{} // path -> how many elems seen
currentTablePath := []string{}
for p.NextExpression() {
e := p.Expression()
switch e.Kind {
case unstable.Table:
currentTablePath = keyParts(e.Key())
_ = ensureContainer(root, currentTablePath, aotActive)
case unstable.ArrayTable:View on GitHub (pinned to 4f31cd3a0c)
Solutions
- Validate the TOML input with a standalone parser (e.g. toml.Validate or a test Unmarshal into a scratch value) before calling ToJSON
- Fix the syntax error in the TOML source file reported by the parse failure
- Ensure the correct file is being passed (not JSON/YAML) and the --toml flag or .toml extension is intended
Example fix
// before
var typed any
if err := toml.Unmarshal(in, &typed); err != nil {
panic(in)
}
// after
var typed any
if err := toml.Unmarshal(in, &typed); err != nil {
return nil, fmt.Errorf("invalid TOML: %w", err)
} Defensive patterns
Strategy: validation
Validate before calling
// validate TOML before calling ToJSON
var probe any
if err := toml.Unmarshal(input, &probe); err != nil {
return fmt.Errorf("invalid TOML input: %w", err)
}
out, err := toml.ToJSON(input) Try / catch
// guard against the panic with recover
func safeToJSON(in []byte) (out []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("invalid TOML: %v", r)
}
}()
return toml.ToJSON(in)
} Prevention
- Lint TOML files with a TOML linter (e.g. taplo) before processing
- Never pass JSON/YAML files with --toml; rely on the .toml extension auto-detection
- Validate user-supplied config files at pipeline boundaries, not inside the converter
When it happens
Trigger: Calling toml.ToJSON with bytes that are not valid TOML — syntax errors, mismatched brackets, invalid key names — causing toml.Unmarshal to return an error.
Common situations: Users piping a malformed .toml file (or accidentally a JSON/YAML file) into fx with --toml; truncated config files; TOML features unsupported by the go-toml version in use.
Related errors
- unsupported node kind %d
- <error from json.Marshal of the theme export map> (panic(err
- <error from io.ReadAll of input source> (panic(err))
- <error returned from pipeline p.Run()> (panic(err))
- <non-PathError from os.Open(filePath)> (panic(err))
AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02).
Data as JSON: /api/errors/6fe0318cd4724a7c.
Report an issue: GitHub.