alibaba/open-code-review · error

code_search failed: %w

Error message

code_search failed: %w

What it means

Wrapper error returned by CodeSearchProvider.Execute when the underlying git grep invocation (p.gitGrep) fails. Any error from spawning or running git grep — including timeouts, exit codes indicating no git repository, or invalid arguments — is wrapped as 'code_search failed: <cause>'. A plain 'no matches' is not an error; this only fires on real execution failures.

Source

Thrown at internal/tool/code_search.go:53

	filePatternsIface, _ := args["file_patterns"].([]any)
	var patterns []string
	for _, item := range filePatternsIface {
		if s, ok := item.(string); ok && s != "" {
			if hasTraversalPathComponent(s) {
				return "Error: file_patterns must not contain ..", nil
			}
			patterns = append(patterns, s)
		}
	}

	if strings.TrimSpace(searchText) == "" {
		return "Error: search_text is blank", nil
	}

	result, err := p.gitGrep(ctx, searchText, caseSensitive, usePerlRegexp, patterns)
	if err != nil {
		return "", fmt.Errorf("code_search failed: %w", err)
	}
	return result, nil
}

func (p *CodeSearchProvider) buildGrepArgs(searchText string, caseSensitive bool, usePerlRegexp bool, noIndex bool, pathspec []string) []string {
	cmdArgs := []string{"--no-pager", "grep"}

	if noIndex {
		// Non-git directory: search the working tree directly while still
		// honoring .gitignore and skipping .git (via --exclude-standard).
		cmdArgs = append(cmdArgs, "--no-index", "--exclude-standard")
	} else if p.FileReader.Ref == "" {
		cmdArgs = append(cmdArgs, "--untracked")
	}

	if !caseSensitive {
		cmdArgs = append(cmdArgs, "-i")
	}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Unwrap the error ('code_search failed: ...') to see the underlying git message and address it directly.
  2. Verify you are inside a git repository, or that the working tree is searchable when the --no-index fallback is used.
  3. If use_perl_regexp is true, validate the pattern is a valid PCRE, or retry with use_perl_regexp=false (fixed-string -F mode is the default).
  4. Ensure git is installed and new enough (git --version); narrow the search or add file_patterns to avoid the 10s timeout.

Example fix

// before: fails outside git repos or with bad PCRE
Execute(ctx, map[string]any{"search_text": "TODO(", "use_perl_regexp": true})
// after: fixed-string search, patterns scoped
Execute(ctx, map[string]any{"search_text": "TODO(", "file_patterns": []any{"*.go"}})
Defensive patterns

Strategy: validation

Validate before calling

if strings.TrimSpace(searchText) == "" { return errors.New("search_text required") }
if usePerlRegexp { if _, err := regexp.Compile(searchText); err != nil { return fmt.Errorf("invalid regex: %w", err) } }
if _, err := exec.LookPath("git"); err != nil { return errors.New("git not installed") }
if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil { return errors.New("not a git repository") }

Try / catch

result, err := provider.Execute(ctx, args)
if err != nil {
    var execErr *exec.ExitError
    if errors.As(err, &execErr) {
        return retryWithoutPerlRegexp(args) // fall back to -F fixed-string mode
    }
    if errors.Is(err, context.DeadlineExceeded) {
        return retryWithNarrowerPatterns(args)
    }
    return err
}

Prevention

When it happens

Trigger: Executing the code_search tool with a non-blank search_text when git grep exits non-zero: git not installed, the working directory is not a git repository and --no-index fallback also fails, the regex is invalid with use_perl_regexp=true, the command exceeds the 10s timeout, or the context is cancelled.

Common situations: Running the tool outside a git checkout (no .git directory); an old git version lacking -P (PCRE) support; a pathological pattern causing the 10-second timeout; agent passing a malformed Perl regex.

Related errors


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