Repository navigation
Commit 70ff789
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
File tree
- .drive/projects/project-toolchain
- docs
- architecture
- adrs
- reference
- packages/cli
- e2e
- src
- lib
- agent
- skills
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | | - | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
0 commit comments