alibaba/open-code-review · error

load rules: %w

Error message

load rules: %w

What it means

runRulesCheck builds a rules.Resolver from the repo and any --rule file. If NewResolver fails — malformed custom rule JSON, unreadable project/global rule file, or IO errors — it is wrapped as "load rules". No rule detail can be shown until the rule set loads.

Source

Thrown at cmd/opencodereview/rules_cmd.go:53

		return runRulesCheck(args[0])
	},
}

func init() {
	addRepoFlag(rulesCheckCmd, &rulesCheckRepoDir)
	rulesCheckCmd.Flags().StringVar(&rulesCheckRulePath, "rule", "", "path to a custom rule JSON file")
	rulesCmd.AddCommand(rulesCheckCmd)
}

func runRulesCheck(filePath string) error {
	resolvedRepo, err := resolveRepoDir(rulesCheckRepoDir)
	if err != nil {
		return err
	}

	resolver, _, err := rules.NewResolver(resolvedRepo, rulesCheckRulePath, rules.ResolverOptions{})
	if err != nil {
		return fmt.Errorf("load rules: %w", err)
	}

	dr, ok := resolver.(rules.DetailResolver)
	if !ok {
		return fmt.Errorf("resolver does not support detail inspection")
	}

	detail := dr.ResolveDetail(filePath)

	sourceLabel := map[string]string{
		"custom":  "Custom (--rule)",
		"project": "Project (.opencodereview/rule.json)",
		"global":  "Global (~/.opencodereview/rule.json)",
		"system":  "System built-in",
	}

	fmt.Printf("File: %s\n", filePath)
	fmt.Printf("Source: %s\n", sourceLabel[detail.Source])

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Look at the wrapped cause for the specific file/parse error.
  2. Validate your rule JSON (e.g. `jq . rule.json`) and fix syntax/schema issues.
  3. Temporarily move ~/.opencodereview/rule.json aside to see if the global layer is the culprit.
  4. Check file permissions on the rule file and repo path.

Example fix

// before
ocr rules check --rule custom.json src/a.xml   # custom.json has a trailing comma
// after
jq . custom.json   # fix the JSON error it reports
ocr rules check --rule custom.json src/a.xml
Defensive patterns

Strategy: validation

Validate before calling

for _, f := range []string{rulePath, ".opencodereview/rule.json", os.Getenv("HOME") + "/.opencodereview/rule.json"} {
    if f == "" { continue }
    if data, err := os.ReadFile(f); err == nil && !json.Valid(data) {
        return fmt.Errorf("invalid rule JSON: %s", f)
    }
}

Try / catch

resolver, _, err := rules.NewResolver(repo, rulePath, rules.ResolverOptions{})
if err != nil {
    return fmt.Errorf("load rules: %w", err) // surface wrapped cause to user
}

Prevention

When it happens

Trigger: `ocr rules check <file>` (or --repo variant) with a broken --rule JSON, a corrupt ~/.opencodereview/rule.json or .opencodereview/rule.json, or permission errors reading those files.

Common situations: Hand-edited custom rule file with invalid JSON; rule file referencing missing paths; upgrading the tool to a schema the old rule file no longer satisfies.

Related errors


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