Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/api-server/src/watchPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path'
import {
getDbDir,
getPaths,
getPrismaGeneratorOutputPath,
importStatementPath,
} from '@cedarjs/project-config'

Expand Down Expand Up @@ -94,6 +95,16 @@ async function apiIgnorePaths() {
dbDir,
]

// Always ignore the Prisma client generator output. It's usually inside
// `dbDir` (already ignored above), but projects that point the generator
// `output` outside the schema directory — e.g. into `api/src/lib/...` —
// otherwise trigger an infinite rebuild loop: each build regenerates the
// client, chokidar sees the writes, and another build is scheduled.
const prismaGeneratorOutputPath = await getPrismaGeneratorOutputPath()
if (prismaGeneratorOutputPath) {
ignoredApiPaths.push(prismaGeneratorOutputPath)
}

return ignoredApiPaths
}

Expand Down
30 changes: 30 additions & 0 deletions packages/project-config/src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ type ResolveReturnType =
| { clientPath: string; error: undefined }
| { clientPath: string | undefined; error: string }

/**
* Resolves the absolute path the Prisma client generator writes to.
* Returns `undefined` if the schema or generator can't be read.
*/
export async function getPrismaGeneratorOutputPath(): Promise<
string | undefined
> {
try {
const prismaInternalsMod = await import('@prisma/internals')
const { getConfig } = prismaInternalsMod.default || prismaInternalsMod

const { schemas, schemaRootDir } = await getPrismaSchemas()
const config = await getConfig({ datamodel: schemas })
const generator =
config.generators.find((entry) => entry.name === 'client') ??
config.generators[0]
const output = generator?.output?.value

if (!output) {
return undefined
}

return path.isAbsolute(output)
? output
: path.resolve(schemaRootDir, output)
} catch {
return undefined
}
}

export async function resolveGeneratedPrismaClient(): Promise<ResolveReturnType> {
let generatorOutputPath: string | undefined
let ext = 'ts'
Expand Down
Loading