Skip to content

Commit 70ff789

Browse files
feat(cli): refuse an ORM or Composer command when this prisma would not run the project's own copy of the ORM packages (#417)
## At a glance The `prisma` command-line tool is installed with its own copy of the packages that implement the Prisma ORM commands (`@prisma/orm-toolchain` and `@prisma/orm-framework`). An application that uses the ORM installs those packages too. Usually the package manager installs one copy for both. Sometimes it cannot: for example, the project has version 8.0.0-rc.24 and someone runs a globally installed `prisma` that uses 8.0.0-rc.25. Today that fails with an error that says nothing about versions or copies: ```text $ prisma db migrate ✘ [CONTRACT.SOURCE_LOAD_FAILED] Failed to resolve contract source why: Codec pg/bool@1 takes only values of pg/bool, and was handed an object no data type constructed. ``` With this change: ```text $ prisma db migrate ✘ [CLI.SECOND_PACKAGE_COPY] prisma db migrate cannot run here: the project uses @prisma/orm-toolchain 8.0.0-rc.24, and this prisma uses 8.0.0-rc.25. why: The project's @prisma/orm-toolchain 8.0.0-rc.24 is at /work/shop/node_modules/@prisma/orm-toolchain. This prisma's @prisma/orm-toolchain 8.0.0-rc.25 is at /usr/local/lib/node_modules/prisma/node_modules/@prisma/orm-toolchain. A command can only work with the project's own copy of these packages: values made by one copy are refused by another, and the files a command writes are read later by the project's copy. → Run the project's own prisma instead, adding your own arguments after the command: npx --no-install prisma db migrate ``` ## The decision `prisma` refuses to run an ORM command when the copy of the ORM packages it would run is not the copy installed in the project. It stops before it reads the project's config, names both copies with their versions and locations, and says how to get to one copy. The same applies to the Composer commands (`prisma dev`, `prisma deploy`; Composer is the product that runs and deploys an app), which also compare `@prisma/composer`. Who meets this: - someone running a global `prisma`, or `npx prisma` in a project that has not installed `prisma`, in a project that has the ORM packages; - someone who updates `prisma` in a project without updating the project's ORM packages, when the new `prisma` uses a newer ORM version. These cases mostly fail already, with the first error above, as soon as the project's data model has a literal default value. What changes is that they now fail every time, at once, with an error that says what to do. The cost we are accepting: `prisma` will not run ORM commands for a project on another ORM version, so updating `prisma` and updating the project's ORM packages have to happen together. The decision is recorded in `docs/architecture/adrs/0007-no-second-copy-of-a-product.md`. ## Why two copies fail today An ORM command loads the project's `prisma.config.ts`. That file builds its values (data types, descriptors) with the project's copy of `@prisma/orm-framework`. The command then reads them with the copy `prisma` brought. The framework marks every value it creates with a private class field, which only the same loaded copy of the code can read, so a second copy treats the value as foreign and rejects it. That is true when the versions differ, and also when one version is installed in two places. ## Why refuse, and not make two copies work together What an ORM command writes is read later by the application's own runtime: the emitted contract, migration files, generated files. If `prisma` runs a newer toolchain than the project has, it writes those files in the newer version's format, and the project's older runtime is what has to read them. We have no rule that keeps that format readable by older releases. So even if the framework accepted values from another copy, a command run by another version could leave the project with files its runtime cannot use. The command has to be run by the version the project uses. That argument is about different versions. One version installed twice would be harmless if the framework accepted values across copies, which it does not today. We refuse it too: it fails today in the same way, and comparing locations and not version numbers keeps the rule simple (one copy, or stop). If the ORM later accepts values across copies, that case can be allowed. ## What the check does 1. **Which commands.** Every ORM and Composer command except `prisma emulator list`, which reads nothing from the project. Platform commands such as `prisma auth login` are untouched, and a run that names no ORM or Composer command does not look at the disk. 2. **What is compared.** `@prisma/orm-toolchain` and `@prisma/orm-framework`, plus `@prisma/composer` for Composer commands. It compares the real directory each side would load, not version numbers. 3. **Where the project's copy is looked for.** In `node_modules` folders, starting from the directory of the nearest `prisma.config.ts` at or above the current directory (the current directory when there is none, or the directory of the file named by `--config`), and going up no further than the repository root (the first directory containing `.git`). `prisma` already refuses to read config files above the repository root; this uses the same limit. 4. **Nothing installed.** A package the project does not have is not compared, so `prisma orm init` in an empty folder runs as before. 5. **The advice** depends on the situation: - The project has its own `prisma`, and that one would pass the check: run it. The suggested command never downloads anything (`npx --no-install`, `pnpm exec`, `yarn exec`, `bunx --no-install`), never runs a `package.json` script of the same name, and contains the command name and `--config` when the user gave it, but none of the other arguments, because those can hold a database URL. - Otherwise: change the project's packages to the version this `prisma` uses, or install a `prisma` that uses the project's version. Both versions are named. - One version installed twice: reinstall the project's dependencies. 6. **Output.** `--help` for a refused command is unchanged. With `--json` the refusal is the usual error envelope, with code `CLI.SECOND_PACKAGE_COPY` and `meta.packages` listing each package's name and both versions and paths. ## How it is wired, and why that way The CLI has two layers in this repository: the engine (`@prisma/cli-engine`), which parses arguments, loads config and runs a command; and the shell (`packages/cli`), which decides which commands exist and at which path. The ORM and Composer packages build their commands against the engine, so an engine change needs a new release of each of them. The engine loads a command's config before it calls the command's code, and offers the shell no place to act in between. To avoid an engine release, the shell acts before it hands over to the engine: it looks in the arguments for a word that begins an ORM or Composer command, runs the comparison, and on a mismatch registers those commands with code that only raises the error, and without the declaration that makes the engine load config. Their paths, help and arguments stay the same. The decision record says an engine hook should replace this the next time the engine changes for another reason. ## Decisions made while building - **`@prisma/cli-engine` is not compared.** An application does not list the engine in its `package.json`, so whether it can be found from the project depends on the package manager; under pnpm it cannot. Also, between an engine release and the ORM and Composer releases that adopt it, a development build of `prisma` is installed with two engine versions on purpose, and comparing would refuse every ORM command in those builds. - **Only the nearest config file's directory is searched from.** A monorepo can have a root config for repository-wide settings and packages on different ORM versions. A copy hoisted to the root belongs to another package, and must not stop this package's own `prisma`. A wrong refusal is worse than a missed one, because the user has no way past it. - **The project's copy is found by walking `node_modules`, not with Node's resolver.** The resolver returns the real path after following symlinks, so the repository-root limit could not be applied to where the package was found. Without the limit, a stray `~/node_modules` (some machines have one) would refuse `prisma orm init` in every new folder below it. ## What the check can miss In these cases the check finds nothing and the command fails as before, with the first error in this description: - a copy above the repository root; - a config file further up than the nearest one, or one named by another config's `parent` key, that loads the packages from its own directory; - a pnpm project whose `package.json` does not list the ORM packages. pnpm only makes listed packages reachable from the project. `prisma orm init` writes those lines, and the ORM's database packages name them as peer dependencies, so a project set up as documented lists them; - a file the config points to in another directory, most often the contract source, which loads the packages from where it is; - Yarn Plug'n'Play, which has no `node_modules` folders; - a package folder that cannot be read. ## How it was checked - Unit tests on real directory layouts: hoisted, nested, pnpm-style symlinks, and a monorepo with the config at the root. - Tests through the CLI's entry point for refusal, non-refusal, `--json`, `--help` and each advice case, and two end-to-end cases with the built binary. - One test evaluates a real config file and asserts that the check's verdict agrees with the copy that config loads. - Real installs: the built `prisma` bundle placed in npm and pnpm installs of `prisma` 8.0.0-rc.36 beside the ORM and Composer packages at the versions it uses. Each install had one copy of every compared package and the project's own `prisma` ran. Running one install's `prisma` inside the other project (one version, installed twice) was refused. With Yarn (node-modules linker) and Bun, the same install was checked for layout only: each also has one copy of every compared package. - Three rounds of independent design and code review; findings fixed. ## Also in this pull request - `docs/architecture/adrs/0004-engine-version-pinning.md` said that applications never install `@prisma/orm-toolchain`. Since 2026-11-24 the ORM requires applications to list it (its decision record 329, "An application lists the platform packages"), so that paragraph is amended. - `.drive/projects/project-toolchain/`: this work's spec, plan and list of deferred items. The deferred item, a written compatibility policy for the CLI and Composer, is #416. ## Alternatives considered - **Have `prisma` start the project's own `prisma` for the user.** It works in only one of the three situations above: the project may have no `prisma`, or its own `prisma` may also use another ORM version than the project's packages. It also silently runs a different CLI version from the one the user invoked. It could be added later for the one case; the error already gives the command. - **Warn and continue.** The command would then fail with the unexplained error, or write files in the wrong version's format. - **Load and run the project's copy of the toolchain, whatever its version.** This would let a newer `prisma` serve an older project. It needs the engine in `prisma` to run commands built against another engine copy, which the engine does not support, and it needs substitutes for commands the newer CLI lists but the older toolchain does not have. - **Make framework values work across copies.** Does not address what a newer toolchain writes for an older runtime (see "Why refuse"). - **A check inside the ORM packages that fails when two copies are loaded.** The ORM had one and removed it after its 8.0.0-rc.22 release: Composer's deploy loads a second ORM copy on purpose, and every deployed Composer service that used the ORM crashed when it started. - **A Node module hook that makes both sides load one copy.** The CLI also runs on Bun and Deno. - **Compare version numbers only.** One version installed twice fails the same way. Agent: anastasia-63 --------- Signed-off-by: willbot <w.a.madden+machine@gmail.com> Signed-off-by: Will Madden <madden@prisma.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e9f54e9 commit 70ff789

