{"record":{"id":"18d3c8eb1d1915b9","repo":"alibaba/open-code-review","slug":"get-working-directory-w","errorCode":null,"errorMessage":"get working directory: %w","messagePattern":"get working directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/shared.go","lineNumber":137,"sourceCode":"\n\treturn &commonContext{\n\t\tTemplate:   tpl,\n\t\tRepoDir:    repoDir,\n\t\tResolver:   resolver,\n\t\tFileFilter: fileFilter,\n\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","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/shared.go#L119-L155","documentation":"resolveWorkingDir wraps os.Getwd() failure with this message when the user passed no --dir/input path and the process working directory cannot be determined. This is nearly impossible in normal use and typically indicates the current directory was deleted while the process ran, or a permission problem on the cwd. The wrapped os error names the root cause.","triggerScenarios":"Calling `ocr review` (or scan/session paths via resolveRepoDir, resolveWorkingDirForSession, loadCommonContext) with an empty directory argument while os.Getwd() fails — e.g. the shell's cwd was removed (rm -rf of the directory you are standing in) or cwd lacks read/execute permission.","commonSituations":"Running ocr from a terminal whose current directory was deleted by another process; container or CI workspaces cleaned up mid-run; NFS/automount directories that vanished.","solutions":["cd into an existing directory (or re-open the shell) before running ocr","Pass an explicit existing directory via the --dir/input flag instead of relying on the cwd","Check filesystem/mount health if the cwd genuinely still exists (dmesg, mount status)"],"exampleFix":"// before\ncd /tmp/build && rm -rf /tmp/build && ocr review\n// after\ncd /tmp/build && rm -rf /tmp/build && cd /tmp && ocr review --dir /tmp/myrepo","handlingStrategy":"validation","validationCode":"wd, err := os.Getwd()\nif err != nil {\n\treturn fmt.Errorf(\"current directory unusable: %w\", err)\n}\n// proceed with explicit --dir","typeGuard":null,"tryCatchPattern":"if err != nil {\n\tvar pe *fs.PathError\n\tif errors.As(err, &pe) && errors.Is(pe.Err, syscall.ENOENT) {\n\t\t// cwd was deleted; chdir to a safe dir and retry\n\t}\n\treturn err\n}","preventionTips":["Always pass an explicit --dir path in scripts and CI","Re-open the shell after deleting the directory you were working in","Check os.Getwd() early in wrapper scripts"],"tags":["go","filesystem","cli"],"backgroundTag":"working-directory-missing","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}