Skip to content

Commit c095c7c

Browse files
fix: unify differnet codepath
1 parent 23b48e9 commit c095c7c

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

Diff for: src/extension/common.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,29 @@ export function resolveBinary() {
4242
return config
4343
}
4444

45-
export async function testBinaryExist(command: string) {
45+
// see https://github.com/ast-grep/ast-grep-vscode/pull/448 for more information
46+
// TL;DR: windows needs shell: true for non-exe command and quotation for command with space
47+
export function normalizeCommandForWindows(command: string) {
4648
// windows user may input space in command
4749
const normalizedCommand =
4850
/\s/.test(command) && !command.endsWith('.exe') ? `"${command}"` : command
51+
return {
52+
normalizedCommand,
53+
shell:
54+
process.platform === 'win32' && !command.toLowerCase().endsWith('.exe'),
55+
}
56+
}
57+
58+
export async function testBinaryExist(command: string) {
4959
const uris = workspace.workspaceFolders?.map(i => i.uri?.fsPath) ?? []
60+
const { normalizedCommand, shell } = normalizeCommandForWindows(command)
5061
return new Promise(r => {
5162
execFile(
5263
normalizedCommand,
5364
['-h'],
5465
{
66+
shell,
5567
// for windows
56-
shell:
57-
process.platform === 'win32' &&
58-
!command.toLowerCase().endsWith('.exe'),
5968
cwd: uris[0],
6069
},
6170
err => {

Diff for: src/extension/search.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import path from 'node:path'
22
import { type ExtensionContext, commands, workspace, window } from 'vscode'
33
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
44

5-
import { parentPort, resolveBinary, streamedPromise } from './common'
5+
import {
6+
parentPort,
7+
resolveBinary,
8+
streamedPromise,
9+
normalizeCommandForWindows,
10+
} from './common'
611
import type { SgSearch, DisplayResult, SearchQuery } from '../types'
712

813
/**
@@ -114,9 +119,7 @@ export function buildCommand(query: SearchQuery) {
114119
return
115120
}
116121
const command = resolveBinary()
117-
// windows user may input space in command
118-
const normalizedCommand =
119-
/\s/.test(command) && !command.endsWith('.exe') ? `"${command}"` : command
122+
const { normalizedCommand, shell } = normalizeCommandForWindows(command)
120123
const uris = workspace.workspaceFolders?.map(i => i.uri?.fsPath) ?? []
121124
const args = ['run', '--pattern', pattern, '--json=stream']
122125
if (query.selector) {
@@ -141,9 +144,7 @@ export function buildCommand(query: SearchQuery) {
141144
console.debug('running', query, normalizedCommand, args)
142145
// TODO: multi-workspaces support
143146
return spawn(normalizedCommand, args, {
144-
// for windows
145-
shell:
146-
process.platform === 'win32' && !command.toLowerCase().endsWith('.exe'),
147+
shell,
147148
cwd: uris[0],
148149
})
149150
}

0 commit comments

Comments
 (0)