From 4c71ca94e3fe7b5a680df306bb30a2ea508925c6 Mon Sep 17 00:00:00 2001 From: Ashunar0 Date: Sun, 22 Nov 2026 15:47:03 +0900 Subject: [PATCH 1/2] fix(auth): resolve @auth-schema without app configuration Fixes #900 Resolve `@auth-schema` to `.void/better-auth-schema.ts` the way `@schema` already is: in the Drizzle plugin's `resolveId` for dev and builds, and as a path in the generated `.void/tsconfig.json` for types. The path is added while the generated file exists and dropped when it does not, also when the routing plugin writes the auth files into an existing tsconfig. The auth guide no longer asks apps to add the alias to their root tsconfig, which also replaced every path `.void/tsconfig.json` provides. --- docs/guide/auth.md | 18 ++--------- packages/void/src/codegen/project-tsconfig.ts | 29 +++++++++++++++-- packages/void/src/plugin-drizzle.ts | 15 +++++++-- .../integration/auth-schema-startup.test.ts | 9 ++---- .../void/test/unit/project-tsconfig.test.ts | 32 ++++++++++++++++++- 5 files changed, 75 insertions(+), 28 deletions(-) diff --git a/docs/guide/auth.md b/docs/guide/auth.md index 9e73cb82b..70ea8bb14 100644 --- a/docs/guide/auth.md +++ b/docs/guide/auth.md @@ -139,23 +139,9 @@ Calls such as `signUpEmail` sign the current browser in as the new user. To crea ## Querying Authentication Tables -To query users or reference them from your own tables, import Void's generated authentication schema. It exports `userTable`, `sessionTable`, `accountTable`, `verificationTable`, and any tables added by your Better Auth plugins. +To query users or reference them from your own tables, import Void's generated authentication schema from `@auth-schema`. It exports `userTable`, `sessionTable`, `accountTable`, `verificationTable`, and any tables added by your Better Auth plugins. Like `@schema`, it needs no setup when your root `tsconfig.json` extends `.void/tsconfig.json`. -Add an alias to your root `tsconfig.json`: - -```json -{ - "compilerOptions": { - "paths": { - "@auth-schema": ["./.void/better-auth-schema.ts"] - } - } -} -``` - -Keep your other path aliases, including any you use from `.void/tsconfig.json`, such as `@schema`. - -You can now query users with Drizzle: +Query users with Drizzle: ```ts import { userTable } from '@auth-schema'; diff --git a/packages/void/src/codegen/project-tsconfig.ts b/packages/void/src/codegen/project-tsconfig.ts index 4be81f0f4..21e917f62 100644 --- a/packages/void/src/codegen/project-tsconfig.ts +++ b/packages/void/src/codegen/project-tsconfig.ts @@ -3,6 +3,11 @@ import { join, relative } from 'pathslash'; import { readProjectPaths, type ProjectPaths } from '../project-paths.ts'; import { writeFileIfChanged } from './write-if-changed.ts'; +/** The import name of the auth tables Void generates, as `@schema` is the app's own tables. */ +export const AUTH_SCHEMA_ID = '@auth-schema'; +/** The generated auth tables in .void/, written by the routing plugin while auth is on. */ +export const AUTH_SCHEMA_FILE = 'better-auth-schema.ts'; + export type ProjectTsconfigOptions = { redirectDbModule?: boolean; paths?: ProjectPaths; @@ -93,6 +98,8 @@ export function createProjectTsconfig( paths['@void/md/pages'] = ['./md.d.ts']; } + syncAuthSchemaPath(tsconfig, outDir); + return tsconfig; } @@ -114,9 +121,10 @@ export function ensureProjectTsconfig( } /** - * Add or drop `auth.d.ts` in an existing .void/tsconfig.json to match whether - * `.void/auth.d.ts` exists, keeping every other entry as written. The dev/build - * routing plugin uses this after it writes or removes auth.d.ts, so it does not + * Add or drop `auth.d.ts` and the `@auth-schema` path in an existing .void/tsconfig.json + * to match whether `.void/auth.d.ts` and `.void/better-auth-schema.ts` exist, keeping every + * other entry as written. The dev/build routing plugin uses this after it writes or removes + * the auth files, so it does not * recompute the routes/db choices the Drizzle plugin or `void prepare` made. * A missing or unreadable tsconfig is left for those writers to create. */ @@ -132,5 +140,20 @@ export function syncAuthTypesInProjectTsconfig(root: string): boolean { const files = Array.isArray(tsconfig.files) ? tsconfig.files : []; const others = files.filter((file) => file !== 'auth.d.ts'); tsconfig.files = existsSync(join(outDir, 'auth.d.ts')) ? [...others, 'auth.d.ts'] : others; + syncAuthSchemaPath(tsconfig, outDir); return writeFileIfChanged(tsconfigPath, JSON.stringify(tsconfig, null, 2) + '\n'); } + +/** + * Map `@auth-schema` to .void/better-auth-schema.ts while that file exists, and drop the + * mapping when it does not. Apps then import the auth tables without `paths` of their own, + * which would replace every path this file provides. + */ +function syncAuthSchemaPath(tsconfig: ProjectTsconfig, outDir: string): void { + if (!existsSync(join(outDir, AUTH_SCHEMA_FILE))) { + delete tsconfig.compilerOptions?.paths?.[AUTH_SCHEMA_ID]; + return; + } + const compilerOptions = (tsconfig.compilerOptions ??= {}); + (compilerOptions.paths ??= {})[AUTH_SCHEMA_ID] = [`./${AUTH_SCHEMA_FILE}`]; +} diff --git a/packages/void/src/plugin-drizzle.ts b/packages/void/src/plugin-drizzle.ts index 22e373f5e..9480743ee 100644 --- a/packages/void/src/plugin-drizzle.ts +++ b/packages/void/src/plugin-drizzle.ts @@ -4,7 +4,11 @@ import { fileURLToPath } from 'node:url'; import { dirname, isAbsolute, join, relative } from 'pathslash'; import { normalizePath, type Plugin } from 'vite'; import { DRIZZLE_OPERATORS, generateDbDts } from './codegen/db-types.ts'; -import { ensureProjectTsconfig } from './codegen/project-tsconfig.ts'; +import { + AUTH_SCHEMA_FILE, + AUTH_SCHEMA_ID, + ensureProjectTsconfig, +} from './codegen/project-tsconfig.ts'; import { getDatabaseCasing, getDatabaseDialect, @@ -130,6 +134,13 @@ function createVoidDbModule( } return join(schema.path, id.slice('@schema/'.length)); } + + // `@auth-schema` is the auth tables Void generates, for foreign keys from the app's + // own tables. Resolved here like `@schema`, so dev and builds need no tsconfig `paths` + // or `resolve.tsconfigPaths` for it (the type side is in .void/tsconfig.json). + if (id === AUTH_SCHEMA_ID) { + return join(root, '.void', AUTH_SCHEMA_FILE); + } }, load: { @@ -400,7 +411,7 @@ export function drizzlePlugin( }, resolveId: { - filter: { id: /^(void\/db|@schema)/ }, + filter: { id: /^(void\/db|@schema|@auth-schema$)/ }, handler(id, importer) { // Only intercept void/db in server/worker environments (not client) if (id === 'void/db' && this.environment?.name === 'client') { diff --git a/packages/void/test/integration/auth-schema-startup.test.ts b/packages/void/test/integration/auth-schema-startup.test.ts index 500688a00..85298d730 100644 --- a/packages/void/test/integration/auth-schema-startup.test.ts +++ b/packages/void/test/integration/auth-schema-startup.test.ts @@ -45,16 +45,13 @@ function freshAuthApp() { }; write( 'vite.config.ts', - "import { voidPlugin } from 'void'; export default { plugins: [voidPlugin()], resolve: { tsconfigPaths: true } };", + // No tsconfig `paths` and no `resolve.tsconfigPaths`: Void provides `@auth-schema` itself. + "import { voidPlugin } from 'void'; export default { plugins: [voidPlugin()] };", ); write('void.config.ts', "export default { auth: { providers: ['email'] } };"); write( 'tsconfig.json', - JSON.stringify({ - extends: './.void/tsconfig.json', - compilerOptions: { paths: { '@auth-schema': ['./.void/better-auth-schema.ts'] } }, - include: ['**/*.ts'], - }), + JSON.stringify({ extends: './.void/tsconfig.json', include: ['**/*.ts'] }), ); write( 'auth.ts', diff --git a/packages/void/test/unit/project-tsconfig.test.ts b/packages/void/test/unit/project-tsconfig.test.ts index b198c5a8d..eaf4b0b42 100644 --- a/packages/void/test/unit/project-tsconfig.test.ts +++ b/packages/void/test/unit/project-tsconfig.test.ts @@ -2,7 +2,10 @@ import { mkdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterEach, beforeEach, describe, expect, it } from 'vite-plus/test'; -import { ensureProjectTsconfig } from '../../src/codegen/project-tsconfig.ts'; +import { + ensureProjectTsconfig, + syncAuthTypesInProjectTsconfig, +} from '../../src/codegen/project-tsconfig.ts'; describe('ensureProjectTsconfig', () => { let root: string; @@ -92,6 +95,33 @@ describe('ensureProjectTsconfig', () => { expect(readTsconfig().files).not.toContain('auth.d.ts'); }); + it('maps @auth-schema only while the generated auth schema exists', () => { + const authSchema = join(root, '.void', 'better-auth-schema.ts'); + ensureProjectTsconfig(root); + expect(readTsconfig().compilerOptions?.paths?.['@auth-schema']).toBeUndefined(); + + writeFileSync(authSchema, 'export const userTable = {};\n'); + ensureProjectTsconfig(root); + expect(readTsconfig().compilerOptions.paths['@auth-schema']).toEqual([ + './better-auth-schema.ts', + ]); + + rmSync(authSchema); + ensureProjectTsconfig(root); + expect(readTsconfig().compilerOptions?.paths?.['@auth-schema']).toBeUndefined(); + }); + + it('adds @auth-schema to an existing tsconfig once the auth schema is written', () => { + ensureProjectTsconfig(root, true, true, false); + writeFileSync(join(root, '.void', 'better-auth-schema.ts'), 'export const userTable = {};\n'); + syncAuthTypesInProjectTsconfig(root); + expect(readTsconfig().compilerOptions.paths).toEqual({ + 'void/db': ['./db.d.ts'], + 'void/routes': ['./routes.d.ts'], + '@auth-schema': ['./better-auth-schema.ts'], + }); + }); + it('combines routes, queues, and md when all present', () => { mkdirSync(join(root, 'routes'), { recursive: true }); mkdirSync(join(root, 'queues'), { recursive: true }); From 4745052de365ec475b0301b201a3a88f7ff2ce76 Mon Sep 17 00:00:00 2001 From: Ashunar0 Date: Sun, 22 Nov 2026 19:46:04 +0900 Subject: [PATCH 2/2] fix(deploy): copy the generated tsconfig into the schema drift check The deploy drift check copies the app without .void/, so an app tsconfig that extends .void/tsconfig.json could not resolve @auth-schema there. --- packages/void/src/cli/deploy-drift.ts | 18 ++++++----- packages/void/test/unit/deploy-drift.test.ts | 33 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/packages/void/src/cli/deploy-drift.ts b/packages/void/src/cli/deploy-drift.ts index 803bf1388..721c88105 100644 --- a/packages/void/src/cli/deploy-drift.ts +++ b/packages/void/src/cli/deploy-drift.ts @@ -162,13 +162,17 @@ function createDeployDriftProject(root: string, driftProjectRoot: string): void } // The build-generated Better Auth schema participates in `void db generate`, so - // it must also participate in the isolated drift check. Copy only this known - // generated input; the rest of `.void` stays excluded. - const authSchemaPath = join(root, '.void', 'better-auth-schema.ts'); - if (existsSync(authSchemaPath)) { - const target = join(driftProjectRoot, '.void', 'better-auth-schema.ts'); - mkdirSync(dirname(target), { recursive: true }); - cpSync(authSchemaPath, target); + // it must also participate in the isolated drift check. The generated tsconfig + // maps `@auth-schema`, and the app's tsconfig may extend it, so schema imports + // resolve only with it present. Copy only these known generated inputs; the rest + // of `.void` stays excluded. + for (const file of ['better-auth-schema.ts', 'tsconfig.json']) { + const source = join(root, '.void', file); + if (existsSync(source)) { + const target = join(driftProjectRoot, '.void', file); + mkdirSync(dirname(target), { recursive: true }); + cpSync(source, target); + } } } diff --git a/packages/void/test/unit/deploy-drift.test.ts b/packages/void/test/unit/deploy-drift.test.ts index b18d96586..0af58b846 100644 --- a/packages/void/test/unit/deploy-drift.test.ts +++ b/packages/void/test/unit/deploy-drift.test.ts @@ -15,6 +15,7 @@ import { packageFixturePath } from '../../../../test/fixture-dir.ts'; import { generateVoidAuthDrizzleSchema } from '../../src/auth/drizzle.ts'; import { getDeploySchemaDriftMessage, hasDeploySchemaDrift } from '../../src/cli/deploy-drift.ts'; import { validateAuthMigrationSchema } from '../../src/cli/deploy-migrations.ts'; +import { ensureProjectTsconfig } from '../../src/codegen/project-tsconfig.ts'; import { writeDrizzleConfig } from '../../src/drizzle/config.ts'; import { collectMigrations } from '../../src/migrations/collect.ts'; import { WRANGLER_AUTH_CREDENTIAL_ENV_VARS } from '../../src/shared/untrusted-env.ts'; @@ -73,6 +74,38 @@ describe('hasDeploySchemaDrift', { timeout: driftTestTimeout }, () => { expect(existsSync(join(root, '.void', 'deploy-drift-check'))).toBe(false); }); + it.each([ + ['extends the generated tsconfig', { extends: './.void/tsconfig.json' }], + [ + 'also maps @auth-schema itself', + { + extends: './.void/tsconfig.json', + compilerOptions: { paths: { '@auth-schema': ['./.void/better-auth-schema.ts'] } }, + }, + ], + ])('resolves @auth-schema when the app tsconfig %s', (_, tsconfig) => { + const root = createTempApp(); + writeAuthSchema(root); + writeFileSync( + join(root, 'db', 'schema.ts'), + [ + 'import { sqliteTable, text } from "drizzle-orm/sqlite-core";', + 'import { user } from "@auth-schema";', + '', + 'export const posts = sqliteTable("posts", {', + ' id: text("id").primaryKey(),', + ' userId: text("user_id").references(() => user.id),', + '});', + '', + ].join('\n'), + ); + writeFileSync(join(root, 'tsconfig.json'), JSON.stringify(tsconfig)); + ensureProjectTsconfig(root); + generateMigrations(root, { includeAuth: true }); + + expect(hasDeploySchemaDrift(root, 'sqlite')).toBe(false); + }); + it('checks auth-only migration history without requiring an application schema', () => { const root = createTempApp(); writeAuthSchema(root);