alibaba/open-code-review · error

unmarshal global rule: %w

Error message

unmarshal global rule: %w

What it means

loadGlobalRule unmarshals ~/.opencodereview/rule.json into ProjectRule. This error wraps json.Unmarshal's failure: the global config exists but is not valid JSON or does not match the ProjectRule schema.

Source

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

	return nil
}

func loadGlobalRule() (*ProjectRule, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return nil, nil
	}
	path := filepath.Join(home, ".opencodereview", "rule.json")
	data, err := os.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("read global rule %s: %w", path, err)
	}
	var pr ProjectRule
	if err := json.Unmarshal(data, &pr); err != nil {
		return nil, fmt.Errorf("unmarshal global rule: %w", err)
	}
	resolveRuleEntries(pr.Rules, filepath.Dir(path), "")
	return &pr, nil
}

func loadRuleFile(path string) (*ProjectRule, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("read rule file %s: %w", path, err)
	}
	var pr ProjectRule
	if err := json.Unmarshal(data, &pr); err != nil {
		return nil, fmt.Errorf("unmarshal rule file %s: %w", path, err)
	}
	resolveRuleEntries(pr.Rules, filepath.Dir(path), "")
	return &pr, nil
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Validate the file: jq . ~/.opencodereview/rule.json and fix the reported syntax error
  2. Ensure the top-level shape matches ProjectRule (an object with a rules field, not an array)
  3. If the file is unneeded, delete or rename it — a missing file is treated as 'no global rule'

Example fix

// before (truncated)
{"rules": [{"pattern": "**/*.go", "rule":
// after
{"rules": [{"pattern": "**/*.go", "rule": "go.md"}]}
Defensive patterns

Strategy: validation

Validate before calling

p := filepath.Join(home, ".opencodereview", "rule.json")
if data, err := os.ReadFile(p); err == nil {
	var v any
	if err := json.Unmarshal(data, &v); err != nil {
		return fmt.Errorf("global rule %s is not valid JSON: %v", p, err)
	}
}

Try / catch

pr, err := loadGlobalRule()
if err != nil {
	if strings.Contains(err.Error(), "unmarshal global rule") {
		log.Printf("warning: ~/.opencodereview/rule.json is invalid (%v) — ignoring global rules", err)
		return nil // degrade to project/system rules only
	}
	return err
}

Prevention

When it happens

Trigger: os.ReadFile succeeds but json.Unmarshal fails — malformed JSON (trailing commas, truncation), or content of a different type entirely (e.g. a YAML file saved with a .json name, or JSON that doesn't match ProjectRule's fields).

Common situations: Editing rule.json by hand and leaving a syntax error; a truncated write from a crash or full disk; pasting an array instead of the expected object shape.

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