{"record":{"id":"03b51892c5c6605e","repo":"alibaba/open-code-review","slug":"stat-s-w","errorCode":null,"errorMessage":"stat %s: %w","messagePattern":"stat (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/shared.go","lineNumber":146,"sourceCode":"}\n\n// resolveWorkingDir returns (absPath, isGitRepo, err). When requireGit is\n// true, returns an error if the directory is not a git repo. When false,\n// returns IsGitRepo=false instead of erroring (scan path uses this).\nfunc resolveWorkingDir(input string, requireGit bool) (string, bool, error) {\n\tif input == \"\" {\n\t\twd, err := os.Getwd()\n\t\tif err != nil {\n\t\t\treturn \"\", false, fmt.Errorf(\"get working directory: %w\", err)\n\t\t}\n\t\tinput = wd\n\t}\n\tabsPath, err := filepath.Abs(input)\n\tif err != nil {\n\t\treturn \"\", false, fmt.Errorf(\"resolve absolute path: %w\", err)\n\t}\n\tif _, statErr := os.Stat(absPath); statErr != nil {\n\t\treturn \"\", false, fmt.Errorf(\"stat %s: %w\", absPath, statErr)\n\t}\n\tout, err := runGitCmd(absPath, \"rev-parse\", \"--git-dir\")\n\tisGit := err == nil && len(out) > 0\n\tif !isGit && requireGit {\n\t\treturn \"\", false, fmt.Errorf(\"%s is not a git repository\", absPath)\n\t}\n\t// #287: git reports diff and `git show HEAD:<path>` paths relative to the\n\t// repository root, not the current directory. When `ocr review` runs from a\n\t// subdirectory of a monorepo, anchor RepoDir at the git top-level so those\n\t// root-relative paths resolve for both disk reads and git-show reads.\n\t// requireGit is true only for the review path; scan (requireGit=false) keeps\n\t// the CWD so its `git ls-files` walk stays scoped to the subdirectory.\n\tif isGit && requireGit {\n\t\t// runGitCmdStdout captures stdout only so git stderr notices can't\n\t\t// pollute the resolved path. --show-toplevel fails (or is empty) when\n\t\t// there is no work tree — e.g. a bare repo, where --git-dir succeeds so\n\t\t// isGit is true. Fail loudly there instead of silently reusing the\n\t\t// subdir, which would reproduce the #287 root-relative-path bug.","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/shared.go#L128-L164","documentation":"After computing the absolute path, resolveWorkingDir stats it and wraps any os.Stat failure as `stat %s: %w`. It fires when the given directory does not exist, is unreachable, or the path component is not searchable. The wrapped syscall error (ENOENT, EACCES, ENOTDIR) says which.","triggerScenarios":"ocr review/scan run with a --dir path that does not exist, contains a typo, points through a broken symlink, or traverses a directory the user cannot enter.","commonSituations":"Typo in the path; repository checked out at a different location than the script assumes; CI checkout step skipped; running in a container without the volume mounted; permission-restricted directories.","solutions":["Verify the path exists: run `ls <path>` (or `test -d <path>`) before invoking ocr","Fix typos or stale paths in scripts/CI config; confirm the checkout/volume mount ran","Check permissions on every path component; use a directory the current user can traverse"],"exampleFix":"// before (CI script)\nocr review --dir $WORKSPACE/src\n// after\nif [ ! -d \"$WORKSPACE/src\" ]; then echo \"directory missing\"; exit 1; fi\nocr review --dir \"$WORKSPACE/src\"","handlingStrategy":"validation","validationCode":"info, err := os.Stat(dir)\nif err != nil {\n\treturn fmt.Errorf(\"directory %s not accessible: %w\", dir, err)\n}\nif !info.IsDir() {\n\treturn fmt.Errorf(\"%s is not a directory\", dir)\n}","typeGuard":null,"tryCatchPattern":"var pe *fs.PathError\nif errors.As(err, &pe) {\n\tswitch {\n\tcase errors.Is(pe.Err, syscall.ENOENT): // path does not exist\n\tcase errors.Is(pe.Err, syscall.EACCES): // permission denied\n\t}\n}","preventionTips":["test -d the directory before running ocr in scripts","Pin repository paths in CI config and verify the checkout step succeeded","Check mount/volume status in containers before the review step"],"tags":["go","filesystem","path-validation"],"backgroundTag":"no-such-file-or-directory","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}