Skip to content

Commit 7e897ec

Browse files
authored
Merge pull request #96 from jimeh/fix-ripgrep-quoted-paths
fix(ripgrep): handle paths with spaces by quoting paths
2 parents 733dadf + 7511939 commit 7e897ec

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/lib/ripgrep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function getRgCommand(value: string, extraFlags?: string[]) {
3434
...rgRequiredFlags,
3535
...config.rgOptions,
3636
...cx.rgMenuActionsSelected,
37-
...paths,
37+
...paths.filter((path) => typeof path === 'string').map(ensureQuotedPath),
3838
...config.addSrcPaths.map(ensureQuotedPath),
3939
...(extraFlags || []),
4040
...excludes,

src/test/suite/ripgrep.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as assert from 'assert';
22
import * as sinon from 'sinon';
3+
import * as vscode from 'vscode';
34
import { context as cx } from '../../lib/context';
4-
import { checkAndExtractRgFlagsFromQuery, checkKillProcess } from '../../lib/ripgrep';
5+
import { checkAndExtractRgFlagsFromQuery, checkKillProcess, rgSearch } from '../../lib/ripgrep';
56

67
// Add dedicated test suite for checkAndExtractRgFlagsFromQuery
78
suite('checkAndExtractRgFlagsFromQuery', () => {
@@ -161,3 +162,63 @@ suite('checkKillProcess', () => {
161162
assert.deepStrictEqual(cx.spawnRegistry, []);
162163
});
163164
});
165+
166+
suite('rgSearch - Path with Spaces Bug', () => {
167+
let sandbox: sinon.SinonSandbox;
168+
let spawnStub: sinon.SinonStub;
169+
170+
setup(() => {
171+
sandbox = sinon.createSandbox();
172+
173+
// Minimal mock spawn that just captures the command
174+
spawnStub = sandbox.stub(require('child_process'), 'spawn').returns({
175+
stdout: { on: sandbox.stub() },
176+
stderr: { on: sandbox.stub() },
177+
on: sandbox.stub(),
178+
killed: false,
179+
} as any);
180+
181+
// Minimal required context
182+
cx.spawnRegistry = [];
183+
cx.qp = { busy: false } as any;
184+
185+
// Minimal config mock
186+
sandbox.stub(require('../../utils/getConfig'), 'getConfig').returns({
187+
rgPath: 'rg',
188+
rgOptions: [],
189+
rgGlobExcludes: [],
190+
addSrcPaths: [],
191+
});
192+
});
193+
194+
teardown(() => {
195+
sandbox.restore();
196+
});
197+
198+
test('workspace path with spaces should be properly quoted', () => {
199+
const pathWithSpaces = '/Users/test/My Projects/workspace';
200+
sandbox.stub(vscode.workspace, 'workspaceFolders').value([{ uri: { fsPath: pathWithSpaces } }]);
201+
202+
rgSearch('test');
203+
204+
const command = spawnStub.firstCall.args[0];
205+
assert(command.includes(`"${pathWithSpaces}"`), `Path not quoted in: ${command}`);
206+
});
207+
208+
test('current file path with spaces should be properly quoted', () => {
209+
const filePathWithSpaces = '/Users/test/My Documents/current file.ts';
210+
211+
// Mock getCurrentFilePath to return a path with spaces
212+
sandbox
213+
.stub(require('../../utils/searchCurrentFile'), 'getCurrentFilePath')
214+
.returns(filePathWithSpaces);
215+
216+
// Set to search current file only
217+
cx.currentFileOnly = true;
218+
219+
rgSearch('test');
220+
221+
const command = spawnStub.firstCall.args[0];
222+
assert(command.includes(`"${filePathWithSpaces}"`), `File path not quoted in: ${command}`);
223+
});
224+
});

0 commit comments

Comments
 (0)