cli: manual recovery `node postinstall.mjs` fails on Windows - `npm_execpath` unset causes silent `spawnSync("npm")` ENOENT
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 73/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- node.js, typescript
- 领域
- build-system, cli
调研方向
从packages/cli/postinstall.mjs开始,重点查看报告中描述的npm可执行文件选择和installPackage()处理。要复现问题,请在Windows上、npm生命周期之外运行node postinstall.mjs,并检查spawn结果。手动恢复能够成功调用npm,且失败时会显示底层错误,即为完成。
由索引模型根据 Issue 内容生成。
描述
Summary
The manual recovery step recommended in #55888 - running node postinstall.mjs inside the installed @opencode/cli package - cannot work on Windows when invoked outside an npm lifecycle context.
postinstall.mjs only resolves the npm CLI correctly when npm_execpath is set. Otherwise it falls back to spawnSync("npm", ...), which always fails with ENOENT on Windows because the npm shim is npm.cmd.
The failure is silent: installPackage() treats a non-zero status as a failed install and continues, so the user only ever sees the generic Failed to install OpenCode. Try manually installing ... message, with no npm output and no hint of the real cause.
Environment
- opencode version: 2.0.31
- OS: Windows 11 (win32 x64)
- Node: v24.18.0
- npm: 11.16.0
- Install method: global npm install (
npm i -g @opencode/cli) npm_execpath: unset (becausepostinstall.mjswas run directly withnode)
Root cause
packages/cli/postinstall.mjs:
// Windows cannot spawn npm's .cmd shim directly, so reuse the npm CLI that runs this script.
const npm = process.env.npm_execpath?.endsWith("npm-cli.js")
? [process.execPath, process.env.npm_execpath]
: ["npm"]
The comment states the intent, but the ["npm"] fallback is unreachable on Windows. childProcess.spawnSync("npm", ...) without shell: true does not resolve .cmd shims through PATHEXT.
Reproduction
PS> cd $env:APPDATA\npm\node_modules\@opencode\cli
PS> node postinstall.mjs
Failed to install OpenCode. Try manually installing "@opencode/cli-windows-x64" or "@opencode/cli-windows-x64-baseline".
# exit code 1, ~0.4s, no npm output
Evidence
Same machine, same session:
npm_execpath = undefined
spawnSync("npm", ...) -> status = null error = ENOENT
spawnSync("npm.cmd", ...) -> status = null error = EINVAL
spawnSync(process.execPath, [npmCliJs, "--version"]) -> status = 0 stdout = "11.16.0"
Setting npm_execpath makes the identical invocation succeed:
PS> $env:npm_execpath = "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js"
PS> node postinstall.mjs
# exit code 0
Suggested fix
Resolve the npm CLI robustly instead of assuming it is directly spawnable:
- resolve
npm-cli.jsrelative toprocess.execPath(../node_modules/npm/bin/npm-cli.js), or - fall back to
npm.cmdwithshell: true, or - resolve through
PATHEXT
Separately, consider surfacing result.error when installPackage fails - the current message hides ENOENT entirely, which makes this hard to diagnose.
Additional context
This machine was in the state described by #55888 (bin/opencode.exe left as the 229-byte placeholder). Because node postinstall.mjs was the suggested recovery and it fails on Windows, the install could not be repaired without manually setting npm_execpath.
Related
- #55888 (closed) - recommends
node postinstall.mjsas manual recovery - #51553 (open) - npm 12 upgrade leaves the Windows executable stub
- 主要语言
- TypeScript
- 星标
- 213k
- 派生
- 28.4k
- 平均合并
- 8 小时 29 分钟
- 30 天内合并 PR
- 252
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
anomalyco/opencode 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
anomalyco/opencode#56352 · 2 条评论 ·
维护者通常 1 天内回复
-
web / desktop client: model tooltip shows "No reasoning" for models that expose reasoning variants未关闭pending close triaging
难度 2/5 1-3 小时 新手友好度 76/100
anomalyco/opencode#56317 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 75/100
anomalyco/opencode#56277 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
anomalyco/opencode#56251 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
anomalyco/opencode#56179 · 1 条评论 ·
维护者通常 1 天内回复
查看 anomalyco/opencode 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 83/100
-
难度 2/5 1-3 小时 新手友好度 72/100
ranxianglei/billion-context#3583 ·
维护者通常 1 天内回复
-
bug:new
难度 2/5 1-3 小时 新手友好度 76/100
callstackincubator/simlock#673 ·
维护者通常 1 天内回复
-
documentation v3
难度 2/5 1-3 小时 新手友好度 73/100
维护者通常 1 天内回复
-
bug good first issue
难度 2/5 1-3 小时 新手友好度 65/100
NIAEFEUP/website-niaefeup-frontend#489 ·
维护者通常 2 天内回复