Skip to content

Commit e3ad671

Browse files
committed
test(plugin-code-server): fix windows child_process spawning and cleanup
1 parent 8df4348 commit e3ad671

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

plugins/code-server/src/node/detect.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Buffer } from 'node:buffer'
22
import { spawn } from 'node:child_process'
3+
import process from 'node:process'
34

45
export interface DetectCodeServerResult {
56
installed: boolean
@@ -30,7 +31,11 @@ export function detectCodeServer(bin = 'code-server', timeoutMs = 5000): Promise
3031

3132
let child
3233
try {
33-
child = spawn(bin, ['--version'], { windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] })
34+
child = spawn(bin, ['--version'], {
35+
windowsHide: true,
36+
stdio: ['ignore', 'pipe', 'ignore'],
37+
shell: process.platform === 'win32' && bin.endsWith('.cmd'),
38+
})
3439
}
3540
catch {
3641
finish({ installed: false, bin })

plugins/code-server/src/node/supervisor.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
CodeServerStartResult,
1313
CodeServerStatusResult,
1414
} from '../types'
15-
import { spawn } from 'node:child_process'
15+
import { execSync, spawn } from 'node:child_process'
1616
import { createHash, randomBytes } from 'node:crypto'
1717
import { request as httpRequest } from 'node:http'
1818
import process from 'node:process'
@@ -153,6 +153,7 @@ export class CodeServerSupervisor {
153153
env,
154154
windowsHide: true,
155155
stdio: ['ignore', 'pipe', 'pipe'],
156+
shell: process.platform === 'win32' && this.bin.endsWith('.cmd'),
156157
})
157158
}
158159
catch (error) {
@@ -240,7 +241,12 @@ export class CodeServerSupervisor {
240241

241242
private terminate(child: ChildProcess): void {
242243
try {
243-
child.kill('SIGTERM')
244+
if (process.platform === 'win32' && this.bin.endsWith('.cmd') && child.pid) {
245+
execSync(`taskkill /pid ${child.pid} /t /f`, { stdio: 'ignore' })
246+
}
247+
else {
248+
child.kill('SIGTERM')
249+
}
244250
}
245251
catch {
246252
// Already gone.

plugins/code-server/test/_utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,12 @@ process.on('SIGINT', () => process.exit(0))
5959
`
6060
writeFileSync(binPath, script)
6161
chmodSync(binPath, 0o755)
62+
63+
if (process.platform === 'win32') {
64+
const cmdPath = `${binPath}.cmd`
65+
writeFileSync(cmdPath, `@node "${binPath}" %*`)
66+
return cmdPath
67+
}
68+
6269
return binPath
6370
}

0 commit comments

Comments
 (0)