alibaba/open-code-review · error

parse app config: %w

Error message

parse app config: %w

What it means

LoadAppConfig reads the OCR app config file from disk and unmarshals it into Config. When the file exists but is not valid JSON (or does not match the Config schema), json.Unmarshal fails and the error is wrapped as "parse app config: %w". The underlying encoding/json error names the offset and cause (e.g. syntax error or type mismatch).

Source

Thrown at cmd/opencodereview/config_cmd.go:412

	var cfg Config
	if err := json.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("parse config: %w", err)
	}
	return &cfg, nil
}

// LoadAppConfig loads config from path. Returns nil, nil if file does not exist.
func LoadAppConfig(path string) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("read app config %s: %w", path, err)
	}
	var cfg Config
	if err := json.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("parse app config: %w", err)
	}
	return &cfg, nil
}

// supportedConfigKeys is the single source of truth for the top-level config
// keys accepted by setConfigValue. The unknown-key error message is generated
// from this list so the two cannot drift apart when a new key is added.
var supportedConfigKeys = []string{
	"provider",
	"model",
	"max_tokens",
	"effort",
	"providers.<name>.<field>",
	"custom_providers.<name>.<field>",
	"mcp_servers.<name>.<field>",
	"llm.url",
	"llm.auth_token",
	"llm.auth_token_cmd",

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Open the config file at the path shown and fix the JSON syntax error reported by the wrapped encoding/json message (it includes a byte offset)
  2. Validate the file with `cat <path> | python3 -m json.tool` or `jq . <path>` to pinpoint the bad offset
  3. If the file is unrecoverable, delete or rename it — LoadAppConfig returns nil, nil for a missing file and defaults apply
  4. Re-create settings with `ocr config set <key> <value>` instead of manual edits

Example fix

// before (config.json)
{ "provider": "bedrock", "max_tokens": "4096", }
// after
{ "provider": "bedrock", "max_tokens": 4096 }
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(path)
if err != nil { return err }
if !json.Valid(data) {
    return fmt.Errorf("%s is not valid JSON; fix or delete it before running ocr", path)
}
cfg, err := LoadAppConfig(path)

Try / catch

cfg, err := LoadAppConfig(path)
if err != nil {
    var syn *json.SyntaxError
    if errors.As(err, &syn) {
        log.Fatalf("config JSON invalid at offset %d: %v — fix %s", syn.Offset, syn, path)
    }
    return err
}

Prevention

When it happens

Trigger: Running `ocr config set/get` or any command calling LoadAppConfig on a config file whose content is malformed JSON — trailing commas, comments, single quotes, or a JSON value where a struct field expects a different type (e.g. max_tokens as a string).

Common situations: Hand-editing ~/.opencodereview/config.json and introducing a syntax error; pasting YAML instead of JSON; an editor or script writing partial/truncated JSON; merging config changes by hand and corrupting the file.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/24e7ff48a5ec6668. Report an issue: GitHub.