fix(studio): vitest exits when a project is open in the dev data folder (#8514)

This commit is contained in:
Miguel Ángel
2026-11-22 15:20:25 +00:00
committed by GitHub
parent dd77990373
commit b3d37f948c
2 changed files with 39 additions and 7 deletions
+33 -2
View File
@@ -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<object>(), closed: new Set<object>() }));
vi.mock("chokidar", async (importOriginal) => {
const chokidar = await importOriginal<typeof import("chokidar")>();
return {
...chokidar,
watch: (...args: Parameters<typeof chokidar.watch>) => {
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<string, string> };
@@ -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);
});
});
+6 -5
View File
@@ -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(),
};
}