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
- Look at the wrapped cause for the specific file/parse error.
- Validate your rule JSON (e.g. `jq . rule.json`) and fix syntax/schema issues.
- Temporarily move ~/.opencodereview/rule.json aside to see if the global layer is the culprit.
- 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
- Validate rule JSON with `jq .` or a schema check after every edit.
- Keep custom rule files under version control so bad edits are revertible.
- Check permissions on global/project rule files before running in CI.
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
- parse config: %w
- parse config: %w
- parse app config: %w
- invalid JSON for llm.extra_body: %w
- invalid JSON array for %s: %w
AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02).
Data as JSON: /api/errors/f4ff445d4a1dde36.
Report an issue: GitHub.