{"record":{"id":"582f71c55080473f","repo":"alibaba/open-code-review","slug":"read-background-file-q-w","errorCode":null,"errorMessage":"read background file %q: %w","messagePattern":"read background file %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/background_file.go","lineNumber":69,"sourceCode":"\tif backgroundFile != \"\" {\n\t\tfileBg, err := loadBackgroundFile(resolveBackgroundFilePath(repoDir, backgroundFile))\n\t\tif err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\t\treturn selectBackground(inline, fileBg), nil\n\t}\n\tif inline == \"\" && commit != \"\" {\n\t\tif msg, err := getCommitMessage(repoDir, commit); err == nil && msg != \"\" {\n\t\t\treturn msg, nil\n\t\t}\n\t}\n\treturn inline, nil\n}\n\nfunc loadBackgroundFile(path string) (string, error) {\n\tinfo, err := os.Stat(path)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"read background file %q: %w\", path, err)\n\t}\n\tif info.IsDir() {\n\t\treturn \"\", fmt.Errorf(\"background file %q is a directory, not a file\", path)\n\t}\n\tif info.Size() > maxBackgroundFileBytes {\n\t\treturn \"\", fmt.Errorf(\n\t\t\t\"background file %q is %d bytes, exceeding the maximum of %d bytes; please provide a smaller file\",\n\t\t\tpath, info.Size(), maxBackgroundFileBytes,\n\t\t)\n\t}\n\n\traw, err := os.ReadFile(path)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"read background file %q: %w\", path, err)\n\t}\n\n\tcleaned := sanitizeMarkdown(string(raw))\n\tif cleaned == \"\" {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/background_file.go#L51-L87","documentation":"loadBackgroundFile fails to os.Stat the --background file and wraps the OS error. The path could not be read at all — most often it does not exist, but the wrapped error distinguishes permission and path-syntax issues too.","triggerScenarios":"Calling resolveBackground where the user-supplied background file path fails os.Stat: nonexistent file, wrong relative directory, typo, or no read permission on a parent directory.","commonSituations":"Relative path assuming repo root as CWD but running from elsewhere; file deleted or renamed; Windows vs Unix path separators in scripts; symlink to a missing target.","solutions":["Check the path is correct and relative to the directory you run ocr from (see TestLoadBackgroundFileRelativeToRepo — paths resolve against the repo).","Run ls <path> / os.Stat manually to see the underlying error (ENOENT vs EACCES).","Use an absolute path to eliminate CWD ambiguity.","Verify parent-directory read/execute permissions if the file itself looks fine."],"exampleFix":"// before\nocr review --background ./notes.md   # run from home dir, notes.md is in repo\n// after\ncd /path/to/repo && ocr review --background ./notes.md\n// or use an absolute path:\nocr review --background /path/to/repo/notes.md","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst path = process.argv[2];\nif (!fs.existsSync(path) || !fs.statSync(path).isFile()) {\n  throw new Error(`background file not found: ${path} (resolve against your repo/CWD)`);\n}","typeGuard":"func isReadableFile(path string) bool {\n    info, err := os.Stat(path)\n    return err == nil && info.Mode().IsRegular()\n}","tryCatchPattern":"bg, err := loadBackgroundFile(path)\nif err != nil {\n    return fmt.Errorf(\"loading --background: %w\", err) // wraps the stat error for user display\n}","preventionTips":["Run ocr from the repo root or use absolute paths for --background","Stat the file yourself before invoking to catch ENOENT/EACCES early","Avoid symlinks whose targets may be missing","Note this message appears for both stat and read failures — check the wrapped OS error to distinguish"],"tags":["cli","filesystem","go"],"backgroundTag":"background-file-not-found","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}