Skip to content

Commit 59eb93f

Browse files
fix(desktop): preserve relaunch identity and startup environment
1 parent d2f8b3b commit 59eb93f

5 files changed

Lines changed: 43 additions & 29 deletions

File tree

‎apps/desktop/src/app/DesktopConfig.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ export const DesktopConfig = Config.all({
5555
otlpProtocol: Config.schema(OtlpProtocol, "T3CODE_OTLP_PROTOCOL").pipe(
5656
Config.withDefault("http/json"),
5757
),
58-
appImagePath: Config.String("APPIMAGE").pipe(
59-
Config.option,
60-
Config.map(Option.filter((value) => value.trim().length > 0)),
61-
),
58+
appImagePath: trimmedString("APPIMAGE"),
6259
disableAutoUpdate: optionalBoolean("T3CODE_DISABLE_AUTO_UPDATE"),
6360
mockUpdates: optionalBoolean("T3CODE_DESKTOP_MOCK_UPDATES"),
6461
mockUpdateServerPort: Config.Port("T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT").pipe(

‎apps/desktop/src/app/DesktopEnvironment.test.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ const makeEnvironment = (
3939
) => DesktopEnvironment.DesktopEnvironment.pipe(Effect.provide(layerEnvironment(overrides, env)));
4040

4141
describe("DesktopEnvironment", () => {
42-
it.effect("preserves whitespace in the packaged AppImage launcher path", () =>
43-
Effect.gen(function* () {
44-
const APPIMAGE = " /tmp/T3 Code.AppImage ";
45-
const environment = yield* makeEnvironment(
46-
{ platform: "linux", isPackaged: true },
47-
{ APPIMAGE },
48-
);
49-
assert.deepEqual(environment.appImagePath, Option.some(APPIMAGE));
50-
}),
51-
);
52-
5342
it.effect("derives state paths and development identity inside Effect", () =>
5443
Effect.gen(function* () {
5544
const environment = yield* makeEnvironment(

‎apps/desktop/src/backend/DesktopBackendPool.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { assert, describe, it } from "@effect/vitest";
2-
import { vi } from "vite-plus/test";
32
import * as Duration from "effect/Duration";
43
import * as Effect from "effect/Effect";
54
import * as FileSystem from "effect/FileSystem";
@@ -22,8 +21,6 @@ import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
2221
import * as DesktopBackendPool from "./DesktopBackendPool.ts";
2322
import type { DesktopBackendSnapshot, DesktopBackendStartConfig } from "./DesktopBackendManager.ts";
2423

25-
vi.mock("electron", () => ({ app: { isPackaged: false } }));
26-
2724
function makeStubInstance(
2825
id: DesktopBackendPool.BackendInstanceId,
2926
label: string,

‎apps/desktop/src/electron/ElectronApp.test.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ describe("ElectronApp", () => {
119119
{ platform: "linux", APPIMAGE: " ", packaged: true },
120120
{ platform: "linux", APPIMAGE: "/tmp/app.AppImage", packaged: false },
121121
{ platform: "darwin", APPIMAGE: "/tmp/app.AppImage", packaged: true },
122+
{
123+
platform: "linux",
124+
APPIMAGE: "/tmp/other.AppImage",
125+
packaged: true,
126+
executablePath: "/tmp/mount-other/t3code",
127+
},
122128
] as const)("keeps native relaunch outside packaged AppImages: %j", (input) => {
123129
isPackagedMock.mockReturnValue(input.packaged);
124130
return Effect.gen(function* () {
@@ -130,7 +136,14 @@ describe("ElectronApp", () => {
130136
}).pipe(
131137
Effect.provide(electronAppLayer),
132138
Effect.provideService(HostProcess.Platform, input.platform),
133-
Effect.provideService(HostProcess.Environment, { APPIMAGE: input.APPIMAGE }),
139+
Effect.provideService(HostProcess.Environment, {
140+
APPIMAGE: input.APPIMAGE,
141+
APPDIR: "/tmp/mount",
142+
}),
143+
Effect.provideService(
144+
HostProcess.ExecutablePath,
145+
"executablePath" in input ? input.executablePath : "/tmp/mount/t3code",
146+
),
134147
);
135148
});
136149

@@ -144,10 +157,10 @@ describe("ElectronApp", () => {
144157
const output = path.join(directory, "result");
145158
const startup = path.join(directory, "startup");
146159
const startupMarker = path.join(directory, "startup-ran");
147-
yield* fs.writeFileString(startup, `printf ran > "${startupMarker}"\nexec </dev/null\n`);
160+
yield* fs.writeFileString(startup, `printf ran >> "${startupMarker}"\nexec </dev/null\n`);
148161
yield* fs.writeFileString(
149162
launcher,
150-
'#!/bin/sh\nif [ -e /proc/self/fd/3 ] || [ -e /proc/self/fd/65 ]; then exit 65; fi\nif [ -n "$BASH_ENV$ENV" ]; then exit 66; fi\nprintf "%s\\n" "$@" > "$1"\n',
163+
'#!/usr/bin/env bash\nif [ -e /proc/self/fd/3 ] || [ -e /proc/self/fd/65 ]; then exit 65; fi\nprintf "%s\\n" "$BASH_ENV" "$ENV" "$@" > "$1"\n',
151164
);
152165
yield* fs.chmod(launcher, 0o700);
153166
let command: ChildProcess.StandardCommand | undefined;
@@ -181,11 +194,16 @@ describe("ElectronApp", () => {
181194
Effect.provide(ElectronApp.layer),
182195
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, recordingSpawner),
183196
Effect.provideService(HostProcess.Platform, "linux"),
197+
Effect.provideService(HostProcess.ExecutablePath, path.join(directory, "mount", "t3code")),
184198
Effect.provideService(HostProcess.Environment, {
199+
...process.env,
185200
APPIMAGE: launcher,
201+
APPDIR: path.join(directory, "mount"),
186202
BASH_ENV: startup,
187203
ENV: startup,
188-
PATH: process.env.PATH,
204+
SHELLOPTS: "braceexpand:hashall:interactive-comments",
205+
// Node uses a socket for stdin; skip Bash's remote-shell startup heuristic.
206+
SHLVL: "1",
189207
}),
190208
);
191209
assert.equal(relaunchMock.mock.calls.length, 0);
@@ -194,7 +212,6 @@ describe("ElectronApp", () => {
194212
const waiting = yield* spawner.spawn(
195213
ChildProcess.make(command!.command, command!.args, {
196214
...command!.options,
197-
env: command!.options.env ?? { ...process.env, BASH_ENV: startup },
198215
detached: false,
199216
additionalFds: { fd3: { type: "output" }, fd65: { type: "output" } },
200217
}),
@@ -204,10 +221,11 @@ describe("ElectronApp", () => {
204221
// Closing the parent-owned pipe stands in for parent exit, without a PID poll.
205222
yield* Stream.run(Stream.empty, waiting.stdin);
206223
assert.equal(yield* waiting.exitCode, 0);
207-
assert.isFalse(yield* fs.exists(startupMarker));
224+
// Only the replacement may source startup files, after the parent-exit wait.
225+
assert.equal(yield* fs.readFileString(startupMarker), "ran");
208226
assert.equal(
209227
yield* fs.readFileString(output),
210-
`${output}\ntwo words\n$HOME\n(Nightly)\n trailing \n`,
228+
`${startup}\n${startup}\n${output}\ntwo words\n$HOME\n(Nightly)\n trailing \n`,
211229
);
212230
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
213231
);
@@ -223,7 +241,11 @@ describe("ElectronApp", () => {
223241
}).pipe(
224242
Effect.provide(ElectronApp.layer),
225243
Effect.provideService(HostProcess.Platform, "linux"),
226-
Effect.provideService(HostProcess.Environment, { APPIMAGE: "/tmp/app.AppImage" }),
244+
Effect.provideService(HostProcess.Environment, {
245+
APPIMAGE: "/tmp/app.AppImage",
246+
APPDIR: "/tmp/mount",
247+
}),
248+
Effect.provideService(HostProcess.ExecutablePath, "/tmp/mount/t3code"),
227249
Effect.provideService(
228250
ChildProcessSpawner.ChildProcessSpawner,
229251
ChildProcessSpawner.make(() =>

‎apps/desktop/src/electron/ElectronApp.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ for fd in /proc/self/fd/*; do
224224
fi
225225
done
226226
while read -r _; do :; done
227+
set +p
227228
exec "$@" </dev/null
228229
`;
229230

@@ -232,7 +233,14 @@ export const make = Effect.gen(function* () {
232233
const platform = yield* HostProcess.Platform;
233234
const env = yield* HostProcess.Environment;
234235
const appImagePath = env.APPIMAGE;
235-
if (platform !== "linux" || !Electron.app.isPackaged || !appImagePath?.trim()) {
236+
const executablePath = yield* HostProcess.ExecutablePath;
237+
if (
238+
platform !== "linux" ||
239+
!appImagePath?.trim() ||
240+
!env.APPDIR ||
241+
!executablePath.startsWith(`${env.APPDIR}/`) ||
242+
!Electron.app.isPackaged
243+
) {
236244
return electronApp;
237245
}
238246
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -247,6 +255,8 @@ export const make = Effect.gen(function* () {
247255
ChildProcess.make(
248256
"bash",
249257
[
258+
// Ignore startup files in the waiter; +p before exec keeps this mode helper-local.
259+
"-p",
250260
"-c",
251261
appImageRelaunchScript,
252262
"t3code-appimage-relaunch",
@@ -258,8 +268,7 @@ export const make = Effect.gen(function* () {
258268
stdin: "pipe",
259269
stdout: "ignore",
260270
stderr: "ignore",
261-
// Startup files could redirect stdin and bypass the parent-exit wait.
262-
env: { ...env, BASH_ENV: undefined, ENV: undefined },
271+
env,
263272
},
264273
),
265274
);

0 commit comments

Comments
 (0)