alibaba/open-code-review · error

read path_rule_map key: %w

Error message

read path_rule_map key: %w

What it means

While iterating path_rule_map keys with the streaming decoder, dec.Token() failed mid-object — the map started with '{' but the stream broke before the next key could be read (unexpected EOF or invalid character). This wraps the decoder's underlying error, so a malformed/tuncated JSON object surfaces here.

Source

Thrown at internal/config/rules/system_rules.go:73

	if !ok || len(mapData) == 0 || string(mapData) == "null" {
		return nil
	}

	// Parse ordered keys using a streaming decoder.
	dec := json.NewDecoder(strings.NewReader(string(mapData)))
	// Read opening '{'
	t, err := dec.Token()
	if err != nil {
		return fmt.Errorf("expected '{' in path_rule_map: %w", err)
	}
	if t != json.Delim('{') {
		return fmt.Errorf("expected '{' in path_rule_map, got %v", t)
	}
	for dec.More() {
		// Read key
		keyTok, err := dec.Token()
		if err != nil {
			return fmt.Errorf("read path_rule_map key: %w", err)
		}
		key, ok := keyTok.(string)
		if !ok {
			return fmt.Errorf("expected string key in path_rule_map, got %T", keyTok)
		}
		// Read value
		var value string
		if err := dec.Decode(&value); err != nil {
			return fmt.Errorf("read path_rule_map value for %q: %w", key, err)
		}
		r.PathRules = append(r.PathRules, PathRule{Pattern: key, Rule: value})
	}
	return nil
}

//go:embed system_rules.json rule_docs/*
var rulesFS embed.FS

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Validate the file with 'jq .' or any JSON parser to locate the syntax error
  2. Ensure every key in path_rule_map is a double-quoted string and members are comma-separated
  3. Restore the file from a known-good version or regenerate it
  4. Check for encoding issues (BOM, control characters) that break the decoder

Example fix

// before (unquoted key, no comma)
{"*.go": "golang.md" "*.py": python.md}
// after
{"*.go": "golang.md", "*.py": "python.md"}
Defensive patterns

Strategy: validation

Validate before calling

func preflightRulesJSON(data []byte) error {
    if err := json.Valid(data); !err { return errors.New("file is not valid JSON") }
    var probe map[string]json.RawMessage
    if err := json.Unmarshal(data, &probe); err != nil { return err }
    m, ok := probe["path_rule_map"]
    if !ok || string(m) == "null" { return nil }
    return json.Unmarshal(m, &map[string]string{})
}

Try / catch

if err := json.Unmarshal(data, &rule); err != nil {
    if strings.Contains(err.Error(), "read path_rule_map key") {
        // decoder broke mid-object: report the decoder cause
        log.Errorf("path_rule_map truncated or malformed: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: path_rule_map is a JSON object that is syntactically truncated or corrupted after the opening brace — missing closing '}', an unquoted key, or invalid characters between entries — encountered during SystemRule unmarshalling.

Common situations: Configs cut off by failed writes or size-limited传输; hand-edited files with an unquoted key or a missing comma between members; template rendering that dropped the tail of the file.

Related errors


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