{"record":{"id":"45f4fa2393df52e6","repo":"alibaba/open-code-review","slug":"unmarshal-global-rule-w","errorCode":null,"errorMessage":"unmarshal global rule: %w","messagePattern":"unmarshal global rule: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/config/rules/system_rules.go","lineNumber":387,"sourceCode":"\treturn nil\n}\n\nfunc loadGlobalRule() (*ProjectRule, error) {\n\thome, err := os.UserHomeDir()\n\tif err != nil {\n\t\treturn nil, nil\n\t}\n\tpath := filepath.Join(home, \".opencodereview\", \"rule.json\")\n\tdata, err := os.ReadFile(path)\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, fmt.Errorf(\"read global rule %s: %w\", path, err)\n\t}\n\tvar pr ProjectRule\n\tif err := json.Unmarshal(data, &pr); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal global rule: %w\", err)\n\t}\n\tresolveRuleEntries(pr.Rules, filepath.Dir(path), \"\")\n\treturn &pr, nil\n}\n\nfunc loadRuleFile(path string) (*ProjectRule, error) {\n\tdata, err := os.ReadFile(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read rule file %s: %w\", path, err)\n\t}\n\tvar pr ProjectRule\n\tif err := json.Unmarshal(data, &pr); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal rule file %s: %w\", path, err)\n\t}\n\tresolveRuleEntries(pr.Rules, filepath.Dir(path), \"\")\n\treturn &pr, nil\n}\n","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/internal/config/rules/system_rules.go#L369-L405","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Validate the file: jq . ~/.opencodereview/rule.json and fix the reported syntax error","Ensure the top-level shape matches ProjectRule (an object with a rules field, not an array)","If the file is unneeded, delete or rename it — a missing file is treated as 'no global rule'"],"exampleFix":"// before (truncated)\n{\"rules\": [{\"pattern\": \"**/*.go\", \"rule\":\n// after\n{\"rules\": [{\"pattern\": \"**/*.go\", \"rule\": \"go.md\"}]}","handlingStrategy":"validation","validationCode":"p := filepath.Join(home, \".opencodereview\", \"rule.json\")\nif data, err := os.ReadFile(p); err == nil {\n\tvar v any\n\tif err := json.Unmarshal(data, &v); err != nil {\n\t\treturn fmt.Errorf(\"global rule %s is not valid JSON: %v\", p, err)\n\t}\n}","typeGuard":null,"tryCatchPattern":"pr, err := loadGlobalRule()\nif err != nil {\n\tif strings.Contains(err.Error(), \"unmarshal global rule\") {\n\t\tlog.Printf(\"warning: ~/.opencodereview/rule.json is invalid (%v) — ignoring global rules\", err)\n\t\treturn nil // degrade to project/system rules only\n\t}\n\treturn err\n}","preventionTips":["Run jq . ~/.opencodereview/rule.json after every manual edit","Save as strict JSON — no comments, no YAML in disguise, no BOM","Back up a known-good copy before hand-editing the global config"],"tags":["json","config","unmarshal"],"backgroundTag":"json-parse-error","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}