Skip to content

Commit e517063

Browse files
committed
fix(server): editors opened from a boot-started server get a window on Linux
The systemd user service starts before the desktop session, so the server has no DISPLAY or WAYLAND_DISPLAY and an editor it launches exits without showing a window. When both are missing on Linux, editor launches now take DISPLAY, WAYLAND_DISPLAY and XAUTHORITY from `systemctl --user show-environment`. Browser launches are unchanged, and a missing or slow user manager leaves the server's own environment.
1 parent d53873b commit e517063

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,74 @@ it.effect.skipIf(windowsHost)(
140140
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
141141
);
142142

143+
it.effect.skipIf(windowsHost)(
144+
"a Linux editor launch without a display takes the desktop session's display from systemd",
145+
() =>
146+
Effect.gen(function* () {
147+
const fileSystem = yield* FileSystem.FileSystem;
148+
const path = yield* Path.Path;
149+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
150+
const codiumPath = path.join(binDir, "codium");
151+
yield* fileSystem.writeFileString(codiumPath, "#!/bin/sh\n");
152+
yield* fileSystem.chmod(codiumPath, 0o755);
153+
154+
const launchEnv = (sessionManager: MockSpawnResult) =>
155+
Effect.gen(function* () {
156+
const spawned: ChildProcess.StandardCommand[] = [];
157+
yield* Effect.gen(function* () {
158+
const launcher = yield* ExternalLauncher.ExternalLauncher;
159+
yield* launcher.launchEditor({ editor: "vscodium", cwd: "/workspace" });
160+
yield* launcher.launchBrowser("https://example.com");
161+
}).pipe(
162+
Effect.provide(
163+
layerTest({
164+
platform: "linux",
165+
env: { PATH: binDir },
166+
onSpawn: (command) => spawned.push(command),
167+
spawnResult: (command) =>
168+
command.command === "systemctl" ? sessionManager : undefined,
169+
}),
170+
),
171+
Effect.provideService(HostProcess.Environment, { PATH: binDir }),
172+
);
173+
// Only editors borrow the session display; a browser must not open on that screen.
174+
assert.deepEqual(spawned.find((command) => command.command === "xdg-open")?.options.env, {
175+
PATH: binDir,
176+
});
177+
return spawned.find((command) => command.command === "codium")?.options.env;
178+
});
179+
180+
assert.deepEqual(
181+
yield* launchEnv({
182+
stdout: [
183+
"DISPLAY=:0",
184+
"HOME=/home/user",
185+
"WAYLAND_DISPLAY=wayland-1",
186+
"XAUTHORITY=/run/user/1000/xauth",
187+
"",
188+
].join("\n"),
189+
}),
190+
{
191+
PATH: binDir,
192+
DISPLAY: ":0",
193+
WAYLAND_DISPLAY: "wayland-1",
194+
XAUTHORITY: "/run/user/1000/xauth",
195+
},
196+
);
197+
// Without a systemd user manager the editor still launches with the server's environment.
198+
assert.deepEqual(
199+
yield* launchEnv({
200+
spawnError: PlatformError.systemError({
201+
_tag: "NotFound",
202+
module: "ChildProcess",
203+
method: "spawn",
204+
}),
205+
}),
206+
{ PATH: binDir },
207+
);
208+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
209+
);
210+
143211
it.effect("launches an installed editor with platform-safe arguments", () =>
144212
Effect.gen(function* () {
145213
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,42 @@ const launchEditor = Effect.fn("externalLauncher.launchEditor")(function* (
708708
yield* launchEditorProcess(launch);
709709
});
710710

711+
const GRAPHICAL_SESSION_ENV_KEYS: ReadonlySet<string> = new Set([
712+
"DISPLAY",
713+
"WAYLAND_DISPLAY",
714+
"XAUTHORITY",
715+
]);
716+
717+
// A boot service starts before the desktop session and never receives the
718+
// variables a window needs, so an editor launched from it exits without one. The
719+
// systemd user manager learns them when the session starts; read them there.
720+
const readGraphicalSessionEnv = Effect.fn("externalLauncher.readGraphicalSessionEnv")(
721+
function* () {
722+
const hostEnv = yield* HostProcess.Environment;
723+
if ((yield* HostProcess.Platform) !== "linux" || hasGraphicalLinuxSession(hostEnv)) {
724+
return {};
725+
}
726+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
727+
const output = yield* spawner.string(
728+
ChildProcess.make("systemctl", ["--user", "show-environment"], {
729+
stdin: "ignore",
730+
stderr: "ignore",
731+
}),
732+
);
733+
return Object.fromEntries(
734+
output.split("\n").flatMap((line) => {
735+
const separator = line.indexOf("=");
736+
const key = line.slice(0, separator);
737+
return separator > 0 && GRAPHICAL_SESSION_ENV_KEYS.has(key)
738+
? [[key, line.slice(separator + 1)]]
739+
: [];
740+
}),
741+
);
742+
},
743+
Effect.timeout("2 seconds"),
744+
Effect.orElseSucceed((): NodeJS.ProcessEnv => ({})),
745+
);
746+
711747
const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
712748
launch: ProcessLaunch,
713749
onError: (cause: unknown) => ExternalLauncherError,
@@ -719,7 +755,7 @@ const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
719755
const { ELECTRON_RUN_AS_NODE: _runAsNode, ...env } = yield* HostProcess.Environment;
720756
const command = ChildProcess.make(launch.command, launch.args, {
721757
...launch.options,
722-
env,
758+
env: { ...env, ...launch.options.env },
723759
extendEnv: false,
724760
});
725761

@@ -773,6 +809,7 @@ const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(fu
773809
options: {
774810
detached: launch.detached ?? true,
775811
shell: spawnCommand.shell,
812+
env: yield* readGraphicalSessionEnv(),
776813
stdin: "ignore",
777814
stdout: "ignore",
778815
stderr: "ignore",

0 commit comments

Comments
 (0)