fix(feature-index): 바뀌지 않은 기능 인덱스는 다시 쓰지 않는다
문제: 빌드, dev 서버, 타입 검사, vitest가 모두 시작할 때 `scripts/generate-product-feature-index.ts`로 `src/.generated/*.ts`를 다시 쓴다. 생성기는 `writeFile`로 파일을 제자리에서 비우고 다시 썼다. 한 checkout에서 두 실행이 겹치면, 한쪽이 파일을 비운 순간 다른 쪽의 Vite나 tsc가 빈 파일이나 반쯤 쓴 파일을 읽을 수 있다(1105/12-04의 "한 checkout 두 실행" 중 공유 소스 파일 부분). 고른 방법: 내용이 같으면 쓰지 않는다. 다르면 같은 폴더의 임시 파일에 쓰고 rename으로 바꾼다. 읽는 쪽은 옛 파일이나 새 파일 하나만 본다. 같은 선택(같은 PRODUCT_APP_ID·VITE_PRODUCT_FEATURES)으로 도는 두 실행은 아예 쓰지 않는다. 버린 대안: 실행마다 `src/.generated`를 따로 두기. 앱 소스가 이 경로를 import하므로 경로를 바꾸면 Vite·tsc·ESLint 설정이 모두 따라 바뀐다. 잠금 파일: 겹친 실행을 직렬로 만들 뿐이고, 죽은 프로세스가 잠금을 남긴다. 위험·한계: 선택이 서로 다른 두 실행(예: 한쪽만 VITE_PRODUCT_FEATURES=none)은 여전히 서로의 인덱스를 바꾼다. 그건 실행이 아니라 checkout 상태가 다른 것이라 이 커밋 범위 밖이다. 임시 파일은 `.tmp`로 끝나 tsc·Vite·ESLint 대상이 아니다. 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/unit/product-feature-index-write.test.ts Refs: 1105/12-04
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { dirname, relative, resolve } from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
||||
import { basename, dirname, join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { selectCompiledProductFeatures } from "../src/contracts/product-features.ts";
|
||||
@@ -404,6 +405,26 @@ export async function generateAppIndexes(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1105/12-04. Every build, dev server, type check and test run renders these
|
||||
* files first, so two runs in one checkout render them at the same time while
|
||||
* the other run's Vite or tsc reads them. `writeFile` truncated the file in
|
||||
* place, and a reader could see it empty or half written. An index that has
|
||||
* not changed is left alone; a changed one replaces the old file with one
|
||||
* rename, so a reader sees the old file or the new one, never a part.
|
||||
*/
|
||||
export async function writeIndexIfChanged(file: string, content: string): Promise<void> {
|
||||
if ((await readFile(file, "utf8").catch(() => null)) === content) return;
|
||||
const temporary = join(dirname(file), `.${basename(file)}.${randomUUID()}.tmp`);
|
||||
try {
|
||||
await writeFile(temporary, content, { encoding: "utf8", flag: "wx" });
|
||||
await rename(temporary, file);
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
// `--root=` points the same production command at another checkout (used by
|
||||
@@ -448,7 +469,7 @@ async function main(): Promise<void> {
|
||||
|
||||
await Promise.all(
|
||||
(["feature", "context", "app"] as const).map((key) =>
|
||||
writeFile(outputs[key], rendered[key], "utf8"),
|
||||
writeIndexIfChanged(outputs[key], rendered[key]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user