{"record":{"id":"4cb116280d850d46","repo":"sinelaw/fresh","slug":"git-grep-exited-with-code-r-exit-code-r-stderr","errorCode":null,"errorMessage":"git grep exited with code ${r.exit_code}: ${r.stderr}","messagePattern":"git grep exited with code (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/fresh-editor/plugins/live_grep.ts","lineNumber":570,"sourceCode":"    if (!gitCwd) return [] as GrepMatch[];\n    const args = [\"grep\", \"-n\", \"--column\", \"-I\"];\n    // Default git-grep is basic regex; use extended when regex is on, or\n    // fixed-strings when it's off so the query is matched literally.\n    args.push(regex === false ? \"-F\" : \"-E\");\n    if (wholeWord) args.push(\"-w\");\n    if (includeIgnored) {\n      // Widen beyond tracked files: include untracked, and stop\n      // honouring the standard ignore files so `.gitignore`d content\n      // is searched too.\n      args.push(\"--untracked\", \"--no-exclude-standard\");\n    }\n    args.push(\"-e\", query);\n    const r = await editor.spawnProcess(\"git\", args, gitCwd);\n    // git grep exits 1 when no matches — treat as empty, not error.\n    if (r.exit_code === 0 || r.exit_code === 1) {\n      return parseGrepOutput(r.stdout, maxResults, (msg) => editor.debug(msg)) as GrepMatch[];\n    }\n    throw new Error(`git grep exited with code ${r.exit_code}: ${r.stderr}`);\n  },\n});\n\nregisterProvider({\n  name: \"ack\",\n  priority: -3,\n  // Note: ack/grep are kept at lower priority than ripgrep/ag/\n  // git-grep because they're slower on large trees; the cycler\n  // skips them automatically when a faster backend is available.\n  isAvailable: async () => {\n    try {\n      const r = await editor.spawnProcess(\"ack\", [\"--version\"], editor.getCwd());\n      return r.exit_code === 0;\n    } catch {\n      return false;\n    }\n  },\n  search: async (query, { cwd, maxResults, wholeWord, regex }) => {","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/sinelaw/fresh/blob/67894ca5463dbd7a89bb31add4627c27d6b79d83/crates/fresh-editor/plugins/live_grep.ts#L552-L588","documentation":"The live_grep plugin's `git grep` provider throws this when the spawned `git grep` process exits with a code other than 0 (matches) or 1 (no matches — deliberately treated as an empty result). Any other exit code means git itself failed: not inside a git repository (exit 128), a bad pattern (exit 2), or a broken git installation. The message includes git's stderr.","triggerScenarios":"Calling live-grep with the git-grep provider when: (1) the resolved cwd (preferred cwd or active buffer dir, per gitCwdFor) is not inside a git repository, (2) the query is an invalid regex/POSIX pattern for git grep, or (3) git is not installed or the repo is corrupt.","commonSituations":"Opening a single file outside any repo and searching (monorepo edge the gitCwd fallback can't fix); searching with regex mode on and an invalid pattern; a bare/worktree layout where `git grep` is refused; missing git binary in PATH.","solutions":["Ensure the search cwd is inside a git working tree (`git rev-parse --show-toplevel` in that directory); otherwise pick the rg/ag/grep provider instead.","Fix the query pattern if regex mode is enabled.","Run `git grep -n -e <query>` manually in the target cwd to read the exact stderr.","Verify git is installed and the repository is not corrupt (`git status`)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import { execSync } from \"child_process\";\nfunction insideGitRepo(cwd: string): boolean {\n  try { execSync(\"git rev-parse --is-inside-work-tree\", { cwd, stdio: \"ignore\" }); return true; } catch { return false; }\n}\n// skip the git-grep provider when insideGitRepo(gitCwd) is false","typeGuard":"function isRepoCheck(r) { return r.exit_code === 0 || r.exit_code === 1; } // exit 128 = not a repo","tryCatchPattern":"try {\n  results = await gitGrepProvider.search(query, opts);\n} catch (e) {\n  if (/exited with code 128/.test(String(e))) {\n    results = await rgProvider.search(query, opts); // not a git repo — use another backend\n  } else { throw e; }\n}","preventionTips":["Only enable the git-grep scope when the cwd or open file is inside a repository.","Check `git rev-parse --is-inside-work-tree` in the intended cwd before searching.","Validate regex patterns in regex mode.","Ensure git is installed in the editor's environment."],"tags":["git","git-grep","external-process","search"],"backgroundTag":"git-command-failed","analyzedSha":"67894ca5463dbd7a89bb31add4627c27d6b79d83","analyzedAt":"2026-09-13T15:04:03.701Z","contentChangedAt":"2026-09-13T15:04:03.701Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}