alibaba/open-code-review · error

preview failed: %w

Error message

preview failed: %w

What it means

executeDelegatePreview calls dc.preview(ctx) to compute the delegate review preview (merge-base, diff scope, etc.) and wraps any failure as "preview failed: <cause>". It means the underlying git/preview computation errored; the real cause is preserved by %w.

Source

Thrown at cmd/opencodereview/delegate_cmd.go:166

		return "workspace"
	}
}

// resolver returns the rules Resolver, asserting DetailResolver if available.
func (dc *delegateContext) resolver() rules.Resolver {
	return dc.cc.Resolver
}

func executeDelegatePreview(opts delegateOptions) error {
	dc, err := loadDelegateContext(opts)
	if err != nil {
		return err
	}

	ctx := context.Background()
	preview, err := dc.preview(ctx)
	if err != nil {
		return fmt.Errorf("preview failed: %w", err)
	}
	mergeBase := dc.mergeBase(ctx)
	if opts.format == "json" {
		return writeDelegateJSON(delegatePreviewJSON{
			SchemaVersion:   delegateSchemaVersion,
			Mode:            dc.reviewMode(),
			Repository:      dc.cc.RepoDir,
			From:            dc.opts.from,
			To:              dc.opts.to,
			Commit:          dc.opts.commit,
			MergeBase:       mergeBase,
			Background:      dc.opts.background,
			TotalFiles:      preview.TotalFiles,
			ReviewableCount: preview.ReviewableCount,
			ExcludedCount:   preview.ExcludedCount,
			TotalInsertions: preview.TotalInsertions,
			TotalDeletions:  preview.TotalDeletions,
			ReviewableFiles: previewFiles(preview, true),

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Read the wrapped cause after `preview failed:` — it names the underlying git error
  2. Run the command inside a git repository with at least one commit on both sides of the merge base
  3. Fetch/unshallow the repo if the merge base commit is missing
  4. Verify `git log`, `git merge-base` work manually in the same directory

Example fix

// before (empty repo)
$ ocr delegate --preview
preview failed: exit status 128 (bad revision 'HEAD')
// after
$ git commit --allow-empty -m init && ocr delegate --preview
Defensive patterns

Strategy: try-catch

Validate before calling

func repoHasCommits(dir string) bool {
    return runGitCmd(dir, "rev-parse", "--verify", "HEAD") == nil
}

Try / catch

preview, err := dc.preview(ctx)
if err != nil {
    if errors.Is(err, exec.ErrNotFound) { /* git missing */ }
    var ee *exec.ExitError
    if errors.As(err, &ee) { log.Fatalf("preview failed: %s", ee.Stderr) }
    return fmt.Errorf("preview failed: %w", err)
}

Prevention

When it happens

Trigger: Running a delegate command in preview mode outside a git repository, with no commits reachable from the merge base, on an unborn branch (no HEAD), or with a corrupted git index.

Common situations: Running the command in a freshly `git init`-ed repo with no commits; detached/empty branch; git binary missing or misconfigured; shallow clone lacking the merge base.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


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