Description
There seems to be a bug in the typegen tool where running it with multiple schema configurations results in only the last layout being exported in the client/index.ts file. All previous layouts' exports are lost.
The Problem
In the generateTypedClientsSingle function in packages/typegen/src/typegen.ts, the client/index.ts file is deleted at the beginning of each configuration processing:
// Current code (line 126-127)
const clientIndexFilePath = path.join(rootDir, "client", "index.ts");
fs.rmSync(clientIndexFilePath, { force: true }); // ensure clean slate for this file
This means when processing multiple configurations, each one deletes the index.ts file that was just populated by the previous configuration.
Reproduction Steps
- Create a configuration with multiple schemas:
[
{
"clientSuffix": "Layout",
"path": "./src/schema",
"clearOldFiles": false,
"envNames": {
"auth": {
"username": "FM_USERNAME",
"password": "FM_PASSWORD"
},
"server": "MyFMServer",
"db": "FM_DATABASE_AB"
},
"layouts": [
{
"schemaName": "MySchema",
"layoutName": "MyLayout"
}
],
"validator": "zod/v3"
},
{
"clientSuffix": "Layout",
"path": "./src/schema",
"clearOldFiles": false,
"envNames": {
"auth": {
"username": "FM_USERNAME",
"password": "FM_PASSWORD"
},
"server": "MyFMServer",
"db": "FM_DATABASE_CD"
},
"layouts": [
{
"schemaName": "MySchema3",
"layoutName": "MyLayout3"
}
],
"validator": "zod/v3"
}
]
- Run typegen
- Check
src/schema/client/index.ts
Current Behavior
The generated index.ts file contains only:
export { client as MySchema3Layout } from "./MySchema3.js";
The export for MySchemaLayout is missing.
Expected Behavior
The generated index.ts file should contain exports for all layouts:
export { client as MySchemaLayout } from "./MySchema.js";
export { client as MySchema3Layout } from "./MySchema3.js";
Root Cause
The issue occurs because:
generateTypedClients loops through each config and calls generateTypedClientsSingle
- Each call to
generateTypedClientsSingle deletes the client/index.ts file at the start
- Previous exports are lost when the file is deleted
Proposed Solution
Move the index.ts deletion to the top-level generateTypedClients function to ensure it's only deleted once at the beginning of the entire process:
- In
generateTypedClients, delete the index.ts files once for all unique paths before processing any configs
- Remove the
fs.rmSync(clientIndexFilePath, { force: true }) line from generateTypedClientsSingle
Related Code Lines
Description
There seems to be a bug in the typegen tool where running it with multiple schema configurations results in only the last layout being exported in the
client/index.tsfile. All previous layouts' exports are lost.The Problem
In the
generateTypedClientsSinglefunction inpackages/typegen/src/typegen.ts, theclient/index.tsfile is deleted at the beginning of each configuration processing:This means when processing multiple configurations, each one deletes the index.ts file that was just populated by the previous configuration.
Reproduction Steps
[ { "clientSuffix": "Layout", "path": "./src/schema", "clearOldFiles": false, "envNames": { "auth": { "username": "FM_USERNAME", "password": "FM_PASSWORD" }, "server": "MyFMServer", "db": "FM_DATABASE_AB" }, "layouts": [ { "schemaName": "MySchema", "layoutName": "MyLayout" } ], "validator": "zod/v3" }, { "clientSuffix": "Layout", "path": "./src/schema", "clearOldFiles": false, "envNames": { "auth": { "username": "FM_USERNAME", "password": "FM_PASSWORD" }, "server": "MyFMServer", "db": "FM_DATABASE_CD" }, "layouts": [ { "schemaName": "MySchema3", "layoutName": "MyLayout3" } ], "validator": "zod/v3" } ]src/schema/client/index.tsCurrent Behavior
The generated
index.tsfile contains only:The export for
MySchemaLayoutis missing.Expected Behavior
The generated
index.tsfile should contain exports for all layouts:Root Cause
The issue occurs because:
generateTypedClientsloops through each config and callsgenerateTypedClientsSinglegenerateTypedClientsSingledeletes theclient/index.tsfile at the startProposed Solution
Move the index.ts deletion to the top-level
generateTypedClientsfunction to ensure it's only deleted once at the beginning of the entire process:generateTypedClients, delete the index.ts files once for all unique paths before processing any configsfs.rmSync(clientIndexFilePath, { force: true })line fromgenerateTypedClientsSingleRelated Code Lines