From bc558e5c5baa0018b88d9739772867399803cd48 Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Mon, 1 Dec 2025 22:29:05 -0800 Subject: [PATCH] fix: add rg to path (#146) --- src/tools/impl/Bash.ts | 5 +++-- src/tools/impl/Shell.ts | 3 ++- src/tools/impl/shellEnv.ts | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/tools/impl/shellEnv.ts diff --git a/src/tools/impl/Bash.ts b/src/tools/impl/Bash.ts index 40d269e..5a91d1c 100644 --- a/src/tools/impl/Bash.ts +++ b/src/tools/impl/Bash.ts @@ -2,6 +2,7 @@ import type { ExecOptions } from "node:child_process"; import { exec, spawn } from "node:child_process"; import { promisify } from "node:util"; import { backgroundProcesses, getNextBashId } from "./process_manager.js"; +import { getShellEnv } from "./shellEnv.js"; import { LIMITS, truncateByChars } from "./truncation.js"; import { validateRequiredParams } from "./validation.js"; @@ -60,7 +61,7 @@ export async function bash(args: BashArgs): Promise { const childProcess = spawn(command, [], { shell: true, cwd: userCwd, - env: { ...process.env }, + env: getShellEnv(), }); backgroundProcesses.set(bashId, { process: childProcess, @@ -118,7 +119,7 @@ export async function bash(args: BashArgs): Promise { timeout: effectiveTimeout, maxBuffer: 10 * 1024 * 1024, cwd: userCwd, - env: { ...process.env }, + env: getShellEnv(), signal, }; const { stdout, stderr } = await execAsync(command, options); diff --git a/src/tools/impl/Shell.ts b/src/tools/impl/Shell.ts index 51fdef4..f6a1521 100644 --- a/src/tools/impl/Shell.ts +++ b/src/tools/impl/Shell.ts @@ -1,5 +1,6 @@ import { spawn } from "node:child_process"; import * as path from "node:path"; +import { getShellEnv } from "./shellEnv.js"; import { validateRequiredParams } from "./validation.js"; interface ShellArgs { @@ -50,7 +51,7 @@ export async function shell(args: ShellArgs): Promise { const child = spawn(executable, execArgs, { cwd, - env: process.env, + env: getShellEnv(), stdio: ["ignore", "pipe", "pipe"], }); diff --git a/src/tools/impl/shellEnv.ts b/src/tools/impl/shellEnv.ts new file mode 100644 index 0000000..3e17f4a --- /dev/null +++ b/src/tools/impl/shellEnv.ts @@ -0,0 +1,42 @@ +/** + * Shell environment utilities + * Provides enhanced environment variables for shell execution, + * including bundled tools like ripgrep in PATH. + */ + +import { createRequire } from "node:module"; +import * as path from "node:path"; +import { fileURLToPath } from "node:url"; + +/** + * Get the directory containing the bundled ripgrep binary. + * Returns undefined if @vscode/ripgrep is not installed. + */ +function getRipgrepBinDir(): string | undefined { + try { + const __filename = fileURLToPath(import.meta.url); + const require = createRequire(__filename); + const rgPackage = require("@vscode/ripgrep"); + // rgPath is the full path to the binary, we want the directory + return path.dirname(rgPackage.rgPath); + } catch (_error) { + return undefined; + } +} + +/** + * Get enhanced environment variables for shell execution. + * Includes bundled tools (like ripgrep) in PATH. + */ +export function getShellEnv(): NodeJS.ProcessEnv { + const env = { ...process.env }; + + // Add ripgrep bin directory to PATH if available + const rgBinDir = getRipgrepBinDir(); + if (rgBinDir) { + const currentPath = env.PATH || ""; + env.PATH = `${rgBinDir}${path.delimiter}${currentPath}`; + } + + return env; +}