Skip to content

Commit ca3661c

Browse files
committed
fix(server): read the session display variables raw over D-Bus
`systemctl --user show-environment` shell-quotes values containing spaces or special characters as $'...', which the parser forwarded literally, so an XAUTHORITY path with a space reached the editor broken. The user manager's Environment property is now read with `busctl --json=short`, which carries the values raw.
1 parent e517063 commit ca3661c

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ it.effect.skipIf(windowsHost)(
141141
);
142142

143143
it.effect.skipIf(windowsHost)(
144-
"a Linux editor launch without a display takes the desktop session's display from systemd",
144+
"a Linux editor launch without a display takes the desktop session's display from the systemd user manager",
145145
() =>
146146
Effect.gen(function* () {
147147
const fileSystem = yield* FileSystem.FileSystem;
@@ -165,7 +165,7 @@ it.effect.skipIf(windowsHost)(
165165
env: { PATH: binDir },
166166
onSpawn: (command) => spawned.push(command),
167167
spawnResult: (command) =>
168-
command.command === "systemctl" ? sessionManager : undefined,
168+
command.command === "busctl" ? sessionManager : undefined,
169169
}),
170170
),
171171
Effect.provideService(HostProcess.Environment, { PATH: binDir }),
@@ -179,19 +179,22 @@ it.effect.skipIf(windowsHost)(
179179

180180
assert.deepEqual(
181181
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"),
182+
stdout: JSON.stringify({
183+
type: "as",
184+
data: [
185+
"DISPLAY=:0",
186+
"HOME=/home/user",
187+
"WAYLAND_DISPLAY=wayland-1",
188+
"XAUTHORITY=/home/user/auth files/cookie",
189+
],
190+
}),
189191
}),
190192
{
191193
PATH: binDir,
192194
DISPLAY: ":0",
193195
WAYLAND_DISPLAY: "wayland-1",
194-
XAUTHORITY: "/run/user/1000/xauth",
196+
// Raw, not the $'...' quoting `systemctl show-environment` would print.
197+
XAUTHORITY: "/home/user/auth files/cookie",
195198
},
196199
);
197200
// Without a systemd user manager the editor still launches with the server's environment.

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import * as Layer from "effect/Layer";
3131
import * as Option from "effect/Option";
3232
import * as Path from "effect/Path";
3333
import * as Ref from "effect/Ref";
34+
import * as Schema from "effect/Schema";
3435
import * as Scope from "effect/Scope";
3536
import * as Stream from "effect/Stream";
3637
import * as ChildProcess from "effect/process/ChildProcess";
@@ -714,6 +715,12 @@ const GRAPHICAL_SESSION_ENV_KEYS: ReadonlySet<string> = new Set([
714715
"XAUTHORITY",
715716
]);
716717

718+
// The user manager's environment over D-Bus. `systemctl show-environment` would
719+
// shell-quote values with spaces as $'...', while this JSON carries them raw.
720+
const decodeManagerEnvironment = Schema.decodeUnknownEffect(
721+
Schema.fromJsonString(Schema.Struct({ data: Schema.Array(Schema.String) })),
722+
);
723+
717724
// A boot service starts before the desktop session and never receives the
718725
// variables a window needs, so an editor launched from it exits without one. The
719726
// systemd user manager learns them when the session starts; read them there.
@@ -725,13 +732,23 @@ const readGraphicalSessionEnv = Effect.fn("externalLauncher.readGraphicalSession
725732
}
726733
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
727734
const output = yield* spawner.string(
728-
ChildProcess.make("systemctl", ["--user", "show-environment"], {
729-
stdin: "ignore",
730-
stderr: "ignore",
731-
}),
735+
ChildProcess.make(
736+
"busctl",
737+
[
738+
"--user",
739+
"--json=short",
740+
"get-property",
741+
"org.freedesktop.systemd1",
742+
"/org/freedesktop/systemd1",
743+
"org.freedesktop.systemd1.Manager",
744+
"Environment",
745+
],
746+
{ stdin: "ignore", stderr: "ignore" },
747+
),
732748
);
749+
const { data } = yield* decodeManagerEnvironment(output);
733750
return Object.fromEntries(
734-
output.split("\n").flatMap((line) => {
751+
data.flatMap((line) => {
735752
const separator = line.indexOf("=");
736753
const key = line.slice(0, separator);
737754
return separator > 0 && GRAPHICAL_SESSION_ENV_KEYS.has(key)

0 commit comments

Comments
 (0)