Skip to content
TypeScript

fix(desktop): avoid temporary icon copies on Linux launches (#19589)

Victor Vogel115e40545385
diff --git a/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts b/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts
index 295f63eafe4..41f20ef984d 100644
--- a/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts
+++ b/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts
@@ -22,8 +22,31 @@ interface RecordedRegistration {
readonly files: Array<{ readonly path: string; readonly content: string }>;
readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
readonly copies: Array<{ readonly source: string; readonly destination: string }>;
+ readonly binaryReads: string[];
+ readonly binaryWrites: Array<{
+ readonly path: string;
+ readonly bytes: Uint8Array;
+ readonly mode: number | undefined;
+ }>;
}
+const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png";
+const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png";
+const iconBytes = new Uint8Array([0x89, 0x50, 0x00, 0xff, 0x80, 0x0a]);
+const currentDesktopEntry = DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
+ displayName: "T3 Code (Alpha)",
+ execTarget: "/home/alice/Applications/T3-Code.AppImage",
+ scheme: "t3code",
+ iconPath,
+});
+const registrationCommands = [
+ { command: "update-desktop-database", args: ["/home/alice/.local/share/applications"] },
+ {
+ command: "xdg-mime",
+ args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
+ },
+];
+
const makeEnvironment = (path: Path.Path, overrides: Record<string, unknown> = {}) =>
DesktopEnvironment.DesktopEnvironment.of({
platform: "linux",
@@ -64,7 +87,8 @@ const layerHandler = (
readonly writeError?: PlatformError.PlatformError;
readonly existingEntry?: string;
readonly iconSource?: string;
- readonly iconCopyError?: PlatformError.PlatformError;
+ readonly existingIcon?: Uint8Array;
+ readonly iconReadError?: PlatformError.PlatformError;
} = {},
) =>
DesktopLinuxUrlHandler.layer.pipe(
@@ -87,11 +111,34 @@ const layerHandler = (
}),
FileSystem.layerNoop({
copyFile: (source, destination) =>
- input.iconCopyError
- ? Effect.fail(input.iconCopyError)
- : Effect.sync(() => {
- recorded.copies.push({ source, destination });
- }),
+ Effect.sync(() => {
+ recorded.copies.push({ source, destination });
+ }),
+ readFile: (path) =>
+ Effect.gen(function* () {
+ recorded.binaryReads.push(path);
+ if (path === input.iconSource) {
+ if (input.iconReadError) return yield* input.iconReadError;
+ return new Uint8Array(iconBytes);
+ }
+ if (path === iconPath && input.existingIcon !== undefined) {
+ return input.existingIcon;
+ }
+ return yield* PlatformError.systemError({
+ _tag: "NotFound",
+ module: "FileSystem",
+ method: "readFile",
+ pathOrDescriptor: path,
+ });
+ }),
+ writeFile: (path, bytes, options) =>
+ Effect.sync(() => {
+ recorded.binaryWrites.push({
+ path,
+ bytes: new Uint8Array(bytes),
+ mode: options?.mode,
+ });
+ }),
readFileString: () => Effect.succeed(input.existingEntry ?? ""),
makeDirectory: (path) =>
Effect.sync(() => {
@@ -156,6 +203,8 @@ const emptyRecording = (): RecordedRegistration => ({
files: [],
commands: [],
copies: [],
+ binaryReads: [],
+ binaryWrites: [],
});
describe("DesktopLinuxUrlHandler", () => {
@@ -306,42 +355,57 @@ describe("DesktopLinuxUrlHandler", () => {
it.effect("installs a persistent icon even when the desktop entry is already current", () => {
const recorded = emptyRecording();
- const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png";
return Effect.gen(function* () {
yield* runRegister(recorded, {
- iconSource: "/tmp/.mount_T3/resources/icon.png",
- existingEntry: DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
- displayName: "T3 Code (Alpha)",
- execTarget: "/home/alice/Applications/T3-Code.AppImage",
- scheme: "t3code",
- iconPath,
- }),
+ iconSource,
+ existingEntry: currentDesktopEntry,
});
assert.deepEqual(recorded.files, []);
- assert.deepEqual(recorded.copies, [
- { source: "/tmp/.mount_T3/resources/icon.png", destination: iconPath },
- ]);
- assert.equal(recorded.commands.at(-1)?.command, "xdg-mime");
+ assert.deepEqual(recorded.copies, []);
+ assert.deepEqual(recorded.binaryReads, [iconSource, iconPath]);
+ assert.deepEqual(recorded.binaryWrites, [{ path: iconPath, bytes: iconBytes, mode: 0o600 }]);
+ assert.deepEqual(recorded.commands, registrationCommands);
});
});
- it.effect("still registers the handler when copying its icon fails", () => {
+ it.effect("refreshes a stale icon and skips equal bytes on the next registration", () => {
const recorded = emptyRecording();
return Effect.gen(function* () {
yield* runRegister(recorded, {
- iconSource: "/tmp/.mount_T3/resources/icon.png",
- iconCopyError: PlatformError.systemError({
+ iconSource,
+ existingEntry: currentDesktopEntry,
+ existingIcon: new Uint8Array([0x89, 0x50, 0x00, 0xfe, 0x80, 0x0a]),
+ });
+ assert.deepEqual(recorded.binaryWrites, [{ path: iconPath, bytes: iconBytes, mode: 0o600 }]);
+ yield* runRegister(recorded, {
+ iconSource,
+ existingEntry: currentDesktopEntry,
+ existingIcon: new Uint8Array(recorded.binaryWrites[0]!.bytes),
+ });
+ assert.equal(recorded.binaryWrites.length, 1);
+ assert.deepEqual(recorded.copies, []);
+ assert.deepEqual(recorded.files, []);
+ assert.deepEqual(recorded.commands, [...registrationCommands, ...registrationCommands]);
+ });
+ });
+
+ it.effect("still registers the handler when reading its icon fails", () => {
+ const recorded = emptyRecording();
+ return Effect.gen(function* () {
+ yield* runRegister(recorded, {
+ iconSource,
+ iconReadError: PlatformError.systemError({
_tag: "PermissionDenied",
module: "FileSystem",
- method: "copyFile",
- description: "read-only icon directory",
+ method: "readFile",
+ description: "unreadable bundled icon",
}),
});
+ assert.deepEqual(recorded.binaryReads, [iconSource]);
+ assert.deepEqual(recorded.binaryWrites, []);
+ assert.deepEqual(recorded.copies, []);
assert.equal(recorded.files.length, 1);
- assert.deepEqual(
- recorded.commands.map(({ command }) => command),
- ["update-desktop-database", "xdg-mime"],
- );
+ assert.deepEqual(recorded.commands, registrationCommands);
});
});
diff --git a/apps/desktop/src/app/DesktopLinuxUrlHandler.ts b/apps/desktop/src/app/DesktopLinuxUrlHandler.ts
index e03418964e3..83c595ea678 100644
--- a/apps/desktop/src/app/DesktopLinuxUrlHandler.ts
+++ b/apps/desktop/src/app/DesktopLinuxUrlHandler.ts
@@ -301,12 +301,16 @@ export const make = Effect.gen(function* () {
yield* Effect.gen(function* () {
const { png } = yield* assets.iconPaths;
if (Option.isNone(png)) return;
+ // Electron's copyFile extracts ASAR entries to temporary files it never removes.
+ const icon = yield* fileSystem.readFile(png.value);
+ const current = yield* fileSystem.readFile(iconPath).pipe(Effect.option);
+ if (Option.isSome(current) && sameBytes(current.value, icon)) return;
// The AppImage mount is temporary; the chooser needs the icon after exit.
yield* fileSystem.makeDirectory(iconsDir, { recursive: true });
- yield* fileSystem.copyFile(png.value, iconPath);
+ yield* fileSystem.writeFile(iconPath, icon, { mode: 0o600 });
}).pipe(
Effect.catch((error) =>
- logWarning("URL handler icon copy failed", { iconPath, category: error.reason._tag }),
+ logWarning("URL handler icon install failed", { iconPath, category: error.reason._tag }),
),
);
diff --git a/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts b/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts
index 9c1ef0b69b9..c4e13b4de52 100644
--- a/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts
+++ b/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts
@@ -14,10 +14,10 @@ const {
setNameMock,
appState,
mkdirSyncMock,
+ readFileSyncMock,
writeFileSyncMock,
copyFileSyncMock,
execFileSyncMock,
- readFileSyncMock,
} = vi.hoisted(() => ({
appendSwitchMock: vi.fn(),
getSwitchValueMock: vi.fn(),
@@ -27,10 +27,10 @@ const {
setNameMock: vi.fn(),
appState: { version: "0.0.37", isPackaged: true },
mkdirSyncMock: vi.fn(),
+ readFileSyncMock: vi.fn(),
writeFileSyncMock: vi.fn(),
copyFileSyncMock: vi.fn(),
execFileSyncMock: vi.fn(),
- readFileSyncMock: vi.fn(),
}));
vi.mock("electron", () => ({
@@ -66,6 +66,11 @@ vi.mock("node:child_process", () => ({
import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts";
+const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png";
+const iconPath = "/xdg/icons/com.t3tools.T3Code.desktop.png";
+const desktopEntryPath = "/xdg/applications/com.t3tools.T3Code.desktop";
+const iconBytes = Buffer.from([0x89, 0x50, 0x00, 0xff, 0x80, 0x0a]);
+
describe("DesktopPreReadyPlatform", () => {
beforeEach(() => {
appendSwitchMock.mockReset();
@@ -77,10 +82,15 @@ describe("DesktopPreReadyPlatform", () => {
appState.version = "0.0.37";
appState.isPackaged = true;
mkdirSyncMock.mockReset();
+ readFileSyncMock.mockReset();
writeFileSyncMock.mockReset();
copyFileSyncMock.mockReset();
execFileSyncMock.mockReset();
- readFileSyncMock.mockReset().mockReturnValue("{}");
+ readFileSyncMock.mockReset().mockImplementation((path: string) => {
+ if (path.endsWith("/desktop-settings.json")) return "{}";
+ if (path === iconSource) return Buffer.from(iconBytes);
+ throw new Error(`missing file: ${path}`);
+ });
});
it.effect("persists Linux scaling synchronously across relaunches and settings rewrites", () => {
@@ -222,14 +232,13 @@ describe("DesktopPreReadyPlatform", () => {
let desktopName = "t3code.desktop";
let desktopEntry = previousEntry;
let iconInstalled = false;
- copyFileSyncMock.mockImplementation((_source: string, destination: string) => {
- iconInstalled = destination === "/xdg/icons/com.t3tools.T3Code.desktop.png";
- });
setDesktopNameMock.mockImplementation((name: string) => {
desktopName = name;
});
- writeFileSyncMock.mockImplementation((path: string, contents: string) => {
- if (path === "/xdg/applications/com.t3tools.T3Code.desktop") desktopEntry = contents;
+ writeFileSyncMock.mockImplementation((path: string, contents: string | Buffer) => {
+ if (path === iconPath)
+ iconInstalled = Buffer.isBuffer(contents) && contents.equals(iconBytes);
+ if (path === desktopEntryPath && typeof contents === "string") desktopEntry = contents;
});
return Effect.scoped(
@@ -254,6 +263,15 @@ describe("DesktopPreReadyPlatform", () => {
"Icon=/xdg/icons/com.t3tools.T3Code.desktop.png",
);
assert.isTrue(identity.iconInstalled);
+ assert.deepEqual(
+ readFileSyncMock.mock.calls.filter(([path]) => path === iconSource),
+ [[iconSource]],
+ );
+ assert.deepEqual(
+ writeFileSyncMock.mock.calls.filter(([path]) => path === iconPath),
+ [[iconPath, iconBytes, { mode: 0o600 }]],
+ );
+ assert.equal(copyFileSyncMock.mock.calls.length, 0);
}),
).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())));
});
@@ -301,18 +319,62 @@ describe("DesktopPreReadyPlatform", () => {
);
});
- it.effect("still prepares the portal entry when the bundled icon cannot be copied", () => {
+ it.effect("refreshes a stale icon once across repeated launches", () => {
+ vi.stubEnv("VITE_DEV_SERVER_URL", "");
+ vi.stubEnv("XDG_DATA_HOME", "/xdg");
getSwitchValueMock.mockReturnValue("");
- copyFileSyncMock.mockImplementation(() => {
- throw new Error("missing bundled icon");
+ let installedIcon = Buffer.from([0x89, 0x50, 0x00, 0xfe, 0x80, 0x0a]);
+ readFileSyncMock.mockImplementation((path: string) => {
+ if (path === iconSource) return Buffer.from(iconBytes);
+ if (path === iconPath) return Buffer.from(installedIcon);
+ return "{}";
+ });
+ writeFileSyncMock.mockImplementation((path: string, contents: Buffer) => {
+ if (path === iconPath) installedIcon = Buffer.from(contents);
});
+
return Effect.gen(function* () {
yield* DesktopPreReadyPlatform.make;
- const contents = writeFileSyncMock.mock.calls[0]?.[1];
+ assert.deepEqual(installedIcon, iconBytes);
+ yield* DesktopPreReadyPlatform.make;
+ assert.deepEqual(
+ writeFileSyncMock.mock.calls.filter(([path]) => path === iconPath),
+ [[iconPath, iconBytes, { mode: 0o600 }]],
+ );
+ assert.equal(copyFileSyncMock.mock.calls.length, 0);
+ assert.equal(
+ writeFileSyncMock.mock.calls.filter(([path]) => path === desktopEntryPath).length,
+ 2,
+ );
+ assert.equal(setDesktopNameMock.mock.calls.length, 2);
+ }).pipe(
+ Effect.provideService(HostProcess.Platform, "linux"),
+ Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
+ );
+ });
+
+ it.effect("still prepares the portal entry when the bundled icon cannot be read", () => {
+ vi.stubEnv("VITE_DEV_SERVER_URL", "");
+ vi.stubEnv("XDG_DATA_HOME", "/xdg");
+ getSwitchValueMock.mockReturnValue("");
+ readFileSyncMock.mockImplementation((path: string) => {
+ if (path === iconSource) throw new Error("unreadable bundled icon");
+ return "{}";
+ });
+ return Effect.gen(function* () {
+ yield* DesktopPreReadyPlatform.make;
+ assert.isTrue(readFileSyncMock.mock.calls.some(([path]) => path === iconSource));
+ assert.equal(copyFileSyncMock.mock.calls.length, 0);
+ assert.equal(writeFileSyncMock.mock.calls.length, 1);
+ const [path, contents] = writeFileSyncMock.mock.calls[0]!;
+ assert.equal(path, desktopEntryPath);
assert.include(contents, "MimeType=x-scheme-handler/t3code;");
- assert.include(contents, "Icon=");
+ assert.include(contents, `Icon=${iconPath}`);
assert.equal(setDesktopNameMock.mock.calls.length, 1);
- }).pipe(Effect.provideService(HostProcess.Platform, "linux"));
+ }).pipe(
+ Effect.provideService(HostProcess.Platform, "linux"),
+ Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
+ );
});
it.effect(
diff --git a/apps/desktop/src/app/DesktopPreReadyPlatform.ts b/apps/desktop/src/app/DesktopPreReadyPlatform.ts
index d9ffba80197..275367bab92 100644
--- a/apps/desktop/src/app/DesktopPreReadyPlatform.ts
+++ b/apps/desktop/src/app/DesktopPreReadyPlatform.ts
@@ -97,14 +97,21 @@ export const make = Effect.gen(function* () {
: undefined;
if (iconPath !== undefined) {
try {
- NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
- NodeFS.copyFileSync(
+ // Electron's copyFile extracts ASAR entries to temporary files it never removes.
+ const icon = NodeFS.readFileSync(
NodePath.posix.join(
Electron.app.getAppPath(),
"apps/desktop/prod-resources/icon.png",
),
- iconPath,
);
+ let current: Buffer | null = null;
+ try {
+ current = NodeFS.readFileSync(iconPath);
+ } catch {}
+ if (current === null || !current.equals(icon)) {
+ NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
+ NodeFS.writeFileSync(iconPath, icon, { mode: 0o600 });
+ }
} catch {}
}
NodeFS.writeFileSync(