From 5140ec7160b35d6ba68c020e3c51477259837b09 Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Sun, 15 Nov 2026 11:33:07 -0800 Subject: [PATCH 1/3] fix(onboarding): Set up no longer misses a legacy ComfyUI Desktop install found at startup On a cold start the app looks for an old ComfyUI Desktop install and, when it finds one, adds it to the builds list. The Set up screen then offers "Bring over workflows, models and settings from ComfyUI Desktop" and its button reads Migrate instead of Install. The screen asks for that once, when it opens, and the answer is read from the builds list on disk. The legacy install is added by a write that startup requests and does not wait for, queued behind three other startup writes, and reads of the list did not wait for queued writes. The first window is created about 70 ms after those writes are requested, so the two race on every cold start. When the read comes first the screen shows a plain Install with no offer and never asks again. Reads of the builds list now wait for the writes that were requested before them. Startup reads the list before it opens the first window, so the window now opens after the legacy install is in the list, and the Set up screen's question cannot be answered from an older list either. Seen as a flaky e2e/lifecycle-first-use-migrate.test.ts on Windows. On main, 10 of 60 repeats of that spec had no offer; in all 10 the legacy install was in the list afterwards, written 100 to 300 ms after the panel had loaded (run 43751943810, throwaway branch bl/diag-lifecycle-migrate-windows). --- src/main/installations.test.ts | 80 ++++++++++++++++++ src/main/installations.ts | 14 ++-- src/main/lib/firstUseDetection.test.ts | 112 +++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 7 deletions(-) create mode 100644 src/main/lib/firstUseDetection.test.ts diff --git a/src/main/installations.test.ts b/src/main/installations.test.ts index 92703af85..084aef2d8 100644 --- a/src/main/installations.test.ts +++ b/src/main/installations.test.ts @@ -916,3 +916,83 @@ describe('mutations fail closed when installations.json cannot be recovered (iss expect(fs.readFileSync(dataPath, 'utf-8')).toBe(corrupt) }) }) + +function holdNextWriteAtItsRename(): { reached: Promise; land: () => void } { + const realRename = fs.promises.rename.bind(fs.promises) + let markReached: () => void = () => {} + const reached = new Promise((resolve) => { + markReached = resolve + }) + let land: () => void = () => {} + const allowed = new Promise((resolve) => { + land = resolve + }) + vi.spyOn(fs.promises, 'rename').mockImplementationOnce(async (from, to) => { + markReached() + await allowed + return realRename(from, to) + }) + return { reached, land } +} + +describe('reading the list while a write requested earlier is still reaching the disk', () => { + it('lists a build that is still being added', async () => { + const installations = await loadInstallations() + const disk = holdNextWriteAtItsRename() + const adding = installations.ensureExists('desktop', { + name: 'ComfyUI Legacy Desktop', + sourceId: 'desktop', + installPath: path.join(tmpRoot, 'legacy'), + status: 'installed' + }) + const listed = installations.list() + + await disk.reached + disk.land() + + expect((await listed).map((inst) => inst.sourceId)).toEqual(['desktop']) + await adding + }) + + it('returns a build with a change that is still being saved', async () => { + const installations = await loadInstallations() + const entry = await installations.add({ + name: 'Local A', + installPath: path.join(tmpRoot, 'a'), + sourceId: 'standalone', + status: 'installed' + }) + const disk = holdNextWriteAtItsRename() + const renaming = installations.update(entry.id, { name: 'Renamed' }) + const fetched = installations.get(entry.id) + + await disk.reached + disk.land() + + expect((await fetched)!.name).toBe('Renamed') + await renaming + }) + + it('still answers after a write that failed', async () => { + const installations = await loadInstallations() + await installations.add({ + name: 'Local A', + installPath: path.join(tmpRoot, 'a'), + sourceId: 'standalone', + status: 'installed' + }) + const full = new Error('fake ENOSPC') as NodeJS.ErrnoException + full.code = 'ENOSPC' + vi.spyOn(fs.promises, 'rename').mockRejectedValueOnce(full) + const adding = installations.ensureExists('desktop', { + name: 'ComfyUI Legacy Desktop', + sourceId: 'desktop', + installPath: path.join(tmpRoot, 'legacy'), + status: 'installed' + }) + const listed = installations.list() + + await expect(adding).rejects.toThrow('fake ENOSPC') + expect((await listed).map((inst) => inst.name)).toEqual(['Local A']) + }) +}) diff --git a/src/main/installations.ts b/src/main/installations.ts index 1250b9842..07f7ca38e 100644 --- a/src/main/installations.ts +++ b/src/main/installations.ts @@ -113,8 +113,8 @@ function maybeSeedFromEnv(): void { } } -async function load(): Promise { - return (await loadOutcome()).records +function loadAfterRequestedWrites(): Promise { + return _queue.then(async () => (await loadOutcome()).records) } async function loadOutcome(): Promise<{ records: InstallationRecord[]; unreadable: boolean }> { @@ -152,11 +152,11 @@ async function save(installations: InstallationRecord[]): Promise { } export async function list(): Promise { - return load() + return loadAfterRequestedWrites() } export async function hasNameConflict(id: string, name: string): Promise { - const all = await load() + const all = await loadAfterRequestedWrites() return all.some((i) => i.id !== id && i.name === name) } @@ -281,7 +281,7 @@ export async function associateUnownedBuildInstalls( } export async function get(id: string): Promise { - return (await load()).find((i) => i.id === id) ?? null + return (await loadAfterRequestedWrites()).find((i) => i.id === id) ?? null } export async function reorder(orderedIds: string[]): Promise { @@ -376,7 +376,7 @@ export async function clearPendingTemplateOpen(installationId: string): Promise< } export async function getRecent(): Promise { - const list = await load() + const list = await loadAfterRequestedWrites() let best: InstallationRecord | null = null let bestTs = -Infinity for (const inst of list) { @@ -393,7 +393,7 @@ export async function getRecentByCategory( category: string, resolveCategory: (inst: InstallationRecord) => string | undefined ): Promise { - const list = await load() + const list = await loadAfterRequestedWrites() let best: InstallationRecord | null = null let bestTs = -Infinity for (const inst of list) { diff --git a/src/main/lib/firstUseDetection.test.ts b/src/main/lib/firstUseDetection.test.ts new file mode 100644 index 000000000..52f111c1c --- /dev/null +++ b/src/main/lib/firstUseDetection.test.ts @@ -0,0 +1,112 @@ +import fs from 'fs' +import os from 'os' +import path from 'path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +let tmpRoot = '' +let userDataPath = '' + +async function loadStartup() { + const installations = await import('../installations') + const { detectFirstUseState } = await import('./firstUseDetection') + return { installations, detectFirstUseState } +} + +const CLOUD = { + name: 'Comfy Cloud', + sourceId: 'cloud', + remoteUrl: 'https://cloud.comfy.org/', + launchMode: 'window', + browserPartition: 'shared' +} + +function legacyDesktop(): Record { + return { + name: 'ComfyUI Legacy Desktop', + sourceId: 'desktop', + installPath: path.join(tmpRoot, 'legacy'), + launchMode: 'external', + status: 'installed' + } +} + +function holdNextWriteAtItsRename(): { reached: Promise; land: () => void } { + const realRename = fs.promises.rename.bind(fs.promises) + let markReached: () => void = () => {} + const reached = new Promise((resolve) => { + markReached = resolve + }) + let land: () => void = () => {} + const allowed = new Promise((resolve) => { + land = resolve + }) + vi.spyOn(fs.promises, 'rename').mockImplementationOnce(async (from, to) => { + markReached() + await allowed + return realRename(from, to) + }) + return { reached, land } +} + +beforeEach(() => { + tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'comfyui-desktop-2-first-use-')) + userDataPath = path.join(tmpRoot, 'user-data') + fs.mkdirSync(userDataPath, { recursive: true }) + + vi.resetModules() + vi.restoreAllMocks() + vi.doMock('electron', () => ({ + app: { + getPath: () => userDataPath + } + })) +}) + +afterEach(() => { + vi.restoreAllMocks() + fs.rmSync(tmpRoot, { recursive: true, force: true }) +}) + +describe('what Set up is told about the builds already on this computer', () => { + it('has nothing to offer on a computer with only Comfy Cloud', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + + expect(await detectFirstUseState()).toEqual({ skipPick: false, hasLegacyDesktop: false }) + }) + + it('offers to migrate a legacy ComfyUI Desktop install found at startup', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + await installations.ensureExists('desktop', legacyDesktop()) + + expect(await detectFirstUseState()).toEqual({ skipPick: false, hasLegacyDesktop: true }) + }) + + it('offers to migrate a legacy install that startup is still adding to the builds list', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + const disk = holdNextWriteAtItsRename() + const adding = installations.ensureExists('desktop', legacyDesktop()) + const asked = detectFirstUseState() + + await disk.reached + disk.land() + + expect(await asked).toEqual({ skipPick: false, hasLegacyDesktop: true }) + await adding + }) + + it('lets someone who already has a build keep it', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + await installations.add({ + name: 'ComfyUI', + installPath: path.join(tmpRoot, 'build'), + sourceId: 'standalone', + status: 'installed' + }) + + expect(await detectFirstUseState()).toEqual({ skipPick: true, hasLegacyDesktop: false }) + }) +}) From 78e6e35676fdbc40938e5a77a943329634fa759f Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Sun, 15 Nov 2026 11:33:41 -0800 Subject: [PATCH 2/3] test(e2e): the lifecycle delete spec fails with the error the app shows When the app refused the delete, the build's card showed Error and the spec waited 30 seconds for the card to go away, then failed with "deleted tile never disappeared from chooser". That is what happened in run 43676395717: the delete was refused because the build's own update check held its operation slot, which #2117 has since fixed. The spec now stops waiting when the card shows Error, opens it, and fails with the message the app gives. --- e2e/lifecycle-delete-untrack.test.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/e2e/lifecycle-delete-untrack.test.ts b/e2e/lifecycle-delete-untrack.test.ts index 543223da9..a78098401 100644 --- a/e2e/lifecycle-delete-untrack.test.ts +++ b/e2e/lifecycle-delete-untrack.test.ts @@ -18,6 +18,8 @@ const DELETE_NAME = 'Delete Me' const MARKER_FILENAME = '.comfyui-desktop-2' +const DELETE_ERROR_BADGE = `${byTestId(TID.dashboardTile(DELETE_ID))} .chooser-tile-error-badge` + async function pathExists(p: string): Promise { try { await access(p) @@ -31,6 +33,13 @@ async function tileExists(installationId: string): Promise { return ctx.panel.exists(byTestId(TID.dashboardTile(installationId))) } +async function errorShownOnDeletedBuild(): Promise { + if (!(await ctx.panel.isVisible(DELETE_ERROR_BADGE))) return null + expect(await ctx.panel.click(DELETE_ERROR_BADGE), 'error badge click dispatched').toBe(true) + await ctx.panel.waitForVisible('.base-alert-message-text', { timeout: 5_000 }) + return ctx.panel.textOf('.base-alert-message-text') +} + async function openKebabAndClick(installationId: string, menuItemId: string): Promise { const kebabClicked = await ctx.panel.click(byTestId(TID.dashboardTileKebab(installationId))) expect(kebabClicked, `kebab click on ${installationId}`).toBe(true) @@ -123,10 +132,11 @@ test('kebab Delete drops the record AND removes the install directory @lifecycle const confirmed = await ctx.panel.click(byTestId(TID.baseAlertAction)) expect(confirmed, 'delete confirm click dispatched').toBe(true) - await ctx.panel.waitFor(async () => !(await tileExists(DELETE_ID)), { - timeout: 30_000, - message: 'deleted tile never disappeared from chooser' - }) + await ctx.panel.waitFor( + async () => !(await tileExists(DELETE_ID)) || (await ctx.panel.isVisible(DELETE_ERROR_BADGE)), + { timeout: 30_000, message: 'the delete neither removed the build nor reported an error' } + ) + expect(await errorShownOnDeletedBuild(), 'the app reported an error for the delete').toBeNull() await expect .poll(() => pathExists(deletePath), { timeout: 30_000, intervals: [250, 500, 1000] }) .toBe(false) From fc737582bbc26371c82a887306bc0dd63503a62c Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Sun, 15 Nov 2026 11:37:52 -0800 Subject: [PATCH 3/3] test(onboarding): keep the first-use state test in its own temporary folder on Linux On Linux the builds list lives under XDG_DATA_HOME when that is set, so the new test could have read and written the real app data folder of whoever ran it. It now runs with the same platform stub as installations.test.ts, which puts the list in the test's temporary folder on every platform. --- src/main/lib/firstUseDetection.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/lib/firstUseDetection.test.ts b/src/main/lib/firstUseDetection.test.ts index 52f111c1c..749f829b0 100644 --- a/src/main/lib/firstUseDetection.test.ts +++ b/src/main/lib/firstUseDetection.test.ts @@ -60,9 +60,14 @@ beforeEach(() => { getPath: () => userDataPath } })) + vi.stubGlobal('process', { + ...process, + platform: 'win32' + }) }) afterEach(() => { + vi.unstubAllGlobals() vi.restoreAllMocks() fs.rmSync(tmpRoot, { recursive: true, force: true }) })