alibaba/open-code-review · error
get working directory: %w
Error message
get working directory: %w
What it means
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.
Source
Thrown at cmd/opencodereview/shared.go:137
return &commonContext{
Template: tpl,
RepoDir: repoDir,
Resolver: resolver,
FileFilter: fileFilter,
GitRunner: gitRunner,
IsGitRepo: isGit,
}, nil
}
// resolveWorkingDir returns (absPath, isGitRepo, err). When requireGit is
// true, returns an error if the directory is not a git repo. When false,
// returns IsGitRepo=false instead of erroring (scan path uses this).
func resolveWorkingDir(input string, requireGit bool) (string, bool, error) {
if input == "" {
wd, err := os.Getwd()
if err != nil {
return "", false, fmt.Errorf("get working directory: %w", err)
}
input = wd
}
absPath, err := filepath.Abs(input)
if err != nil {
return "", false, fmt.Errorf("resolve absolute path: %w", err)
}
if _, statErr := os.Stat(absPath); statErr != nil {
return "", false, fmt.Errorf("stat %s: %w", absPath, statErr)
}
out, err := runGitCmd(absPath, "rev-parse", "--git-dir")
isGit := err == nil && len(out) > 0
if !isGit && requireGit {
return "", false, fmt.Errorf("%s is not a git repository", absPath)
}
// #287: git reports diff and `git show HEAD:<path>` paths relative to the
// repository root, not the current directory. When `ocr review` runs from a
// subdirectory of a monorepo, anchor RepoDir at the git top-level so thoseView on GitHub (pinned to 5cf97d0d15)
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)
Example fix
// before cd /tmp/build && rm -rf /tmp/build && ocr review // after cd /tmp/build && rm -rf /tmp/build && cd /tmp && ocr review --dir /tmp/myrepo
Defensive patterns
Strategy: validation
Validate before calling
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("current directory unusable: %w", err)
}
// proceed with explicit --dir Try / catch
if err != nil {
var pe *fs.PathError
if errors.As(err, &pe) && errors.Is(pe.Err, syscall.ENOENT) {
// cwd was deleted; chdir to a safe dir and retry
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- read background file %q: %w
- background file %q is a directory, not a file
- list sessions: %w
- resolve absolute path: %w
- "%q %s" (+ positional signature, valid values, usage line, e
AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02).
Data as JSON: /api/errors/18d3c8eb1d1915b9.
Report an issue: GitHub.