Skip to content

Commit 6583670

Browse files
fix(desktop): Linux launches no longer leave an icon copy in /tmp (pingdotgg#25775)
1 parent 25d2239 commit 6583670

4 files changed

Lines changed: 54 additions & 22 deletions

File tree

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ interface RecordedRegistration {
2626
readonly files: Array<{ readonly path: string; readonly content: string }>;
2727
readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
2828
readonly copies: Array<{ readonly source: string; readonly destination: string }>;
29+
readonly icons: Array<{ readonly path: string; readonly bytes: ReadonlyArray<number> }>;
2930
}
3031

32+
const ICON_BYTES = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
33+
3134
const makeEnvironment = (path: Path.Path, overrides: Record<string, unknown> = {}) =>
3235
DesktopEnvironment.DesktopEnvironment.of({
3336
platform: "linux",
@@ -69,7 +72,7 @@ const layerHandler = (
6972
readonly writeError?: PlatformError.PlatformError;
7073
readonly existingEntry?: string;
7174
readonly iconSource?: string;
72-
readonly iconCopyError?: PlatformError.PlatformError;
75+
readonly iconWriteError?: PlatformError.PlatformError;
7376
} = {},
7477
) =>
7578
DesktopLinuxUrlHandler.layer.pipe(
@@ -92,10 +95,25 @@ const layerHandler = (
9295
}),
9396
FileSystem.layerNoop({
9497
copyFile: (source, destination) =>
95-
input.iconCopyError
96-
? Effect.fail(input.iconCopyError)
98+
Effect.sync(() => {
99+
recorded.copies.push({ source, destination });
100+
}),
101+
readFile: (path) =>
102+
path === input.iconSource
103+
? Effect.succeed(ICON_BYTES)
104+
: Effect.fail(
105+
PlatformError.systemError({
106+
_tag: "NotFound",
107+
module: "FileSystem",
108+
method: "readFile",
109+
pathOrDescriptor: path,
110+
}),
111+
),
112+
writeFile: (path, bytes) =>
113+
input.iconWriteError
114+
? Effect.fail(input.iconWriteError)
97115
: Effect.sync(() => {
98-
recorded.copies.push({ source, destination });
116+
recorded.icons.push({ path, bytes: [...bytes] });
99117
}),
100118
readFileString: () => Effect.succeed(input.existingEntry ?? ""),
101119
makeDirectory: (path) =>
@@ -162,6 +180,7 @@ const emptyRecording = (): RecordedRegistration => ({
162180
files: [],
163181
commands: [],
164182
copies: [],
183+
icons: [],
165184
});
166185

167186
describe("DesktopLinuxUrlHandler", () => {
@@ -290,22 +309,21 @@ describe("DesktopLinuxUrlHandler", () => {
290309
}),
291310
});
292311
assert.deepEqual(recorded.files, []);
293-
assert.deepEqual(recorded.copies, [
294-
{ source: "/tmp/.mount_T3/resources/icon.png", destination: iconPath },
295-
]);
312+
assert.deepEqual(recorded.copies, [], "icon must not be copied out of the app bundle");
313+
assert.deepEqual(recorded.icons, [{ path: iconPath, bytes: [...ICON_BYTES] }]);
296314
assert.equal(recorded.commands.at(-1)?.command, "xdg-mime");
297315
});
298316
});
299317

300-
it.effect("still registers the handler when copying its icon fails", () => {
318+
it.effect("still registers the handler when installing its icon fails", () => {
301319
const recorded = emptyRecording();
302320
return Effect.gen(function* () {
303321
yield* runRegister(recorded, {
304322
iconSource: "/tmp/.mount_T3/resources/icon.png",
305-
iconCopyError: PlatformError.systemError({
323+
iconWriteError: PlatformError.systemError({
306324
_tag: "PermissionDenied",
307325
module: "FileSystem",
308-
method: "copyFile",
326+
method: "writeFile",
309327
description: "read-only icon directory",
310328
}),
311329
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ export const make = Effect.gen(function* () {
298298
if (Option.isNone(png)) return;
299299
// The AppImage mount is temporary; the chooser needs the icon after exit.
300300
yield* fileSystem.makeDirectory(iconsDir, { recursive: true });
301-
yield* fileSystem.copyFile(png.value, iconPath);
301+
// Electron's copyFile from app.asar extracts to a temp file it never deletes.
302+
yield* fileSystem.writeFile(iconPath, yield* fileSystem.readFile(png.value));
302303
}).pipe(
303304
Effect.catch((error) =>
304-
logWarning("URL handler icon copy failed", { iconPath, category: error.reason._tag }),
305+
logWarning("URL handler icon install failed", { iconPath, category: error.reason._tag }),
305306
),
306307
);
307308

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,19 @@ describe("DesktopPreReadyPlatform", () => {
129129
let desktopName = "t3code.desktop";
130130
let desktopEntry = previousEntry;
131131
let iconInstalled = false;
132-
copyFileSyncMock.mockImplementation((_source: string, destination: string) => {
133-
iconInstalled = destination === "/xdg/icons/com.t3tools.T3Code.desktop.png";
132+
const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png";
133+
const iconPath = "/xdg/icons/com.t3tools.T3Code.desktop.png";
134+
const iconBytes = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
135+
readFileSyncMock.mockImplementation((path: string) => {
136+
if (path === iconSource) return iconBytes;
137+
return "{}";
134138
});
135139
setDesktopNameMock.mockImplementation((name: string) => {
136140
desktopName = name;
137141
});
138-
writeFileSyncMock.mockImplementation((path: string, contents: string) => {
139-
if (path === "/xdg/applications/com.t3tools.T3Code.desktop") desktopEntry = contents;
142+
writeFileSyncMock.mockImplementation((path: string, contents: string | Buffer) => {
143+
if (path === "/xdg/applications/com.t3tools.T3Code.desktop") desktopEntry = String(contents);
144+
if (path === iconPath) iconInstalled = Buffer.compare(Buffer.from(contents), iconBytes) === 0;
140145
});
141146

142147
return Effect.scoped(
@@ -166,7 +171,12 @@ describe("DesktopPreReadyPlatform", () => {
166171
identity.desktopEntry ?? "",
167172
"Icon=/xdg/icons/com.t3tools.T3Code.desktop.png",
168173
);
169-
assert.isTrue(identity.iconInstalled);
174+
assert.equal(
175+
copyFileSyncMock.mock.calls.length,
176+
0,
177+
"icon must not be copied out of the app bundle",
178+
);
179+
assert.isTrue(identity.iconInstalled, "icon must be written from the bundled bytes");
170180
}),
171181
).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())));
172182
});

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,15 @@ export const make = Effect.gen(function* () {
104104
if (iconPath !== undefined) {
105105
try {
106106
NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
107-
NodeFS.copyFileSync(
108-
NodePath.posix.join(
109-
Electron.app.getAppPath(),
110-
"apps/desktop/prod-resources/icon.png",
111-
),
107+
// Electron's copyFileSync from app.asar extracts to a temp file it never deletes.
108+
NodeFS.writeFileSync(
112109
iconPath,
110+
NodeFS.readFileSync(
111+
NodePath.posix.join(
112+
Electron.app.getAppPath(),
113+
"apps/desktop/prod-resources/icon.png",
114+
),
115+
),
113116
);
114117
} catch {}
115118
}

0 commit comments

Comments
 (0)