21 files changed

Lines changed: 2372 additions & 35 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Deferred
2+
3+
- A compatibility policy for the `prisma` CLI and Composer: https://github.com/prisma/prisma-cli/issues/416
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Plan: `prisma` refuses a second copy of our packages
2+
3+
Part of the Prisma 8.0 GA compatibility stream (stream 7, project 13; rollout item 19 in prisma/orm `planning/rollout-8.0.md`). The stream is led by khan-87, session `local_b2fe1b5d-9100-4799-8ab2-e6a4f369ada5`.
4+
5+
| Step | State |
6+
| --- | --- |
7+
| The check in prisma/prisma-cli (`CLI.SECOND_PACKAGE_COPY`), with decision record 0007 | Merged in the pull request that adds this file |
8+
| Rollout item 19 and the project 13 plan in prisma/orm describe this behaviour and its cost | Done by khan-87 on 2026-11-30 |
9+
| prisma/orm ADR 329 still describes a check that compares minor versions; amend it | To do, in prisma/orm |
10+
| Tell khan-87 the check has merged | To do |
11+
| Update the policy page in prisma/web (`apps/docs/content/docs/(index)/versioning-and-support.mdx`, the section "Prisma CLI" and the "Commands" bullet under "What stays compatible") to describe what merged: the toolchain and the framework are compared, plus `@prisma/composer` for Composer commands, and not the engine | To do, after the merge. Handed over by khan-87 on 2026-12-01 |
12+
| Check the published `prisma` in a real project: a global `prisma` beside a project's own install is refused, the project's own runs | To do, once a release carries the check |
13+
14+
See `spec.md` for the design and `deferred.md` for what is out of scope.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# `prisma` refuses a product command when the project has its own copy of that product
2+
3+
Status: built in prisma/prisma-cli, 2026-11-30. The decision is recorded in `docs/architecture/adrs/0007-no-second-copy-of-a-product.md`. Rollout item 19 in prisma/orm `planning/rollout-8.0.md`, stream 7 project 13 in prisma/orm `projects/ga-compatibility/plan.md`.
4+
5+
## Purpose
6+
7+
No package of ours runs as two copies in one `prisma` process. Before an ORM or Composer command, the shell checks that the copy of the product it would run is the copy the project's config will load. If it is not, the command stops and names both copies.
8+
9+
Supporting two versions side by side is not a goal.
10+
11+
## Why
12+
13+
1. **What a command writes must match the project's runtime.** `contract emit`, migrations and generated files are read later by the application's own runtime, at the version the application installed. A newer toolchain could write output an older runtime does not understand, even if it reads the older project perfectly. Only running the project's own release avoids that.
14+
2. **Two copies fail today with an error that names neither.** Values made by one copy of the ORM framework are refused by another (they carry a private-field mark; prisma/orm `packages/1-framework/1-core/framework-components/src/shared/data-type.ts`). The user sees `CONTRACT.SOURCE_LOAD_FAILED … was handed an object no data type constructed`. It happens at the same version too, when the two are separate installs.
15+
16+
## Design
17+
18+
1. Before an ORM or Composer command, the shell finds each checked package in the project and in its own install, and compares real directory paths. Checked: `@prisma/orm-toolchain` and `@prisma/orm-framework`; plus `@prisma/composer` for Composer commands.
19+
2. The project's copy is looked for from the directory of the nearest `prisma.config.ts` at or above the current directory (the current directory when there is none, or the directory of the `--config` file), upward through `node_modules` folders to the repository boundary (the first directory with `.git`).
20+
3. Same path for every package: the command runs.
21+
4. Any different path: the command fails before the project's config is loaded, with `CLI.SECOND_PACKAGE_COPY`. It names the package, both versions and both locations. Next action: run the project's own `prisma` when the project has one, it is not the one running, and it would itself pass; otherwise change the project's packages to the version this `prisma` uses, or use a `prisma` that uses the project's version.
22+
5. A package the project does not have is skipped. `orm init` in an empty folder runs with the shell's copies.
23+
6. The shell decides the command tree, as before. A refused command keeps its help.
24+
7. The check is the shell's and applies only to the ORM and Composer commands that read the project's config. It is not a guard inside our packages, so Composer's deploy, which loads its own ORM copy on purpose, is unaffected.
25+
26+
## Decided while building
27+
28+
- **`@prisma/cli-engine` is not compared**, though the handover listed it. An application does not list the engine, so from the project it is found only when the package manager hoists it. A development build installs two engine copies on purpose while the products catch up to a new engine (prisma-cli ADR 0004), and the comparison would refuse every ORM command in such a build. And when the toolchain is one copy, the engine it resolves is one copy.
29+
- **The project's copy is found by walking `node_modules` folders, not by `require.resolve`.** `require.resolve` returns real paths, so the repository boundary could not be applied to where the package was found. The cost: under Yarn Plug'n'Play the check finds nothing and the command runs as before.
30+
31+
- **Only commands that read the project's config are refused.** A code review pointed out that a command such as `emulator list` neither reads the config nor writes files the runtime reads. Every other ORM and Composer command is refused: they declare a config section, or (`lsp`, `orm init`) load the project's config or packages themselves.
32+
- **Checked against real installs.** With `prisma` 8.0.0-rc.36 beside the ORM and Composer packages at the versions it uses, npm, pnpm, Yarn (node-modules linker) and Bun each install one copy of the toolchain, the framework and `@prisma/composer`. Under pnpm the engine is not reachable from the project at all.
33+
34+
## Cost
35+
36+
This reverses project 13's original reason ("updating `prisma` must not lock a user out of an older project"). A user who updates `prisma` past their project's ORM release has to run the project's own `prisma` or update the project. The prisma/orm plan, rollout item 19 and ADR 329 must be rewritten to say so.
37+
38+
## Rejected
39+
40+
- Running the project's toolchain at another version. It needs a promise that two engine copies work together, and stand-in commands for ones an older toolchain lacks.
41+
- A Node module hook to share one engine: the CLI may run on Bun or Deno.
42+
- Making framework values work across copies by recognising them by shape. The private-field mark does contradict the ORM's error decision record (ADR 239), which is worth raising with the ORM separately, but it does not fix reason 1.
43+
- Splitting the config entrypoint out of the toolchain. The config reaches the toolchain's migration code through its control descriptors, the application needs the toolchain for migration files and the vite plugin anyway, and the collision is between framework copies.
44+
- A global "two copies loaded" guard inside the packages. It existed as `RUNTIME.MULTIPLE_ORM_COPIES` and crashed Composer services.
45+
46+
## Out of scope
47+
48+
- A compatibility policy for the `prisma` CLI and for Composer. See `deferred.md`.

