alibaba/open-code-review · error

scan preview failed: %w

Error message

scan preview failed: %w

What it means

With `ocr scan --preview`, the CLI enumerates which files would be scanned (respecting template budgets like MaxFileSizeBytes) via scan.Preview, without calling the LLM. Any error from that enumeration (path resolution, git/traversal failure, IO) is wrapped as "scan preview failed: %w".

Source

Thrown at cmd/opencodereview/scan_cmd.go:278

	if state.CompletedCount() == 0 {
		return nil, fmt.Errorf("resume session %q has no completed scan items (run 'ocr session list' to see available sessions)", opts.resume)
	}
	return state, nil
}

func runScanPreview(cc *commonContext, scanTpl *template.ScanTemplate, scanPaths []string, outputFormat string, out io.Writer) error {
	preview, err := scan.Preview(context.Background(), scan.Args{
		RepoDir:          cc.RepoDir,
		Paths:            scanPaths,
		FileFilter:       cc.FileFilter,
		GitRunner:        cc.GitRunner,
		MaxFileSizeBytes: scanTpl.MaxFileSizeBytes,
		// Template's prompt fields are unused by Preview; pass the same
		// value so MaxFileSizeBytes is consistent.
		Template: *scanTpl,
	})
	if err != nil {
		return fmt.Errorf("scan preview failed: %w", err)
	}
	return outputPreview(preview, outputFormat, out)
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Check each --path exists inside the repository and is spelled correctly.
  2. Run from the repository root (or pass the right repo dir) so file traversal works.
  3. Simplify --exclude globs to rule out a bad pattern, then add them back one at a time.
  4. Read the wrapped cause — it names the failing path or git operation.

Example fix

// before
ocr scan --preview --path interal/agent
// after
ocr scan --preview --path internal/agent
Defensive patterns

Strategy: validation

Validate before calling

for _, p := range splitPaths(opts.paths) {
    if _, err := os.Stat(filepath.Join(repoDir, p)); err != nil {
        return fmt.Errorf("--path %q does not exist in repo", p)
    }
}
if _, err := exec.Command("git", "-C", repoDir, "rev-parse", "--git-dir").Output(); err != nil {
    return fmt.Errorf("not a git repository: %w", err)
}

Try / catch

if err := runScanPreview(cc, scanTpl, scanPaths, format, out); err != nil {
    log.Fatalf("scan preview failed: %v", err) // inner error names the bad path/glob
}

Prevention

When it happens

Trigger: Running `ocr scan --preview` when scan.Args enumeration fails: invalid --path entries, a broken git worktree, unreadable directories, or errors applying the template's file-size constraints.

Common situations: Passing a --path that doesn't exist or is outside the repo; running outside a git repository; permission errors on subdirectories; glob typos in --exclude.

Related errors


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