fix(#91+#88): 兜底扫描不跨挂载点 + revertShim symlink 保护

#91 macOS 官方宿主启动卡死 (KeithW0726):
  - findAiBaseRecursive 加 rootDev 参数,readdir 下钻时跳过跨设备子目录
    (云盘/NFS/SMB 挂载点每层 readdir 是网络往返,CloudDrive 下几万个目录会把 apply() 挂死)
  - findAiBaseWeb 兜底扫描根用 path.normalize + Set 去重
    (DSH_HOME/DSH_HOME/..  / $HOME 常撞成同一路径,原来 $HOME 整个扫两遍)

#88 revertShim 跟随符号链接把旧备份写回目标 (9agj PR):
  - restoreFromBak 用 lstatSync 判定 fp 是否 symlink
  - 是 symlink 则 unlink bak 丢弃陈旧备份,返回 _skipped_symlink
    (DSH 跨版本升级后旧 bak 的 chunk 名已失效,写回会把新 bin.js 顶掉导致
    ERR_MODULE_NOT_FOUND)
  - hostFs.promises 白名单无 lstat,所以必须走同步 fs.lstatSync

#89 (mergeLayers over=undefined 丢 base) 早已在 288c9db 修复
#90 (/IM /T 自杀) 早已在 eed95f79 改为 install-root 过滤 + per-PID 杀
This commit is contained in:
叫我小杨同学 committed 2026-10-09 23:45:48 +08:00
1 parent 775e90f34f
commit f18ad6f75e
1 file changed
+40 -3
+40 -3
View File
@@ -1772,8 +1772,15 @@ function findAiBaseWeb() {
}
const needle = path.join("node_modules", nested);
// #91:三个兜底根(DSH_HOME / DSH_HOME/.. / $HOME)在容器/非标准布局下常撞到同一目录,
// 原来会把 $HOME 整个扫两遍。normalize+Set 去重后至少省掉重复的几万个 readdir。
const scannedRoots = new Set();
for (const sr of [findDshHome(), path.join(findDshHome(), ".."), os.homedir()]) {
const found = findAiBaseRecursive(sr, needle, 0);
if (!sr) continue;
const rootKey = path.normalize(sr);
if (scannedRoots.has(rootKey)) continue;
scannedRoots.add(rootKey);
const found = findAiBaseRecursive(rootKey, needle, 0);
if (found && !desktop.isInsideDesktopInstall(found)) return found;
}
return null;
@@ -2234,7 +2241,7 @@ export function findAiBase() {
});
}
function findAiBaseRecursive(root, needle, depth, visited) {
function findAiBaseRecursive(root, needle, depth, visited, rootDev) {
if (depth > 6) return null;
// H21:realpath + Set 防 symlink 循环,否则 pnpm/junction 环会把栈打爆。
const seen = visited || new Set();
@@ -2243,6 +2250,11 @@ function findAiBaseRecursive(root, needle, depth, visited) {
const key = String(real).replace(/\\/g, "/").toLowerCase();
if (seen.has(key)) return null;
seen.add(key);
// #91:第一次调用记下根设备号,之后 readdir 下钻时不跨设备 — 云盘/NFS/SMB 每层 readdir
// 是一次网络往返,挂载点下有巨型目录树(如 macFUSE CloudDrive)会把 apply() 挂死永不返回。
if (rootDev === undefined) {
try { rootDev = fs.statSync(root).dev; } catch { rootDev = null; }
}
let entries;
try {
entries = fs.readdirSync(root, { withFileTypes: true });
@@ -2251,6 +2263,13 @@ function findAiBaseRecursive(root, needle, depth, visited) {
}
for (const e of entries) {
if (!e.isDirectory()) continue;
const child = path.join(root, e.name);
// #91:跨挂载点不下钻 — 本盘枚举几千个本地目录没问题,踩进云盘就卡死
if (rootDev !== null) {
let childDev;
try { childDev = fs.statSync(child).dev; } catch { continue; }
if (childDev !== rootDev) continue;
}
if (e.name === "node_modules") {
const nestedHit = path.join(root, needle);
if (isAiBase(nestedHit)) return path.normalize(nestedHit);
@@ -2261,7 +2280,7 @@ function findAiBaseRecursive(root, needle, depth, visited) {
if (pnpmHit) return pnpmHit;
}
}
const sub = findAiBaseRecursive(path.join(root, e.name), needle, depth + 1, seen);
const sub = findAiBaseRecursive(child, needle, depth + 1, seen, rootDev);
if (sub) return sub;
}
return null;
@@ -7037,6 +7056,24 @@ export async function patchAllShims() {
}
async function restoreFromBak(fp, bak, status) {
// #88:fp 可能是符号链接(npm 的 bin 链接指向包内真实文件)。copyFile 会跟随链接,把备份内容
// 写进目标文件;而备份一旦跨版本升级就是陈旧的,这一步就把新版本的文件顶掉了。
// 实测:DSH 0.1.7-rc.2 → 0.2.0-rc.2 升级后,/usr/local/bin/dsh 的旧备份(11237 字节的旧
// bin.js)覆盖了 lib/bin.js,而 chunk 已是新版本 → `dsh plugin` 报
// ERR_MODULE_NOT_FOUND: plugin-DkYIj96-.js(旧版 chunk 名,磁盘上只有新版的 plugin-BGnVfe_D.js)。
// 符号链接本身没有「内容」可还原,所以只丢弃陈旧备份,绝不写目标。
// 注意:hostFs.promises 是白名单(只有 readFile/writeFile/mkdir/unlink/rm/copyFile),
// 没有 lstat,所以符号链接判定必须走同步的 lstatSync。
let link = false;
try {
link = fs.lstatSync(fp).isSymbolicLink();
} catch {
// 目标不存在:交给下面的 copyFile 按原有语义报错
}
if (link) {
await fsp.unlink(bak);
return `${status}_skipped_symlink`;
}
await fsp.copyFile(bak, fp);
await fsp.unlink(bak);
return status;