From b3d37f948cabec76e187bed2f2fbb1336da5ae3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 15:20:25 +0000 Subject: [PATCH] fix(studio): vitest exits when a project is open in the dev data folder (#8514) --- packages/studio/vite.config.test.ts | 35 +++++++++++++++++++++++++++-- packages/studio/vite.config.ts | 11 ++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/studio/vite.config.test.ts b/packages/studio/vite.config.test.ts index 29f35e916e..53455966ff 100644 --- a/packages/studio/vite.config.test.ts +++ b/packages/studio/vite.config.test.ts @@ -1,8 +1,24 @@ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; -import { describe, expect, it } from "vitest"; -import config, { stableStylesCssPlugin } from "./vite.config"; +import { createServer } from "vite"; +import { describe, expect, it, vi } from "vitest"; +import config, { devProjectApi, stableStylesCssPlugin } from "./vite.config"; + +const watchers = vi.hoisted(() => ({ open: new Set(), closed: new Set() })); +vi.mock("chokidar", async (importOriginal) => { + const chokidar = await importOriginal(); + return { + ...chokidar, + watch: (...args: Parameters) => { + const watcher = chokidar.watch(...args); + const close = watcher.close.bind(watcher); + watchers.open.add(watcher); + watcher.close = () => (watchers.closed.add(watcher), close()); + return watcher; + }, + }; +}); const packageJson = JSON.parse(readFileSync(resolve(__dirname, "package.json"), "utf8")) as { publishConfig: { exports: Record }; @@ -87,3 +103,18 @@ describe("stableStylesCssPlugin", () => { } }); }); + +describe("devProjectApi", () => { + // fails without the fix: the project watcher closed only with an http server, so `vitest list` never exited. + it("closes its project watcher when a server with no http server closes", async () => { + const server = await createServer({ + configFile: false, + logLevel: "silent", + plugins: [devProjectApi()], + server: { middlewareMode: true, ws: false }, + }); + expect(watchers.open.size).toBe(1); + await server.close(); + expect(watchers.closed).toEqual(watchers.open); + }); +}); diff --git a/packages/studio/vite.config.ts b/packages/studio/vite.config.ts index d9cca1cf66..f0b50a7502 100644 --- a/packages/studio/vite.config.ts +++ b/packages/studio/vite.config.ts @@ -13,7 +13,7 @@ import { isIP } from "node:net"; import { join, resolve } from "node:path"; import { readNodeRequestBody } from "./vite.request-body.js"; import { bindNodeRequestSignal } from "./vite.request-signal.js"; -import { watch } from "chokidar"; +import { type FSWatcher, watch } from "chokidar"; import { createProjectSignatureCache, createViteAdapter } from "./vite.adapter"; import { previewConfigPayload } from "./vite.preview-config"; import { loadStudioServerDevModule } from "./vite.studio-server-module"; @@ -111,9 +111,10 @@ function viteAllowsHost(host: string, allowedHosts: readonly string[]): boolean // ── Vite plugin ────────────────────────────────────────────────────────────── -function devProjectApi(): Plugin { +export function devProjectApi(): Plugin { const dataDir = resolve(__dirname, "data/projects"); const runtimePath = resolve(__dirname, "../core/dist/hyperframe.runtime.iife.js"); + let watching: FSWatcher | undefined; return { name: "studio-dev-api", @@ -139,12 +140,12 @@ function devProjectApi(): Plugin { /* dataDir doesn't exist yet */ } - const projectWatcher = watch([...watchedProjects.keys()], { + const projectWatcher = (watching = watch([...watchedProjects.keys()], { ignoreInitial: true, // A project write is a whole-file replace; wait for it to settle so a // half-written composition is never announced. awaitWriteFinish: { stabilityThreshold: 40, pollInterval: 10 }, - }); + })); // This watcher, and not Vite's, is what clears the preview signature. // Vite's ignores `data/projects/**`, so subscribing the cache to it left @@ -355,8 +356,8 @@ function devProjectApi(): Plugin { }, }); }); - server.httpServer?.on("close", () => void projectWatcher.close()); }, + closeBundle: () => watching?.close(), }; }