‎docs/architecture/adrs/0004-engine-version-pinning.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Per edge:
3535

3636
**Consequence for packaging:** a product may not mix its application-facing library and its command family in one published package, because a peer on the combined package would be auto-installed for every library consumer. The ORM already conforms (`@prisma/orm-toolchain` is dev/CLI-only; applications reach its vite plugin through `@prisma/orm-postgres`'s forwarded export). Composer splits: `@prisma/composer-cli` takes the family, the testing surface and the bin; `@prisma/composer` keeps the library exports and drops the engine entirely. The import graphs were verified disjoint before the split was scheduled, so it is a packaging change, not an untangling.
3737

38+
**Amended 2026-11-30.** The ORM half of this paragraph, and edge 4's "product CLI packages reach applications only transitively through the CLI", no longer hold for the ORM. Since prisma/orm ADR 329 (2026-11-24) an application lists `@prisma/orm-toolchain` and `@prisma/orm-framework` in its own `package.json`, because its config, its migration files and the vite plugin use them. The toolchain still names the engine as an exact peer and the application still does not list the engine. But the toolchain is now also installed for the application, so when the application's toolchain and the CLI's are different releases, the installed tree can hold two engines, one for each. Edge 3's "exactly one engine exists in any tree" therefore holds for the CLI's own tree, not for every application's; what holds for an application is one engine for the product code a command runs, because ADR 0007 makes the CLI run only the toolchain copy the project has. A project's config file is an exception that is harmless today: it imports `definePrismaConfig` from `prisma/config`, which resolves to the project's own `prisma` and its engine, so a global `prisma` reading that config loads a second engine copy for that one function. The config marker it stamps is a plain value, read by shape. What happens when the application's copy and the CLI's copy are separate installs is decided in [ADR 0007](0007-no-second-copy-of-a-product.md).
39+
3840
**Exact now, range later.** During the rc line the engine breaks its consumers deliberately, slice by slice, so a version range would be fiction. Post-GA, the recorded destination is widening the peers to a range under a written engine compatibility contract, so a non-breaking engine release ships in the next shell release with zero family republishes. Widening is a deliberate future decision against that contract, not a drift.
3941

4042
**Engine versioning — RULED (operator, 2026-08-13): the engine versions independently.** Under the shell lockstep it previously shared, every CLI release manufactured a new engine version — and obliged every product to publish a new peer version — whether or not the engine changed. Decoupled, an engine version means "the engine changed", the exact peers stay valid between real engine changes, and the products only have to move when there is a real change to move to. Mechanically: the engine is excluded from `set-version.ts`'s lockstep (alongside `@prisma/compute`), follows honest pre-1.0 semver (breaking bumps the minor), and publishes at its own manifest version, with an already-published version treated as a no-op by the publish workflow.
@@ -55,7 +57,7 @@ The conformance checkers in both publishing repos are the mechanism, evolved fro
5557
- *App depends on a product library and the CLI*: the library carries no engine, so no duplication is possible through that route. This case was unsolvable under `dependencies` pinning and is the reason peers plus the split were chosen.
5658
- *Release skew between the component repos*: an unsatisfied exact peer fails at install/publish-check time instead of shipping a duplicated tree; with the engine versioned independently, skew windows open only when the engine actually changes.
5759
- *Two CLI versions in one monorepo*: nested and isolated; each tree satisfies its own peers.
58-
- *CLI ↔ library version skew* (the app's `@prisma/composer` vs the CLI's `composer-cli`): explicitly **not** a duplication problem and not addressed here; it is a compatibility-policy question (support floor or runtime detection) that needs its own owner.
60+
- *CLI ↔ library version skew* (the app's `@prisma/composer` vs the CLI's `composer-cli`): explicitly **not** a duplication problem and not addressed here; it is a compatibility-policy question (support floor or runtime detection) that needs its own owner. Since 2026-11-30 the shell detects it at run time and refuses the command ([ADR 0007](0007-no-second-copy-of-a-product.md)); the policy itself is still unwritten.
5961

6062
## Alternatives considered
6163

0 commit comments

Comments
 (0)