Skip to content

Commit 1b8e9bb

Browse files
committed
fix(desktop): refresh Linux MIME handler cache
1 parent ba79610 commit 1b8e9bb

2 files changed

Lines changed: 93 additions & 6 deletions

File tree

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const makeHandlerLayer = (
5050
recorded: RecordedRegistration,
5151
input: {
5252
readonly environment?: Record<string, unknown>;
53+
readonly updateDesktopDatabaseExitCode?: number;
5354
readonly xdgMimeExitCode?: number;
5455
readonly writeError?: PlatformError.PlatformError;
5556
readonly existingEntry?: string;
@@ -83,7 +84,11 @@ const makeHandlerLayer = (
8384
command: childProcess.command,
8485
args: childProcess.args,
8586
});
86-
return Effect.succeed(mockProcess(input.xdgMimeExitCode ?? 0));
87+
const exitCode =
88+
childProcess.command === "update-desktop-database"
89+
? (input.updateDesktopDatabaseExitCode ?? 0)
90+
: (input.xdgMimeExitCode ?? 0);
91+
return Effect.succeed(mockProcess(exitCode));
8792
}),
8893
),
8994
),
@@ -154,7 +159,7 @@ describe("DesktopLinuxUrlHandler", () => {
154159
);
155160
});
156161

