Repository navigation
fix(install): bun 全局安装的 postinstall 读不到 npm_config_global,launcher 从不写入 - #1119
Merged
Merged
Conversation
`bun add -g botmux` / `pnpm add -g botmux` 装完后**用户根本没有 `botmux` 命令**。 真机复现(bun 1.4.0,真下载 86s):`.bun/bin/` 为空、`~/.botmux/bin/botmux` 不存在、 平台二进制只躺在下载缓存里。 这是**两个独立故障**,本 PR 修第二个: ① bun/pnpm 默认 block 依赖的生命周期脚本(bun 明确打印 `Blocked 2 postinstalls`)。 属于包管理器策略,需要用户 `bun pm trust` 或另行处理,本 PR 不动。 ② 即使脚本跑起来也没用 —— Guard 1 是 `npm_config_global !== 'true'` 就静默 exit 0, 而 bun 传给生命周期脚本的 env 里**没有 `npm_config_global`**。用探针包(postinstall 自己 dump env)实测,bun 给的完整集合是 `BUN_INSTALL` / `BUN_WHICH_IGNORE_CWD` / `npm_config_user_agent` / `npm_execpath` / `npm_node_execpath` —— 有几个 npm_* 变量, 但**恰恰缺 Guard 1 读的那一个**。所以 `bun pm -g trust botmux` 会报 `1 script ran` 而 launcher 依然没写 ——**最自然的那个 workaround 是无效的**,只修 ① 拿不到命令。⚠️ 分层:pnpm 10/11 与 bun 不在同一层。pnpm 的 `onlyBuiltDependencies` 让脚本**根本不跑**, 本 PR 救不了它(需 approve-builds / onlyBuiltDependencies 或换机制);bun 是「trust 能跑 但 Guard 1 杀掉」,修好后 `bun pm trust` 才成为真正可用的 workaround。 改法:Guard 1 变成「env 说是全局(npm/yarn) **或** 安装位置本身说是全局」。位置判定 复用仓库自己的 `detectGlobalInstallManager`(已被单测覆盖,且 `dist/` 随包发布,见 package.json `files`),**不新写第二套路径匹配** —— 那正是这块反复出问题的根因。 载入失败一律返回 false(fail-closed),只会更保守不会更宽松。⚠️ Guard 1 原本的职责必须保住:它防的是 repo 内 `install` 改写共享 `~/.botmux/bin/botmux`(本机 ~50 个 daemon 共用)。源码 checkout 与普通本地依赖都被 分类为 `unknown`,实测两者都不写 launcher;Guard 2 仍在,且实测「无 .git/src 的非全局 目录」下 Guard 1 单独也挡得住。 验证(同一条命令的 A/B,真实 bun 全局安装): 修前 `bun pm -g trust botmux` → `1 script ran`,**launcher 未写** 修后 `bun pm -g trust botmux` → `1 script ran`,launcher 写入且 `botmux --version` = 3.18.8 npm 路径逐字未变(`npm_config_global=true` 仍单独成立,实测仍写 launcher)。 反变异 3/3 全红:① 去掉位置判定(即线上缺陷)2 红 ② 放宽成「任何 node_modules/botmux 都算全局」(=本地依赖劫持共享 launcher)2 红 ③ 载入失败改成 fail-open 3 红。 新增 3 条用例(bun 全局布局无 env、本地依赖不误判、缺 dist 时 fail-closed)+ 1 条源码 守卫(钉住复用 classifier 而非手写匹配)。`bun run build`、`tsc --noEmit` 通过, 该测试文件 29/29。
Owner
Author
|
Review (pi bot, automated). LGTM. Independently verified:
The two-layer separation (bun: trust lifts the block but Guard 1 killed it; pnpm 10/11: script never runs) is documented correctly, and narrowing the title to bun is right. |
# Conflicts: # test/npm-binary-distribution.test.ts
deepcoldy
added a commit
that referenced
this pull request
Aug 31, 2026
#1119 的位置判定用 await import('./dist/utils/global-install.js') 复用仓库分类器, 但 #1115 已把 dist/ 从 package.json 的 files 移除 ⟹ 发布包里 import 必然 ENOENT ⟹ catch 返回 false ⟹ Guard 1 退回只看 env ⟹ bun add -g botmux 与修之前一样没有 botmux 命令。#1119 在发布形态下等于没生效(fail-closed,不崩不误写)。 判据是真打包,不是读代码: git archive <ref> | tar x && npm pack tar tzf botmux-*.tgz | grep -c '^package/dist/' → 0 已发布的 3.18.9 实测同样只有 6 个文件、零 dist。 改法:把布局判定内联进 postinstall-bin.mjs。它是无构建步骤的独立脚本,dist/ 与 src/ 都不发布,自包含是它的天然形态。⚠️ 内联版故意比 detectGlobalInstallManager 更严格:后者结尾是 platform === 'win32' ? 'npm' : 'unknown',实测把 Windows 本地依赖也判成 'npm'。在 update 路径无害,但在 postinstall 里意味着 Windows 本地依赖改写共享 ~/.botmux/bin/botmux,正是 Guard 1 要防的劫持。所以内联版只认已知全局布局、无平台兜底; Windows npm 全局带 npm_config_global=true,不经过这条检查。 验证:端到端在发布形态(tarball 内 dist 条目 = 0)跑通 —— bun add -g <tarball> → postinstall(无 npm_config_*)→ launcher 写入 + PATH 启动文件写入 + launcher 可执行; 真实 3.18.9 tarball 上 bun pm -g trust 后 botmux --version = 3.18.9。npm 路径逐字未变 (env 信号单独仍成立)。反变异 3/3 全红。 测试:把原「缺 dist 时 fail-closed」用例反转为「发布形态(无 dist)必须能工作」—— 原用例把缺陷写成了期望,正是它让套件在真实用户坏掉时保持绿色;新增 PARITY 矩阵(从发布 脚本抽出内联函数真跑,与分类器在 7 全局 + 4 非全局路径上比对结论)、Windows 分歧用例 (同时断言两侧)、源码守卫(postinstall 只许 import node:* 与 ./install-path-entry.mjs, 且不得出现 dist/)。
This was referenced Aug 31, 2026
|
🚀 Released in v3.18.11 |
deepcoldy
added a commit
that referenced
this pull request
Sep 1, 2026
install.sh 写启动文件前有一道
case ":$PATH:" in *":$INSTALL_DIR:"*) : ;; # already on PATH
它问的是「执行安装那个进程的 PATH 里有没有 INSTALL_DIR」,而决定
`botmux` 能不能用的是「用户以后开的终端有没有」——那是启动文件的属性,
不是当前进程的属性。这与 #1117 在 npm 侧(scripts/postinstall-bin.mjs)
修掉的是同一个错,curl 侧当时没跟上。
两者何时会分叉:botmux 自己就制造这个场景。daemon 会把 ~/.botmux/bin
前置进它 spawn 的每个 CLI 会话的 PATH(5 处 prependBotmuxBin),所以
在 botmux 会话里跑这个安装器,就会命中 skip 分支——零文件、零提示、
exit 0,而新终端里 `botmux` 依然 command not found。而自 #1047 起
npm 包没有 bin 字段,那个 launcher 是唯一入口,PATH 没配就等于没装上。
改法:删掉这层外壳,让写启动文件无条件执行。不需要这道闸,因为
下面本来就有两层各自独立的幂等,且都不依赖当前 PATH:
- path_line_present():文件里已有的行不再追加
- 写出去的那行自带 case 守卫:重复 source 也不会把 PATH 撑大
原注释整段保留并改写为「为什么不能加这道闸」,与 postinstall-bin.mjs
里同款说明保持一致。
## 实测(隔离 HOME,四格 A/B)
| dir 不在 PATH | dir 已在 PATH
修前 | 写 2 个文件 | 写 0 个(exit 0,无任何提示)← bug
修后 | 写 2 个文件 | 写 2 个文件
修后两列一致 = PATH 不再影响结果,这正是要的性质。
## 新增回归测试 test/install-sh-path-entry.test.ts(8 用例)
抽出 install.sh 里真实的 PATH 段执行,而不是断言脚本文本——原代码
文本上完全合理,错的只是「预置 PATH 下的行为」,纯文本断言抓不到。
⚠️ harness 必须整段取到 EOF:第一版从 posix_line= 起取,导致把闸
重新加回去(就加在那行上面)时 8 个用例全绿——即对本 bug 完全失明,
已实测。现改为从最后一个 helper 之后一路取到文件尾。
另加 assertHarnessIsWellFormed:本文件的症状就是「零文件」,若抽取
失败也会产出零文件,两者无法区分,故先证明 harness 语法合法且含
真实逻辑再下结论。
反变异 6/6 全红:
M1 把 $PATH 闸加回去 → 2 failed(即本 bug)
M2 per-shell 分发恒走 .profile → 4 failed
M3 path_line_present 恒 false → 2 failed
M4 写出的行丢掉自带 case 守卫 → 1 failed
M5 bash 只写 .bashrc 不写登录文件 → 3 failed
M6 awk 整元素比对改前缀比对 → 1 failed
每次变异后立即 cp 还原并 cmp 校验。
## 影响面
只动 install.sh 的 PATH 段 + 新增一个测试文件,不含 src/ 变更,
不影响 npm/pnpm/bun 安装路径(那半边由 #1117/#1119/#1122 负责)、
也不影响任何平台 / CLI / 会话类型的运行时行为。
测试:新文件 8/8 绿;install-sh-musl-asset / install-path-entry /
npm-binary-distribution 并集 99 passed,其中 npm-binary-distribution
有 2 条失败——已在干净 origin/master 的独立 worktree 上同样跑出
`2 failed | 33 passed`,逐字相同,属既有失败,与本 PR 无关。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
bun add -g botmux装完后,用户根本没有botmux命令。真机复现(bun 1.4.0,真下载 86s,隔离 HOME):这比「更新识别失效」(#1112)严重得多:那个是装好了但更新不了,这个是装了等于没装。
这是两个独立故障,本 PR 修第二个
① bun 默认 block 依赖的生命周期脚本(
Blocked 2 postinstalls)。属包管理器策略,bun pm trust可解,本 PR 不动。② 即使脚本跑起来也没用 ← 本 PR 修这个。Guard 1 是
npm_config_global !== 'true'就静默exit 0,而 bun 传给生命周期脚本的 env 里没有npm_config_global。用探针包(postinstall 自己 dump env)实测,bun 给的完整集合是:
有几个
npm_*,但恰恰缺 Guard 1 读的那一个。⟹ 所以
bun pm -g trust botmux会报1 script ran而 launcher 依然没写 —— 看起来最自然的那个 workaround 是无效的。只修 ① 用户仍然拿不到命令,这也是本 PR 必须存在的理由。onlyBuiltDependencies让脚本根本不跑,本 PR 救不了它(需 approve-builds /onlyBuiltDependencies或换机制);bun 是「trust 能跑但 Guard 1 杀掉」,修好后bun pm trust才成为真正可用的 workaround。标题因此只写 bun。改法
Guard 1 变成「env 说是全局(npm/yarn) 或 安装位置本身说是全局」。
位置判定复用仓库自己的
detectGlobalInstallManager(已被单测覆盖,且dist/随包发布,见package.json的files),不新写第二套路径匹配 —— 那正是这块反复出问题的根因(#1112 就是两套规则漂移的产物)。载入失败一律返回false(fail-closed),只会更保守不会更宽松。它防的是 repo 内
install改写共享~/.botmux/bin/botmux(本机 ~50 个 daemon 共用这一个文件;我在排查 #1112 时亲手踩过这个坑)。实测:.git+src/)unknownproj/node_modules/botmuxunknown.git/src(Guard 2 盲区)unknownnpm_config_global=true)bun验证
同一条命令的 A/B,真实 bun 全局安装:
bun pm -g trust botmux的结果1 script ran→ launcher 未写(静默无用)1 script ran→ launcher 写入,botmux --version= 3.18.8npm_config_global=true仍单独成立,实测仍写 launcher)~/.botmux/bin/botmux全程未被触碰node_modules/botmux都算全局」(= 本地依赖劫持共享 launcher)→ 2 红 ③ 载入失败改 fail-open → 3 红dist时 fail-closed)+ 1 条源码守卫(钉住「复用 classifier 而非手写匹配」)dist文件(global-install有 3 个传递依赖):只拷 1 个会 fail-closed,让用例因为错误的原因通过。这一点写进注释了,并核过真实发布包里 4 个都在bun run build、tsc --noEmit通过;该测试文件 29/29遗留(不在本 PR)
cannot run on this host(170MB 二进制在高负载下 30s probe 偏紧),重跑即过。fail-closed 本身对,但错误信息可以区分「probe 超时」与「二进制真不兼容」