feat(FN-2089): merge fusion/fn-2089

This commit is contained in:
Fusion
2026-04-19 00:37:03 -07:00
committed by gsxdsm
parent f3de45b050
commit 7424c843f2
12 changed files with 312 additions and 113 deletions

View File

@@ -16,7 +16,30 @@ import { tmpdir } from "node:os";
import { dirname, join, resolve, sep } from "node:path";
import { isMainThread } from "node:worker_threads";
const realProjectRootRaw = process.cwd();
const originalCwd = process.cwd.bind(process);
function ensureValidCwd(): string {
try {
return originalCwd();
} catch {
const fallback = tmpdir();
try {
process.chdir(fallback);
} catch {
// Ignore — if this fails too, callers will still get fallback.
}
return fallback;
}
}
// Guard against uv_cwd crashes if a prior test removed the current directory.
process.cwd = (() => {
return function guardedCwd() {
return ensureValidCwd();
};
})() as typeof process.cwd;
const realProjectRootRaw = ensureValidCwd();
const realProjectRoot = (() => {
try {
return realpathSync(realProjectRootRaw);

View File

@@ -1,11 +1,12 @@
/**
* Vitest globalSetup hook. The returned function runs once after the entire
* test run completes, regardless of whether individual workers exited cleanly.
* Wipes the shared FUSION_TEST_WORKER_ROOT directory that holds per-worker
* temp dirs created by vitest-setup.ts.
* Vitest globalSetup hook.
*
* We only publish the shared worker-root env var here. Teardown is intentionally
* a no-op because deleting shared temp roots during teardown can race with
* still-running suites in some Vitest pool modes and trigger uv_cwd failures.
* Worker dirs are cleaned by vitest-setup.ts on process exit.
*/
import { readdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -17,27 +18,11 @@ export default function setup(): () => Promise<void> {
process.env.FUSION_TEST_WORKER_ROOT = WORKER_ROOT;
return async function teardown() {
// IMPORTANT: do not remove WORKER_ROOT recursively here.
// In some Vitest pool modes, global setup/teardown can run in multiple
// processes. If one process deletes the shared root while another process
// is still running with cwd inside it, the other process will fail with
// ENOENT uv_cwd.
// Intentionally no-op.
//
// Instead, clean up only this process's worker dirs. Other processes clean
// up their own dirs via their exit hooks.
const ownPrefix = `w-${process.pid}-`;
try {
const entries = readdirSync(WORKER_ROOT, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory() || !entry.name.startsWith(ownPrefix)) continue;
try {
rmSync(join(WORKER_ROOT, entry.name), { recursive: true, force: true });
} catch {
// Ignore per-dir cleanup errors.
}
}
} catch {
// Ignore — OS cleans /tmp eventually.
}
// Worker temp dirs are cleaned by vitest-setup.ts using process.on("exit")
// after first chdir-ing out of the worker dir. Deleting shared temp roots
// from global teardown is unsafe under some Vitest pool modes because it
// can run while other suites are still active, causing ENOENT uv_cwd.
};
}