Skip to content

Commit f6bda04

Browse files
committed
Fix check mode in generate-schemas.ts
Check mode previously compared the gitignored intermediate schema/draft/generated/schema.ts against fresh output, then wrote a temp file under that same directory to dynamic-import for JSON Schema generation. On fresh checkouts (every CI run, since schema/**/generated/ is gitignored) the directory does not exist, so the temp-file write crashed with ENOENT before reporting any real result. Local runs hid the bug because once a developer runs the generator the directory sticks around. Drop the comparison against the gitignored intermediate (it cannot be stale on CI by definition) and check only the committed schema.json. The Zod schemas file is now written unconditionally so the dynamic import resolves.
1 parent 0503f39 commit f6bda04

1 file changed

Lines changed: 19 additions & 39 deletions

File tree

scripts/generate-schemas.ts

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,66 +107,46 @@ async function main() {
107107
);
108108
schemasContent = postProcess(schemasContent);
109109

110+
// Ensure generated directory exists. Required in both modes because check
111+
// mode writes a temp file here for dynamic import (its relative imports
112+
// resolve against this directory).
113+
mkdirSync(GENERATED_DIR, { recursive: true });
114+
110115
if (isCheck) {
111-
// Check mode: compare generated output against existing files
116+
// Check mode: only schema.json is tracked in git (generated/ is gitignored
117+
// as an intermediate artifact). We verify the committed schema.json matches
118+
// what would be regenerated from schema.ts.
112119
let hasChanges = false;
113120

114-
if (existsSync(SCHEMA_OUTPUT_FILE)) {
115-
const existing = readFileSync(SCHEMA_OUTPUT_FILE, "utf-8");
116-
if (existing !== schemasContent) {
121+
// Write the freshly generated Zod schemas to disk so we can dynamic-import
122+
// them. This file is gitignored, so writing it is not a check concern.
123+
writeFileSync(SCHEMA_OUTPUT_FILE, schemasContent, "utf-8");
124+
const jsonSchemaContent = await generateJsonSchemaContent(SCHEMA_OUTPUT_FILE);
125+
126+
if (existsSync(JSON_SCHEMA_OUTPUT_FILE)) {
127+
const existing = readFileSync(JSON_SCHEMA_OUTPUT_FILE, "utf-8");
128+
if (existing !== jsonSchemaContent) {
117129
console.error(
118-
"❌ Generated Zod schemas are out of date. Run: npm run generate:schemas"
130+
"❌ JSON Schema is out of date. Run: npm run generate:schemas"
119131
);
120132
hasChanges = true;
121133
} else {
122-
console.log(" ✓ Zod schemas are up to date");
134+
console.log(" ✓ JSON Schema is up to date");
123135
}
124136
} else {
125137
console.error(
126-
"❌ Generated Zod schemas file does not exist. Run: npm run generate:schemas"
138+
"❌ JSON Schema file does not exist. Run: npm run generate:schemas"
127139
);
128140
hasChanges = true;
129141
}
130142

131-
// Write temp file to generate JSON Schema for comparison
132-
const tempFile = SCHEMA_OUTPUT_FILE + ".check.ts";
133-
writeFileSync(tempFile, schemasContent, "utf-8");
134-
try {
135-
const jsonSchemaContent = await generateJsonSchemaContent(tempFile);
136-
if (existsSync(JSON_SCHEMA_OUTPUT_FILE)) {
137-
const existing = readFileSync(JSON_SCHEMA_OUTPUT_FILE, "utf-8");
138-
if (existing !== jsonSchemaContent) {
139-
console.error(
140-
"❌ JSON Schema is out of date. Run: npm run generate:schemas"
141-
);
142-
hasChanges = true;
143-
} else {
144-
console.log(" ✓ JSON Schema is up to date");
145-
}
146-
} else {
147-
console.error(
148-
"❌ JSON Schema file does not exist. Run: npm run generate:schemas"
149-
);
150-
hasChanges = true;
151-
}
152-
} finally {
153-
// Clean up temp file
154-
const { unlinkSync } = await import("node:fs");
155-
try {
156-
unlinkSync(tempFile);
157-
} catch {}
158-
}
159-
160143
if (hasChanges) {
161144
process.exit(1);
162145
}
163146
console.log("\nAll schemas are up to date!");
164147
return;
165148
}
166149

167-
// Ensure generated directory exists
168-
mkdirSync(GENERATED_DIR, { recursive: true });
169-
170150
// Write Zod schemas
171151
writeFileSync(SCHEMA_OUTPUT_FILE, schemasContent, "utf-8");
172152
console.log(`✅ Written: ${SCHEMA_OUTPUT_FILE}`);

0 commit comments

Comments
 (0)