fix(desktop): DSH_PROFILE 兜底 + console.warn 污染治理 + --updated 抢前台

- patch #82 main.js 启动顶部补 DSH_PROFILE=desktop 兜底 (双击 exe 从 explorer 继承 env 常缺 → host "cannot determine the boot profile" crash → dsh-purge 没 activate → inject 全断)
- lib/desktop.js 全部 console.warn("[dsh-purge] ...") 换成 logInternal() 写内部 log (不污染 host stderr → main.js:3729 fail 回调不再误报假 crash)
- lib/restart-desktop.js spawnEnv() 补 DSH_PROFILE + spawn argv 加 --updated (复用 Electron main.js:11256 raiseAfterUpdate 内部 moveTop+focus 强制前台,比外部 PS SetForegroundWindow 可靠) + PS 兜底保留做双保险
This commit is contained in:
叫我小杨同学 committed 2026-10-09 20:49:41 +08:00
1 parent 288c9dbefd
commit f184ae222d
3 files changed
+138 -17

No files matched your search

+44 -15
View File
@@ -15,6 +15,19 @@ function restartUniqSuffix() {
// 无归档时用 node:fs。app.asar 还在时不能把映射出来的 resources/app 当成已经解开。
const fs = hostFs;
// [dsh-purge-fix 2026-10-09] console.warn 写 host stderr → main.js:3729 host.exitPromise
// fail 回调把最后一行 stderr 当 Error reason。helper restart 期间任何 warn 都可能
// 被误判成 crash log。改走内部文件,不污染 stderr。
const INTERNAL_LOG_PATH = path.join(os.tmpdir(), "dsh-purge-desktop.log");
function logInternal(msg) {
try {
const line = `${new Date().toISOString()} [dsh-purge] ${String(msg)}\n`;
fs.appendFileSync(INTERNAL_LOG_PATH, line, "utf8");
} catch {
/* 日志写不进去就吞,绝不走 stderr */
}
}
/** 启动一次:清掉旧版本留下的无 pid 后缀的 restart 脚本/日志(超过 24h)。 */
export function cleanupLegacyRestartScripts() {
const tmp = os.tmpdir();
@@ -1064,18 +1077,18 @@ export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit
// 没 runtime —— 短 timeout 给 PowerShell 启动完成的时间(实测 ~200-500ms),
// 然后主动退出。PowerShell 会在这期间杀掉 Electron,即便 host 自己退出
// Electron 也已经不在了,不会弹 dialog。
try { console.warn("[dsh-purge] asar swap: no runtime handoff, relying on PowerShell to kill Electron first"); } catch {}
logInternal("asar swap: no runtime handoff, relying on PowerShell to kill Electron first");
setTimeout(() => { try { process.exit(0); } catch {} }, 1500);
return;
}
try { console.warn("[dsh-purge] asar swap: handing off restart to desktopRuntime"); } catch {}
logInternal("asar swap: handing off restart to desktopRuntime");
try {
// #64:requestRestart 可能"空返回"但副作用也许仍标记预期退出,双保险之一。
Promise.resolve(runtime.requestRestart()).catch((err) => {
try { console.warn("[dsh-purge] desktopRuntime.requestRestart rejected:", err?.message || err); } catch {}
logInternal("desktopRuntime.requestRestart rejected: " + (err?.message || err));
});
} catch (err) {
try { console.warn("[dsh-purge] desktopRuntime.requestRestart threw:", err?.message || err); } catch {}
logInternal("desktopRuntime.requestRestart threw: " + (err?.message || err));
}
// 2000ms 兜底:给宿主 requestRestart 做完 + PowerShell 启动完成。
// 即便两条路径都挂,PowerShell 也会在 ~500ms 内 kill Electron,不会弹 crash。
@@ -1119,7 +1132,7 @@ export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit
// 没挂 'error' listener,PowerShell 不在 PATH / 被 AV 拦就是 unhandled 'error' event,
// 会被 Node 当 uncaughtException 直接把宿主崩掉。
child.on("error", (err) => {
try { console.warn("[dsh-purge] asar swap PowerShell spawn failed:", err?.message || err); } catch {}
logInternal("asar swap PowerShell spawn failed: " + (err?.message || err));
});
child.unref();
if (exit !== false) gracefulExit();
@@ -1216,7 +1229,7 @@ export function scheduleRestart(request, ctx, options = {}) {
if (runtime) {
// 只走桌面整应用 relaunch,不要 exit Host(那会先把内嵌 web 单独拉起来)。
Promise.resolve(runtime.requestRestart()).catch((err) => {
console.warn("[dsh-purge] desktop requestRestart failed:", err);
logInternal("desktop requestRestart failed: " + (err?.message || err));
});
return { restarting: true };
}
@@ -1444,15 +1457,31 @@ export function restartOfficialHarness(options = {}) {
try { fs.unlinkSync(inflight); } catch { /* 没有残留标记 */ }
}
if (plan.kind === "powershell") {
// [dsh-purge-fix 2026-10-09] asar 已被挪走(unpacked 模式常态)时,再 schedule
// 一次 PowerShell kill+swap 没意义:fs.rename ENOENT,但 PowerShell 已经 kill
// Electron 一次 → 用户看到启动闪屏 + 启动慢 (多拉一次 Electron)。此处 early-
// return,让宿主自然启动走 unpacked /app/,不再多 kill 一次。
// [dsh-purge-fix 2026-10-09] asar 已被挪走(unpacked 模式常态):
// ASAR_SWAP_PS1 fs.rename asar 会 ENOENT, 返 restarting:false 时 UI 显示
// "失败: not started"。改走 helper restart-desktop.js:它的 moveAsarAsideAsync
// 本身就处理 asar=null(第 283 行 fs.existsSync 不存在直接 return true),
// relaunchDesktop 走 execPath spawn,干净 kill+relaunch,无 asar swap。
if (!asar || !asarArchiveIsFile(asar)) {
console.warn("[dsh-purge] asar 已不存在(unpacked 模式),跳过 PowerShell swap restart");
return { restarting: false, asarMissing: true };
logInternal("asar 已不存在(unpacked 模式),改走 helper kill+relaunch");
try {
const helper = fileURLToPath(new URL("./restart-desktop.js", import.meta.url));
spawnHelper(helper, {
respawn: true,
killApp: true,
asar: null,
execPath: exe,
cwd: path.dirname(exe),
waitPid: process.pid,
}, { exit: options.exit !== false });
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logInternal("unpacked helper restart failed: " + message);
return { restarting: false, error: message };
}
return { restarting: true, mode: "unpacked-helper", engine: "helper" };
}
console.warn("[dsh-purge] wscript 不能执行 .js,改用 PowerShell 挪开 app.asar");
logInternal("wscript 不能执行 .js,改用 PowerShell 挪开 app.asar");
try {
scheduleAsarSwapRestart({
exe,
@@ -1463,7 +1492,7 @@ export function restartOfficialHarness(options = {}) {
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn("[dsh-purge] PowerShell asar swap failed:", message);
logInternal("PowerShell asar swap failed: " + message);
return { restarting: false, error: message };
}
return { restarting: true, asarSwap: true, engine: "powershell" };
@@ -1509,7 +1538,7 @@ export function scheduleCleanupRestart({ cleanup = [], ctx } = {}) {
if (runtime) {
spawnHelper(dest, { cleanup, helper: dest, respawn: false }, { exit: false });
Promise.resolve(runtime.requestRestart()).catch((err) => {
console.warn("[dsh-purge] desktop uninstall restart failed:", err);
logInternal("desktop uninstall restart failed: " + (err?.message || err));
});
return { restarting: true };
}