문제: 빌드는 `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/<id>/` 아래 같은 상대 경로에 둔다. 없으면 지금 경로 그대로라 게이트, 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
193 lines
6.5 KiB
TypeScript
193 lines
6.5 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { mkdir, readFile, rm } from "node:fs/promises";
|
|
import process from "node:process";
|
|
import { z } from "zod";
|
|
|
|
import {
|
|
ROUTE_REGISTRY,
|
|
ROUTE_RUNTIME_CONTRACT,
|
|
} from "../src/features/installed-feature-contracts.ts";
|
|
import {
|
|
buildManifestArtifactSchema,
|
|
jsonSchemaDocumentArtifactSchema,
|
|
moduleInventoryArtifactSchema,
|
|
releaseManifestV2ArtifactSchema,
|
|
runtimeConfigV2ArtifactSchema,
|
|
} from "./contracts/release-artifacts.ts";
|
|
import { buildContractSet } from "./generate-contract-set.ts";
|
|
import {
|
|
assertCiBuildEnvironment,
|
|
buildDate,
|
|
} 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;
|
|
isDynamicEntry?: boolean;
|
|
}>;
|
|
|
|
const packageJson = parsePackageMetadata(
|
|
JSON.parse(await readFile("package.json", "utf8")),
|
|
);
|
|
const packageManagerVersion = packageJson.packageManager.split("@").at(-1);
|
|
const buildId = process.env.VITE_BUILD_ID ?? "local-build";
|
|
const commitSha = process.env.VITE_COMMIT_SHA ?? "local";
|
|
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(at(CANONICAL_VITE_MANIFEST_PATH), "utf8");
|
|
const viteManifestObject = parseViteManifest(JSON.parse(viteManifest));
|
|
const moduleInventory = await readFile(
|
|
at("dist/.vite/module-inventory.json"),
|
|
"utf8",
|
|
);
|
|
const moduleInventoryDocument = moduleInventoryArtifactSchema.parse(
|
|
JSON.parse(moduleInventory),
|
|
);
|
|
const moduleInventoryBytes = `${JSON.stringify(moduleInventoryDocument, null, 2)}\n`;
|
|
const assetManifestHash = createHash("sha256")
|
|
.update(viteManifest)
|
|
.digest("hex");
|
|
const moduleInventoryHash = createHash("sha256")
|
|
.update(moduleInventoryBytes)
|
|
.digest("hex");
|
|
const runtimeConfig = runtimeConfigV2ArtifactSchema.parse(
|
|
JSON.parse(await readFile(at("dist/config.json"), "utf8")),
|
|
);
|
|
const routeChunks: Record<string, string> = {};
|
|
const runtimeContracts: Readonly<Record<string, { moduleId: string }>> =
|
|
ROUTE_RUNTIME_CONTRACT;
|
|
for (const definition of Object.values(ROUTE_REGISTRY)) {
|
|
const runtime = runtimeContracts[definition.routeId];
|
|
const asset = Object.values(viteManifestObject).find(
|
|
(entry) => entry.name === runtime?.moduleId && entry.isDynamicEntry,
|
|
);
|
|
if (!runtime || !asset?.file) {
|
|
throw new Error(`Missing built route chunk: ${definition.routeId}`);
|
|
}
|
|
routeChunks[definition.chunkId] = asset.file;
|
|
}
|
|
const runtimeConfigJsonSchema = z.toJSONSchema(runtimeConfigV2ArtifactSchema);
|
|
|
|
runtimeConfig.BUILD_ID = buildId;
|
|
runtimeConfig.RELEASE_ID = releaseId;
|
|
|
|
const manifest = buildManifestArtifactSchema.parse({
|
|
schemaVersion: 1,
|
|
buildId,
|
|
commitSha,
|
|
releaseId,
|
|
moduleInventoryHash,
|
|
generatedAt: builtAt,
|
|
buildContext: {
|
|
nodeVersion: process.version,
|
|
packageManagerVersion,
|
|
runnerImage,
|
|
sourceDateEpoch: process.env.SOURCE_DATE_EPOCH ?? null,
|
|
},
|
|
outputs: {
|
|
directory: "dist",
|
|
viteManifest: CANONICAL_VITE_MANIFEST_PATH,
|
|
moduleInventory: "artifacts/quality/vite-module-inventory.json",
|
|
routeChunks,
|
|
runtimeConfigSchema: "dist/runtime-config.schema.json",
|
|
},
|
|
});
|
|
|
|
// §5.2 / §24.6 RC-3. The writer emits V2 only; the scalar API contract
|
|
// version has no writer source left.
|
|
const releaseManifest = releaseManifestV2ArtifactSchema.parse({
|
|
schemaVersion: 2,
|
|
appVersion: packageJson.version,
|
|
buildId,
|
|
commitSha,
|
|
configSchemaVersion: runtimeConfig.CONFIG_SCHEMA_VERSION,
|
|
assetManifestHash,
|
|
releaseId,
|
|
builtAt,
|
|
routeChunks,
|
|
contractSet: buildContractSet(),
|
|
});
|
|
|
|
await mkdir(at("artifacts/release"), { recursive: true });
|
|
await mkdir(at("artifacts/quality"), { recursive: true });
|
|
await writeValidatedJsonArtifact({
|
|
path: at("artifacts/quality/vite-module-inventory.json"),
|
|
schema: moduleInventoryArtifactSchema,
|
|
value: moduleInventoryDocument,
|
|
});
|
|
await rm(at("dist/.vite/module-inventory.json"));
|
|
await writeValidatedJsonArtifact({
|
|
path: at("dist/config.json"),
|
|
schema: runtimeConfigV2ArtifactSchema,
|
|
value: runtimeConfig,
|
|
});
|
|
await writeValidatedJsonArtifact({
|
|
path: at("dist/release-manifest.json"),
|
|
schema: releaseManifestV2ArtifactSchema,
|
|
value: releaseManifest,
|
|
});
|
|
await writeValidatedJsonArtifact({
|
|
path: at("dist/runtime-config.schema.json"),
|
|
schema: jsonSchemaDocumentArtifactSchema,
|
|
value: runtimeConfigJsonSchema,
|
|
});
|
|
await writeValidatedJsonArtifact({
|
|
path: at("artifacts/release/runtime-config.schema.json"),
|
|
schema: jsonSchemaDocumentArtifactSchema,
|
|
value: runtimeConfigJsonSchema,
|
|
});
|
|
await writeValidatedJsonArtifact({
|
|
path: at("artifacts/release/build-manifest.json"),
|
|
schema: buildManifestArtifactSchema,
|
|
value: manifest,
|
|
});
|
|
|
|
function parsePackageMetadata(value: unknown): Readonly<{
|
|
version: string;
|
|
packageManager: string;
|
|
}> {
|
|
if (
|
|
!isRecord(value) ||
|
|
typeof value.version !== "string" ||
|
|
typeof value.packageManager !== "string"
|
|
) {
|
|
throw new TypeError("package.json release metadata is invalid");
|
|
}
|
|
return { version: value.version, packageManager: value.packageManager };
|
|
}
|
|
|
|
function parseViteManifest(
|
|
value: unknown,
|
|
): Readonly<Record<string, ViteManifestEntry>> {
|
|
if (!isRecord(value)) throw new TypeError("Vite manifest must be an object");
|
|
const entries: Record<string, ViteManifestEntry> = {};
|
|
for (const [key, candidate] of Object.entries(value)) {
|
|
if (!isRecord(candidate) || typeof candidate.file !== "string") {
|
|
throw new TypeError(`Invalid Vite manifest entry: ${key}`);
|
|
}
|
|
entries[key] = {
|
|
file: candidate.file,
|
|
...(typeof candidate.name === "string" ? { name: candidate.name } : {}),
|
|
...(typeof candidate.isDynamicEntry === "boolean"
|
|
? { isDynamicEntry: candidate.isDynamicEntry }
|
|
: {}),
|
|
};
|
|
}
|
|
return entries;
|
|
}
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
}
|