Repository navigation
[Bug][Windows][Desktop 0.2.0-rc.2] 从会话头部打开 VS Code 恒失败(launch-failed):宿主继承的 ELECTRON_RUN_AS_NODE 被透传给外部 Electron 应用 #8652
zechenchen
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
正文
TL;DR
Windows 桌面版里,会话头部「Open In…」分体按钮选中 VS Code 后点击 → 提示「打开失败,请重试」(快捷键 Ctrl+Alt+O 则完全无反应)。 应用探测是正常的(GET /open-in-app/apps 返回含 vscode),POST /open-in-app/open 稳定回 502 launch-failed。
根因:桌面壳用 Electron 的 Node 模式启动宿主进程,因此宿主的 process.env 里一直带着 ELECTRON_RUN_AS_NODE=1;dsh-host-open-in-app 的 launchDetachedApp() 用 scrubbedParentEnv() 构造子进程环境,而该函数只剔除凭证形状的名字(KEY|PASSWORD|SECRET|TOKEN)和 DSH_*,于是这个开关被原样传给了 Code.exe —— VS Code 以 Node 模式启动、把目录参数当脚本加载、退出码 1,落在 launchWatchMs 看护窗内被判为启动失败。
只有 Electron 系目标中招(VS Code / VS Code Insiders / Cursor / Windsurf / GitHub Desktop);Explorer、IntelliJ、Git Bash 等不受影响。
根因
变量来源(桌面壳):resources/app.asar/lib/main.js 的 desktopNodeEnvironment() 给 Node 模式的宿主子进程注入该变量(发布包里 ELECTRON_RUN_AS_NODE: "1" 在第 3532 行附近),宿主因此全程带着它,并向下继承给所有子进程。
透传:packages/host/open-in-app/src/resolver.ts 的 launchDetachedApp()(发布包 lib/index.js 348–380 行):
ts
const child = spawn(command, [...args], {
detached: true, stdio: 'ignore', windowsHide: options.windowsHide,
env: { ...scrubbedParentEnv(), ...options.env }, // ← 发布包 354 行
})
过滤不彻底:packages/subprocess/subprocess/src/index.ts 的 scrubbedParentEnv()(发布包 lib/index.js 32、50–56 行)只剔除 SENSITIVE_ENV_PATTERN = /KEY|PASSWORD|SECRET|TOKEN/i 与 DSH_* 前缀,ELECTRON_RUN_AS_NODE 两条都不命中。
结果:Code.exe <目录> 等价于 node <目录>,秒退 1 → runLaunch() 归类 failed → 路由回 502(lib/index.js 1390–1399 行)→ 前端 path.openError toast。
为什么这是"该删的没删"而不是"目标应用的问题":这个开关描述的是宿主的运行时形态,对任何外部 GUI 程序都是污染;同仓库 packages/ptc-runtime/node/src/index.ts(发布包 dsh-ptc-runtime-node/lib/index.js:956)在为子进程构造环境时已经显式排除了这个名字,说明团队清楚它会外泄 —— 只是 open-in-app 这条路径漏了。GitHub Desktop 适配器是靠 launch.env: { ELECTRON_RUN_AS_NODE: '1' } 显式要求它的(catalog.ts),而 options.env 在 scrub 之后合并,所以修法只要保证"删除发生在合并之前"就不会破坏它。
All reactions