Skip to content

Commit d8a48b8

Browse files
fix(desktop): a failed first update install reports its error
electron-updater reports most quitAndInstall failures (no installer path, a refused pkexec/dpkg run) by emitting `error` synchronously and returning. The adapter treated that as success, so the install result said `failed: false` with no message until the global error listener's recovery finished later: no "Could not install update" toast, and remote installs never reported the failure. quitAndInstall now listens for `error` during the call and fails with the first emitted error. Listeners registered through `on` skip errors while the call runs, so the failure is handled once by the install path instead of the global handler racing it for recovery. Errors after the call reach the listeners as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6a85a27 commit d8a48b8

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ describe("ElectronUpdater.quitAndInstall", () => {
4040
autoUpdaterMock.quitAndInstall.mockImplementationOnce(() => {
4141
autoUpdaterMock.events.emit("error", cause);
4242
});
43+
const errorListener = vi.fn();
4344
const updater = yield* ElectronUpdater.ElectronUpdater;
4445

4546
const result = yield* Effect.scoped(
46-
updater
47-
.on("error", () => {})
48-
.pipe(
49-
Effect.andThen(updater.quitAndInstall({ isSilent: true, isForceRunAfter: true })),
50-
Effect.result,
51-
),
47+
Effect.gen(function* () {
48+
yield* updater.on("error", errorListener);
49+
const result = yield* updater
50+
.quitAndInstall({ isSilent: true, isForceRunAfter: true })
51+
.pipe(Effect.result);
52+
// The failure reports the error, so listeners skip it during the call
53+
// but still receive the same Error if the updater emits it again later.
54+
assert.deepEqual(errorListener.mock.calls, []);
55+
autoUpdaterMock.events.emit("error", cause);
56+
assert.deepEqual(errorListener.mock.calls, [[cause]]);
57+
return result;
58+
}),
5259
);
5360

5461
assert.isTrue(Result.isFailure(result), "quitAndInstall should fail");

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export class ElectronUpdater extends Context.Service<
8080
}
8181
>()("@t3tools/desktop/electron/ElectronUpdater") {}
8282

83+
// Set while quitAndInstall runs so the `error` listeners registered through
84+
// `on` skip the errors that call fails with.
85+
let installing = false;
86+
8387
/** @public Service construction is part of the canonical Effect module API. */
8488
export const make = ElectronUpdater.of({
8589
setFeedURL: (options) =>
@@ -140,30 +144,47 @@ export const make = ElectronUpdater.of({
140144
quitAndInstall: ({ isSilent, isForceRunAfter }) =>
141145
Effect.suspend(() => {
142146
const channel = autoUpdater.channel;
143-
return Effect.try({
144-
try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter),
145-
catch: (cause) =>
146-
new ElectronUpdaterQuitAndInstallError({
147-
channel,
148-
isSilent,
149-
isForceRunAfter,
150-
cause,
151-
}),
152-
});
147+
const fail = (cause: unknown) =>
148+
Effect.fail(
149+
new ElectronUpdaterQuitAndInstallError({ channel, isSilent, isForceRunAfter, cause }),
150+
);
151+
// electron-updater reports most install failures by emitting `error`
152+
// synchronously and returning, so this call fails with them instead.
153+
const emitted: Array<unknown> = [];
154+
const capture = (error: unknown) => {
155+
emitted.push(error);
156+
};
157+
autoUpdater.on("error", capture);
158+
installing = true;
159+
try {
160+
autoUpdater.quitAndInstall(isSilent, isForceRunAfter);
161+
} catch (cause) {
162+
return fail(cause);
163+
} finally {
164+
installing = false;
165+
autoUpdater.removeListener("error", capture);
166+
}
167+
return emitted.length > 0 ? fail(emitted[0]) : Effect.void;
153168
}),
154169
on: (eventName, listener) => {
155170
const eventTarget = autoUpdater as unknown as {
156171
on: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
157172
removeListener: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
158173
};
159174
const untypedListener = listener as unknown as (...args: Array<unknown>) => void;
175+
const registeredListener =
176+
eventName === "error"
177+
? (...args: Array<unknown>) => {
178+
if (!installing) untypedListener(...args);
179+
}
180+
: untypedListener;
160181
return Effect.acquireRelease(
161182
Effect.sync(() => {
162-
eventTarget.on(eventName, untypedListener);
183+
eventTarget.on(eventName, registeredListener);
163184
}),
164185
() =>
165186
Effect.sync(() => {
166-
eventTarget.removeListener(eventName, untypedListener);
187+
eventTarget.removeListener(eventName, registeredListener);
167188
}),
168189
).pipe(Effect.asVoid);
169190
},

0 commit comments

Comments
 (0)