[Windows][Bug] postinstall 解压 getnote.exe 失败
[Windows][Bug] postinstall 解压 getnote.exe 失败:powershell $args 参数未传递,安装报错且每次失败残留临时文件
问题描述 / Description
在 Windows 上安装 @getnote/cli 时,postinstall 下载 zip 成功,但随后用 PowerShell 解压时参数校验失败,安装以 ELIFECYCLE 报错退出:
node_modules/@getnote/cli postinstall: Expand-Archive : Cannot validate argument on parameter 'LiteralPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
node_modules/@getnote/cli postinstall: At line:1 char:29
node_modules/@getnote/cli postinstall: + Expand-Archive -LiteralPath $args[0] -DestinationPath $args[1] -Force ...
node_modules/@getnote/cli postinstall: + ~~~~~~~~
node_modules/@getnote/cli postinstall: + CategoryInfo : InvalidData: (:) [Expand-Archive], ParameterBindingValidationException
node_modules/@getnote/cli postinstall: + FullyQualifiedErrorId : ParameterArgumentValidationError,Expand-Archive
node_modules/@getnote/cli postinstall: Failed to install getnote: powershell exited with status 1
node_modules/@getnote/cli postinstall: Failed
[ELIFECYCLE] Command failed with exit code 1.
结果:包文件正常安装,但 bin/ 下只有 getnote.js,没有生成 getnote.exe。任何依赖该二进制的外部工具(例如 DeepSeek Harness 的 @getnote/dsh-plugin,以及 CLI 本身)都会因找不到可执行文件而无法工作。
环境 / Environment
- OS:Windows 11
- Node.js:v24.19.0(在 Windows PowerShell 7 下复现)
- 安装方式:
npm install或pnpm add(pnpm 10+ 需要先在pnpm-workspace.yaml放行构建脚本,见文末附注) - 版本:
@getnote/[email protected]
复现步骤 / Steps to Reproduce
- 在 Windows 上安装:
npm install @getnote/cli(或pnpm add @getnote/cli并放行allowBuilds) - 观察 postinstall 输出,即出现上述
Expand-Archive参数错误
根因分析 / Root Cause
scripts/postinstall.js 第 70 行:
run('powershell', ['-NoProfile', '-Command', 'Expand-Archive -LiteralPath $args[0] -DestinationPath $args[1] -Force', tmpFile, binDir]);
powershell -Command "<字符串>" arg1 arg2 这种调用方式下,命令字符串后面追加的参数不会绑定到脚本里的 $args——只有当命令是脚本块调用(& { ... })时,后续参数才会以 $args 传入。因此 $args[0] 为 null,Expand-Archive 的 -LiteralPath 参数校验失败。
建议修复 / Suggested Fix
把命令包成脚本块调用即可让 $args 正常绑定:
run('powershell', ['-NoProfile', '-Command', '& { Expand-Archive -LiteralPath $args[0] -DestinationPath $args[1] -Force }', tmpFile, binDir]);
更稳妥的替代方案(任选其一):
- 改用 Windows 自带
tar(libarchive bsdtar,Windows 10 1803+ 支持 zip):run('tar', ['-xf', tmpFile, '-C', binDir]) - 或在 Node 侧用解压库(如
extract-zip/adm-zip/yauzl),完全绕开 shell 引号问题 - 或把路径拼进
-Command字符串(注意空格与引号转义,不推荐)
次要问题 / Secondary Issue(建议一并修复)
main() 的 catch 块里直接 process.exit(1),会跳过 finally 中的 fs.unlinkSync(tmpFile) 清理(process.exit() 立即终止,不再执行 finally)。因此每次失败的安装都会在系统临时目录残留一个约 3.3MB 的 getnote-download-* 文件,反复安装会持续累积垃圾文件。
建议改为 process.exitCode = 1(让 finally 先执行清理再退出),或在 process.exit(1) 前手动删除临时文件。
附注 / Note(非 bug,文档建议)
pnpm 10+ 默认拦截依赖的构建脚本,安装时会提示 ERR_PNPM_IGNORED_BUILDS。建议在 README 里补充 pnpm 用户的安装说明:
# pnpm-workspace.yaml
allowBuilds:
'@getnote/cli': true
感谢 @fengjihua 提交这么完整、准确的报告!你不仅给出了 Windows 11 + npm/pnpm 的稳定复现,还准确定位到了 PowerShell -Command 后参数没有进入 $args 的根因,并提醒了 process.exit(1) 会让临时文件清理失效,这两点都非常有价值。
我们已经采纳建议并完成修复:
- 用脚本块
& { ... }调用Expand-Archive,让压缩包路径和目标目录正确进入$args - 改用
process.exitCode = 1,并通过finally确保失败时清理临时归档 - README 补充 pnpm 10+
allowBuilds配置 - 增加 PowerShell 命令构造和失败清理的自动化测试
已合并并发布 v1.5.7:
- PR: https://github.com/iswalle/getnote-cli/pull/21
- GitHub Release: https://github.com/iswalle/getnote-cli/releases/tag/v1.5.7
- npm: https://www.npmjs.com/package/@getnote/cli/v/1.5.7
再次感谢你的细致排查和明确修复建议。欢迎继续反馈 Windows 使用体验,也欢迎后续继续提 Issue 或 PR!