Skip to content

Commit 9fb62db

Browse files
committed
fix(server): apps opened from T3 Code no longer inherit ELECTRON_RUN_AS_NODE
The desktop app runs the server with ELECTRON_RUN_AS_NODE=1, and the external launcher spawned browsers, editors and the file manager with the server's full environment. Anything started from those apps inherited the flag, so Electron apps launched from them ran as plain Node and exited. Refs #16456
1 parent e3b35d1 commit 9fb62db

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,52 @@ it.effect("launches the default browser through the platform command", () => {
122122
);
123123
});
124124

125+
it.effect.skipIf(windowsHost)(
126+
"launched browsers and editors do not inherit ELECTRON_RUN_AS_NODE",
127+
() =>
128+
Effect.gen(function* () {
129+
const fileSystem = yield* FileSystem.FileSystem;
130+
const path = yield* Path.Path;
131+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
132+
const codePath = path.join(binDir, "code");
133+
yield* fileSystem.writeFileString(codePath, "#!/bin/sh\n");
134+
yield* fileSystem.chmod(codePath, 0o755);
135+
136+
const spawned: ChildProcess.StandardCommand[] = [];
137+
yield* Effect.gen(function* () {
138+
const launcher = yield* ExternalLauncher.ExternalLauncher;
139+
yield* launcher.launchBrowser("https://example.com");
140+
yield* launcher.launchEditor({ editor: "vscode", cwd: "/workspace" });
141+
}).pipe(
142+
Effect.provide(
143+
layerTest({
144+
platform: "linux",
145+
env: { PATH: binDir, DISPLAY: ":0" },
146+
onSpawn: (command) => spawned.push(command),
147+
}),
148+
),
149+
Effect.provideService(HostProcess.Environment, {
150+
PATH: binDir,
151+
DISPLAY: ":0",
152+
ELECTRON_RUN_AS_NODE: "1",
153+
}),
154+
);
155+
156+
assert.deepEqual(
157+
spawned.map((command) => ({
158+
command: command.command,
159+
env: command.options.env,
160+
extendEnv: command.options.extendEnv,
161+
})),
162+
["xdg-open", "code"].map((command) => ({
163+
command,
164+
env: { PATH: binDir, DISPLAY: ":0" },
165+
extendEnv: false,
166+
})),
167+
);
168+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
169+
);
170+
125171
it.effect.each(["win32", "linux"] as const)(
126172
"launches the browser through PowerShell on %s",
127173
(platform) => {

‎apps/server/src/process/externalLauncher.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,15 @@ const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
659659
onError: (cause: unknown) => ExternalLauncherError,
660660
): Effect.fn.Return<void, ExternalLauncherError, ChildProcessSpawner.ChildProcessSpawner> {
661661
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
662-
const command = ChildProcess.make(launch.command, launch.args, launch.options);
662+
// The desktop app runs this server with ELECTRON_RUN_AS_NODE. A launched file
663+
// manager or browser passes its environment on to everything started from it,
664+
// so any Electron app opened from there would start as plain Node and exit.
665+
const { ELECTRON_RUN_AS_NODE: _runAsNode, ...env } = yield* HostProcess.Environment;
666+
const command = ChildProcess.make(launch.command, launch.args, {
667+
...launch.options,
668+
env,
669+
extendEnv: false,
670+
});
663671

664672
yield* spawner.spawn(command).pipe(
665673
Effect.flatMap((handle) => handle.unref),

0 commit comments

Comments
 (0)