fix(build): 빌드 식별값의 기본값을 한 함수로 읽는다

문제: 빌드 ID·커밋·release ID의 기본값을 일곱 곳에서 따로 계산하고
`||`와 `??`가 섞였다. 그래서 VITE_BUILD_ID=""이면 runtime config는
local-build가 되고, build manifest·Service Worker·재현 빌드 기록은 ""가
된다(R-2).

고른 방법: src/contracts/env.ts에 BUILD_IDENTITY_PLACEHOLDERS와
readBuildIdentity(environment)를 두고(빈 값은 없는 것과 같다), 페이지의
getBuildConfig와 빌드 스크립트 다섯·vite.service-worker.config.ts가 모두
그것을 쓴다. ENV_REGISTRY 기본값과 LOCAL_BUILD_IDENTITY도 같은 상수에서
나온다. 시험이 빈 값 규칙과, 읽는 파일마다 자기 기본값을 다시 쓰지
않는지를 본다.
버린 대안: 빈 값을 거절하기 — local 빌드가 쓰는 placeholder 규칙을 바꾸고
admission이 이미 placeholder를 공개 대상에서 거절한다.

위험·한계: generate-build-manifest.ts·verify-reproducible-build.ts는 그룹 C도
고치는 파일이다. 줄은 겹치지 않는다.

검증: desktop 결과는 PR 본문.

