alibaba/open-code-review · critical

unmarshal default system rules: %w

Error message

unmarshal default system rules: %w

What it means

LoadDefault unmarshals the embedded system_rules.json into SystemRule (using the custom UnmarshalJSON). This error wraps json.Unmarshal's failure: the embedded JSON is syntactically invalid or violates the custom decoder's expectations (e.g. path_rule_map value not a string).

Source

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

			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

// LoadDefault parses the embedded system_rules.json and resolves rule file references.
func LoadDefault() (*SystemRule, error) {
	data, err := rulesFS.ReadFile("system_rules.json")
	if err != nil {
		return nil, fmt.Errorf("read embedded system_rules.json: %w", err)
	}
	var rule SystemRule
	if err := json.Unmarshal(data, &rule); err != nil {
		return nil, fmt.Errorf("unmarshal default system rules: %w", err)
	}
	content, err := rulesFS.ReadFile("rule_docs/" + rule.DefaultRule)
	if err != nil {
		return nil, fmt.Errorf("read default rule file %q: %w", rule.DefaultRule, err)
	}
	rule.DefaultRule = strings.TrimRight(string(content), "\n")
	for i := range rule.PathRules {
		content, err := rulesFS.ReadFile("rule_docs/" + rule.PathRules[i].Rule)
		if err != nil {
			return nil, fmt.Errorf("read rule file %q for pattern %q: %w", rule.PathRules[i].Rule, rule.PathRules[i].Pattern, err)
		}
		rule.PathRules[i].Rule = strings.TrimRight(string(content), "\n")
	}
	return &rule, nil
}

// loadObjCRule reads the embedded Objective-C rule doc used by the ".m"
// content sniff. It is not referenced from system_rules.json's path_rule_map,

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Validate internal/config/rules/system_rules.json with jq or a JSON linter and fix the syntax error at the reported offset
  2. Check for leftover merge-conflict markers (<<<<<<<) or trailing commas
  3. Run the package tests (make test) after fixing to confirm LoadDefault succeeds

Example fix

// before (invalid)
{"default_rule": "default.md", "path_rule_map": {"**/*.go": "go.md",}}
// after
{"default_rule": "default.md", "path_rule_map": {"**/*.go": "go.md"}}
Defensive patterns

Strategy: validation

Validate before calling

var pretty map[string]any
if err := json.Unmarshal(systemRulesJSON, &pretty); err != nil {
	var se *json.SyntaxError
	if errors.As(err, &se) {
		line := strings.Count(string(systemRulesJSON[:se.Offset]), "\n") + 1
		return fmt.Errorf("system_rules.json invalid at line %d: %v", line, err)
	}
	return err
}

Try / catch

rule, err := rules.LoadDefault()
if err != nil {
	if strings.Contains(err.Error(), "unmarshal default system rules") {
		log.Fatalf("embedded system_rules.json is invalid JSON: %v — this is a build bug, report/fix the file", err)
	}
	return err
}

Prevention

When it happens

Trigger: The embedded system_rules.json contains invalid JSON — trailing commas, unquoted keys, bad escapes, or a path_rule_map entry whose value is not a string.

Common situations: A bad edit to the checked-in system_rules.json got committed; an automated script rewrote the file; merge conflict markers left in the JSON.

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/da3aeb869f280d40. Report an issue: GitHub.