chenhg5/cc-connect · error

invalid TOML: %w

Error message

invalid TOML: %w

What it means

FormatConfigFile first validates the TOML by unmarshalling it into Config before formatting; invalid syntax is wrapped as "invalid TOML: %w" so the formatter never rewrites a file it could not fully parse (which would risk destroying content). The wrapped decoder error pinpoints the line/column of the syntax problem.

Source

Thrown at config/config.go:3708

		return err
	}
	if err := tmp.Close(); err != nil {
		os.Remove(tmpPath)
		return err
	}
	return os.Rename(tmpPath, ConfigPath)
}

// FormatConfigFile reads the config file at the given path, formats it, and
// writes it back. It validates the TOML syntax before writing.
func FormatConfigFile(path string) error {
	data, err := os.ReadFile(path)
	if err != nil {
		return fmt.Errorf("read config: %w", err)
	}
	cfg := &Config{}
	if err := toml.Unmarshal(data, cfg); err != nil {
		return fmt.Errorf("invalid TOML: %w", err)
	}
	formatted := formatTOML(string(data))
	if formatted == string(data) {
		return nil
	}
	dir := filepath.Dir(path)
	tmp, err := os.CreateTemp(dir, ".config-*.tmp")
	if err != nil {
		return fmt.Errorf("create temp file: %w", err)
	}
	tmpPath := tmp.Name()
	if _, err := tmp.WriteString(formatted); err != nil {
		tmp.Close()
		os.Remove(tmpPath)
		return fmt.Errorf("write formatted config: %w", err)
	}
	if err := tmp.Sync(); err != nil {
		tmp.Close()

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Fix the TOML syntax at the line/column named in the wrapped decoder error.
  2. Use a TOML linter/editor plugin to validate before re-running format.
  3. Restore the file from backup if it was truncated; note format refuses to write until the file parses.

Example fix

// before (config.toml)
name = main
// after
name = "main"
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(path)
if err != nil { return err }
probe := &config.Config{}
if err := toml.Unmarshal(data, probe); err != nil {
    return fmt.Errorf("%s is not valid TOML, formatter will refuse: %w", path, err)
}

Try / catch

if err := config.FormatConfigFile(path); err != nil {
    if strings.Contains(err.Error(), "invalid TOML:") {
        slog.Error("fix TOML before formatting", "file", path, "detail", err)
        return err // do NOT overwrite the file
    }
    return err
}

Prevention

When it happens

Trigger: toml.Unmarshal fails in FormatConfigFile (config/config.go:3708) on malformed TOML: unterminated string, stray bracket, duplicate key, or a value whose type conflicts with the Config schema.

Common situations: Formatting a hand-edited config with a typo; trying to format a YAML/JSON file by mistake; truncated download or copy-paste cut the file short.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/722e1c2d42860493. Report an issue: GitHub.