Skip to content

The agent that grows with you

fix(desktop): test runs stop leaking temp dirs into TMPDIR

teknium1989798cd5e69
diff --git a/apps/desktop/e2e/core/playwright.config.ts b/apps/desktop/e2e/core/playwright.config.ts
index f8a3d846b98..ff95dcce9f4 100644
--- a/apps/desktop/e2e/core/playwright.config.ts
+++ b/apps/desktop/e2e/core/playwright.config.ts
@@ -1,3 +1,4 @@
+import '../run-tmp'
import '../fix-electron-tracing'
import { defineConfig } from '@playwright/test'
diff --git a/apps/desktop/e2e/run-tmp.ts b/apps/desktop/e2e/run-tmp.ts
new file mode 100644
index 00000000000..2e963da87e8
--- /dev/null
+++ b/apps/desktop/e2e/run-tmp.ts
@@ -0,0 +1,24 @@
+/**
+ * One temp root per Playwright run, removed when the runner exits.
+ *
+ * Playwright (artifact and stack-trace dirs), the Electron app, its backend
+ * and every sandbox mkdtemp under os.tmpdir(), and a run that fails or times
+ * out in setup leaves all of it behind (a clean run of two specs left eight
+ * `playwright-tracing-*` dirs). Workers and the apps they launch inherit
+ * TMPDIR, so everything lands here; a SIGKILLed run leaves one dir, not dozens.
+ *
+ * Config modules load in the runner and again in each worker; only the first
+ * (runner) load creates the root.
+ */
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+if (!process.env.HERMES_E2E_RUN_TMP) {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'pw-'))
+ process.env.HERMES_E2E_RUN_TMP = root
+ process.env.TMPDIR = root
+ process.env.TEMP = root
+ process.env.TMP = root
+ process.on('exit', () => fs.rmSync(root, { recursive: true, force: true }))
+}
diff --git a/apps/desktop/playwright.config.ts b/apps/desktop/playwright.config.ts
index 71b67a8139d..a51beeda528 100644
--- a/apps/desktop/playwright.config.ts
+++ b/apps/desktop/playwright.config.ts
@@ -1,3 +1,4 @@
+import './e2e/run-tmp'
import './e2e/fix-electron-tracing'
import { defineConfig, type ReporterDescription } from '@playwright/test'
diff --git a/apps/desktop/vitest.config.ts b/apps/desktop/vitest.config.ts
index faf89847e2f..9c2199c323d 100644
--- a/apps/desktop/vitest.config.ts
+++ b/apps/desktop/vitest.config.ts
@@ -34,6 +34,7 @@ const electronNative: TestProjectConfiguration = {
export default defineConfig({
test: {
+ globalSetup: ['./vitest.run-tmp.ts'],
projects: [reactUi, electronNative]
}
})
diff --git a/apps/desktop/vitest.run-tmp.ts b/apps/desktop/vitest.run-tmp.ts
new file mode 100644
index 00000000000..16969686df7
--- /dev/null
+++ b/apps/desktop/vitest.run-tmp.ts
@@ -0,0 +1,19 @@
+/**
+ * One temp root per vitest run, removed at teardown.
+ *
+ * Tests mkdtemp under os.tmpdir() and not all of them remove what they make:
+ * one run of the `electron` project left 54 dirs behind. Workers inherit
+ * TMPDIR from this process, so everything a test creates lands here instead.
+ */
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+export default function setup(): () => void {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'vitest-'))
+ process.env.TMPDIR = root
+ process.env.TEMP = root
+ process.env.TMP = root
+
+ return () => fs.rmSync(root, { recursive: true, force: true })
+}