Limit ripgrep output to avoid expensive searches

This commit is contained in:
Saoud Rizwan
2024-08-30 03:03:11 -04:00
parent 1df8b1673a
commit 246070bf8a

View File

@@ -2,6 +2,7 @@ import * as vscode from "vscode"
import * as childProcess from "child_process" import * as childProcess from "child_process"
import * as path from "path" import * as path from "path"
import * as fs from "fs" import * as fs from "fs"
import * as readline from "readline"
/* /*
This file provides functionality to perform regex searches on files using ripgrep. This file provides functionality to perform regex searches on files using ripgrep.
@@ -57,6 +58,8 @@ interface SearchResult {
afterContext: string[] afterContext: string[]
} }
const MAX_RESULTS = 300
async function getBinPath(vscodeAppRoot: string): Promise<string | undefined> { async function getBinPath(vscodeAppRoot: string): Promise<string | undefined> {
const checkPath = async (pkgFolder: string) => { const checkPath = async (pkgFolder: string) => {
const fullPath = path.join(vscodeAppRoot, pkgFolder, binName) const fullPath = path.join(vscodeAppRoot, pkgFolder, binName)
@@ -81,25 +84,41 @@ async function pathExists(path: string): Promise<boolean> {
async function execRipgrep(bin: string, args: string[]): Promise<string> { async function execRipgrep(bin: string, args: string[]): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const process = childProcess.spawn(bin, args) const rgProcess = childProcess.spawn(bin, args)
let output = "" // cross-platform alternative to head, which is ripgrep author's recommendation for limiting output.
let errorOutput = "" const rl = readline.createInterface({
input: rgProcess.stdout,
process.stdout.on("data", (data) => { crlfDelay: Infinity, // treat \r\n as a single line break even if it's split across chunks. This ensures consistent behavior across different operating systems.
output += data.toString()
}) })
process.stderr.on("data", (data) => { let output = ""
let lineCount = 0
const maxLines = MAX_RESULTS * 10 // limiting ripgrep output with max lines since there's no other way to limit results. it's okay that we're outputting as json, since we're parsing it line by line and ignore anything that's not part of a match. This assumes each result is at most 10 lines.
rl.on("line", (line) => {
if (lineCount < maxLines) {
output += line + "\n"
lineCount++
} else {
rl.close()
rgProcess.kill()
}
})
let errorOutput = ""
rgProcess.stderr.on("data", (data) => {
errorOutput += data.toString() errorOutput += data.toString()
}) })
rl.on("close", () => {
process.on("close", (code) => { if (errorOutput) {
if (code === 0) { reject(new Error(`ripgrep process error: ${errorOutput}`))
resolve(output)
} else { } else {
reject(new Error(`ripgrep process exited with code ${code}: ${errorOutput}`)) resolve(output)
} }
}) })
rgProcess.on("error", (error) => {
reject(new Error(`ripgrep process error: ${error.message}`))
})
}) })
} }
@@ -167,14 +186,14 @@ function formatResults(results: SearchResult[], cwd: string): string {
const groupedResults: { [key: string]: SearchResult[] } = {} const groupedResults: { [key: string]: SearchResult[] } = {}
let output = "" let output = ""
if (results.length >= 300) { if (results.length >= MAX_RESULTS) {
output += `Showing first 300 of ${results.length.toLocaleString()} results, use a more specific search if necessary...\n\n` output += `Showing first ${MAX_RESULTS} of ${MAX_RESULTS}+ results. Use a more specific search if necessary...\n\n`
} else { } else {
output += `Found ${results.length.toLocaleString()} results...\n\n` output += `Found ${results.length.toLocaleString()} results...\n\n`
} }
// Group results by file name // Group results by file name
results.slice(0, 300).forEach((result) => { results.slice(0, MAX_RESULTS).forEach((result) => {
const relativeFilePath = path.relative(cwd, result.file) const relativeFilePath = path.relative(cwd, result.file)
if (!groupedResults[relativeFilePath]) { if (!groupedResults[relativeFilePath]) {
groupedResults[relativeFilePath] = [] groupedResults[relativeFilePath] = []