Repository navigation
fix(install): 位置判定内联进 postinstall,修掉「import 发布包里不存在的 dist/」 - #1122
Merged
Merged
Conversation
#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/` 都不发布,**自包含是它的天然形态**;相比之下「加 4 个 dist 文件回 files」会与 #1115「不发布 Node 形态」的意图冲突。⚠️ 内联版**故意比 `detectGlobalInstallManager` 更严格**:后者结尾是 `platform === 'win32' ? 'npm' : 'unknown'`,实测 `detectGlobalInstallManager('C:/projects/myapp/node_modules/botmux', 'win32')` = `'npm'` —— 在 update 路径无害(顶多重装一次),但在 postinstall 里意味着 **Windows 本地依赖 改写共享 `~/.botmux/bin/botmux`**,正是 Guard 1 要防的劫持。所以内联版只认已知全局布局、 无平台兜底;Windows npm 全局带 `npm_config_global=true`,不经过这条检查,不受影响。 顺带修掉注释里那句已经变成假话的「shipped in `dist/` (see package.json `files`)」, 并删掉不再使用的 `pathToFileURL` import。 测试: - 把原「缺 dist 时 fail-closed」那条**反转**为「发布形态(无 dist)必须能工作」—— 原来那条恰恰把缺陷写成了期望,是它让套件在真实用户坏掉时保持绿色 - 新增 PARITY 矩阵:从发布脚本里**抽出内联函数真跑**,与 `detectGlobalInstallManager` 在 7 种真实全局布局 + 4 种非全局路径上逐条比对结论(不是比源码文本) - 新增 Windows 分歧用例:**同时断言两侧**(分类器给 `'npm'`、内联版给 `false`), 把这处刻意的不一致钉成文档 - 新增源码守卫:postinstall 除 `node:*` 与 `./install-path-entry.mjs` 外**不得 import 任何东西**,且源码不得出现 `dist/` —— 防止再次依赖发布包里没有的文件 该测试文件 35/35,`bun run build` 通过。
This was referenced Aug 31, 2026
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.
问题:#1119 在发布形态下等于没生效
#1119 给 Guard 1 加了「安装位置是否是全局布局」这条判定,实现是
await import('./dist/utils/global-install.js')复用仓库自己的分类器。但 #1115 已经把
dist/从package.json的files里移除了(不再发布 Node 形态)⟹ 发布包里那个 import 必然 ENOENT ⟹catch返回 false ⟹ Guard 1 退回成只看 env ⟹bun add -g botmux与修之前一样没有botmux命令。fail-closed 那一层是对的(不崩、不误写),但修复本身是死代码。
判据是真打包,不是读代码:
已发布的 3.18.9 实测同样只有 6 个文件(LICENSE / package.json / 2×README / 2×scripts),零
dist。端到端也验了:真bun add -g <该 tarball>→ 按 bun 的方式跑 postinstall →EXIT=0、无 launcher。诊断收窄到唯一原因:把那个安装路径喂给分类器返回
bun⟹ 判定逻辑本身是对的,唯一阻塞就是模块在运行现场不存在。改法:内联,不是妥协
把布局判定内联进
postinstall-bin.mjs。它是无构建步骤的独立脚本,dist/与src/都不发布 ⟹ 自包含是它的天然形态。对比「把 4 个 dist 文件加回
files」:技术上可行,但与 #1115「不发布 Node 形态」的意图冲突,不该在这个修复里动别人 PR 的意图。detectGlobalInstallManager更严格(复审抓到,我复现确认)那个函数结尾是
platform === 'win32' ? 'npm' : 'unknown',所以在 Windows 上分不清全局安装和本地依赖。实测:在 update 路径无害(顶多多重装一次),但在 postinstall 里就是 Windows 本地依赖改写共享
~/.botmux/bin/botmux—— 正是 Guard 1 存在的理由。所以内联版只认已知全局布局、无平台兜底。Windows npm 全局带npm_config_global=true,不经过这条检查,不受影响。验证
端到端,发布形态,零手工
cp:(中途一次
no prebuilt binary package是我本地 tarball 版本为0.0.0、没有平台子包所致 —— Guard 1 已经放行、脚本走到了二进制查找并大声失败,补上平台子包即写入 launcher。)~/.botmux/bin/botmux未被触碰dist/(= 本次的死代码缺陷)→ 3 红bun run build、tsc --noEmit通过;该测试文件 35/35测试改动
detectGlobalInstallManager在 7 种真实全局布局 + 4 种非全局路径上逐条比对结论 ⟹ 两套实现漂移有测试兜底'npm'、内联版给false),把这处刻意的不一致钉成文档而不是注释里的口头承诺node:*与./install-path-entry.mjs外不得 import 任何东西,且源码不得出现dist/⟹ 防止再次依赖发布包里没有的文件遗留(不在本 PR)
detectGlobalInstallManager的 win32 兜底对本地依赖误判,建议在 update 路径也收窄(pre-existing,不急)onlyBuiltDependencies),需pnpm approve-builds -g botmux两步走,文档待补