MoonshotAI / MoonshotAI/kimi-code

kimi update fails to detect npm-global install source on Windows Git Bash (EINVAL in execFile)

Open Beginner friendly
#1,356 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.5k
Forks
1.2k
Avg merge
11h 53m
Merged PRs (30d)
350

Description

问题描述

在 Windows 11 上通过 Git Bash 运行 kimi update 时,无法检测到 npm 全局安装来源,始终显示:

Detected install source: unsupported package manager or layout.
To update manually, run: npm install -g @moonshot-ai/kimi-code@x.x.x

即使 kimi-code 已通过 npm install -g 安装,detectInstallSource() 仍然返回 "unsupported"

根因

源文件: apps/kimi-code/src/cli/update/source.ts

detectInstallSource 函数(约第 103 行)调用:

execFileText(npmCommand(platform), ["prefix", "-g"])

platform === "win32" 时,npmCommand 返回 "npm.cmd"。当 Node.js 进程由 Git Bash / MSYS2 环境启动时,child_process.execFile("npm.cmd", ...) 会抛出 EINVAL 错误(spawn 同样失败)。

catch 块直接返回 "unsupported",导致 canAutoInstall 返回 false,进入手动更新提示分支。

复现环境

  • OS: Windows 11
  • Shell: Git Bash (MSYS2)
  • Node.js: v24.18.0 (via fnm)
  • npm: 全局 prefix 为 %USERPROFILE%\.npm-global

验证

execFile("npm.cmd", ["prefix", "-g"])           → EINVAL
spawn("npm.cmd", ["prefix", "-g"])              → EINVAL
execFile("npm.cmd", ["prefix", "-g"], {shell:true}) → 正常工作

修复建议

catch 块中添加 fallback 启发式检测——当 npm prefix -g 因平台兼容性失败时,通过包路径模式判断是否属于 npm 全局安装:

} catch {
  // Fallback: when npm prefix detection fails (e.g. EINVAL on Git Bash),
  // check if package path matches a typical npm global node_modules layout
  const normalized = normalizeForHeuristic(packageRoot);
  const npmNodeModulesSeg = `/node_modules/${NPM_PACKAGE_NAME}`;
  const npmLibSeg = `/lib${npmNodeModulesSeg}`;
  if (normalized.endsWith(npmNodeModulesSeg.toLowerCase()) ||
      normalized.endsWith(npmLibSeg.toLowerCase())) {
    return "npm-global";
  }
  return "unsupported";
}

npm 全局安装的包路径始终以 <prefix>/node_modules/@moonshot-ai/kimi-code 结尾,这个启发式检测无需运行 npm 命令即可完成判断。

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in apps/kimi-code/src/cli/update/source.ts and read detectInstallSource around line 103, focusing on the npm prefix lookup and its catch path. Reproduce the npm.cmd EINVAL case in Windows Git Bash, then verify that the fallback recognizes npm-global package paths while leaving unrelated layouts unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.