Skip to content

Commit 115e405

Browse files
only21milclaudejuliusmarminge
authored
fix(desktop): avoid temporary icon copies on Linux launches (#19589)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent adda1db commit 115e405

4 files changed

Lines changed: 183 additions & 46 deletions

File tree

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

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,31 @@ interface RecordedRegistration {
2222
readonly files: Array<{ readonly path: string; readonly content: string }>;
2323
readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
2424
readonly copies: Array<{ readonly source: string; readonly destination: string }>;
25+
readonly binaryReads: string[];
26+
readonly binaryWrites: Array<{
27+
readonly path: string;
28+
readonly bytes: Uint8Array;
29+
readonly mode: number | undefined;
30+
}>;
2531
}
2632

33+
const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png";
34+
const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png";
35+
const iconBytes = new Uint8Array([0x89, 0x50, 0x00, 0xff, 0x80, 0x0a]);
36+
const currentDesktopEntry = DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
37+
displayName: "T3 Code (Alpha)",
38+
execTarget: "/home/alice/Applications/T3-Code.AppImage",
39+
scheme: "t3code",
40+
iconPath,
41+
});
42+
const registrationCommands = [
43+
{ command: "update-desktop-database", args: ["/home/alice/.local/share/applications"] },
44+
{
45+
command: "xdg-mime",
46+
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
47+
},
48+
];
49+
2750
const makeEnvironment = (path: Path.Path, overrides: Record<string, unknown> = {}) =>
2851
DesktopEnvironment.DesktopEnvironment.of({
2952
platform: "linux",
@@ -64,7 +87,8 @@ const layerHandler = (
6487
readonly writeError?: PlatformError.PlatformError;
6588
readonly existingEntry?: string;
6689
readonly iconSource?: string;
67-
readonly iconCopyError?: PlatformError.PlatformError;
90+
readonly existingIcon?: Uint8Array;
91+
readonly iconReadError?: PlatformError.PlatformError;
6892
} = {},
6993
) =>
7094
DesktopLinuxUrlHandler.layer.pipe(
@@ -87,11 +111,34 @@ const layerHandler = (
87111
}),
88112
FileSystem.layerNoop({
89113
copyFile: (source, destination) =>
90-
input.iconCopyError
91-
? Effect.fail(input.iconCopyError)
92-
: Effect.sync(() => {
93-
recorded.copies.push({ source, destination });
94-
}),
114+
Effect.sync(() => {
115+
recorded.copies.push({ source, destination });
116+
}),
117+
readFile: (path) =>
118+
Effect.gen(function* () {
119+
recorded.binaryReads.push(path);
120+
if (path === input.iconSource) {
121+
if (input.iconReadError) return yield* input.iconReadError;
122+
return new Uint8Array(iconBytes);
123+
}
124+
if (path === iconPath && input.existingIcon !== undefined) {
125+
return input.existingIcon;
126+
}
127+
return yield* PlatformError.systemError({
128+
_tag: "NotFound",
129+
module: "FileSystem",
130+
method: "readFile",
131+
pathOrDescriptor: path,
132+
});
133+
}),
134+
writeFile: (path, bytes, options) =>
135+
Effect.sync(() => {
136+
recorded.binaryWrites.push({
137+
path,
138+
bytes: new Uint8Array(bytes),
139+
mode: options?.mode,
140+
});
141+
}),
95142
readFileString: () => Effect.succeed(input.existingEntry ?? ""),
96143
makeDirectory: (path) =>
97144
Effect.sync(() => {
@@ -156,6 +203,8 @@ const emptyRecording = (): RecordedRegistration => ({
156203
files: [],
157204
commands: [],
158205
copies: [],
206+
binaryReads: [],
207+
binaryWrites: [],
159208
});
160209

161210
describe("DesktopLinuxUrlHandler", () => {
@@ -306,42 +355,57 @@ describe("DesktopLinuxUrlHandler", () => {
306355

307356
it.effect("installs a persistent icon even when the desktop entry is already current", () => {
308357
const recorded = emptyRecording();
309-
const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png";
310358
return Effect.gen(function* () {
311359
yield* runRegister(recorded, {
312-
iconSource: "/tmp/.mount_T3/resources/icon.png",
313-
existingEntry: DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
314-
displayName: "T3 Code (Alpha)",
315-
execTarget: "/home/alice/Applications/T3-Code.AppImage",
316-
scheme: "t3code",
317-
iconPath,
318-
}),
360+
iconSource,
361+
existingEntry: currentDesktopEntry,
319362
});
320363
assert.deepEqual(recorded.files, []);
321-
assert.deepEqual(recorded.copies, [
322-
{ source: "/tmp/.mount_T3/resources/icon.png", destination: iconPath },
323-
]);
324-
assert.equal(recorded.commands.at(-1)?.command, "xdg-mime");
364+
assert.deepEqual(recorded.copies, []);
365+
assert.deepEqual(recorded.binaryReads, [iconSource, iconPath]);
366+
assert.deepEqual(recorded.binaryWrites, [{ path: iconPath, bytes: iconBytes, mode: 0o600 }]);
367+
assert.deepEqual(recorded.commands, registrationCommands);
325368
});
326369
});
327370

328-
it.effect("still registers the handler when copying its icon fails", () => {
371+
it.effect("refreshes a stale icon and skips equal bytes on the next registration", () => {
329372
const recorded = emptyRecording();
330373
return Effect.gen(function* () {
331374
yield* runRegister(recorded, {
332-
iconSource: "/tmp/.mount_T3/resources/icon.png",
333-
iconCopyError: PlatformError.systemError({
375+
iconSource,
376+
existingEntry: currentDesktopEntry,
377+
existingIcon: new Uint8Array([0x89, 0x50, 0x00, 0xfe, 0x80, 0x0a]),
378+
});
379+
assert.deepEqual(recorded.binaryWrites, [{ path: iconPath, bytes: iconBytes, mode: 0o600 }]);
380+
yield* runRegister(recorded, {
381+
iconSource,
382+
existingEntry: currentDesktopEntry,
383+
existingIcon: new Uint8Array(recorded.binaryWrites[0]!.bytes),
384+
});
385+
assert.equal(recorded.binaryWrites.length, 1);
386+
assert.deepEqual(recorded.copies, []);
387+
assert.deepEqual(recorded.files, []);
388+
assert.deepEqual(recorded.commands, [...registrationCommands, ...registrationCommands]);
389+
});
390+
});
391+
392+
it.effect("still registers the handler when reading its icon fails", () => {
393+
const recorded = emptyRecording();
394+
return Effect.gen(function* () {
395+
yield* runRegister(recorded, {
396+
iconSource,
397+
iconReadError: PlatformError.systemError({
334398
_tag: "PermissionDenied",
335399
module: "FileSystem",
336-
method: "copyFile",
337-
description: "read-only icon directory",
400+
method: "readFile",
401+
description: "unreadable bundled icon",
338402
}),
339403
});
404+
assert.deepEqual(recorded.binaryReads, [iconSource]);
405+
assert.deepEqual(recorded.binaryWrites, []);
406+
assert.deepEqual(recorded.copies, []);
340407
assert.equal(recorded.files.length, 1);
341-
assert.deepEqual(
342-
recorded.commands.map(({ command }) => command),
343-
["update-desktop-database", "xdg-mime"],
344-
);
408+
assert.deepEqual(recorded.commands, registrationCommands);
345409
});
346410
});
347411

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,16 @@ export const make = Effect.gen(function* () {
301301
yield* Effect.gen(function* () {
302302
const { png } = yield* assets.iconPaths;
303303
if (Option.isNone(png)) return;
304+
// Electron's copyFile extracts ASAR entries to temporary files it never removes.
305+
const icon = yield* fileSystem.readFile(png.value);
306+
const current = yield* fileSystem.readFile(iconPath).pipe(Effect.option);
307+
if (Option.isSome(current) && sameBytes(current.value, icon)) return;
304308
// The AppImage mount is temporary; the chooser needs the icon after exit.
305309
yield* fileSystem.makeDirectory(iconsDir, { recursive: true });
306-
yield* fileSystem.copyFile(png.value, iconPath);
310+
yield* fileSystem.writeFile(iconPath, icon, { mode: 0o600 });
307311
}).pipe(
308312
Effect.catch((error) =>
309-
logWarning("URL handler icon copy failed", { iconPath, category: error.reason._tag }),
313+
logWarning("URL handler icon install failed", { iconPath, category: error.reason._tag }),
310314
),
311315
);
312316

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

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const {
1414
setNameMock,
1515
appState,
1616
mkdirSyncMock,
17+
readFileSyncMock,
1718
writeFileSyncMock,
1819
copyFileSyncMock,
1920
execFileSyncMock,
20-
readFileSyncMock,
2121
} = vi.hoisted(() => ({
2222
appendSwitchMock: vi.fn(),
2323
getSwitchValueMock: vi.fn(),
@@ -27,10 +27,10 @@ const {
2727
setNameMock: vi.fn(),
2828
appState: { version: "0.0.37", isPackaged: true },
2929
mkdirSyncMock: vi.fn(),
30+
readFileSyncMock: vi.fn(),
3031
writeFileSyncMock: vi.fn(),
3132
copyFileSyncMock: vi.fn(),
3233
execFileSyncMock: vi.fn(),
33-
readFileSyncMock: vi.fn(),
3434
}));
3535

3636
vi.mock("electron", () => ({
@@ -66,6 +66,11 @@ vi.mock("node:child_process", () => ({
6666

6767
import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts";
6868

69+
const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png";
70+
const iconPath = "/xdg/icons/com.t3tools.T3Code.desktop.png";
71+
const desktopEntryPath = "/xdg/applications/com.t3tools.T3Code.desktop";
72+
const iconBytes = Buffer.from([0x89, 0x50, 0x00, 0xff, 0x80, 0x0a]);
73+
6974
describe("DesktopPreReadyPlatform", () => {
7075
beforeEach(() => {
7176
appendSwitchMock.mockReset();
@@ -77,10 +82,15 @@ describe("DesktopPreReadyPlatform", () => {
7782
appState.version = "0.0.37";
7883
appState.isPackaged = true;
7984
mkdirSyncMock.mockReset();
85+
readFileSyncMock.mockReset();
8086
writeFileSyncMock.mockReset();
8187
copyFileSyncMock.mockReset();
8288
execFileSyncMock.mockReset();
83-
readFileSyncMock.mockReset().mockReturnValue("{}");
89+
readFileSyncMock.mockReset().mockImplementation((path: string) => {
90+
if (path.endsWith("/desktop-settings.json")) return "{}";
91+
if (path === iconSource) return Buffer.from(iconBytes);
92+
throw new Error(`missing file: ${path}`);
93+
});
8494
});
8595

8696
it.effect("persists Linux scaling synchronously across relaunches and settings rewrites", () => {
@@ -222,14 +232,13 @@ describe("DesktopPreReadyPlatform", () => {
222232
let desktopName = "t3code.desktop";
223233
let desktopEntry = previousEntry;
224234
let iconInstalled = false;
225-
copyFileSyncMock.mockImplementation((_source: string, destination: string) => {
226-
iconInstalled = destination === "/xdg/icons/com.t3tools.T3Code.desktop.png";
227-
});
228235
setDesktopNameMock.mockImplementation((name: string) => {
229236
desktopName = name;
230237
});
231-
writeFileSyncMock.mockImplementation((path: string, contents: string) => {
232-
if (path === "/xdg/applications/com.t3tools.T3Code.desktop") desktopEntry = contents;
238+
writeFileSyncMock.mockImplementation((path: string, contents: string | Buffer) => {
239+
if (path === iconPath)
240+
iconInstalled = Buffer.isBuffer(contents) && contents.equals(iconBytes);
241+
if (path === desktopEntryPath && typeof contents === "string") desktopEntry = contents;
233242
});
234243

235244
return Effect.scoped(
@@ -254,6 +263,15 @@ describe("DesktopPreReadyPlatform", () => {
254263
"Icon=/xdg/icons/com.t3tools.T3Code.desktop.png",
255264
);
256265
assert.isTrue(identity.iconInstalled);
266+
assert.deepEqual(
267+
readFileSyncMock.mock.calls.filter(([path]) => path === iconSource),
268+
[[iconSource]],
269+
);
270+
assert.deepEqual(
271+
writeFileSyncMock.mock.calls.filter(([path]) => path === iconPath),
272+
[[iconPath, iconBytes, { mode: 0o600 }]],
273+
);
274+
assert.equal(copyFileSyncMock.mock.calls.length, 0);
257275
}),
258276
).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())));
259277
});
@@ -301,18 +319,62 @@ describe("DesktopPreReadyPlatform", () => {
301319
);
302320
});
303321

304-
it.effect("still prepares the portal entry when the bundled icon cannot be copied", () => {
322+
it.effect("refreshes a stale icon once across repeated launches", () => {
323+
vi.stubEnv("VITE_DEV_SERVER_URL", "");
324+
vi.stubEnv("XDG_DATA_HOME", "/xdg");
305325
getSwitchValueMock.mockReturnValue("");
306-
copyFileSyncMock.mockImplementation(() => {
307-
throw new Error("missing bundled icon");
326+
let installedIcon = Buffer.from([0x89, 0x50, 0x00, 0xfe, 0x80, 0x0a]);
327+
readFileSyncMock.mockImplementation((path: string) => {
328+
if (path === iconSource) return Buffer.from(iconBytes);
329+
if (path === iconPath) return Buffer.from(installedIcon);
330+
return "{}";
331+
});
332+
writeFileSyncMock.mockImplementation((path: string, contents: Buffer) => {
333+
if (path === iconPath) installedIcon = Buffer.from(contents);
308334
});
335+
309336
return Effect.gen(function* () {
310337
yield* DesktopPreReadyPlatform.make;
311-
const contents = writeFileSyncMock.mock.calls[0]?.[1];
338+
assert.deepEqual(installedIcon, iconBytes);
339+
yield* DesktopPreReadyPlatform.make;
340+
assert.deepEqual(
341+
writeFileSyncMock.mock.calls.filter(([path]) => path === iconPath),
342+
[[iconPath, iconBytes, { mode: 0o600 }]],
343+
);
344+
assert.equal(copyFileSyncMock.mock.calls.length, 0);
345+
assert.equal(
346+
writeFileSyncMock.mock.calls.filter(([path]) => path === desktopEntryPath).length,
347+
2,
348+
);
349+
assert.equal(setDesktopNameMock.mock.calls.length, 2);
350+
}).pipe(
351+
Effect.provideService(HostProcess.Platform, "linux"),
352+
Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
353+
);
354+
});
355+
356+
it.effect("still prepares the portal entry when the bundled icon cannot be read", () => {
357+
vi.stubEnv("VITE_DEV_SERVER_URL", "");
358+
vi.stubEnv("XDG_DATA_HOME", "/xdg");
359+
getSwitchValueMock.mockReturnValue("");
360+
readFileSyncMock.mockImplementation((path: string) => {
361+
if (path === iconSource) throw new Error("unreadable bundled icon");
362+
return "{}";
363+
});
364+
return Effect.gen(function* () {
365+
yield* DesktopPreReadyPlatform.make;
366+
assert.isTrue(readFileSyncMock.mock.calls.some(([path]) => path === iconSource));
367+
assert.equal(copyFileSyncMock.mock.calls.length, 0);
368+
assert.equal(writeFileSyncMock.mock.calls.length, 1);
369+
const [path, contents] = writeFileSyncMock.mock.calls[0]!;
370+
assert.equal(path, desktopEntryPath);
312371
assert.include(contents, "MimeType=x-scheme-handler/t3code;");
313-
assert.include(contents, "Icon=");
372+
assert.include(contents, `Icon=${iconPath}`);
314373
assert.equal(setDesktopNameMock.mock.calls.length, 1);
315-
}).pipe(Effect.provideService(HostProcess.Platform, "linux"));
374+
}).pipe(
375+
Effect.provideService(HostProcess.Platform, "linux"),
376+
Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
377+
);
316378
});
317379

318380
it.effect(

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,21 @@ export const make = Effect.gen(function* () {
9797
: undefined;
9898
if (iconPath !== undefined) {
9999
try {
100-
NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
101-
NodeFS.copyFileSync(
100+
// Electron's copyFile extracts ASAR entries to temporary files it never removes.
101+
const icon = NodeFS.readFileSync(
102102
NodePath.posix.join(
103103
Electron.app.getAppPath(),
104104
"apps/desktop/prod-resources/icon.png",
105105
),
106-
iconPath,
107106
);
107+
let current: Buffer | null = null;
108+
try {
109+
current = NodeFS.readFileSync(iconPath);
110+
} catch {}
111+
if (current === null || !current.equals(icon)) {
112+
NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
113+
NodeFS.writeFileSync(iconPath, icon, { mode: 0o600 });
114+
}
108115
} catch {}
109116
}
110117
NodeFS.writeFileSync(

0 commit comments

Comments
 (0)