{"record":{"id":"b0661fb74aa9c692","repo":"alibaba/open-code-review","slug":"code-search-failed-w","errorCode":null,"errorMessage":"code_search failed: %w","messagePattern":"code_search failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tool/code_search.go","lineNumber":53,"sourceCode":"\n\tfilePatternsIface, _ := args[\"file_patterns\"].([]any)\n\tvar patterns []string\n\tfor _, item := range filePatternsIface {\n\t\tif s, ok := item.(string); ok && s != \"\" {\n\t\t\tif hasTraversalPathComponent(s) {\n\t\t\t\treturn \"Error: file_patterns must not contain ..\", nil\n\t\t\t}\n\t\t\tpatterns = append(patterns, s)\n\t\t}\n\t}\n\n\tif strings.TrimSpace(searchText) == \"\" {\n\t\treturn \"Error: search_text is blank\", nil\n\t}\n\n\tresult, err := p.gitGrep(ctx, searchText, caseSensitive, usePerlRegexp, patterns)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"code_search failed: %w\", err)\n\t}\n\treturn result, nil\n}\n\nfunc (p *CodeSearchProvider) buildGrepArgs(searchText string, caseSensitive bool, usePerlRegexp bool, noIndex bool, pathspec []string) []string {\n\tcmdArgs := []string{\"--no-pager\", \"grep\"}\n\n\tif noIndex {\n\t\t// Non-git directory: search the working tree directly while still\n\t\t// honoring .gitignore and skipping .git (via --exclude-standard).\n\t\tcmdArgs = append(cmdArgs, \"--no-index\", \"--exclude-standard\")\n\t} else if p.FileReader.Ref == \"\" {\n\t\tcmdArgs = append(cmdArgs, \"--untracked\")\n\t}\n\n\tif !caseSensitive {\n\t\tcmdArgs = append(cmdArgs, \"-i\")\n\t}","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/internal/tool/code_search.go#L35-L71","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Unwrap the error ('code_search failed: ...') to see the underlying git message and address it directly.","Verify you are inside a git repository, or that the working tree is searchable when the --no-index fallback is used.","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).","Ensure git is installed and new enough (git --version); narrow the search or add file_patterns to avoid the 10s timeout."],"exampleFix":"// before: fails outside git repos or with bad PCRE\nExecute(ctx, map[string]any{\"search_text\": \"TODO(\", \"use_perl_regexp\": true})\n// after: fixed-string search, patterns scoped\nExecute(ctx, map[string]any{\"search_text\": \"TODO(\", \"file_patterns\": []any{\"*.go\"}})","handlingStrategy":"validation","validationCode":"if strings.TrimSpace(searchText) == \"\" { return errors.New(\"search_text required\") }\nif usePerlRegexp { if _, err := regexp.Compile(searchText); err != nil { return fmt.Errorf(\"invalid regex: %w\", err) } }\nif _, err := exec.LookPath(\"git\"); err != nil { return errors.New(\"git not installed\") }\nif _, err := os.Stat(filepath.Join(dir, \".git\")); err != nil { return errors.New(\"not a git repository\") }","typeGuard":null,"tryCatchPattern":"result, err := provider.Execute(ctx, args)\nif err != nil {\n    var execErr *exec.ExitError\n    if errors.As(err, &execErr) {\n        return retryWithoutPerlRegexp(args) // fall back to -F fixed-string mode\n    }\n    if errors.Is(err, context.DeadlineExceeded) {\n        return retryWithNarrowerPatterns(args)\n    }\n    return err\n}","preventionTips":["Run the search inside a git repository or rely on the --no-index fallback knowingly.","Prefer use_perl_regexp=false unless PCRE features are truly needed.","Add file_patterns to narrow the search and stay under the 10s timeout.","Verify git is installed and reasonably recent (git --version >= 2.x)."],"tags":["search","git","grep","tool-execution"],"backgroundTag":"git-grep-execution-failed","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}