@@ -11,7 +11,7 @@ This file provides functionality to perform regex searches on files using ripgre
1111Inspired by: https://github.com/DiscreteTom/vscode-ripgrep-utils
1212
1313Key components:
14- 1. getBinPath: Locates the ripgrep binary within the VSCode installation .
14+ 1. getBinPath: Locates the ripgrep binary (in the VS Code install, or on the system PATH) .
15152. execRipgrep: Executes the ripgrep command and returns the output.
16163. regexSearchFiles: The main function that performs regex searches on files.
1717 - Parameters:
@@ -51,6 +51,11 @@ rel/path/to/helper.ts
5151const isWindows = process . platform . startsWith ( "win" )
5252const binName = isWindows ? "rg.exe" : "rg"
5353
54+ // VS Code's @vscode/ripgrep-universal package (used by recent VS Code builds,
55+ // including the Insiders staged-install layout) nests the binary under
56+ // bin/<platform>-<arch>/ rather than directly in bin/.
57+ const ripgrepUniversalBinDir = `bin/${ process . platform } -${ process . arch } `
58+
5459interface SearchFileResult {
5560 file : string
5661 searchResults : SearchResult [ ]
@@ -80,7 +85,47 @@ export function truncateLine(line: string, maxLength: number = MAX_LINE_LENGTH):
8085 return line . length > maxLength ? line . substring ( 0 , maxLength ) + " [truncated...]" : line
8186}
8287/**
83- * Get the path to the ripgrep binary within the VSCode installation
88+ * Look up the ripgrep binary on the system PATH.
89+ *
90+ * Used as a fallback after the VS Code installation has been checked. Covers
91+ * VS Code forks whose install layout is not recognized by getBinPath, headless
92+ * / CLI hosts with no VS Code installation, and machines where the user has
93+ * installed ripgrep themselves.
94+ */
95+ async function findRipgrepOnPath ( ) : Promise < string | undefined > {
96+ const pathEnv = process . env . PATH
97+
98+ if ( ! pathEnv ) {
99+ return undefined
100+ }
101+
102+ for ( const dir of pathEnv . split ( path . delimiter ) ) {
103+ if ( dir . length === 0 ) {
104+ continue
105+ }
106+
107+ const candidate = path . join ( dir , binName )
108+
109+ if ( await fileExistsAtPath ( candidate ) ) {
110+ return candidate
111+ }
112+ }
113+
114+ return undefined
115+ }
116+
117+ /**
118+ * Get the path to the ripgrep binary.
119+ *
120+ * Resolution order:
121+ * 1. ripgrep shipped inside the VS Code installation. Both the long-standing
122+ * `@vscode/ripgrep` layout and the newer `@vscode/ripgrep-universal`
123+ * layout are checked — the latter is what VS Code Insiders' staged-install
124+ * builds use (see microsoft/vscode#252063).
125+ * 2. ripgrep on the system PATH — covers VS Code forks with an unrecognized
126+ * install layout, headless / CLI hosts, and a user-installed ripgrep.
127+ *
128+ * Returns `undefined` when ripgrep cannot be located anywhere.
84129 */
85130export async function getBinPath ( vscodeAppRoot : string ) : Promise < string | undefined > {
86131 const checkPath = async ( pkgFolder : string ) => {
@@ -92,7 +137,10 @@ export async function getBinPath(vscodeAppRoot: string): Promise<string | undefi
92137 ( await checkPath ( "node_modules/@vscode/ripgrep/bin/" ) ) ||
93138 ( await checkPath ( "node_modules/vscode-ripgrep/bin" ) ) ||
94139 ( await checkPath ( "node_modules.asar.unpacked/vscode-ripgrep/bin/" ) ) ||
95- ( await checkPath ( "node_modules.asar.unpacked/@vscode/ripgrep/bin/" ) )
140+ ( await checkPath ( "node_modules.asar.unpacked/@vscode/ripgrep/bin/" ) ) ||
141+ ( await checkPath ( `node_modules/@vscode/ripgrep-universal/${ ripgrepUniversalBinDir } ` ) ) ||
142+ ( await checkPath ( `node_modules.asar.unpacked/@vscode/ripgrep-universal/${ ripgrepUniversalBinDir } ` ) ) ||
143+ ( await findRipgrepOnPath ( ) )
96144 )
97145}
98146
0 commit comments