Skip to content

Commit 3009504

Browse files
committed
Scope loadModule default-export unwrap to drizzle config imports
loadModule used to apply `mod?.default ?? mod` in all three branches (Bun/Deno, jiti, ESM Node), so every caller silently received the default export when present. Only drizzleConfigFromFile actually needs that — drizzle.config.ts uses `export default defineConfig(...)` — while the 11 schema-loading call sites in cli/commands/studio.ts and dialects/*/drizzle.ts iterate `Object.entries(mod)` to find PgTable / MySqlTable / Relations values, where the unwrap masks bugs and silently rescues non-canonical `export default { tables }` schemas. Add an explicit `{ defaultExport }` options flag, default false. All three branches now return `defaultExport ? (mod?.default ?? mod) : mod`. drizzleConfigFromFile passes `{ defaultExport: true }`; the schema- loading call sites are unchanged and now receive the raw namespace. Behavior change: schema files using `export default { users, posts }` stop working — must switch to named exports (`export const users = pgTable(...)`), the documented drizzle pattern. - pnpm exec tsc --noEmit -p tsconfig.json → 0 - vitest run tests/other/ → 376/376
1 parent dec6419 commit 3009504

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

drizzle-kit/src/cli/commands/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ export const drizzleConfigFromFile = async (
945945

946946
if (!isExport) humanLog(`Reading config file '${path}'`);
947947

948-
const content = await loadModule<any>(path);
948+
const content = await loadModule<any>(path, { defaultExport: true });
949949

950950
// --- get response and then check by each dialect independently
951951
const res = configCommonSchema.safeParse(content);

drizzle-kit/src/utils/utils-node.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,12 @@ const isDeno = typeof (globalThis as any).Deno !== 'undefined';
428428

429429
export const loadModule = async <T = unknown>(
430430
modulePath: string,
431+
{ defaultExport = false }: { defaultExport?: boolean } = {},
431432
): Promise<T> => {
432433
if (isBun || isDeno) {
433434
const fileUrl = pathToFileURL(modulePath).href;
434435
const mod = await import(fileUrl);
435-
return mod.default ?? mod;
436+
return defaultExport ? (mod?.default ?? mod) : mod;
436437
}
437438

438439
const [major, minor] = process.versions.node.split('.').map(Number);
@@ -466,9 +467,9 @@ export const loadModule = async <T = unknown>(
466467
requireCache: false,
467468
});
468469
const mod = await jiti.import<any>(absoluteModulePath);
469-
return mod?.default ?? mod;
470+
return defaultExport ? (mod?.default ?? mod) : mod;
470471
}
471472
const fileUrl = pathToFileURL(absoluteModulePath).href;
472473
const mod = await import(fileUrl);
473-
return mod.default ?? mod;
474+
return defaultExport ? (mod?.default ?? mod) : mod;
474475
};

0 commit comments

Comments
 (0)