Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-windows-upgrade-spawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Fix `kimi upgrade` failing on Windows with a spawn error when installing the new version.
15 changes: 13 additions & 2 deletions apps/kimi-code/src/cli/update/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,14 @@ export async function installUpdate(
): Promise<void> {
const { cmd, args } = spawnForSource(source, version, platform);
await new Promise<void>((resolve, reject) => {
const child = spawn(cmd, [...args], { stdio: 'inherit' });
// Windows package managers (npm/pnpm/yarn) are .cmd shims. Since the
// CVE-2024-27980 fix, Node throws EINVAL when spawning a .cmd/.bat without
// a shell, so run through the shell on win32. The version is a validated
// semver and the package name is a constant, so args are shell-safe.
const child = spawn(cmd, [...args], {
stdio: 'inherit',
shell: platform === 'win32' ? true : undefined,
});
child.once('error', reject);
child.once('exit', (code, signal) => {
if (code === 0) {
Expand Down Expand Up @@ -596,7 +603,11 @@ async function startBackgroundInstall(
});
};

const child = spawn(cmd, [...args], { detached: true, stdio: 'ignore' });
const child = spawn(cmd, [...args], {
detached: true,
stdio: 'ignore',
shell: platform === 'win32' ? true : undefined,
});
Comment on lines +606 to +610

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Hide the detached Windows updater shell

When auto-install runs on Windows, this background path now starts cmd.exe (shell: true) with detached: true and the default windowsHide: false; Windows detached children get their own console window, so a passive update can pop or flash a command window even though stdio is ignored. Please hide this background child, for example by setting windowsHide: true on win32, so the silent updater stays silent.

Useful? React with 👍 / 👎.

child.once('error', () => { finish(false); });
child.once('exit', (code) => { finish(code === 0); });
child.unref();
Expand Down
22 changes: 22 additions & 0 deletions apps/kimi-code/test/cli/update/preflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,28 @@ describe('runUpdatePreflight', () => {
);
});

it('pnpm-global on win32: spawns pnpm.cmd through a shell', async () => {
disableAutoInstall();
mocks.readUpdateCache.mockResolvedValue(cacheWith('0.5.0'));
mocks.refreshUpdateCache.mockResolvedValue(cacheWith('0.5.0'));
mocks.detectInstallSource.mockResolvedValue('pnpm-global');
mocks.promptForInstallChoice.mockResolvedValue('install');
mockSpawnExit(0);
const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', { value: 'win32' });
try {
const { options } = captureOutput();
await runUpdatePreflight('0.4.0', options);
expect(mocks.spawn).toHaveBeenCalledWith(
'pnpm.cmd',
['add', '-g', '@moonshot-ai/kimi-code@0.5.0'],
{ stdio: 'inherit', shell: true },
);
} finally {
Object.defineProperty(process, 'platform', { value: originalPlatform });
}
});

it('yarn-global: spawns yarn global add', async () => {
disableAutoInstall();
mocks.readUpdateCache.mockResolvedValue(cacheWith('0.5.0'));
Expand Down
Loading