fix(seed): route prisma db seed through pnpm run for cwd resolution #234

Merged
julien merged 1 commits from fix/prisma-seed-cwd into main 2026-05-26 14:45:16 +02:00
Owner

Summary

Follow-up fix on #233. The prisma db seed command failed with Cannot find module './seed.ts' because the seed command's path was resolved against the user's cwd (apps/portal-bff/) and doubled into apps/portal-bff/apps/portal-bff/prisma/seed.ts.

Root cause: Prisma CLI spawns the seed command with cwd = wherever the user invoked prisma db seed from. The path apps/portal-bff/prisma/seed.ts only works if cwd is the workspace root, but the user's habit (matching prisma migrate dev) is to run prisma commands from apps/portal-bff/.

Fix: indirect through pnpm run. pnpm run for a root-level script always sets cwd = workspace root regardless of where invoked from. The seed path then resolves predictably.

What lands

File Change
package.json (root) New db:seed npm script in scripts carrying the ts-node invocation. prisma.seed becomes pnpm run db:seed — the indirection forces cwd = workspace root before ts-node resolves the seed path.

That's it. Two-line diff.

Why pnpm run indirection works

Step cwd Path resolution
User invokes pnpm exec prisma db seed from apps/portal-bff/ apps/portal-bff/
Prisma CLI walks up, finds the workspace package.json, reads prisma.seed apps/portal-bff/
Prisma spawns the seed command apps/portal-bff/ (inherited)
Seed command is pnpm run db:seed apps/portal-bff/
pnpm run resets cwd to the package's root (workspace root) workspace root
ts-node sees apps/portal-bff/prisma/seed.ts workspace root resolves correctly ✓

The same flow works if the user invokes from the workspace root — pnpm run still ends up at workspace root, idempotent.

Test plan

  • Locally: cd apps/portal-bff && pnpm exec prisma db seed — should report [seed] test-tenant complete — created … Person + … User + … UserScope rows; …. No MODULE_NOT_FOUND.
  • Also locally: same command from the workspace root (pnpm exec prisma db seed) — same result.
  • Idempotency — re-run, expect created 0 Person + 0 User + 0 UserScope rows.
  • No application code changed — drift gate, lint, tests untouched.
  • Review focus — the pnpm run indirection trick + the cwd table above.

Notes for the reviewer

  • The deprecation warning about package.json#prisma (Prisma 7 migration to prisma.config.ts) is unchanged — not in this fix's scope.
  • Could have also fixed by changing the path to be relative-to-bff (prisma/seed.ts) and forcing the user to run from apps/portal-bff/. The pnpm run approach is cwd-invariant — more robust to where the user invokes from.
  • The new db:seed script is callable directly (pnpm db:seed) which is a small ergonomic bonus.
## Summary Follow-up fix on [#233](https://git.unespace.com/julien/apf_portal/pulls/233). The `prisma db seed` command failed with `Cannot find module './seed.ts'` because the seed command's path was resolved against the user's cwd (`apps/portal-bff/`) and doubled into `apps/portal-bff/apps/portal-bff/prisma/seed.ts`. Root cause: Prisma CLI spawns the seed command with cwd = wherever the user invoked `prisma db seed` from. The path `apps/portal-bff/prisma/seed.ts` only works if cwd is the workspace root, but the user's habit (matching `prisma migrate dev`) is to run prisma commands from `apps/portal-bff/`. Fix: indirect through `pnpm run`. `pnpm run` for a root-level script always sets cwd = workspace root regardless of where invoked from. The seed path then resolves predictably. ## What lands | File | Change | | --- | --- | | `package.json` (root) | New `db:seed` npm script in `scripts` carrying the ts-node invocation. `prisma.seed` becomes `pnpm run db:seed` — the indirection forces cwd = workspace root before ts-node resolves the seed path. | That's it. Two-line diff. ## Why `pnpm run` indirection works | Step | cwd | Path resolution | | --- | --- | --- | | User invokes `pnpm exec prisma db seed` from `apps/portal-bff/` | `apps/portal-bff/` | — | | Prisma CLI walks up, finds the workspace `package.json`, reads `prisma.seed` | `apps/portal-bff/` | — | | Prisma spawns the seed command | `apps/portal-bff/` (inherited) | — | | Seed command is `pnpm run db:seed` | `apps/portal-bff/` | — | | **`pnpm run` resets cwd to the package's root (workspace root)** | **workspace root** | — | | ts-node sees `apps/portal-bff/prisma/seed.ts` | workspace root | resolves correctly ✓ | The same flow works if the user invokes from the workspace root — `pnpm run` still ends up at workspace root, idempotent. ## Test plan - [ ] **Locally**: `cd apps/portal-bff && pnpm exec prisma db seed` — should report `[seed] test-tenant complete — created … Person + … User + … UserScope rows; …`. No `MODULE_NOT_FOUND`. - [ ] **Also locally**: same command from the workspace root (`pnpm exec prisma db seed`) — same result. - [ ] **Idempotency** — re-run, expect `created 0 Person + 0 User + 0 UserScope rows`. - [x] No application code changed — drift gate, lint, tests untouched. - [ ] **Review focus** — the `pnpm run` indirection trick + the cwd table above. ## Notes for the reviewer - The deprecation warning about `package.json#prisma` (Prisma 7 migration to `prisma.config.ts`) is unchanged — not in this fix's scope. - Could have also fixed by changing the path to be relative-to-bff (`prisma/seed.ts`) and forcing the user to run from `apps/portal-bff/`. The `pnpm run` approach is cwd-invariant — more robust to where the user invokes from. - The new `db:seed` script is callable directly (`pnpm db:seed`) which is a small ergonomic bonus.
julien added 1 commit 2026-05-26 14:44:39 +02:00
fix(seed): route prisma db seed through pnpm run for cwd resolution
CI / check (pull_request) Successful in 1m53s
CI / commits (pull_request) Successful in 4m12s
CI / scan (pull_request) Successful in 5m9s
CI / a11y (pull_request) Successful in 3m37s
Docs site / build (pull_request) Successful in 3m49s
CI / perf (pull_request) Successful in 9m9s
54499d6f64
prisma db seed failed with "Cannot find module './seed.ts'" because
the spawned ts-node command inherited the user's cwd (apps/portal-bff/)
and resolved apps/portal-bff/prisma/seed.ts to
apps/portal-bff/apps/portal-bff/prisma/seed.ts (doubled).

pnpm run resets cwd to the package's root (workspace root) before
running the script. Routing prisma.seed through pnpm run db:seed
makes the path resolution cwd-invariant - works the same whether the
user invokes from the workspace root or from apps/portal-bff/.

Two-line diff: new db:seed script + prisma.seed delegates to it.
Side benefit: pnpm db:seed is directly callable.
julien merged commit 827d69594c into main 2026-05-26 14:45:16 +02:00
julien deleted branch fix/prisma-seed-cwd 2026-05-26 14:45:18 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: julien/apf_portal#234