{"record":{"id":"a917da9b3b5e405a","repo":"alibaba/open-code-review","slug":"resolve-absolute-path-w","errorCode":null,"errorMessage":"resolve absolute path: %w","messagePattern":"resolve absolute path: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/shared.go","lineNumber":143,"sourceCode":"\t\tGitRunner:  gitRunner,\n\t\tIsGitRepo:  isGit,\n\t}, nil\n}\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","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/shared.go#L125-L161","documentation":"resolveWorkingDir calls filepath.Abs on the user-supplied directory (or the resolved cwd) and wraps a failure with this message. filepath.Abs only fails when Getwd fails on a relative input, so this is rare and points at an unusable process cwd rather than a bad path string. The wrapped error identifies the cause.","triggerScenarios":"ocr review/scan invoked with a relative --dir path while os.Getwd() fails (deleted cwd, permission loss), causing filepath.Abs(input) to return an error.","commonSituations":"Same as getwd failures: deleted current directory in CI, stale shell after workspace cleanup, containers with unmounted cwd.","solutions":["Pass an absolute path to the command instead of a relative one","Restore a valid working directory (cd to an existing path) before running","If Getwd keeps failing, check for permission or mount problems on the cwd"],"exampleFix":"// before\nocr review --dir ../myrepo   # fails when cwd is gone\n// after\nocr review --dir /abs/path/to/myrepo","handlingStrategy":"validation","validationCode":"if !filepath.IsAbs(dir) {\n\tdir, err = filepath.Abs(dir)\n\tif err != nil {\n\t\treturn err\n\t}\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n\tlog.Printf(\"could not resolve path %q: %v\", dir, err)\n\treturn err\n}","preventionTips":["Use absolute paths in scripts and CI pipelines","Avoid running tools from directories subject to cleanup","Validate inputs before invoking the CLI"],"tags":["go","filesystem","cli"],"backgroundTag":"working-directory-missing","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}