157-
it.effect("writes the handler entry and claims the scheme default via xdg-mime", () => {
162+
it.effect("writes the handler entry, refreshes the MIME cache, and claims the scheme default", () => {
158163
const recorded = emptyRecording();
159164

160165
return Effect.gen(function* () {
@@ -172,6 +177,10 @@ describe("DesktopLinuxUrlHandler", () => {
172177
);
173178
assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
174179
assert.deepEqual(recorded.commands, [
180+
{
181+
command: "update-desktop-database",
182+
args: ["/home/alice/.local/share/applications"],
183+
},
175184
{
176185
command: "xdg-mime",
177186
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
@@ -207,7 +216,16 @@ describe("DesktopLinuxUrlHandler", () => {
207216

208217
assert.deepEqual(recorded.files, []);
209218
assert.deepEqual(recorded.directories, []);
210-
assert.equal(recorded.commands.length, 1);
219+
assert.deepEqual(recorded.commands, [
220+
{
221+
command: "update-desktop-database",
222+
args: ["/home/alice/.local/share/applications"],
223+
},
224+
{
225+
command: "xdg-mime",
226+
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
227+
},
228+
]);
211229
});
212230
});
213231

@@ -234,10 +252,12 @@ describe("DesktopLinuxUrlHandler", () => {
234252
});
235253

236254
it.effect("never fails startup when registration cannot complete", () => {
255+
const desktopDatabaseFailed = emptyRecording();
237256
const xdgMimeFailed = emptyRecording();
238257
const writeFailed = emptyRecording();
239258

240259
return Effect.gen(function* () {
260+
yield* runRegister(desktopDatabaseFailed, { updateDesktopDatabaseExitCode: 1 });
241261
yield* runRegister(xdgMimeFailed, { xdgMimeExitCode: 1 });
242262
yield* runRegister(writeFailed, {
243263
writeError: PlatformError.systemError({
@@ -249,6 +269,10 @@ describe("DesktopLinuxUrlHandler", () => {
249269
}),
250270
});
251271

272+
assert.deepEqual(
273+
desktopDatabaseFailed.commands.map(({ command }) => command),
274+
["update-desktop-database", "xdg-mime"],
275+
);
252276
assert.equal(xdgMimeFailed.files.length, 1);
253277
assert.deepEqual(writeFailed.commands, []);
254278
});

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

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { makeComponentLogger } from "./DesktopObservability.ts";
1717
// Electron's app.setAsDefaultProtocolClient resolves the desktop id from
1818
// setDesktopName, which cannot match those files — so the browser keeps
1919
// prompting "Choose an application" for every OAuth callback. Instead, write
20-
// our own handler entry pointing at the current AppImage and claim the
21-
// scheme default via xdg-mime, exactly what the file manager's "set as
22-
// default" checkbox would record in mimeapps.list.
20+
// our own handler entry pointing at the current AppImage, refresh the desktop
21+
// MIME cache so desktop environments recognize that entry as a handler, and
22+
// use xdg-mime to record it as the scheme default in mimeapps.list.
2323
const { logInfo, logWarning } = makeComponentLogger("desktop-linux-url-handler");
2424

2525
export class DesktopLinuxUrlHandlerRegistrationError extends Schema.TaggedError<DesktopLinuxUrlHandlerRegistrationError>()(
@@ -40,6 +40,23 @@ export class DesktopLinuxUrlHandlerRegistrationError extends Schema.TaggedError<
4040

4141
const isRegistrationError = Schema.is(DesktopLinuxUrlHandlerRegistrationError);
4242

43+
export class DesktopLinuxUrlHandlerCacheRefreshError extends Schema.TaggedError<DesktopLinuxUrlHandlerCacheRefreshError>()(
44+
"DesktopLinuxUrlHandlerCacheRefreshError",
45+
{
46+
applicationsDir: Schema.String,
47+
exitCode: Schema.optionalKey(Schema.Number),
48+
cause: Schema.optionalKey(Schema.Defect()),
49+
},
50+
) {
51+
override get message(): string {
52+
const exitCode =
53+
this.exitCode === undefined ? "" : `, update-desktop-database exit code ${this.exitCode}`;
54+
return `Failed to refresh the desktop MIME cache at ${this.applicationsDir}${exitCode}.`;
55+
}
56+
}
57+
58+
const isCacheRefreshError = Schema.is(DesktopLinuxUrlHandlerCacheRefreshError);
59+
4360
const escapeDesktopEntryString = (value: string): string =>
4461
value
4562
.replaceAll("\\", "\\\\")
@@ -131,6 +148,37 @@ export const make = Effect.gen(function* () {
131148
),
132149
);
133150

151+
const updateDesktopDatabase = Effect.scoped(
152+
Effect.gen(function* () {
153+
const command = ChildProcess.make(
154+
"update-desktop-database",
155+
[environment.linuxApplicationsDir],
156+
{
157+
stdin: "ignore",
158+
stdout: "ignore",
159+
stderr: "ignore",
160+
},
161+
);
162+
const handle = yield* spawner.spawn(command);
163+
const exitCode = yield* handle.exitCode;
164+
if (exitCode !== 0) {
165+
return yield* new DesktopLinuxUrlHandlerCacheRefreshError({
166+
applicationsDir: environment.linuxApplicationsDir,
167+
exitCode,
168+
});
169+
}
170+
}),
171+
).pipe(
172+
Effect.mapError((error) =>
173+
isCacheRefreshError(error)
174+
? error
175+
: new DesktopLinuxUrlHandlerCacheRefreshError({
176+
applicationsDir: environment.linuxApplicationsDir,
177+
cause: error,
178+
}),
179+
),
180+
);
181+
134182
const setDefaultHandler = Effect.scoped(
135183
Effect.gen(function* () {
136184
const command = ChildProcess.make(
@@ -170,6 +218,21 @@ export const make = Effect.gen(function* () {
170218
}
171219
yield* writeDesktopEntry;
172220
if (!environment.isPackaged) return;
221+
222+
yield* updateDesktopDatabase.pipe(
223+
// Some MIME implementations, including GIO, use mimeinfo.cache to verify
224+
// that a desktop entry is associated with a scheme. Cache refresh is
225+
// independently best-effort so a missing update-desktop-database executable
226+
// does not prevent xdg-mime from recording the requested default.
227+
Effect.catch((error) =>
228+
logWarning("desktop MIME cache refresh failed", {
229+
applicationsDir: environment.linuxApplicationsDir,
230+
message: error.message,
231+
...(error.exitCode === undefined ? {} : { exitCode: error.exitCode }),
232+
}),
233+
),
234+
);
235+
173236
yield* setDefaultHandler;
174237
yield* logInfo("registered URL scheme handler", { scheme });
175238
}).pipe(

0 commit comments

Comments
 (0)