Skip to content

Commit 1bde775

Browse files
Marfuenclaude
andauthored
fix: robust schema resolution in Trigger.dev Prisma extension (#2442)
Replace naive path list with a 3-strategy fallback approach: 1. Node module resolution via require.resolve (follows workspace symlinks) 2. Walk up node_modules hierarchy from workingDir and workspaceDir 3. Relative monorepo paths as last resort Each strategy checks both dist/schema.prisma (combined, from npm) and prisma/schema/schema.prisma (source, in monorepo workspace). Fixes: Trigger.dev deploy fails because combine-schemas.js is not part of packages/db build, so dist/schema.prisma never exists in CI. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2fdac4c commit 1bde775

2 files changed

Lines changed: 68 additions & 26 deletions

File tree

apps/api/customPrismaExtension.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,31 +283,52 @@ export class PrismaExtension implements BuildExtension {
283283
}
284284

285285
private buildSchemaCandidates(context: ExtendedBuildContext): string[] {
286-
const candidates = new Set<string>();
287-
288-
const addNodeModuleCandidates = (start: string | undefined) => {
289-
if (!start) {
290-
return;
286+
const candidates: string[] = [];
287+
const seen = new Set<string>();
288+
const add = (p: string) => {
289+
const resolved = resolve(p);
290+
if (!seen.has(resolved)) {
291+
seen.add(resolved);
292+
candidates.push(resolved);
291293
}
294+
};
295+
296+
// Strategy 1: Resolve @trycompai/db via Node module resolution (follows workspace symlinks)
297+
try {
298+
const dbPkgJson = require.resolve('@trycompai/db/package.json', {
299+
paths: [context.workingDir],
300+
});
301+
const dbRoot = dirname(dbPkgJson);
302+
add(join(dbRoot, 'dist', 'schema.prisma'));
303+
add(join(dbRoot, 'prisma', 'schema', 'schema.prisma'));
304+
} catch {
305+
// Package not resolvable yet (pre-install), fall through to other strategies
306+
}
292307

308+
// Strategy 2: Walk up node_modules hierarchy from workingDir and workspaceDir
309+
const addNodeModuleCandidates = (start: string | undefined) => {
310+
if (!start) return;
293311
let current = start;
294312
while (true) {
295-
candidates.add(resolve(current, 'node_modules/@trycompai/db/dist/schema.prisma'));
313+
const dbDir = resolve(current, 'node_modules', '@trycompai', 'db');
314+
add(join(dbDir, 'dist', 'schema.prisma'));
315+
add(join(dbDir, 'prisma', 'schema', 'schema.prisma'));
296316
const parent = dirname(current);
297-
if (parent === current) {
298-
break;
299-
}
317+
if (parent === current) break;
300318
current = parent;
301319
}
302320
};
303-
304321
addNodeModuleCandidates(context.workingDir);
305322
addNodeModuleCandidates(context.workspaceDir);
306323

307-
candidates.add(resolve(context.workingDir, '../../packages/db/dist/schema.prisma'));
308-
candidates.add(resolve(context.workingDir, '../packages/db/dist/schema.prisma'));
324+
// Strategy 3: Relative monorepo paths (apps/api → packages/db, apps/app → packages/db)
325+
for (const rel of ['../../packages/db', '../packages/db']) {
326+
const dbDir = resolve(context.workingDir, rel);
327+
add(join(dbDir, 'dist', 'schema.prisma'));
328+
add(join(dbDir, 'prisma', 'schema', 'schema.prisma'));
329+
}
309330

310-
return Array.from(candidates);
331+
return candidates;
311332
}
312333
}
313334

apps/app/customPrismaExtension.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,30 +283,51 @@ export class PrismaExtension implements BuildExtension {
283283
}
284284

285285
private buildSchemaCandidates(context: ExtendedBuildContext): string[] {
286-
const candidates = new Set<string>();
287-
288-
const addNodeModuleCandidates = (start: string | undefined) => {
289-
if (!start) {
290-
return;
286+
const candidates: string[] = [];
287+
const seen = new Set<string>();
288+
const add = (p: string) => {
289+
const resolved = resolve(p);
290+
if (!seen.has(resolved)) {
291+
seen.add(resolved);
292+
candidates.push(resolved);
291293
}
294+
};
295+
296+
// Strategy 1: Resolve @trycompai/db via Node module resolution (follows workspace symlinks)
297+
try {
298+
const dbPkgJson = require.resolve('@trycompai/db/package.json', {
299+
paths: [context.workingDir],
300+
});
301+
const dbRoot = dirname(dbPkgJson);
302+
add(join(dbRoot, 'dist', 'schema.prisma'));
303+
add(join(dbRoot, 'prisma', 'schema', 'schema.prisma'));
304+
} catch {
305+
// Package not resolvable yet (pre-install), fall through to other strategies
306+
}
292307

308+
// Strategy 2: Walk up node_modules hierarchy from workingDir and workspaceDir
309+
const addNodeModuleCandidates = (start: string | undefined) => {
310+
if (!start) return;
293311
let current = start;
294312
while (true) {
295-
candidates.add(resolve(current, 'node_modules/@trycompai/db/dist/schema.prisma'));
313+
const dbDir = resolve(current, 'node_modules', '@trycompai', 'db');
314+
add(join(dbDir, 'dist', 'schema.prisma'));
315+
add(join(dbDir, 'prisma', 'schema', 'schema.prisma'));
296316
const parent = dirname(current);
297-
if (parent === current) {
298-
break;
299-
}
317+
if (parent === current) break;
300318
current = parent;
301319
}
302320
};
303-
304321
addNodeModuleCandidates(context.workingDir);
305322
addNodeModuleCandidates(context.workspaceDir);
306323

307-
candidates.add(resolve(context.workingDir, '../../packages/db/dist/schema.prisma'));
308-
candidates.add(resolve(context.workingDir, '../packages/db/dist/schema.prisma'));
324+
// Strategy 3: Relative monorepo paths (apps/api → packages/db, apps/app → packages/db)
325+
for (const rel of ['../../packages/db', '../packages/db']) {
326+
const dbDir = resolve(context.workingDir, rel);
327+
add(join(dbDir, 'dist', 'schema.prisma'));
328+
add(join(dbDir, 'prisma', 'schema', 'schema.prisma'));
329+
}
309330

310-
return Array.from(candidates);
331+
return candidates;
311332
}
312333
}

0 commit comments

Comments
 (0)