From f4d5761a2a9115b192f1ce62e2afb62d682b71ae Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:22:09 +0900 Subject: [PATCH] =?UTF-8?q?fix(test-run):=20=EC=8B=A4=ED=96=89=EB=A7=88?= =?UTF-8?q?=EB=8B=A4=20=EB=B9=8C=EB=93=9C=C2=B7Playwright=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=ED=8F=B4=EB=8D=94=EB=A5=BC=20=EB=94=B0=EB=A1=9C=20?= =?UTF-8?q?=EC=93=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 빌드는 `dist`, `.generated/frontend-runtime`, `artifacts/release`· `artifacts/quality`에 고정으로 쓰고, 다섯 Playwright 설정은 결과·report· JUnit을 `artifacts/tests/<이름>/`에 고정으로 쓴다. 한 checkout에서 두 번째 e2e 실행이 시작하면 빌드 1단계가 첫 실행의 preview가 서빙 중인 `dist`를 지우고, Playwright가 `outputDir`을 비워 첫 실행의 trace를 지운다. 증거 파일도 늦게 끝난 쪽 것으로 섞인다(1105/12-04). 고른 방법: 변수 두 개로 나눈다(`scripts/lib/run-isolation.ts`). - `TEST_RUN_ID`: Playwright 설정이 읽는다. 값이 있으면 그 실행의 결과· report·JUnit과 서빙할 빌드를 `.tmp/runs//` 아래 같은 상대 경로에 둔다. 없으면 지금 경로 그대로라 게이트, CI upload, 증거 검사가 읽는 곳이 바뀌지 않는다. - `RUN_OUTPUT_ROOT`: 설정이 web server에만 넘긴다. 빌드 단계 전부 (`build-frontend`, contract set, runtime config, SW asset 목록, SW 빌드, build manifest)와 `vite.config.ts`가 읽어 `dist`·생성 모듈·증거·Vite 캐시를 그 아래에 쓴다. preview는 `--outDir`로 같은 `dist`를 서빙한다. 빌드 코드는 `TEST_RUN_ID`를 읽지 않는다. e2e worker가 저장소 복사본을 빌드할 때(fixture-builds, service-worker-builds) runner 환경을 물려받는데, 그 빌드는 복사본 자기 `dist`에 써야 하기 때문이다. 두 루트는 `.` 또는 `.tmp/` 아래만 받는다. git, Tailwind 소스 스캔(report 안 문자열이 CSS에 들어가지 않게), Vite watcher, vitest, 저장소 복사 helper가 모두 `.tmp`를 건너뛴다. service-worker-builds의 복사 제외 목록에 `.tmp`를 더했다(fixture-builds와 같아짐). build manifest의 경로는 빌드가 쓴 폴더 기준 상대 경로로 둔다. `vite.config.ts`가 새로 가져오는 `scripts/lib/run-isolation.ts`는 저장소 일부를 임시 루트에 복사해 빌드하는 두 목록(초기 JS 측정 `scripts/scale/measure-feature-shape-growth.ts`, `tests/unit/product-feature-selection-build.test.ts`)에도 더한다. Storybook은 run ID가 있으면 `build:storybook`의 두 단계를 다른 `-o`로 돌린다. 같은 단계인지는 단위 테스트가 package script와 대조한다. 버린 대안: 운영 증거 경로(54개 스크립트, `config/ci/gates.json`)까지 run ID로 옮기기. 게이트와 workflow upload가 고정 경로를 읽으므로 이번 범위 밖이다(아래 한계). 실행마다 저장소를 임시 폴더로 복사하기: 매번 수십 MB 복사와 node_modules 링크가 들고, 개발자가 보는 결과 위치가 사라진다. 위험·한계: 포트는 아직 고정이라 이 커밋만으로는 두 실행이 함께 돌지 못한다(다음 커밋). `ci:gate`와 증거 검사(`check:test-evidence`, `verify:browser-capability-evidence`)는 기본 경로만 읽는다. run ID를 준 채 게이트를 돌리면 증거가 기본 경로에 새로 생기지 않아 게이트가 "not freshly produced"로 실패한다(통과로 새지 않는다). Storybook 내부 캐시 (`node_modules/.cache`)는 여전히 공유한다. capability 설정 (`playwright.capabilities.config.ts`)은 test:browser-file-storage-removal이 capability와 함께 지운다. 설정 테스트는 그 파일이 있을 때만 그 설정을 읽고, 경로를 변수로 두어 타입 검사가 그 파일을 요구하지 않는다. 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/unit/run-isolation.test.ts tests/unit/playwright-run-isolation.test.ts tests/unit/run-output-root-build.test.ts node scripts/run-vitest.ts run tests/unit/feature-shape-growth.test.ts tests/unit/product-feature-selection-build.test.ts corepack pnpm test:browser-file-storage-removal Refs: 1105/12-04 --- playwright.capabilities.config.ts | 15 +- playwright.config.ts | 22 ++- playwright.dev.config.ts | 4 +- playwright.storybook.config.ts | 21 ++- playwright.visual.config.ts | 19 ++- scripts/build-frontend.ts | 19 ++- scripts/generate-build-manifest.ts | 30 ++-- scripts/generate-contract-set.ts | 9 +- scripts/generate-runtime-config.ts | 7 +- scripts/generate-service-worker-assets.ts | 15 +- scripts/lib/run-isolation.ts | 88 ++++++++++ scripts/scale/measure-feature-shape-growth.ts | 1 + tests/e2e/preload-use.spec.ts | 4 +- tests/e2e/support/service-worker-builds.ts | 3 + tests/unit/playwright-run-isolation.test.ts | 158 ++++++++++++++++++ .../product-feature-selection-build.test.ts | 1 + tests/unit/run-isolation.test.ts | 85 ++++++++++ tests/unit/run-output-root-build.test.ts | 120 +++++++++++++ vite.config.ts | 22 +++ vite.service-worker.config.ts | 10 +- 20 files changed, 604 insertions(+), 49 deletions(-) create mode 100644 scripts/lib/run-isolation.ts create mode 100644 tests/unit/playwright-run-isolation.test.ts create mode 100644 tests/unit/run-isolation.test.ts create mode 100644 tests/unit/run-output-root-build.test.ts diff --git a/playwright.capabilities.config.ts b/playwright.capabilities.config.ts index 7756a806..4fded730 100644 --- a/playwright.capabilities.config.ts +++ b/playwright.capabilities.config.ts @@ -1,24 +1,31 @@ import { defineConfig, devices } from "@playwright/test"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; +import { + RUN_OUTPUT_ROOT_VARIABLE, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; +// 1105/12-04. Under TEST_RUN_ID the results and report are the run's own +// (.tmp/runs//), and so is the dev server's Vite cache. export default defineConfig({ testDir: "./tests/browser-capabilities", forbidOnly: true, - outputDir: "./artifacts/tests/browser-capabilities/results", + outputDir: testRunPath("artifacts/tests/browser-capabilities/results"), reporter: [ ["list"], [ "html", { - outputFolder: "./artifacts/tests/browser-capabilities/report", + outputFolder: testRunPath("artifacts/tests/browser-capabilities/report"), open: "never", }, ], [ "junit", { - outputFile: "./artifacts/tests/browser-capabilities/results.xml", + outputFile: testRunPath("artifacts/tests/browser-capabilities/results.xml"), }, ], ], @@ -32,7 +39,7 @@ export default defineConfig({ // BUILD_ID is local-build. CI sets VITE_BUILD_ID for the whole workflow; // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). - env: { ...LOCAL_BUILD_IDENTITY }, + env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: "corepack pnpm dev --host 127.0.0.1 --port 4174", url: "http://127.0.0.1:4174", diff --git a/playwright.config.ts b/playwright.config.ts index b6523844..6c17f4a7 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,13 +1,26 @@ import { defineConfig, devices } from "@playwright/test"; +import { + RUN_OUTPUT_ROOT_VARIABLE, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. With TEST_RUN_ID set, this run's results, report and the build +// its preview serves are under .tmp/runs// (scripts/lib/run-isolation.ts), +// so a second run in the same checkout neither empties this run's dist nor +// deletes its results. Unset, the paths are artifacts/tests/e2e/* and dist, +// which FE-GATE-008 and the CI upload read. +const dist = testRunPath("dist"); + export default defineConfig({ testDir: "./tests/e2e", forbidOnly: true, - outputDir: "./artifacts/tests/e2e/results", + outputDir: testRunPath("artifacts/tests/e2e/results"), reporter: [ ["list"], - ["html", { outputFolder: "./artifacts/tests/e2e/report", open: "never" }], - ["junit", { outputFile: "./artifacts/tests/e2e/results.xml" }], + ["html", { outputFolder: testRunPath("artifacts/tests/e2e/report"), open: "never" }], + ["junit", { outputFile: testRunPath("artifacts/tests/e2e/results.xml") }], ], use: { baseURL: "http://127.0.0.1:4173", @@ -15,8 +28,9 @@ export default defineConfig({ screenshot: "only-on-failure", }, webServer: { + env: { [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: - "corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4173", + `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4173 --outDir ${dist}`, url: "http://127.0.0.1:4173", reuseExistingServer: false, }, diff --git a/playwright.dev.config.ts b/playwright.dev.config.ts index ad112e92..bc63a678 100644 --- a/playwright.dev.config.ts +++ b/playwright.dev.config.ts @@ -2,6 +2,7 @@ import { defineConfig } from "@playwright/test"; import releaseConfig from "./playwright.config.ts"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; +import { RUN_OUTPUT_ROOT_VARIABLE, testRunRoot } from "./scripts/lib/run-isolation.ts"; export default defineConfig({ ...releaseConfig, @@ -14,7 +15,8 @@ export default defineConfig({ // BUILD_ID is local-build. CI sets VITE_BUILD_ID for the whole workflow; // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). - env: { ...LOCAL_BUILD_IDENTITY }, + // 1105/12-04. Under TEST_RUN_ID, Vite's cache is the run's own. + env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: "corepack pnpm dev --host 127.0.0.1", url: "http://127.0.0.1:5173", reuseExistingServer: true, diff --git a/playwright.storybook.config.ts b/playwright.storybook.config.ts index 75abbe7c..de5dbc76 100644 --- a/playwright.storybook.config.ts +++ b/playwright.storybook.config.ts @@ -1,21 +1,34 @@ import { defineConfig, devices } from "@playwright/test"; +import { testRunPath, testRunRoot } from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. Under TEST_RUN_ID the workshop is built into the run's own +// folder (.tmp/runs//artifacts/storybook/static) with build:storybook's +// two steps and another output folder, and the results and report are the +// run's own. Unset, the gate's build:storybook writes artifacts/storybook/static. +const staticDirectory = testRunPath("artifacts/storybook/static"); +const buildWorkshop = + testRunRoot() === "." + ? "corepack pnpm build:storybook" + : "node scripts/generate-product-feature-index.ts && " + + `corepack pnpm exec storybook build -o ${staticDirectory}`; + export default defineConfig({ testDir: "./tests/storybook", forbidOnly: true, - outputDir: "./artifacts/tests/storybook/results", + outputDir: testRunPath("artifacts/tests/storybook/results"), reporter: [ ["list"], [ "html", { - outputFolder: "./artifacts/tests/storybook/report", + outputFolder: testRunPath("artifacts/tests/storybook/report"), open: "never", }, ], [ "junit", - { outputFile: "./artifacts/tests/storybook/results.xml" }, + { outputFile: testRunPath("artifacts/tests/storybook/results.xml") }, ], ], use: { @@ -25,7 +38,7 @@ export default defineConfig({ }, webServer: { command: - "corepack pnpm build:storybook && node scripts/serve-static.ts artifacts/storybook/static 6006", + `${buildWorkshop} && node scripts/serve-static.ts ${staticDirectory} 6006`, url: "http://127.0.0.1:6006", reuseExistingServer: false, }, diff --git a/playwright.visual.config.ts b/playwright.visual.config.ts index 89352b93..e7ff7d45 100644 --- a/playwright.visual.config.ts +++ b/playwright.visual.config.ts @@ -3,19 +3,28 @@ import { fileURLToPath } from "node:url"; import { defineConfig, devices } from "@playwright/test"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; +import { + RUN_OUTPUT_ROOT_VARIABLE, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. Under TEST_RUN_ID the results, report and build are the run's +// own (.tmp/runs//); the goldens in snapshotDir are shared and only read. +const dist = testRunPath("dist"); export default defineConfig({ testDir: "./tests/visual", forbidOnly: true, snapshotDir: "./tests/visual/__snapshots__", - outputDir: "./artifacts/tests/visual/results", + outputDir: testRunPath("artifacts/tests/visual/results"), reporter: [ ["list"], [ "html", - { outputFolder: "./artifacts/tests/visual/report", open: "never" }, + { outputFolder: testRunPath("artifacts/tests/visual/report"), open: "never" }, ], - ["junit", { outputFile: "./artifacts/tests/visual/results.xml" }], + ["junit", { outputFile: testRunPath("artifacts/tests/visual/results.xml") }], ], expect: { toHaveScreenshot: { @@ -54,9 +63,9 @@ export default defineConfig({ // CI run failed here. The goldens are the template's shape, captured with // the local identity. // 1103/11-08: CI too, or the build is checked as a release build. - env: { ...LOCAL_BUILD_IDENTITY }, + env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: - "corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4174", + `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4174 --outDir ${dist}`, url: "http://127.0.0.1:4174", reuseExistingServer: false, }, diff --git a/scripts/build-frontend.ts b/scripts/build-frontend.ts index 61f759f1..67714849 100644 --- a/scripts/build-frontend.ts +++ b/scripts/build-frontend.ts @@ -5,6 +5,7 @@ import process from "node:process"; import { SERVICE_WORKER_SCRIPT_PATH } from "../src/contracts/service-worker.ts"; import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts"; import { makeDistFilesPublic } from "./lib/dist-permissions.ts"; +import { runOutputPath } from "./lib/run-isolation.ts"; import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-service-worker.ts"; /** @@ -36,10 +37,18 @@ import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-serv * is not what keeps the profile out of the worker cache (13-15). Step 6 reads * the release manifest URL from the `dist/config.json` step 4 wrote, and fails * if its `APP_ENV` is not `APP_PROFILE` (06-10). + * + * 1105/12-04. With `RUN_OUTPUT_ROOT` set (a Playwright config sets it for its + * web server under `TEST_RUN_ID`), every path above — `dist`, + * `.generated/frontend-runtime` and the build's `artifacts/` evidence — is + * under that folder instead, so a second build in the same checkout does not + * empty the first one's `dist`. Every step reads the same variable + * (`scripts/lib/run-isolation.ts`). Unset, the paths are the ones above. */ const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker; const buildsActiveWorker = selection?.mode === "ACTIVE"; +const dist = runOutputPath("dist"); function run(command: string, args: readonly string[]): void { const result = spawnSync(command, [...args], { @@ -53,8 +62,8 @@ function run(command: string, args: readonly string[]): void { } // 1. clean -await rm("dist", { recursive: true, force: true }); -await rm(".generated/frontend-runtime", { recursive: true, force: true }); +await rm(dist, { recursive: true, force: true }); +await rm(runOutputPath(".generated/frontend-runtime"), { recursive: true, force: true }); // 2. contract set + build info run("node", ["scripts/generate-contract-set.ts"]); @@ -67,13 +76,13 @@ run("node", ["scripts/generate-runtime-config.ts"]); if (buildsActiveWorker) { // 5. hashed asset inventory - run("node", ["scripts/generate-service-worker-assets.ts", "dist"]); + run("node", ["scripts/generate-service-worker-assets.ts", dist]); // 6. service worker build run("npx", ["vite", "build", "--config", "vite.service-worker.config.ts"]); } else if (selection?.mode === "REMOVE_REGISTRATION") { // 6. self-unregistering worker in place of the active one await writeFile( - `dist/${SERVICE_WORKER_SCRIPT_PATH}`, + `${dist}/${SERVICE_WORKER_SCRIPT_PATH}`, SELF_UNREGISTERING_SERVICE_WORKER, ); } else { @@ -86,4 +95,4 @@ if (buildsActiveWorker) { run("node", ["scripts/generate-build-manifest.ts"]); // 8. publishable permissions: the validated writer creates 0600 files -await makeDistFilesPublic("dist"); +await makeDistFilesPublic(dist); diff --git a/scripts/generate-build-manifest.ts b/scripts/generate-build-manifest.ts index af7a7b9f..2d47f7d7 100644 --- a/scripts/generate-build-manifest.ts +++ b/scripts/generate-build-manifest.ts @@ -21,8 +21,14 @@ import { } from "./lib/build-environment.ts"; import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts"; import { CANONICAL_VITE_MANIFEST_PATH } from "./lib/build-manifest-outputs.ts"; +import { runOutputPath } from "./lib/run-isolation.ts"; assertCiBuildEnvironment(process.env); +// 1105/12-04. With RUN_OUTPUT_ROOT set, this build's dist and artifacts are +// under that folder. The manifest still records `dist`, `artifacts/...`: +// its paths are relative to the folder the build wrote, which is the +// checkout when the variable is unset. +const at = (relativePath: string) => runOutputPath(relativePath); type ViteManifestEntry = Readonly<{ file: string; name?: string; @@ -39,10 +45,10 @@ const releaseId = process.env.RELEASE_ID ?? "local-release"; const runnerImage = process.env.CI_RUNNER_IMAGE ?? `${process.platform}-${process.arch}`; const buildTime = buildDate(process.env); const builtAt = buildTime.toISOString(); -const viteManifest = await readFile(CANONICAL_VITE_MANIFEST_PATH, "utf8"); +const viteManifest = await readFile(at(CANONICAL_VITE_MANIFEST_PATH), "utf8"); const viteManifestObject = parseViteManifest(JSON.parse(viteManifest)); const moduleInventory = await readFile( - "dist/.vite/module-inventory.json", + at("dist/.vite/module-inventory.json"), "utf8", ); const moduleInventoryDocument = moduleInventoryArtifactSchema.parse( @@ -56,7 +62,7 @@ const moduleInventoryHash = createHash("sha256") .update(moduleInventoryBytes) .digest("hex"); const runtimeConfig = runtimeConfigV2ArtifactSchema.parse( - JSON.parse(await readFile("dist/config.json", "utf8")), + JSON.parse(await readFile(at("dist/config.json"), "utf8")), ); const routeChunks: Record = {}; const runtimeContracts: Readonly> = @@ -113,36 +119,36 @@ const releaseManifest = releaseManifestV2ArtifactSchema.parse({ contractSet: buildContractSet(), }); -await mkdir("artifacts/release", { recursive: true }); -await mkdir("artifacts/quality", { recursive: true }); +await mkdir(at("artifacts/release"), { recursive: true }); +await mkdir(at("artifacts/quality"), { recursive: true }); await writeValidatedJsonArtifact({ - path: "artifacts/quality/vite-module-inventory.json", + path: at("artifacts/quality/vite-module-inventory.json"), schema: moduleInventoryArtifactSchema, value: moduleInventoryDocument, }); -await rm("dist/.vite/module-inventory.json"); +await rm(at("dist/.vite/module-inventory.json")); await writeValidatedJsonArtifact({ - path: "dist/config.json", + path: at("dist/config.json"), schema: runtimeConfigV2ArtifactSchema, value: runtimeConfig, }); await writeValidatedJsonArtifact({ - path: "dist/release-manifest.json", + path: at("dist/release-manifest.json"), schema: releaseManifestV2ArtifactSchema, value: releaseManifest, }); await writeValidatedJsonArtifact({ - path: "dist/runtime-config.schema.json", + path: at("dist/runtime-config.schema.json"), schema: jsonSchemaDocumentArtifactSchema, value: runtimeConfigJsonSchema, }); await writeValidatedJsonArtifact({ - path: "artifacts/release/runtime-config.schema.json", + path: at("artifacts/release/runtime-config.schema.json"), schema: jsonSchemaDocumentArtifactSchema, value: runtimeConfigJsonSchema, }); await writeValidatedJsonArtifact({ - path: "artifacts/release/build-manifest.json", + path: at("artifacts/release/build-manifest.json"), schema: buildManifestArtifactSchema, value: manifest, }); diff --git a/scripts/generate-contract-set.ts b/scripts/generate-contract-set.ts index 8aa37f60..53222d2c 100644 --- a/scripts/generate-contract-set.ts +++ b/scripts/generate-contract-set.ts @@ -8,6 +8,7 @@ import { type ContractSetPackage, } from "../src/contracts/contract-set-canonical.ts"; import { EXPECTED_CONTRACT_SET_PACKAGES } from "../src/features/installed-contract-contributions.ts"; +import { moduleSpecifier, runOutputPath } from "./lib/run-isolation.ts"; /** * §5.4 / §17.2.2 step 2. Derives the build-time expected contract set from the @@ -16,6 +17,7 @@ import { EXPECTED_CONTRACT_SET_PACKAGES } from "../src/features/installed-contra * cannot influence the release digest. */ +/** Under `RUN_OUTPUT_ROOT` when a run sets one (1105/12-04). */ const OUTPUT = ".generated/frontend-runtime/contract-set.ts"; function sha256(bytes: Uint8Array): Uint8Array { @@ -38,11 +40,12 @@ export function buildContractSet( async function main(): Promise { const contractSet = buildContractSet(); + const output = runOutputPath(OUTPUT); const source = [ "// Generated by scripts/generate-contract-set.ts. Do not edit.", "// Regenerated from scratch on every build; never committed.", "", - "import type { ContractSet } from \"../../src/contracts/contract-set.ts\";", + `import type { ContractSet } from "${moduleSpecifier(output, "src/contracts/contract-set.ts")}";`, "", `export const BUILD_CONTRACT_SET: ContractSet = ${JSON.stringify( contractSet, @@ -52,8 +55,8 @@ async function main(): Promise { "", ].join("\n"); - await mkdir(dirname(OUTPUT), { recursive: true }); - await writeFile(OUTPUT, source, "utf8"); + await mkdir(dirname(output), { recursive: true }); + await writeFile(output, source, "utf8"); process.stdout.write( `contract set: ${contractSet.packages.length} package(s) ${contractSet.setDigest}\n`, ); diff --git a/scripts/generate-runtime-config.ts b/scripts/generate-runtime-config.ts index 5de3e330..96cd9e3e 100644 --- a/scripts/generate-runtime-config.ts +++ b/scripts/generate-runtime-config.ts @@ -9,6 +9,7 @@ import { type DeploymentTarget, } from "../src/contracts/deployment-admission.ts"; import { runtimeConfigV2ArtifactSchema } from "../src/contracts/release-artifacts.ts"; +import { runOutputPath } from "./lib/run-isolation.ts"; /** * §6.4. Materializes `dist/config.json` from the profile the build declares. @@ -24,6 +25,7 @@ import { runtimeConfigV2ArtifactSchema } from "../src/contracts/release-artifact */ const PROFILE_DIRECTORY = "config/runtime"; +/** Under `RUN_OUTPUT_ROOT` when a run sets one (1105/12-04). */ const OUTPUT_PATH = "dist/config.json"; /** @@ -98,9 +100,10 @@ function resolveTarget(environment: NodeJS.ProcessEnv): DeploymentTarget { async function main(): Promise { const target = resolveTarget(process.env); const config = await generateRuntimeConfig(target); - await writeFile(OUTPUT_PATH, `${JSON.stringify(config, null, 2)}\n`, "utf8"); + const output = runOutputPath(OUTPUT_PATH); + await writeFile(output, `${JSON.stringify(config, null, 2)}\n`, "utf8"); process.stdout.write( - `runtime config: ${target} profile written to ${OUTPUT_PATH} ` + + `runtime config: ${target} profile written to ${output} ` + `(APP_ENV=${String(config["APP_ENV"])}, AUTH_MODE=${String(config["AUTH_MODE"])})\n`, ); } diff --git a/scripts/generate-service-worker-assets.ts b/scripts/generate-service-worker-assets.ts index 282a3ec0..dbcca3a3 100644 --- a/scripts/generate-service-worker-assets.ts +++ b/scripts/generate-service-worker-assets.ts @@ -16,6 +16,7 @@ import { SERVICE_WORKER_SCRIPT_PATH, type StaticAssetManifestV1, } from "../src/contracts/service-worker.ts"; +import { moduleSpecifier, runOutputPath } from "./lib/run-isolation.ts"; /** * §17.2.2 step 4. Scans the completed app `dist` and emits the exact hashed @@ -25,7 +26,10 @@ import { * the runtime config and release manifest, which are network-only (§18.4). */ -/** Read back by tests/e2e/support/service-worker-builds.ts (0929/11-03). */ +/** + * Read back by tests/e2e/support/service-worker-builds.ts (0929/11-03). A + * build with `RUN_OUTPUT_ROOT` writes it under that folder (1105/12-04). + */ export const SERVICE_WORKER_ASSETS_OUTPUT = ".generated/frontend-runtime/service-worker-assets.ts"; @@ -134,7 +138,8 @@ async function walk(root: string, current: string): Promise { } async function main(): Promise { - const distDirectory = process.argv[2] ?? "dist"; + const distDirectory = process.argv[2] ?? runOutputPath("dist"); + const output = runOutputPath(SERVICE_WORKER_ASSETS_OUTPUT); const buildId = process.env.VITE_BUILD_ID ?? "local-build"; const releaseId = process.env.RELEASE_ID ?? "local-release"; const manifest = await collectStaticAssets( @@ -146,7 +151,7 @@ async function main(): Promise { const source = [ "// Generated by scripts/generate-service-worker-assets.ts. Do not edit.", "", - 'import type { StaticAssetManifestV1 } from "../../src/contracts/service-worker.ts";', + `import type { StaticAssetManifestV1 } from "${moduleSpecifier(output, "src/contracts/service-worker.ts")}";`, "", `export const SERVICE_WORKER_ASSETS: StaticAssetManifestV1 = ${JSON.stringify( manifest, @@ -155,8 +160,8 @@ async function main(): Promise { )} as const;`, "", ].join("\n"); - await mkdir(path.dirname(SERVICE_WORKER_ASSETS_OUTPUT), { recursive: true }); - await writeFile(SERVICE_WORKER_ASSETS_OUTPUT, source, "utf8"); + await mkdir(path.dirname(output), { recursive: true }); + await writeFile(output, source, "utf8"); process.stdout.write( `service worker assets: ${manifest.assets.length} file(s) ${manifest.setDigest}\n`, ); diff --git a/scripts/lib/run-isolation.ts b/scripts/lib/run-isolation.ts new file mode 100644 index 00000000..37068f7f --- /dev/null +++ b/scripts/lib/run-isolation.ts @@ -0,0 +1,88 @@ +import path from "node:path"; + +/** + * 1105/12-04. Where one run writes, so that two runs in one checkout do not + * overwrite each other's build, results and reports. + * + * Two variables, read by different code: + * + * - `TEST_RUN_ID` names a Playwright run. The Playwright configs put its + * results, HTML report, JUnit file and the build its web server serves under + * `.tmp/runs//`, at the paths they have in the checkout: + * `artifacts/tests/e2e/results` becomes + * `.tmp/runs//artifacts/tests/e2e/results`, `dist` becomes + * `.tmp/runs//dist`. Unset, a run writes where it always did, which is + * where the gates, the CI upload and the evidence checks read. + * - `RUN_OUTPUT_ROOT` is what a Playwright config hands its web server. The + * build (`scripts/build-frontend.ts` and every step it runs) writes `dist/`, + * `.generated/frontend-runtime/` and its `artifacts/` evidence under it, and + * Vite keeps its cache there. Build code reads only this variable, never + * `TEST_RUN_ID`: a test that builds a copy of the repository inherits the + * runner's environment and must still build into the copy's own `dist`. + * + * Both roots are `.` or a folder under `.tmp/`. Git ignores `.tmp/`, and so do + * Tailwind's source scan (a Playwright report there would add classes to the + * CSS), Vite's file watcher, Vitest and the e2e helpers that copy the + * repository. + */ +export const TEST_RUN_ID_VARIABLE = "TEST_RUN_ID"; +export const RUN_OUTPUT_ROOT_VARIABLE = "RUN_OUTPUT_ROOT"; +export const TEST_RUN_DIRECTORY = ".tmp/runs"; + +type Environment = Readonly>; + +const RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/u; + +/** `.` without a run ID, `.tmp/runs/` with one. */ +export function testRunRoot(environment: Environment = process.env): string { + const runId = environment[TEST_RUN_ID_VARIABLE]; + if (runId === undefined || runId === "") return "."; + if (!RUN_ID.test(runId)) { + throw new TypeError( + `${TEST_RUN_ID_VARIABLE} must be 1 to 64 letters, digits, ".", "_" or "-", ` + + `starting with a letter or digit; received ${JSON.stringify(runId)}`, + ); + } + return `${TEST_RUN_DIRECTORY}/${runId}`; +} + +/** `relativePath` as this run writes it: unchanged without a run ID. */ +export function testRunPath( + relativePath: string, + environment: Environment = process.env, +): string { + return path.posix.join(testRunRoot(environment), relativePath); +} + +/** `.` when unset; otherwise a normalized folder under `.tmp/`. */ +export function runOutputRoot(environment: Environment = process.env): string { + const declared = environment[RUN_OUTPUT_ROOT_VARIABLE]; + if (declared === undefined || declared === "") return "."; + const root = path.posix.normalize(declared).replace(/\/+$/u, ""); + if (root === ".") return "."; + if (declared.includes("\\") || !root.startsWith(".tmp/") || root.split("/").includes("..")) { + throw new TypeError( + `${RUN_OUTPUT_ROOT_VARIABLE} must be "." or a folder under .tmp/ in this checkout; ` + + `received ${JSON.stringify(declared)}`, + ); + } + return root; +} + +/** `relativePath` as the build writes it: unchanged without an output root. */ +export function runOutputPath( + relativePath: string, + environment: Environment = process.env, +): string { + return path.posix.join(runOutputRoot(environment), relativePath); +} + +/** + * The import specifier a generated module at `fromFile` uses for `toFile`, + * both relative to the checkout. A generated module under an output root sits + * deeper than `.generated/` and needs more `../`. + */ +export function moduleSpecifier(fromFile: string, toFile: string): string { + const specifier = path.posix.relative(path.posix.dirname(fromFile), toFile); + return specifier.startsWith(".") ? specifier : `./${specifier}`; +} diff --git a/scripts/scale/measure-feature-shape-growth.ts b/scripts/scale/measure-feature-shape-growth.ts index 94b6e6b5..ff162959 100644 --- a/scripts/scale/measure-feature-shape-growth.ts +++ b/scripts/scale/measure-feature-shape-growth.ts @@ -45,6 +45,7 @@ const BUILD_INPUTS = [ "vite.config.ts", "tsconfig.base.json", "tsconfig.app.json", + "scripts/lib/run-isolation.ts", "scripts/lib/vite-module-inventory.ts", ] as const; // Platform-owned names that only happen to contain "reference". diff --git a/tests/e2e/preload-use.spec.ts b/tests/e2e/preload-use.spec.ts index a891cfa0..5e0f38ed 100644 --- a/tests/e2e/preload-use.spec.ts +++ b/tests/e2e/preload-use.spec.ts @@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises"; import { expect, test } from "@playwright/test"; +import { testRunPath } from "../../scripts/lib/run-isolation.ts"; import { trackPreloadUse } from "../support/browser/preload-use.ts"; import { test as strictTest } from "../support/browser/strict-browser-test.ts"; @@ -21,8 +22,9 @@ type ManifestEntry = Readonly<{ isEntry?: boolean; }>; +/** The build the preview serves: this run's own under TEST_RUN_ID (1105/12-04). */ async function buildManifest(): Promise>> { - return JSON.parse(await readFile("dist/.vite/manifest.json", "utf8")) as Record< + return JSON.parse(await readFile(testRunPath("dist/.vite/manifest.json"), "utf8")) as Record< string, ManifestEntry >; diff --git a/tests/e2e/support/service-worker-builds.ts b/tests/e2e/support/service-worker-builds.ts index d375b66a..df6b39a8 100644 --- a/tests/e2e/support/service-worker-builds.ts +++ b/tests/e2e/support/service-worker-builds.ts @@ -55,11 +55,14 @@ import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worke */ const repoRoot = path.resolve(import.meta.dirname, "../../.."); +// 1105/12-04. `.tmp` holds other runs' builds and results (.tmp/runs/), +// which change while this copy is made; fixture-builds.ts skips it too. const skipped = new Set([ "node_modules", ".git", ".claude", ".generated", + ".tmp", "dist", "artifacts", "graphify-out", diff --git a/tests/unit/playwright-run-isolation.test.ts b/tests/unit/playwright-run-isolation.test.ts new file mode 100644 index 00000000..22abc97e --- /dev/null +++ b/tests/unit/playwright-run-isolation.test.ts @@ -0,0 +1,158 @@ +import { existsSync, readFileSync } from "node:fs"; +import path from "node:path"; + +import type { PlaywrightTestConfig } from "@playwright/test"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +/** + * 1105/12-04. Every Playwright config wrote its results, report and JUnit + * file, and the e2e and visual builds wrote `dist`, at fixed paths. A second + * run in one checkout deleted the first one's results (Playwright empties + * `outputDir` at start) and emptied the `dist` its preview was serving. Under + * TEST_RUN_ID each run gets its own paths; without it the paths stay where + * FE-GATE-008 and the CI upload read them. + * + * Each load evaluates the config again with the environment of one run. + * + * test:browser-file-storage-removal deletes the capabilities config with the + * capability it runs, so a project without it checks the other four. Its path + * is a variable so that typecheck does not need the file. + */ +const CAPABILITIES_CONFIG = "playwright.capabilities.config.ts"; +const HAS_CAPABILITIES = existsSync(CAPABILITIES_CONFIG); +const LOADERS = { + e2e: () => import("../../playwright.config.ts"), + visual: () => import("../../playwright.visual.config.ts"), + capabilities: () => + import(/* @vite-ignore */ `../../${CAPABILITIES_CONFIG}`) as Promise<{ + default: PlaywrightTestConfig; + }>, + storybook: () => import("../../playwright.storybook.config.ts"), + dev: () => import("../../playwright.dev.config.ts"), +} as const; +type ConfigName = keyof typeof LOADERS; +/** The names among `names` whose config file this checkout has. */ +function installed(names: readonly T[]): T[] { + return names.filter((name) => name !== "capabilities" || HAS_CAPABILITIES); +} +const RUN_VARIABLES = ["TEST_RUN_ID", "RUN_OUTPUT_ROOT"] as const; + +afterEach(() => { + vi.resetModules(); +}); + +async function load( + name: ConfigName, + environment: Readonly> = {}, +): Promise { + const saved = RUN_VARIABLES.map((variable) => [variable, process.env[variable]] as const); + for (const variable of RUN_VARIABLES) delete process.env[variable]; + Object.assign(process.env, environment); + vi.resetModules(); + try { + return (await LOADERS[name]()).default; + } finally { + for (const [variable, value] of saved) { + if (value === undefined) delete process.env[variable]; + else process.env[variable] = value; + } + } +} + +function reporterOption(config: PlaywrightTestConfig, reporter: string, option: string): string { + const reporters: readonly unknown[] = Array.isArray(config.reporter) ? config.reporter : []; + const entry = reporters.find( + (candidate) => Array.isArray(candidate) && candidate[0] === reporter, + ) as readonly [string, Record] | undefined; + return String(entry?.[1][option]); +} + +function outputs(config: PlaywrightTestConfig) { + return { + results: path.resolve(String(config.outputDir)), + report: path.resolve(reporterOption(config, "html", "outputFolder")), + junit: path.resolve(reporterOption(config, "junit", "outputFile")), + }; +} + +function webServer(config: PlaywrightTestConfig) { + const server = config.webServer; + if (!server || Array.isArray(server)) throw new TypeError("expected one web server"); + return server; +} + +const RESULT_FOLDERS: Readonly, string>> = { + e2e: "e2e", + visual: "visual", + capabilities: "browser-capabilities", + storybook: "storybook", +}; + +const INSTALLED_RESULT_FOLDERS = installed( + Object.keys(RESULT_FOLDERS) as Exclude[], +).map((name) => [name, RESULT_FOLDERS[name]] as const); + +describe.each(INSTALLED_RESULT_FOLDERS)("the %s config", (name, folder) => { + const expected = (root: string) => ({ + results: path.resolve(root, `artifacts/tests/${folder}/results`), + report: path.resolve(root, `artifacts/tests/${folder}/report`), + junit: path.resolve(root, `artifacts/tests/${folder}/results.xml`), + }); + + it("writes where the gates read without a run ID", async () => { + expect(outputs(await load(name as ConfigName))).toEqual(expected(".")); + }); + + it("gives each run ID its own results, report and JUnit file", async () => { + const a = outputs(await load(name as ConfigName, { TEST_RUN_ID: "a" })); + const b = outputs(await load(name as ConfigName, { TEST_RUN_ID: "b" })); + + expect(a).toEqual(expected(".tmp/runs/a")); + expect(b).toEqual(expected(".tmp/runs/b")); + }); +}); + +describe("the build a run serves", () => { + it.each(["e2e", "visual"] as const)( + "is the checkout's dist without a run ID, and the run's own with one (%s)", + async (name) => { + const checkout = webServer(await load(name)); + expect(checkout.env?.["RUN_OUTPUT_ROOT"]).toBe("."); + expect(checkout.command).toMatch(/ --outDir dist(?: |$)/u); + + const run = webServer(await load(name, { TEST_RUN_ID: "a" })); + // The build writes under RUN_OUTPUT_ROOT and the preview serves it. + expect(run.env?.["RUN_OUTPUT_ROOT"]).toBe(".tmp/runs/a"); + expect(run.command).toMatch(/^corepack pnpm build && corepack pnpm preview /u); + expect(run.command).toMatch(/ --outDir \.tmp\/runs\/a\/dist(?: |$)/u); + }, + ); + + it.each(installed(["capabilities", "dev"] as const))( + "keeps the dev server's Vite cache in the run's folder (%s)", + async (name) => { + expect(webServer(await load(name)).env?.["RUN_OUTPUT_ROOT"]).toBe("."); + expect(webServer(await load(name, { TEST_RUN_ID: "a" })).env?.["RUN_OUTPUT_ROOT"]).toBe( + ".tmp/runs/a", + ); + }, + ); + + it("builds the workshop into the run's folder the way build:storybook does", async () => { + const checkout = webServer(await load("storybook")).command; + expect(checkout).toMatch( + /^corepack pnpm build:storybook && node scripts\/serve-static\.ts artifacts\/storybook\/static /u, + ); + + const run = webServer(await load("storybook", { TEST_RUN_ID: "a" })).command; + const script = ( + JSON.parse(readFileSync("package.json", "utf8")) as { scripts: Record } + ).scripts["build:storybook"]!; + const sameStepsOtherFolder = script + .replace("&& storybook build", "&& corepack pnpm exec storybook build") + .replace("-o artifacts/storybook/static", "-o .tmp/runs/a/artifacts/storybook/static"); + expect(sameStepsOtherFolder).not.toBe(script); + expect(run.startsWith(`${sameStepsOtherFolder} && `)).toBe(true); + expect(run).toMatch(/ node scripts\/serve-static\.ts \.tmp\/runs\/a\/artifacts\/storybook\/static /u); + }); +}); diff --git a/tests/unit/product-feature-selection-build.test.ts b/tests/unit/product-feature-selection-build.test.ts index 2a8242a3..0828abc8 100644 --- a/tests/unit/product-feature-selection-build.test.ts +++ b/tests/unit/product-feature-selection-build.test.ts @@ -27,6 +27,7 @@ const BUILD_INPUTS = [ "vite.config.ts", "tsconfig.base.json", "tsconfig.app.json", + "scripts/lib/run-isolation.ts", "scripts/lib/vite-module-inventory.ts", ] as const; // Feature ids stay: the generated index still declares them so the platform diff --git a/tests/unit/run-isolation.test.ts b/tests/unit/run-isolation.test.ts new file mode 100644 index 00000000..4bd739f3 --- /dev/null +++ b/tests/unit/run-isolation.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from "vitest"; + +import { + moduleSpecifier, + runOutputPath, + runOutputRoot, + testRunPath, + testRunRoot, +} from "../../scripts/lib/run-isolation.ts"; + +/** + * 1105/12-04. Builds and Playwright runs wrote to fixed paths (`dist`, + * `artifacts/tests/*`), so a second run in one checkout emptied the first + * one's dist and deleted its results. These are the path rules every config + * and build step uses. + */ +describe("test run paths (TEST_RUN_ID)", () => { + it("are the checkout's own paths without a run ID", () => { + for (const environment of [{}, { TEST_RUN_ID: "" }]) { + expect(testRunRoot(environment)).toBe("."); + expect(testRunPath("artifacts/tests/e2e/results", environment)).toBe( + "artifacts/tests/e2e/results", + ); + expect(testRunPath("dist", environment)).toBe("dist"); + } + }); + + it("give each run ID its own folder under .tmp/runs", () => { + const a = { TEST_RUN_ID: "gate-008.a" }; + const b = { TEST_RUN_ID: "gate-008.b" }; + expect(testRunRoot(a)).toBe(".tmp/runs/gate-008.a"); + expect(testRunPath("artifacts/tests/e2e/results", a)).toBe( + ".tmp/runs/gate-008.a/artifacts/tests/e2e/results", + ); + expect(testRunPath("dist", b)).toBe(".tmp/runs/gate-008.b/dist"); + expect(testRunPath("dist", a)).not.toBe(testRunPath("dist", b)); + }); + + it("refuse a run ID that is not one folder name", () => { + for (const runId of ["../dist", "a/b", ".hidden", "-a", "a b", "x".repeat(65), "a\\b"]) { + expect(() => testRunRoot({ TEST_RUN_ID: runId }), runId).toThrow(/TEST_RUN_ID/u); + } + }); +}); + +describe("build output root (RUN_OUTPUT_ROOT)", () => { + it("is the checkout without a root", () => { + for (const environment of [{}, { RUN_OUTPUT_ROOT: "" }, { RUN_OUTPUT_ROOT: "." }]) { + expect(runOutputRoot(environment)).toBe("."); + expect(runOutputPath("dist", environment)).toBe("dist"); + expect(runOutputPath(".generated/frontend-runtime", environment)).toBe( + ".generated/frontend-runtime", + ); + } + }); + + it("puts every build output under a root in .tmp", () => { + const environment = { RUN_OUTPUT_ROOT: "./.tmp/runs/a/" }; + expect(runOutputRoot(environment)).toBe(".tmp/runs/a"); + expect(runOutputPath("dist/config.json", environment)).toBe(".tmp/runs/a/dist/config.json"); + expect(runOutputPath("artifacts/release/build-manifest.json", environment)).toBe( + ".tmp/runs/a/artifacts/release/build-manifest.json", + ); + }); + + it("refuses a root outside .tmp, which git, Tailwind and the watchers do not ignore", () => { + for (const root of ["dist", "out/a", "/tmp/a", ".tmp", ".tmp/../dist", "../other", ".tmp\\a"]) { + expect(() => runOutputRoot({ RUN_OUTPUT_ROOT: root }), root).toThrow(/RUN_OUTPUT_ROOT/u); + } + }); +}); + +describe("generated module specifiers", () => { + it("keep today's specifier in .generated and reach src from a deeper root", () => { + expect( + moduleSpecifier(".generated/frontend-runtime/contract-set.ts", "src/contracts/contract-set.ts"), + ).toBe("../../src/contracts/contract-set.ts"); + expect( + moduleSpecifier( + ".tmp/runs/a/.generated/frontend-runtime/contract-set.ts", + "src/contracts/contract-set.ts", + ), + ).toBe("../../../../../src/contracts/contract-set.ts"); + }); +}); diff --git a/tests/unit/run-output-root-build.test.ts b/tests/unit/run-output-root-build.test.ts new file mode 100644 index 00000000..0104f7b3 --- /dev/null +++ b/tests/unit/run-output-root-build.test.ts @@ -0,0 +1,120 @@ +import { spawnSync, type SpawnSyncReturns } from "node:child_process"; +import { access, cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +import { localBuildEnvironment } from "../../scripts/lib/build-environment.ts"; +import { verifyBuildManifestOutputs } from "../../scripts/lib/build-manifest-outputs.ts"; +import { linkFixtureNodeModules } from "../../scripts/lib/fixture-node-modules.ts"; +import { buildManifestArtifactSchema } from "../../src/contracts/release-artifacts.ts"; +import { turnOnServiceWorker } from "../e2e/support/service-worker-builds.ts"; + +/** + * 1105/12-04. Every build wrote `dist`, `.generated/frontend-runtime` and its + * `artifacts/` evidence at fixed paths, so a second build in one checkout + * emptied `dist` while the first run's preview still served it. A build given + * RUN_OUTPUT_ROOT (what a Playwright config sets under TEST_RUN_ID) writes all + * of them under that folder and leaves the checkout's own alone. + * + * One real build of a temp copy with the worker on, so every step that reads + * or writes those paths runs: the app build, the profile config, the worker + * asset list, the worker build and the manifests. + */ +const ROOT = ".tmp/runs/build-probe"; +const repoRoot = process.cwd(); +const skipped = new Set([ + "node_modules", + ".git", + ".claude", + ".generated", + ".tmp", + "dist", + "artifacts", + "graphify-out", +]); +let copy: string | undefined; +let failed: SpawnSyncReturns | undefined; + +beforeAll(async () => { + copy = await mkdtemp(path.join(tmpdir(), "run-output-root-")); + await cp(repoRoot, copy, { + recursive: true, + filter: (source) => !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), + }); + await linkFixtureNodeModules(copy, repoRoot); + await turnOnServiceWorker(copy); + // What another run in the same checkout is serving and reading. + await mkdir(path.join(copy, "dist"), { recursive: true }); + await writeFile(path.join(copy, "dist/served-by-another-run.txt"), "keep\n"); + await mkdir(path.join(copy, ".generated/frontend-runtime"), { recursive: true }); + await writeFile(path.join(copy, ".generated/frontend-runtime/keep.txt"), "keep\n"); + + const env = { + ...localBuildEnvironment(), + RUN_OUTPUT_ROOT: ROOT, + VITE_BUILD_ID: "run-output-probe", + RELEASE_ID: "run-output-probe", + }; + for (const script of ["scripts/generate-product-feature-index.ts", "scripts/build-frontend.ts"]) { + const run = spawnSync(process.execPath, [script], { cwd: copy, encoding: "utf8", env }); + if (run.status !== 0) { + failed = run; + break; + } + } +}, 180_000); + +afterAll(async () => { + if (copy) await rm(copy, { recursive: true, force: true }); +}); + +describe("a build with RUN_OUTPUT_ROOT (1105/12-04)", () => { + it("builds", () => { + expect(failed, `${failed?.stdout ?? ""}\n${failed?.stderr ?? ""}`).toBeUndefined(); + }); + + it("leaves the checkout's dist, generated runtime and evidence alone", async () => { + expect(await readFile(path.join(copy!, "dist/served-by-another-run.txt"), "utf8")).toBe("keep\n"); + expect( + await readFile(path.join(copy!, ".generated/frontend-runtime/keep.txt"), "utf8"), + ).toBe("keep\n"); + await expect(access(path.join(copy!, "dist/index.html"))).rejects.toThrow(); + await expect(access(path.join(copy!, "artifacts/release/build-manifest.json"))).rejects.toThrow(); + }); + + it("writes dist, the generated runtime and the evidence under the root", async () => { + const root = path.join(copy!, ROOT); + for (const file of [ + "dist/index.html", + "dist/config.json", + "dist/release-manifest.json", + "dist/runtime-config.schema.json", + "dist/service-worker.js", + "dist/.vite/manifest.json", + ".generated/frontend-runtime/contract-set.ts", + ".generated/frontend-runtime/service-worker-assets.ts", + "artifacts/release/build-manifest.json", + "artifacts/quality/vite-module-inventory.json", + ]) { + await access(path.join(root, file)); + } + // Its paths are relative to the folder the build wrote. + const manifest = buildManifestArtifactSchema.parse( + JSON.parse(await readFile(path.join(root, "artifacts/release/build-manifest.json"), "utf8")), + ); + expect(await verifyBuildManifestOutputs(manifest, { repositoryRoot: root })).toEqual([]); + }); + + it("points the generated modules' imports at the checkout's src", async () => { + for (const file of ["contract-set.ts", "service-worker-assets.ts"]) { + const generated = path.join(copy!, ROOT, ".generated/frontend-runtime", file); + const specifier = /^import type .+ from "([^"]+)";$/mu.exec( + await readFile(generated, "utf8"), + )?.[1]; + expect(specifier, file).toMatch(/^(?:\.\.\/)+src\/contracts\//u); + await access(path.resolve(path.dirname(generated), specifier!)); + } + }); +}); diff --git a/vite.config.ts b/vite.config.ts index 06854dbb..f0fdafae 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,6 +6,7 @@ import { defineConfig, loadEnv } from "vite"; import react from "@vitejs/plugin-react"; import tailwindcss from "@tailwindcss/vite"; +import { runOutputRoot } from "./scripts/lib/run-isolation.ts"; import { viteModuleInventoryPlugin } from "./scripts/lib/vite-module-inventory.ts"; import { ENV_REGISTRY } from "./src/contracts/env.ts"; @@ -169,12 +170,32 @@ function reactRouterProductionAliases(root: string) { return aliases; } +/** + * 1105/12-04. A run with its own output folder (`RUN_OUTPUT_ROOT`, which a + * Playwright config sets for its web server under `TEST_RUN_ID`) builds into + * `/dist` and keeps Vite's dependency cache in `/.vite`, so two + * builds or dev servers in one checkout do not empty or rewrite each other's + * files. Unset, nothing is set here and Vite's defaults (`dist`, + * `node_modules/.vite`) apply as before; Storybook, which loads this file, + * keeps its own. + */ +function runOutputOverrides( + environment: NodeJS.ProcessEnv, +): Readonly<{ cacheDir: string; outDir: string }> | null { + const root = runOutputRoot(environment); + return root === "." + ? null + : { cacheDir: path.posix.join(root, ".vite"), outDir: path.posix.join(root, "dist") }; +} + export default defineConfig(({ command, mode }) => { assertDeclaredBuildVariables(mode); const base = routerBasePath(process.env); assertRuntimeConfigUnderBase(process.env, base); + const runOutput = runOutputOverrides(process.env); return { base, + ...(runOutput ? { cacheDir: runOutput.cacheDir } : {}), plugins: [react(), tailwindcss(), viteModuleInventoryPlugin()], // Development keeps the router's warnings. resolve: command === "build" ? { alias: reactRouterProductionAliases(process.cwd()) } : {}, @@ -187,6 +208,7 @@ export default defineConfig(({ command, mode }) => { }, }, build: { + ...(runOutput ? { outDir: runOutput.outDir } : {}), manifest: true, sourcemap: false, rolldownOptions: { diff --git a/vite.service-worker.config.ts b/vite.service-worker.config.ts index 156732ef..b03e92cb 100644 --- a/vite.service-worker.config.ts +++ b/vite.service-worker.config.ts @@ -8,6 +8,7 @@ import { SERVICE_WORKER_SCRIPT_PATH, } from "./src/contracts/service-worker.ts"; import { INSTALLED_RUNTIME_CAPABILITIES } from "./src/features/installed-runtime-capabilities.ts"; +import { runOutputPath } from "./scripts/lib/run-isolation.ts"; import { resolveServiceWorkerBuildInput } from "./scripts/lib/service-worker-build-input.ts"; /** @@ -24,6 +25,8 @@ const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker; // Relative to this file: Vite's default `bundle` config loader runs the config // from `node_modules/.vite-temp/`, so a bare `import("./.generated/...")` would // resolve there. Every loader keeps `import.meta.url` pointing at this file. +// 1105/12-04. The generated modules, `dist/config.json` and the output are +// under RUN_OUTPUT_ROOT when the build sets one (scripts/build-frontend.ts). async function readGenerated(path: string, exportName: string): Promise { try { const module = (await import(new URL(path, import.meta.url).href)) as Record< @@ -42,17 +45,17 @@ async function readGenerated(path: string, exportName: string): Promise { } const assets = await readGenerated( - "./.generated/frontend-runtime/service-worker-assets.ts", + `./${runOutputPath(".generated/frontend-runtime/service-worker-assets.ts")}`, "SERVICE_WORKER_ASSETS", ); const contractSet = await readGenerated<{ setDigest?: string }>( - "./.generated/frontend-runtime/contract-set.ts", + `./${runOutputPath(".generated/frontend-runtime/contract-set.ts")}`, "BUILD_CONTRACT_SET", ); // 06-10 / F-8. The profile document `scripts/generate-runtime-config.ts` wrote // in step 4, not `public/config.json`, which is always the local profile. const runtimeConfig = JSON.parse( - await readFile("dist/config.json", "utf8"), + await readFile(runOutputPath("dist/config.json"), "utf8"), ) as Record; const buildId = process.env.VITE_BUILD_ID ?? "local-build"; @@ -73,6 +76,7 @@ export default defineConfig({ // profile document step 4 wrote. publicDir: false, build: { + outDir: runOutputPath("dist"), // §17.2.2: exactly one input, exactly one output, no additional chunk. emptyOutDir: false, sourcemap: false,