alibaba/open-code-review · error

preview failed: %w

Error message

preview failed: %w

What it means

runPreviewContext calls agent.Preview to compute the diff/file set that would be reviewed. Any failure inside Preview (git diff errors, invalid refs, filter problems) is wrapped as "preview failed". The preview never renders; the underlying cause carries the real diagnosis.

Source

Thrown at cmd/opencodereview/review_cmd.go:498

				return fmt.Errorf("%s value %q is not a valid commit ref: %s", item.flag, item.ref, msg)
			}
			return fmt.Errorf("%s value %q is not a valid commit ref", item.flag, item.ref)
		}
	}
	return nil
}

func runPreviewContext(ctx context.Context, cc *commonContext, opts reviewOptions, out io.Writer) error {
	preview, err := agent.Preview(ctx, agent.Args{
		RepoDir:    cc.RepoDir,
		From:       opts.from,
		To:         opts.to,
		Commit:     opts.commit,
		FileFilter: cc.FileFilter,
		GitRunner:  cc.GitRunner,
	})
	if err != nil {
		return fmt.Errorf("preview failed: %w", err)
	}

	return outputPreview(preview, opts.outputFormat, out)
}

func initMCPClients(ctx context.Context, cfg *Config, tools *tool.Registry, repoDir, version string) []*mcp.Client {
	if cfg == nil || len(cfg.MCPServers) == 0 {
		return nil
	}

	mcpNames := make([]string, 0, len(cfg.MCPServers))
	for name := range cfg.MCPServers {
		mcpNames = append(mcpNames, name)
	}
	sort.Strings(mcpNames)

	var clients []*mcp.Client
	for _, name := range mcpNames {

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Check the wrapped %w cause — fix the git/ref issue it names.
  2. Verify both refs with `git rev-parse --verify <ref>^{commit}` before previewing.
  3. Run with a plain range (e.g. --from origin/main --to HEAD) to isolate which ref is bad.

Example fix

// before
ocr review --preview --from v1 --to v2   # v1 not a commit
// after
git rev-parse --verify v1^{commit} && ocr review --preview --from v1 --to v2
Defensive patterns

Strategy: try-catch

Validate before calling

for _, r := range []string{from, to, commit} {
    if r == "" { continue }
    if err := exec.Command("git", "rev-parse", "--verify", r+"^{commit}").Run(); err != nil {
        return fmt.Errorf("preview ref %q invalid", r)
    }
}

Try / catch

if err := runPreviewContext(ctx, cc, opts, out); err != nil {
    var perr *PreviewError
    if errors.As(err, &perr) { /* inspect wrapped git cause */ }
    return fmt.Errorf("preview: %w", err)
}

Prevention

When it happens

Trigger: `ocr review --preview` (or runPreview) with refs that don't resolve, an empty diff context, git errors, or a failing custom GitRunner.

Common situations: --from/--to pointing at non-existent refs; previewing in a non-git directory that slipped past earlier checks; pathological file filters matching nothing combined with ref errors.

Related errors


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