Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/services/ripgrep/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
import * as path from "path" // kilocode_change
import { getBinPath, truncateLine } from "../index" // kilocode_change
import { fileExistsAtPath } from "../../../utils/fs" // kilocode_change
import * as childProcess from "child_process" // kilocode_change
// kilocode_change start
vi.mock("vscode", () => ({
env: {
appRoot: "/mock/vscode/app/root",
},
}))
vi.mock("../../../utils/fs", () => ({
fileExistsAtPath: vi.fn(),
}))
vi.mock("child_process", () => ({
execSync: vi.fn(),
}))
// kilocode_change end
describe("Ripgrep line truncation", () => {
// The default MAX_LINE_LENGTH is 500 in the implementation
Expand Down Expand Up @@ -115,16 +124,38 @@ describe("getBinPath", () => {

it("should return undefined when ripgrep is not found anywhere", async () => {
const vscodeAppRoot = "/path/to/nonexistent"
const mockExecSync = childProcess.execSync as ReturnType<typeof vi.fn>

// Mock all paths not existing
mockFileExists.mockResolvedValue(false)
// Mock which/where failing (binary not in PATH)
mockExecSync.mockImplementation(() => {
throw new Error("not found")
})

const result = await getBinPath(vscodeAppRoot)

// Should return undefined when no paths exist and require.resolve fails
expect(result).toBeUndefined()
})

it("should find ripgrep in system PATH when bundled paths fail", async () => {
const vscodeAppRoot = "/path/to/nonexistent"
const systemRgPath = "/usr/bin/rg"
const mockExecSync = childProcess.execSync as ReturnType<typeof vi.fn>

// Mock bundled paths not existing, but system path exists
mockFileExists.mockImplementation(async (filePath: string) => {
return filePath === systemRgPath
})
// Mock which returning the system rg path
mockExecSync.mockReturnValue(systemRgPath + "\n")

const result = await getBinPath(vscodeAppRoot)

expect(result).toBe(systemRgPath)
})

it("should prioritize traditional paths over require.resolve", async () => {
const vscodeAppRoot = "/path/to/vscode"
const traditionalPath = path.join(vscodeAppRoot, "node_modules/@vscode/ripgrep/bin/", binName)
Expand Down
20 changes: 20 additions & 0 deletions src/services/ripgrep/index.kilocode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from "child_process"
import path from "path"
import { fileExistsAtPath } from "../../utils/fs"

Expand All @@ -16,3 +17,22 @@ export async function checkBunPath(vscodeAppRoot: string, binName: string) {

return undefined
}

export async function checkSystemPath(binName: string): Promise<string | undefined> {
// Try to find rg in system PATH using which/where
const command = process.platform === "win32" ? "where" : "which"
try {
const result = execSync(`${command} ${binName}`, {
encoding: "utf8",
stdio: ["pipe", "pipe", "pipe"],
}).trim()
// which/where may return multiple lines on Windows, take the first
const firstPath = result.split("\n")[0].trim()
if (firstPath && (await fileExistsAtPath(firstPath))) {
return firstPath
}
} catch {
// Command failed, binary not in PATH
}
return undefined
}
5 changes: 3 additions & 2 deletions src/services/ripgrep/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as vscode from "vscode"

import { RooIgnoreController } from "../../core/ignore/RooIgnoreController"
import { fileExistsAtPath } from "../../utils/fs"
import { checkBunPath } from "./index.kilocode" // kilocode_change
import { checkBunPath, checkSystemPath } from "./index.kilocode" // kilocode_change
/*
This file provides functionality to perform regex searches on files using ripgrep.
Inspired by: https://github.com/DiscreteTom/vscode-ripgrep-utils
Expand Down Expand Up @@ -94,7 +94,8 @@ export async function getBinPath(vscodeAppRoot: string): Promise<string | undefi
(await checkPath("node_modules/vscode-ripgrep/bin")) ||
(await checkPath("node_modules.asar.unpacked/vscode-ripgrep/bin/")) ||
(await checkPath("node_modules.asar.unpacked/@vscode/ripgrep/bin/")) ||
(await checkBunPath(vscodeAppRoot, binName)) // kilocode_change
(await checkBunPath(vscodeAppRoot, binName)) ||
(await checkSystemPath(binName)) // kilocode_change: fallback to system PATH for CLI
)
}

Expand Down