Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(desktop): detect Windows CLI versions through command shims

bingqilinweimaotaidad6e45bdea5
diff --git a/electron/cli-path.cjs b/electron/cli-path.cjs
index be648da..fadb8a7 100644
--- a/electron/cli-path.cjs
+++ b/electron/cli-path.cjs
@@ -12,7 +12,10 @@ async function findWindowsCommand(bin, envPath, pathExt = process.env.PATHEXT ??
.map((ext) => ext.trim())
.filter(Boolean)
.map((ext) => ext.startsWith('.') ? ext : `.${ext}`)
- const names = path.extname(bin) ? [bin] : [bin, ...extensions.map((ext) => `${bin}${ext}`)]
+ // npm installs both a POSIX shell shim (`bin`) and a Windows command shim
+ // (`bin.cmd`). The extensionless file is not executable by CreateProcess,
+ // so honour PATHEXT before falling back to a literal filename.
+ const names = path.extname(bin) ? [bin] : [...extensions.map((ext) => `${bin}${ext}`), bin]
for (const rawDir of envPath.split(path.delimiter)) {
const trimmed = rawDir.trim()
@@ -28,4 +31,24 @@ async function findWindowsCommand(bin, envPath, pathExt = process.env.PATHEXT ??
return null
}
-module.exports = { findWindowsCommand }
+/** Build a spawn target for a command resolved on Windows. CreateProcess
+ * cannot execute .cmd/.bat files directly; run those through ComSpec while
+ * leaving native executables alone. */
+function windowsCommandInvocation(command, args, comspec = process.env.ComSpec || 'cmd.exe') {
+ if (!/\.(?:cmd|bat)$/i.test(command)) return { command, args }
+ const commandArgs = args.map((arg) => {
+ const value = String(arg)
+ return /^[\w./:=+-]+$/u.test(value) ? value : `"${value.replace(/"/g, '""')}"`
+ })
+ // With windowsVerbatimArguments, the doubled leading quote is the outer
+ // `cmd /c` quote followed by the executable-path quote. The final quote
+ // closes that outer pair. This is required when the shim path has spaces.
+ const commandLine = `""${command}"${commandArgs.length ? ` ${commandArgs.join(' ')}` : ''}"`
+ return {
+ command: comspec,
+ args: ['/d', '/s', '/c', commandLine],
+ windowsVerbatimArguments: true,
+ }
+}
+
+module.exports = { findWindowsCommand, windowsCommandInvocation }
diff --git a/electron/main.cjs b/electron/main.cjs
index 77e2bd1..2b407b9 100644
--- a/electron/main.cjs
+++ b/electron/main.cjs
@@ -9,7 +9,7 @@ const https = require('node:https')
const crypto = require('node:crypto')
const { pathToFileURL } = require('node:url')
const autoUpdater = require('./autoUpdater.cjs')
-const { findWindowsCommand } = require('./cli-path.cjs')
+const { findWindowsCommand, windowsCommandInvocation } = require('./cli-path.cjs')
const isDev = !app.isPackaged
const DEV_URL = process.env.ELECTRON_RENDERER_URL || 'http://localhost:5180'
@@ -1495,9 +1495,13 @@ function detectSearchPath() {
function spawnText(cmd, args, envPath, timeoutMs = 8000) {
return new Promise((resolve) => {
let settled = false
- const child = spawn(cmd, args, {
+ const invocation = process.platform === 'win32'
+ ? windowsCommandInvocation(cmd, args)
+ : { command: cmd, args }
+ const child = spawn(invocation.command, invocation.args, {
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PATH: envPath ?? process.env.PATH },
+ windowsVerbatimArguments: invocation.windowsVerbatimArguments === true,
})
let out = ''
const onChunk = (buf) => { out += buf.toString('utf8') }
@@ -1611,7 +1615,7 @@ async function resolveCliPath(bin, envPath) {
if (fromWhich) return fromWhich
}
const home = os.homedir()
- const names = process.platform === 'win32' ? [bin, `${bin}.cmd`, `${bin}.exe`] : [bin]
+ const names = process.platform === 'win32' ? [`${bin}.cmd`, `${bin}.exe`, bin] : [bin]
const dirs = [
path.join(home, '.local', 'bin'),
path.join(home, 'bin'),
diff --git a/server/src/__tests__/electron-cli-path.test.ts b/server/src/__tests__/electron-cli-path.test.ts
index c98df89..2a35a4d 100644
--- a/server/src/__tests__/electron-cli-path.test.ts
+++ b/server/src/__tests__/electron-cli-path.test.ts
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
@@ -6,8 +7,13 @@ import { delimiter, join } from 'node:path'
import { test } from 'node:test'
const require = createRequire(import.meta.url)
-const { findWindowsCommand } = require('../../../electron/cli-path.cjs') as {
+const { findWindowsCommand, windowsCommandInvocation } = require('../../../electron/cli-path.cjs') as {
findWindowsCommand: (bin: string, envPath: string, pathExt?: string) => Promise<string | null>
+ windowsCommandInvocation: (
+ command: string,
+ args: string[],
+ comspec?: string,
+ ) => { command: string; args: string[]; windowsVerbatimArguments?: boolean }
}
test('Windows CLI lookup preserves a Unicode PATH entry', async () => {
@@ -17,6 +23,7 @@ test('Windows CLI lookup preserves a Unicode PATH entry', async () => {
const otherDir = join(root, 'other')
await mkdir(unicodeDir)
await mkdir(otherDir)
+ await writeFile(join(unicodeDir, 'qwen'), '#!/bin/sh\n')
const command = join(unicodeDir, 'qwen.CMD')
await writeFile(command, '@echo off\r\n')
@@ -26,3 +33,42 @@ test('Windows CLI lookup preserves a Unicode PATH entry', async () => {
await rm(root, { recursive: true, force: true })
}
})
+
+test('Windows command invocation uses ComSpec for command shims', () => {
+ const comspec = String.raw`C:\Windows\System32\cmd.exe`
+ const command = String.raw`C:\工具 目录\qwen.cmd`
+ assert.deepEqual(windowsCommandInvocation(command, ['--version'], comspec), {
+ command: comspec,
+ args: ['/d', '/s', '/c', `""${command}" --version"`],
+ windowsVerbatimArguments: true,
+ })
+ assert.deepEqual(windowsCommandInvocation(String.raw`C:\tools\qwen.exe`, ['--version'], comspec), {
+ command: String.raw`C:\tools\qwen.exe`,
+ args: ['--version'],
+ })
+})
+
+test('Windows command invocation runs a shim in a Unicode path', { skip: process.platform !== 'win32' }, async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-cli-version-'))
+ try {
+ const unicodeDir = join(root, '工具 目录')
+ await mkdir(unicodeDir)
+ const command = join(unicodeDir, 'qwen.cmd')
+ await writeFile(command, '@echo off\r\necho qwen-code 1.2.3\r\n')
+ const invocation = windowsCommandInvocation(command, ['--version'])
+ const output = await new Promise<string>((resolve, reject) => {
+ const child = spawn(invocation.command, invocation.args, {
+ stdio: ['ignore', 'pipe', 'pipe'],
+ windowsVerbatimArguments: invocation.windowsVerbatimArguments === true,
+ })
+ let text = ''
+ child.stdout.on('data', (chunk) => { text += chunk.toString('utf8') })
+ child.stderr.on('data', (chunk) => { text += chunk.toString('utf8') })
+ child.on('error', reject)
+ child.on('close', (code) => code === 0 ? resolve(text.trim()) : reject(new Error(`exit ${code}: ${text}`)))
+ })
+ assert.equal(output, 'qwen-code 1.2.3')
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})