alibaba/open-code-review · error

stat

Error message

stat %s: %w

What it means

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.

Solutions

  1. Verify the path exists: run `ls <path>` (or `test -d <path>`) before invoking ocr
  2. Fix typos or stale paths in scripts/CI config; confirm the checkout/volume mount ran
  3. Check permissions on every path component; use a directory the current user can traverse

Example fix

// before (CI script)
ocr review --dir $WORKSPACE/src
// after
if [ ! -d "$WORKSPACE/src" ]; then echo "directory missing"; exit 1; fi
ocr review --dir "$WORKSPACE/src"
Defensive patterns

Strategy: validation

Validate before calling

info, err := os.Stat(dir)
if err != nil {
	return fmt.Errorf("directory %s not accessible: %w", dir, err)
}
if !info.IsDir() {
	return fmt.Errorf("%s is not a directory", dir)
}

Try / catch

var pe *fs.PathError
if errors.As(err, &pe) {
	switch {
	case errors.Is(pe.Err, syscall.ENOENT): // path does not exist
	case errors.Is(pe.Err, syscall.EACCES): // permission denied
	}
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/03b51892c5c6605e. Report an issue: GitHub.

Appendix: source

Thrown at cmd/opencodereview/shared.go:146

}

// 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 those
	// root-relative paths resolve for both disk reads and git-show reads.
	// requireGit is true only for the review path; scan (requireGit=false) keeps
	// the CWD so its `git ls-files` walk stays scoped to the subdirectory.
	if isGit && requireGit {
		// runGitCmdStdout captures stdout only so git stderr notices can't
		// pollute the resolved path. --show-toplevel fails (or is empty) when
		// there is no work tree — e.g. a bare repo, where --git-dir succeeds so
		// isGit is true. Fail loudly there instead of silently reusing the
		// subdir, which would reproduce the #287 root-relative-path bug.

View on GitHub (pinned to 5cf97d0d15)