Refs: R-2
This commit is contained in:
DongHyeonka
2026-11-13 20:17:04 +09:00
parent 44538819eb
commit c1a98e9743
10 changed files with 107 additions and 25 deletions
+2 -3
View File
@@ -3,6 +3,7 @@ import { mkdir, readFile, rm } from "node:fs/promises";
import process from "node:process";
import { z } from "zod";
import { readBuildIdentity } from "../src/contracts/env.ts";
import {
ROUTE_REGISTRY,
ROUTE_RUNTIME_CONTRACT,
@@ -39,9 +40,7 @@ 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 { buildId, commitSha, releaseId } = readBuildIdentity(process.env);
const runnerImage = process.env.CI_RUNNER_IMAGE ?? `${process.platform}-${process.arch}`;
const buildTime = buildDate(process.env);
const builtAt = buildTime.toISOString();
+2 -2
View File
@@ -8,6 +8,7 @@ import {
PUBLIC_TARGETS,
type DeploymentTarget,
} from "../src/contracts/deployment-admission.ts";
import { readBuildIdentity } from "../src/contracts/env.ts";
import { runtimeConfigV2ArtifactSchema } from "../src/contracts/release-artifacts.ts";
import { runOutputPath } from "./lib/run-isolation.ts";
@@ -72,8 +73,7 @@ export async function generateRuntimeConfig(
if (PUBLIC_TARGETS.has(target) && !environment["VITE_BUILD_ID"]) {
throw new Error(`${profilePath}: a ${target} build requires VITE_BUILD_ID`);
}
const buildId = environment["VITE_BUILD_ID"] || "local-build";
const releaseId = environment["RELEASE_ID"] ?? "local-release";
const { buildId, releaseId } = readBuildIdentity(environment);
draft["BUILD_ID"] = buildId;
draft["RELEASE_ID"] = releaseId;
+2 -3
View File
@@ -9,7 +9,7 @@ import {
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
import path from "node:path";
import { getBuildConfig } from "../src/contracts/env.ts";
import { getBuildConfig, readBuildIdentity } from "../src/contracts/env.ts";
import {
BUILD_ASSET_DIRECTORY,
HASHED_STATIC_ASSET_NAME,
@@ -175,8 +175,7 @@ async function walk(root: string, current: string): Promise<string[]> {
async function main(): Promise<void> {
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 { buildId, releaseId } = readBuildIdentity(process.env);
const manifest = await collectStaticAssets(
distDirectory,
buildId,
+5 -3
View File
@@ -1,3 +1,5 @@
import { BUILD_IDENTITY_PLACEHOLDERS } from "../../src/contracts/env.ts";
export const CI_BUILD_ENVIRONMENT_VARIABLES = Object.freeze([
"VITE_BUILD_ID",
"VITE_COMMIT_SHA",
@@ -53,9 +55,9 @@ export function ciBuildEnvironmentFailures(
*/
export const LOCAL_BUILD_IDENTITY = Object.freeze({
CI: "",
VITE_BUILD_ID: "local-build",
VITE_COMMIT_SHA: "local",
RELEASE_ID: "local-release",
VITE_BUILD_ID: BUILD_IDENTITY_PLACEHOLDERS.buildId,
VITE_COMMIT_SHA: BUILD_IDENTITY_PLACEHOLDERS.commitSha,
RELEASE_ID: BUILD_IDENTITY_PLACEHOLDERS.releaseId,
});
export function localBuildEnvironment(
+2 -3
View File
@@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process";
import { mkdir, readFile, readdir } from "node:fs/promises";
import path from "node:path";
import { readBuildIdentity } from "../src/contracts/env.ts";
import { reproducibleBuildArtifactSchema } from "./contracts/release-artifacts.ts";
import { assertCiBuildEnvironment } from "./lib/build-environment.ts";
import { supplyChainDigest } from "./lib/supply-chain.ts";
@@ -62,9 +63,7 @@ await writeValidatedJsonArtifact({
value: {
schemaVersion: 1,
sourceDateEpoch: deterministicEnvironment.SOURCE_DATE_EPOCH,
buildId: process.env.VITE_BUILD_ID ?? "local-build",
commitSha: process.env.VITE_COMMIT_SHA ?? "local",
releaseId: process.env.RELEASE_ID ?? "local-release",
...readBuildIdentity(process.env),
runnerImage:
process.env.CI_RUNNER_IMAGE ?? `${process.platform}-${process.arch}`,
firstDigest,
+3 -3
View File
@@ -1,3 +1,4 @@
import { BUILD_IDENTITY_PLACEHOLDERS } from "./env.ts";
import type { RuntimeConfigArtifact } from "./release-artifacts.ts";
/**
@@ -38,9 +39,8 @@ export const PUBLIC_TARGETS: ReadonlySet<DeploymentTarget> = new Set([
/** Placeholder identifiers a developer build emits when nothing supplied one. */
const PLACEHOLDER_IDENTIFIERS: ReadonlySet<string> = new Set([
"local-build",
"local-release",
"local",
// R-2. The values a build without identity carries, from their one source.
...Object.values(BUILD_IDENTITY_PLACEHOLDERS),
"dev",
"unknown",
]);
+35 -4
View File
@@ -33,6 +33,38 @@ const SEMANTIC_KEY_ALLOWLIST = Object.freeze(
*/
const FEATURE_ID_KEYED_MAPS = Object.freeze(new Set(["FEATURE_OVERRIDES"]));
/**
* R-2. The identity a build carries when its environment gives none: a local
* or development build, never a public one (admission refuses these values,
* `deployment-admission.ts`). Every reader of the build identity, the page
* and the build scripts alike, goes through `readBuildIdentity`, so an empty
* value reads as unset everywhere: the runtime config, the build manifest,
* the Service Worker and the reproducible-build record used to disagree on
* `VITE_BUILD_ID=""` (`||` in some, `??` in others). Admission's placeholder
* list reads these values too (`deployment-admission.ts`).
*/
export const BUILD_IDENTITY_PLACEHOLDERS = Object.freeze({
buildId: "local-build",
commitSha: "local",
releaseId: "local-release",
});
export function readBuildIdentity(
environment: Readonly<Record<string, string | undefined>>,
): Readonly<{ buildId: string; commitSha: string; releaseId: string }> {
// Blank (empty or whitespace) is unset, as the CI identity check reads it
// (`scripts/lib/build-environment.ts`).
const read = (name: string, placeholder: string) => {
const value = environment[name]?.trim();
return value === undefined || value === "" ? placeholder : value;
};
return Object.freeze({
buildId: read("VITE_BUILD_ID", BUILD_IDENTITY_PLACEHOLDERS.buildId),
commitSha: read("VITE_COMMIT_SHA", BUILD_IDENTITY_PLACEHOLDERS.commitSha),
releaseId: read("RELEASE_ID", BUILD_IDENTITY_PLACEHOLDERS.releaseId),
});
}
export type EnvironmentPhase = "build" | "runtime";
export type EnvironmentDefinition = Readonly<{
phase: EnvironmentPhase;
@@ -51,8 +83,8 @@ export const ENV_REGISTRY = Object.freeze(withProductRows({
// F-6. Required: a staging or production build fails without it
// (scripts/generate-runtime-config.ts). Local and development builds fall
// back to the placeholder, which admission refuses for public targets.
VITE_BUILD_ID: build("public-metadata", true, "local-build"),
VITE_COMMIT_SHA: build("public-metadata", false, "local"),
VITE_BUILD_ID: build("public-metadata", true, BUILD_IDENTITY_PLACEHOLDERS.buildId),
VITE_COMMIT_SHA: build("public-metadata", false, BUILD_IDENTITY_PLACEHOLDERS.commitSha),
VITE_ROUTER_BASE_PATH: build("compile-time", true, "/"),
VITE_RUNTIME_CONFIG_URL: build("compile-time", true, "/config.json"),
APP_ENV: runtime("public", true, null),
@@ -201,8 +233,7 @@ export function getBuildConfig(
VITE_RUNTIME_CONFIG_URL: import.meta.env.VITE_RUNTIME_CONFIG_URL,
},
) {
const buildId = environment.VITE_BUILD_ID || "local-build";
const commitSha = environment.VITE_COMMIT_SHA || "local";
const { buildId, commitSha } = readBuildIdentity(environment);
const routerBasePath = environment.VITE_ROUTER_BASE_PATH || "/";
// 13-07. dist is served under the base path, so the default follows it.
const runtimeConfigUrl =
+2 -1
View File
@@ -33,6 +33,7 @@ import {
UNREGISTERED_ROUTE_ID,
} from "../../contracts/routes.ts";
import { TEMPLATE_APP_MODULE } from "../../apps/template-app.ts";
import { BUILD_IDENTITY_PLACEHOLDERS } from "../../contracts/env.ts";
import {
createAppRouteModules,
type ContextRouteModule,
@@ -737,7 +738,7 @@ function createRegisteredRoutes(buildId: string): RouteObject[] {
*/
export function AppRouter({
basename = "/",
buildId = "local-build",
buildId = BUILD_IDENTITY_PLACEHOLDERS.buildId,
}: Readonly<{ basename?: string; buildId?: string }>) {
const [router, setRouter] = useState<ReturnType<
typeof createBrowserRouter
+52
View File
@@ -0,0 +1,52 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { BUILD_IDENTITY_PLACEHOLDERS, readBuildIdentity } from "../../src/contracts/env.ts";
/**
* R-2. The build identity's fallback was written seven times with `||` in
* some places and `??` in others, so `VITE_BUILD_ID=""` gave the runtime
* config `local-build` and the build manifest, the Service Worker and the
* reproducible-build record `""`. Every reader goes through one function now.
*/
const READERS = [
"scripts/generate-runtime-config.ts",
"scripts/generate-build-manifest.ts",
"scripts/generate-service-worker-assets.ts",
"scripts/verify-reproducible-build.ts",
"vite.service-worker.config.ts",
];
const OWN_FALLBACK =
/(?:process\.env|import\.meta\.env|environment)\??(?:\.|\[")(?:VITE_BUILD_ID|VITE_COMMIT_SHA|RELEASE_ID)"?\]?\s*(?:\?\?|\|\|)/u;
describe("R-2 one build identity", () => {
it("reads an empty value as unset, like a missing one", () => {
expect(readBuildIdentity({ VITE_BUILD_ID: "", VITE_COMMIT_SHA: "", RELEASE_ID: "" })).toEqual(
BUILD_IDENTITY_PLACEHOLDERS,
);
expect(readBuildIdentity({})).toEqual(BUILD_IDENTITY_PLACEHOLDERS);
expect(
readBuildIdentity({ VITE_BUILD_ID: "b-1", VITE_COMMIT_SHA: "abc", RELEASE_ID: "r-1" }),
).toEqual({ buildId: "b-1", commitSha: "abc", releaseId: "r-1" });
});
it("treats a blank value as unset, as the CI identity check does", () => {
expect(readBuildIdentity({ VITE_BUILD_ID: " " }).buildId).toBe(
BUILD_IDENTITY_PLACEHOLDERS.buildId,
);
});
it("leaves getBuildConfig on the same rule", () => {
const source = readFileSync("src/contracts/env.ts", "utf8");
const getBuildConfig = source.slice(source.indexOf("export function getBuildConfig"));
expect(getBuildConfig).toContain("readBuildIdentity(environment)");
expect(getBuildConfig).not.toMatch(OWN_FALLBACK);
});
it.each(READERS)("%s reads the identity through readBuildIdentity", (file) => {
const source = readFileSync(file, "utf8");
expect(source).toContain("readBuildIdentity(");
expect(source).not.toMatch(OWN_FALLBACK);
});
});
+2 -3
View File
@@ -1,7 +1,7 @@
import { readFile } from "node:fs/promises";
import { defineConfig } from "vite";
import { getBuildConfig } from "./src/contracts/env.ts";
import { getBuildConfig, readBuildIdentity } from "./src/contracts/env.ts";
import {
SERVICE_WORKER_CACHE_SCHEMA_VERSION,
SERVICE_WORKER_PROTOCOL_VERSION,
@@ -58,8 +58,7 @@ const runtimeConfig = JSON.parse(
await readFile(runOutputPath("dist/config.json"), "utf8"),
) as Record<string, string>;
const buildId = process.env.VITE_BUILD_ID ?? "local-build";
const releaseId = process.env.RELEASE_ID ?? "local-release";
const { buildId, releaseId } = readBuildIdentity(process.env);
const buildInput = resolveServiceWorkerBuildInput({
selection,
assets,