@devries-cameron20 / t3code Mirror · read-only / commits / b20d29dc43
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 01:30 UTC

fix(desktop): prevent startup from running twice (#11857)

Julius Marminge committed Sep 14, 2026 at 22:11 UTC b20d29dc438bcabb2c25e85976dc108169bb4c6d
2 files changed +128 -3
apps/desktop/scripts/main-process-bundle.test.mjs new
+110
@@ -0,0 +1,110 @@
1 +import * as NodeFSP from "node:fs/promises";
2 +import * as NodeOS from "node:os";
3 +import * as NodePath from "node:path";
4 +import * as NodeVM from "node:vm";
5 +import { build } from "vite-plus/pack";
6 +import { assert, it } from "vite-plus/test";
7 +
8 +import desktopConfig from "../vite.config.ts";
9 +
10 +it("keeps lazy Linux imports and worker bundles from executing desktop startup twice", async () => {
11 + const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-desktop-bundle-"));
12 + try {
13 + const workerEntries = [
14 + "src/electron/WindowsForegroundFocusWorker.ts",
15 + "src/snapShot/GlobalShiftShortcutWorker.ts",
16 + "src/snapShot/RegionSnapShotWorker.ts",
17 + "src/snapShot/SnapShotAccessibilityWorker.ts",
18 + ];
19 + await Promise.all([
20 + NodeFSP.mkdir(NodePath.join(directory, "src/electron"), { recursive: true }),
21 + NodeFSP.mkdir(NodePath.join(directory, "src/snapShot"), { recursive: true }),
22 + ]);
23 + await Promise.all([
24 + NodeFSP.writeFile(
25 + NodePath.join(directory, "src/main.ts"),
26 + `import { shared } from "./shared.ts";
27 +process.emit("startup", shared.value);
28 +void import("./linux.ts").then(({ result }) => process.emit("ready", result));`,
29 + ),
30 + NodeFSP.writeFile(
31 + NodePath.join(directory, "src/shared.ts"),
32 + "export const shared = { value: 42 };",
33 + ),
34 + NodeFSP.writeFile(
35 + NodePath.join(directory, "src/linux.ts"),
36 + 'import { shared } from "./shared.ts"; export const result = shared.value + 1;',
37 + ),
38 + ...workerEntries.map((entry) =>
39 + NodeFSP.writeFile(
40 + NodePath.join(directory, entry),
41 + 'import { shared } from "../shared.ts"; process.emit("worker", shared.value);',
42 + ),
43 + ),
44 + ]);
45 + assert.ok(Array.isArray(desktopConfig.pack));
46 + const fixtureEntries = new Set(["src/main.ts", ...workerEntries]);
47 + for (const packConfig of desktopConfig.pack) {
48 + if (!Array.isArray(packConfig.entry)) continue;
49 + if (!packConfig.entry.some((entry) => fixtureEntries.has(entry))) continue;
50 + await build({
51 + ...packConfig,
52 + config: false,
53 + cwd: directory,
54 + tsconfig: false,
55 + sourcemap: false,
56 + onSuccess: undefined,
57 + logLevel: "silent",
58 + });
59 + }
60 +
61 + const outputDirectory = NodePath.join(directory, "dist-electron");
62 + const filenames = (await NodeFSP.readdir(outputDirectory, { recursive: true })).filter(
63 + (filename) => filename.endsWith(".cjs"),
64 + );
65 + const sources = new Map(
66 + await Promise.all(
67 + filenames.map(async (filename) => {
68 + const path = NodePath.join(outputDirectory, filename);
69 + return [path, await NodeFSP.readFile(path, "utf8")];
70 + }),
71 + ),
72 + );
73 + const modules = new Map();
74 + const startups = [];
75 + const workers = [];
76 + const ready = Promise.withResolvers();
77 + const load = (filename, cacheModule = true) => {
78 + const cached = modules.get(filename);
79 + if (cached) return cached.exports;
80 + const module = { exports: {} };
81 + if (cacheModule) modules.set(filename, module);
82 + const source = sources.get(filename);
83 + assert.ok(source, `Missing bundle: ${filename}`);
84 + NodeVM.runInNewContext(source, {
85 + exports: module.exports,
86 + module,
87 + require: (specifier) => load(NodePath.resolve(NodePath.dirname(filename), specifier)),
88 + process: {
89 + emit: (event, value) => {
90 + if (event === "startup") startups.push(value);
91 + if (event === "worker") workers.push(value);
92 + if (event === "ready") ready.resolve(value);
93 + },
94 + },
95 + });
96 + return module.exports;
97 + };
98 +
99 + load(NodePath.join(outputDirectory, "main.cjs"), false);
100 + assert.equal(await ready.promise, 43);
101 + assert.deepEqual(startups, [42]);
102 + for (const entry of workerEntries) {
103 + load(NodePath.join(outputDirectory, entry.replace(/^src\//, "").replace(/\.ts$/, ".cjs")));
104 + }
105 + assert.deepEqual(workers, [42, 42, 42, 42]);
106 + assert.deepEqual(startups, [42]);
107 + } finally {
108 + await NodeFSP.rm(directory, { recursive: true, force: true });
109 + }
110 +});
apps/desktop/vite.config.ts
+18 -3
@@ -48,6 +48,23 @@ export default defineConfig({
48 },
49 },
50 pack: [
51 + {
52 + format: "cjs",
53 + outDir: "dist-electron",
54 + dts: false,
55 + sourcemap: true,
56 + outExtensions: () => ({ js: ".cjs" }),
57 + define: publicConfigDefine,
58 + outputOptions: { codeSplitting: false },
59 + entry: ["src/main.ts"],
60 + clean: true,
61 + deps: {
62 + alwaysBundle: (id) => !id.startsWith("node:") && !isMainProcessExternal(id),
63 + neverBundle: isMainProcessExternal,
64 + onlyBundle: false,
65 + },
66 + ...(shouldLaunchElectronAfterPack ? { onSuccess: "node scripts/dev-electron.mjs" } : {}),
67 + },
68 {
69 format: "cjs",
70 outDir: "dist-electron",
@@ -56,19 +73,17 @@ export default defineConfig({
73 outExtensions: () => ({ js: ".cjs" }),
74 define: publicConfigDefine,
75 entry: [
59 - "src/main.ts",
76 "src/electron/WindowsForegroundFocusWorker.ts",
77 "src/snapShot/GlobalShiftShortcutWorker.ts",
78 "src/snapShot/RegionSnapShotWorker.ts",
79 "src/snapShot/SnapShotAccessibilityWorker.ts",
80 ],
65 - clean: true,
81 + clean: false,
82 deps: {
83 alwaysBundle: (id) => !id.startsWith("node:") && !isMainProcessExternal(id),
84 neverBundle: isMainProcessExternal,
85 onlyBundle: false,
86 },
71 - ...(shouldLaunchElectronAfterPack ? { onSuccess: "node scripts/dev-electron.mjs" } : {}),
87 },
88 {
89 format: "cjs",