{"record":{"id":"06569fe529b2acc5","repo":"alibaba/open-code-review","slug":"background-file-q-is-a-directory-not-a-file","errorCode":null,"errorMessage":"background file %q is a directory, not a file","messagePattern":"background file %q is a directory, not a file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/background_file.go","lineNumber":72,"sourceCode":"\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 == \"\" {\n\t\treturn \"\", fmt.Errorf(\"background file %q is empty after sanitisation\", path)\n\t}\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/background_file.go#L54-L90","documentation":"loadBackgroundFile stat'ed the path successfully but info.IsDir() is true, so it rejects directories explicitly — the background must be a readable file of prose, not a directory. Fail-fast before attempting os.ReadFile which would return a confusing EISDIR error.","triggerScenarios":"Passing a directory path to the background file option, e.g. --background ./docs or --background . — a path that exists but resolves to a directory.","commonSituations":"Intending to load all files in a folder but supplying the folder itself; shell completion filling in a directory; a trailing-slash path that exists as a directory; expecting recursive directory concatenation which the feature does not support.","solutions":["Point --background at a single Markdown/text file inside that directory (e.g. ./docs/background.md).","Concatenate the directory's files into one file first if you need folder-level background content.","Check the path for typos or a trailing component that makes it a directory (e.g. mkdir'd file name).","Use ls -la <path> to confirm it is a regular file, not a directory."],"exampleFix":"// before\nocr review --background ./docs\n// after\nocr review --background ./docs/project-context.md","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst st = fs.statSync(process.argv[2]);\nif (st.isDirectory()) throw new Error('background must be a single file, not a directory');","typeGuard":"func isRegularFile(path string) bool {\n    info, err := os.Stat(path)\n    return err == nil && info.Mode().IsRegular() && !info.IsDir()\n}","tryCatchPattern":"bg, err := loadBackgroundFile(path)\nif err != nil && strings.Contains(err.Error(), \"is a directory\") {\n    return fmt.Errorf(\"--background requires a file; concatenate the directory's files first\")\n}","preventionTips":["Pass a single Markdown/text file, never a directory — the feature does not concatenate folders","Watch shell completion: it happily fills in directories","Concatenate folder contents into one curated file beforehand","Sanity-check with test -f <path> in scripts before invoking ocr"],"tags":["cli","filesystem","validation","go"],"backgroundTag":"background-file-is-directory","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}