Skip to content

Commit 6e405b3

Browse files
committed
fix(desktop): relaunch Linux AppImages through the durable launcher
1 parent 4ee6bfd commit 6e405b3

8 files changed

Lines changed: 313 additions & 28 deletions

File tree

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

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ const makeEnvironment = (
4040
DesktopEnvironment.DesktopEnvironment.pipe(Effect.provide(makeEnvironmentLayer(overrides, env)));
4141

4242
describe("DesktopEnvironment", () => {
43+
it.effect.each([
44+
[undefined, Option.none()],
45+
["", Option.none()],
46+
[" ", Option.none()],
47+
[" /tmp/T3 Code.AppImage ", Option.some(" /tmp/T3 Code.AppImage ")],
48+
] as const)("keeps exact non-blank AppImage paths: %j", ([APPIMAGE, expected]) =>
49+
Effect.gen(function* () {
50+
const environment = yield* makeEnvironment(
51+
{ platform: "linux", isPackaged: true },
52+
{ APPIMAGE },
53+
);
54+
assert.deepEqual(environment.appImagePath, expected);
55+
}),
56+
);
57+
4358
it.effect("derives state paths and development identity inside Effect", () =>
4459
Effect.gen(function* () {
4560
const environment = yield* makeEnvironment(

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as DesktopWindow from "../window/DesktopWindow.ts";
1818
function makeElectronAppLayer(
1919
appListeners: Map<string, (...args: readonly unknown[]) => void>,
2020
quit: Effect.Effect<void> = Effect.void,
21+
overrides: Partial<ElectronApp.ElectronApp["Service"]> = {},
2122
) {
2223
const registerListener = (eventName: string, listener: (...args: readonly unknown[]) => void) =>
2324
Effect.acquireRelease(
@@ -51,6 +52,7 @@ function makeElectronAppLayer(
5152
onBeforeQuitForUpdate: (listener) => registerListener("before-quit-for-update", listener),
5253
on: (eventName, listener) =>
5354
registerListener(eventName, listener as unknown as (...args: readonly unknown[]) => void),
55+
...overrides,
5456
} satisfies ElectronApp.ElectronApp["Service"]);
5557
}
5658

@@ -101,6 +103,69 @@ function makeDesktopWindowLayer(
101103
}
102104

103105
describe("DesktopLifecycle", () => {
106+
it.effect.each([false, true])(
107+
"exits after shutdown even if scheduling relaunch fails: %s",
108+
(fails) =>
109+
Effect.gen(function* () {
110+
const requested = yield* Deferred.make<void>();
111+
const complete = yield* Deferred.make<void>();
112+
const exited = yield* Deferred.make<void>();
113+
const events: string[] = [];
114+
let options: Electron.RelaunchOptions | undefined;
115+
const layer = DesktopLifecycle.layer.pipe(
116+
Layer.provideMerge(
117+
makeElectronAppLayer(new Map(), Effect.void, {
118+
relaunch: (value) =>
119+
Effect.sync(() => {
120+
options = value;
121+
events.push("relaunch");
122+
}).pipe(Effect.andThen(fails ? Effect.die("spawn failed") : Effect.void)),
123+
exit: (code) =>
124+
Effect.sync(() => {
125+
assert.equal(code, 0);
126+
events.push("exit");
127+
}).pipe(Effect.andThen(Deferred.succeed(exited, undefined)), Effect.asVoid),
128+
}),
129+
),
130+
Layer.provideMerge(
131+
makeDesktopWindowLayer({
132+
flushMainWindowBounds: Effect.sync(() => {
133+
events.push("flush");
134+
}),
135+
}),
136+
),
137+
Layer.provideMerge(
138+
Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
139+
isDevelopment: false,
140+
} as DesktopEnvironment.DesktopEnvironment["Service"]),
141+
),
142+
Layer.provideMerge(
143+
Layer.succeed(DesktopShutdown.DesktopShutdown, {
144+
request: Deferred.succeed(requested, undefined).pipe(Effect.asVoid),
145+
awaitRequest: Deferred.await(requested),
146+
markComplete: Deferred.succeed(complete, undefined).pipe(Effect.asVoid),
147+
awaitComplete: Deferred.await(complete),
148+
isComplete: Deferred.isDone(complete),
149+
}),
150+
),
151+
Layer.provideMerge(DesktopState.layer),
152+
Layer.provideMerge(electronThemeLayer),
153+
);
154+
yield* Effect.gen(function* () {
155+
const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
156+
yield* lifecycle.relaunch("serverExposureMode=network-accessible");
157+
yield* Deferred.await(requested);
158+
assert.deepEqual(events, ["flush"]);
159+
yield* Deferred.succeed(complete, undefined);
160+
yield* Deferred.await(exited);
161+
assert.deepEqual(options, {
162+
args: process.argv.slice(1),
163+
});
164+
assert.deepEqual(events, ["flush", "relaunch", "exit"]);
165+
}).pipe(Effect.provide(layer));
166+
}),
167+
);
168+
104169
it.effect.each(["darwin", "win32", "linux"] satisfies ReadonlyArray<NodeJS.Platform>)(
105170
"lets the updater's quit event proceed on %s",
106171
(platform) => {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export class DesktopLifecycle extends Context.Service<
6060
const { logInfo: logLifecycleInfo, logError: logLifecycleError } =
6161
makeComponentLogger("desktop-lifecycle");
6262

63+
const logRelaunchFailure = (reason: string) => (cause: unknown) => {
64+
const error = new DesktopLifecycleRelaunchError({ reason, cause });
65+
return logLifecycleError(error.message, { error });
66+
};
67+
6368
function addScopedListener<Args extends ReadonlyArray<unknown>>(
6469
target: unknown,
6570
eventName: string,
@@ -174,19 +179,14 @@ export const make = DesktopLifecycle.of({
174179
yield* electronApp.exit(75);
175180
return;
176181
}
177-
yield* electronApp.relaunch({
178-
execPath: process.execPath,
179-
args: process.argv.slice(1),
180-
});
182+
yield* electronApp
183+
.relaunch({
184+
args: process.argv.slice(1),
185+
})
186+
.pipe(Effect.catchCause(logRelaunchFailure(reason)));
187+
// Shutdown has completed even when scheduling the replacement fails.
181188
yield* electronApp.exit(0);
182-
}).pipe(
183-
Effect.catchCause((cause) => {
184-
const error = new DesktopLifecycleRelaunchError({ reason, cause });
185-
return logLifecycleError(error.message, { error });
186-
}),
187-
Effect.forkDetach,
188-
Effect.asVoid,
189-
);
189+
}).pipe(Effect.catchCause(logRelaunchFailure(reason)), Effect.forkDetach, Effect.asVoid);
190190
}),
191191
register: Effect.gen(function* () {
192192
const desktopWindow = yield* DesktopWindow.DesktopWindow;

0 commit comments

Comments
 (0)