Skip to content
TypeScript

fix(server): apps opened from T3 Code no longer inherit ELECTRON_RUN_AS_NODE (#20206)

Jord31be3e9b49de
diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.ts
index 21c7547374d..9c0935de308 100644
--- a/apps/server/src/process/externalLauncher.test.ts
+++ b/apps/server/src/process/externalLauncher.test.ts
@@ -122,6 +122,52 @@ it.effect("launches the default browser through the platform command", () => {
);
});
+it.effect.skipIf(windowsHost)(
+ "launched browsers and editors do not inherit ELECTRON_RUN_AS_NODE",
+ () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
+ const codePath = path.join(binDir, "code");
+ yield* fileSystem.writeFileString(codePath, "#!/bin/sh\n");
+ yield* fileSystem.chmod(codePath, 0o755);
+
+ const spawned: ChildProcess.StandardCommand[] = [];
+ yield* Effect.gen(function* () {
+ const launcher = yield* ExternalLauncher.ExternalLauncher;
+ yield* launcher.launchBrowser("https://example.com");
+ yield* launcher.launchEditor({ editor: "vscode", cwd: "/workspace" });
+ }).pipe(
+ Effect.provide(
+ layerTest({
+ platform: "linux",
+ env: { PATH: binDir, DISPLAY: ":0" },
+ onSpawn: (command) => spawned.push(command),
+ }),
+ ),
+ Effect.provideService(HostProcess.Environment, {
+ PATH: binDir,
+ DISPLAY: ":0",
+ ELECTRON_RUN_AS_NODE: "1",
+ }),
+ );
+
+ assert.deepEqual(
+ spawned.map((command) => ({
+ command: command.command,
+ env: command.options.env,
+ extendEnv: command.options.extendEnv,
+ })),
+ ["xdg-open", "code"].map((command) => ({
+ command,
+ env: { PATH: binDir, DISPLAY: ":0" },
+ extendEnv: false,
+ })),
+ );
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+);
+
it.effect.each(["win32", "linux"] as const)(
"launches the browser through PowerShell on %s",
(platform) => {
diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.ts
index 13062253235..668e7f74690 100644
--- a/apps/server/src/process/externalLauncher.ts
+++ b/apps/server/src/process/externalLauncher.ts
@@ -663,7 +663,15 @@ const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
onError: (cause: unknown) => ExternalLauncherError,
): Effect.fn.Return<void, ExternalLauncherError, ChildProcessSpawner.ChildProcessSpawner> {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
- const command = ChildProcess.make(launch.command, launch.args, launch.options);
+ // The desktop app runs this server with ELECTRON_RUN_AS_NODE. A launched file
+ // manager or browser passes its environment on to everything started from it,
+ // so any Electron app opened from there would start as plain Node and exit.
+ const { ELECTRON_RUN_AS_NODE: _runAsNode, ...env } = yield* HostProcess.Environment;
+ const command = ChildProcess.make(launch.command, launch.args, {
+ ...launch.options,
+ env,
+ extendEnv: false,
+ });
yield* spawner.spawn(command).pipe(
Effect.flatMap((handle) => handle.unref),