fix: 二次 Apply 不再误报 #26/#27,重启等到会话树落地再进页面

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yangandCursor committed 2026-09-17 13:33:34 +08:00
1 parent 6c88f4d075
commit fd4dfa4e38
11 files changed
+254 -432

No files matched your search

+3 -33
View File
@@ -1,13 +1,3 @@
// Force every child_process launch on Windows to use CREATE_NO_WINDOW.
// Must load before other plugins spawn work — import this module for side effects,
// or call installHideConsole() explicitly from apply().
//
// Node 24: ESM `import { spawn } from "node:child_process"` is NOT a live
// binding to module.exports.spawn. Monkey-patching require("child_process")
// alone does nothing for DSH packages. We register an import hook that
// redirects node:child_process → ./child-process-hide.mjs (which wraps the
// builtin via process.getBuiltinModule).
import { createRequire, registerHooks } from "node:module";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
@@ -70,12 +60,9 @@ function facadeUrl() {
return pathToFileURL(fileURLToPath(new URL("./child-process-hide.mjs", import.meta.url))).href;
}
/** Redirect ESM/CJS resolution of child_process to the windowsHide facade. */
export function installChildProcessImportHook() {
if (process.platform !== "win32") return { ok: true, skipped: "not_win32" };
// DSH Desktop runs on Electron. Electron’s overrideChildProcess patches
// child_process by assigning properties (e.g. exec) onto the module object,
// which throws on a frozen ESM namespace. Skip the redirect on Electron hosts.
// Electron 会给 child_process 赋值;ESM namespace 冻结后会抛错,桌面宿主只做 wrap。
if (typeof process.versions !== "undefined" && process.versions.electron) {
return { ok: true, skipped: "electron_host" };
}
@@ -84,8 +71,6 @@ export function installChildProcessImportHook() {
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier === "node:child_process" || specifier === "child_process") {
// Do not redirect the facade's own getBuiltinModule path (it never
// resolves via this specifier). Short-circuit every importer.
return { shortCircuit: true, url: target, format: "module" };
}
return nextResolve(specifier, context);
@@ -95,12 +80,11 @@ export function installChildProcessImportHook() {
return { ok: true, hooked: true };
}
/** Patch node:child_process in this process (CJS require path). Idempotent. */
export function installHideConsole() {
if (process.platform !== "win32") return { ok: true, skipped: "not_win32" };
const hook = installChildProcessImportHook();
const require = createRequire(import.meta.url);
// Prefer builtin so we patch the real object the facade also wraps.
// 改 builtin 本体,和 facade 包的是同一对象。
const cp =
typeof process.getBuiltinModule === "function"
? process.getBuiltinModule("child_process")
@@ -119,11 +103,7 @@ export function installHideConsole() {
return { ok: true, patched: true, hook };
}
/** Source written next to DSH `lib/bin.js` so the entry imports it first. */
export function hideConsoleEntrySource() {
// Keep the side file self-contained: registerHooks + wrap builtin.
// The facade lives beside this file as dsh-purge-child-process-hide.mjs
// (copied by installHideConsoleIntoBin).
return `// ${IMPORT_MARK} — auto-installed by dsh-purge; do not edit
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
@@ -132,11 +112,9 @@ const HOOK_MARK = "__dshPurgeChildProcessHook";
const MARK = ${JSON.stringify(MARK)};
const facade = pathToFileURL(fileURLToPath(new URL("./dsh-purge-child-process-hide.mjs", import.meta.url))).href;
// DSH Desktop (Electron) must NOT redirect child_process to the ESM facade:
// Electron’s overrideChildProcess assigns e.g. exec onto the module object,
// and a frozen ESM namespace throws TypeError. Only the safe wrapping below runs.
const isElectron = typeof process.versions !== "undefined" && !!process.versions.electron;
// Electron 宿主不改 resolve;只 wrap builtin。
if (process.platform === "win32" && !isElectron && !globalThis[HOOK_MARK]) {
const { registerHooks } = await import("node:module");
registerHooks({
@@ -195,9 +173,6 @@ function requirePath() {
return createRequire(import.meta.url)("node:path");
}
/**
* Resolve the @deepseek-ai/dsh package root that actually owns lib/bin.js.
*/
export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) {
const nodeFs = fs ?? requireFs();
const p = nodePath ?? requirePath();
@@ -229,9 +204,6 @@ export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) {
const IMPORT_LINE = `import "./${IMPORT_MARK}.js"; // [dsh-purge] hide-console`;
/**
* Drop hide-console next to bin.js and ensure it is the first import.
*/
export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) {
if (process.platform !== "win32") return "skipped_not_win32";
const nodeFs = fs ?? requireFs();
@@ -251,7 +223,6 @@ export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) {
let text = nodeFs.readFileSync(binJs, "utf8");
if (text.includes(IMPORT_MARK) && text.includes(IMPORT_LINE.split(" //")[0])) {
// Side + facade refreshed above; bin.js import already present.
return "already_injected";
}
text = text.replace(/^import "\.\/dsh-purge-hide-console\.js";.*\r?\n/m, "");
@@ -268,5 +239,4 @@ export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) {
return "injected";
}
// Side-effect install when this module is imported on win32
installHideConsole();