Skip to content

Commit 91b2a0e

Browse files
feat(engine): prisma orm init can save exact versions under npm, through a new exact option on package installs (#437)
## At a glance `prisma orm init` in an npm project writes the ORM packages with a caret today. After this change, and the ORM change that uses it, it writes the exact version: ```diff "dependencies": { - "@prisma/orm-postgres": "^8.0.0-rc.28", + "@prisma/orm-postgres": "8.0.0-rc.28", ``` `orm init` installs packages through the engine (`@prisma/cli-engine`). A command reaches the engine's package installs through `ctx.packages.install`. That call can now ask for the version to be saved exactly as given: ```ts await ctx.packages.install({ packages: ["@prisma/orm-postgres@8.0.0-rc.28"], exact: true, }); ``` | Manager | Command the engine runs | | --- | --- | | npm | `npm add --save-exact @prisma/orm-postgres@8.0.0-rc.28` | | pnpm | `pnpm add --save-exact @prisma/orm-postgres@8.0.0-rc.28` | | yarn | `yarn add --exact @prisma/orm-postgres@8.0.0-rc.28` | | bun | `bun add --exact @prisma/orm-postgres@8.0.0-rc.28` | | deno | `deno add npm:@prisma/orm-postgres@8.0.0-rc.28` (no flag) | With `dev: true` both flags are passed (`npm add -D --save-exact …`). Without `exact`, every command is unchanged. ## Why Until release 8.0, the ORM's [versioning policy](https://github.com/prisma/orm/blob/main/docs/oss/versioning.md) says an application lists every Prisma ORM package at one exact version, and `orm init` asks for exactly that version. npm saves an exact request with a caret unless told not to. The other managers already save it as given. The caret breaks the project at the next ORM release. Since [#417](#417), `prisma` refuses an ORM command when the project's ORM packages are not the version it runs. `npm update` moves the ORM packages through the caret while the project's `prisma` stays where it is, and every ORM command stops until the user also updates `prisma`. The ORM cannot fix this alone, because the engine writes the install command and had no way to say "save exactly". ## What changes - **The option.** `PackageOperations.install` takes an optional `exact`. `installCommand`, the engine's one place that writes each manager's command line, maps it to the manager's own flag, as in the table above. The engine shows that command line before it runs it, and repeats it in the error when the install fails, so both carry the flag too. - **deno gets no flag.** deno's docs list `--save-exact`, but deno 2.6.10 rejects it and installs nothing. deno already saves an exact request as given. - **Pass `exact` only when every package names an exact version.** npm's flag also turns a range into the version it resolved: `npm add --save-exact pkg@^7.0.0` saves `7.0.0`. pnpm, Yarn and bun keep a range as given under their flags. The option's doc comment says this, so a caller installs ranges in a separate call. - **How the flags were checked.** pnpm 10 and 11, Yarn 1 and 4, bun 1.3 and deno 2.6 were run with each case: an exact pre-release, an exact plain version, a range, and the dev flag. npm's behaviour comes from its documentation and its source (`@npmcli/arborist`). - **Engine 0.10.4 → 0.10.5.** This is a patch because the option is optional. The two packages the CLI mounts commands from, `@prisma/orm-toolchain` (the ORM's commands) and `@prisma/composer-cli` (Composer's), each require one exact engine version, and still require 0.10.4. As [ADR 0004](docs/architecture/adrs/0004-engine-version-pinning.md) prescribes for this case, `packages/cli/scripts/conformance.ts` records an exception for each, for development builds only. The CLI's pre-publish check still refuses a release until both require 0.10.5. ## Tests `tests/package-manager.test.ts` asserts the whole command for each manager with `exact`, and with `dev` and `exact` together. `tests/package-operations.test.ts` asserts that `exact` reaches the spawned command, the command line the engine shows, and the failure's error. The new non-deno cases fail without the change. The two deno cases pass on the old code by design: they stop a later change from adding a flag that breaks deno installs. ## What follows 1. Merging this publishes engine 0.10.5. 2. A prisma/orm pull request moves `@prisma/orm-toolchain` to 0.10.5, and has `orm init` pass `exact` for its exact-version installs. 3. Composer moves `@prisma/composer-cli` to 0.10.5. 4. The next CLI release ships 0.10.5, and the two exceptions are removed. ## Alternatives considered - **`orm init` rewrites `package.json` after the install.** The lockfile still records the caret, and `npm ci` then refuses because `package.json` and the lockfile disagree. - **Set npm's `save_exact` setting in the environment from `orm init`.** It works for npm only, and it goes around the engine, which is meant to own the command line. - **Always save exact.** Other callers install ranges and tags such as `prisma@latest`, and npm would save those as one fixed version. Agent: lucius-31 --------- Signed-off-by: willbot <w.a.madden+machine@gmail.com> Signed-off-by: Will Madden <madden@prisma.io>
1 parent 0193c5d commit 91b2a0e

9 files changed

Lines changed: 184 additions & 16 deletions

File tree

‎packages/cli-engine/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@prisma/cli-engine",
3-
"version": "0.10.4",
3+
"version": "0.10.5",
44
"description": "The execution engine of the unified Prisma CLI.",
55
"type": "module",
66
"exports": {

‎packages/cli-engine/src/execution/package-operations.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Operation =
2828
readonly form: "install";
2929
readonly packages: readonly string[];
3030
readonly dev: boolean;
31+
readonly exact: boolean;
3132
}
3233
| {
3334
readonly form: "run";
@@ -48,6 +49,7 @@ function spell(
4849
? installCommand(manager, {
4950
packages: operation.packages,
5051
dev: operation.dev,
52+
exact: operation.exact,
5153
})
5254
: runCommand(manager, { package: operation.package, args: operation.args });
5355
}
@@ -241,6 +243,7 @@ export function makePackageOperations(
241243
form: "install",
242244
packages: request.packages,
243245
dev: request.dev ?? false,
246+
exact: request.exact ?? false,
244247
},
245248
request,
246249
),

‎packages/cli-engine/src/package-manager.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export interface PackageOperations {
2121
/** Specifiers as the user would type them, e.g. "prisma@latest". */
2222
readonly packages: readonly string[];
2323
readonly dev?: boolean;
24+
/**
25+
* Saves each version as given, with no range prefix. Pass it only
26+
* when every specifier names an exact version: npm saves a range as
27+
* the exact version it resolved.
28+
*/
29+
readonly exact?: boolean;
2430
/** Defaults to ctx.cwd. */
2531
readonly cwd?: string;
2632
/** Overrides detection, so a caller can retry with another manager. */
@@ -135,7 +141,7 @@ export async function resolvePackageManager(request: {
135141
interface Spelling {
136142
readonly install: (
137143
packages: readonly string[],
138-
dev: boolean,
144+
options: { readonly dev: boolean; readonly exact: boolean },
139145
) => PackageManagerCommand;
140146
readonly run: (
141147
packageName: string,
@@ -170,9 +176,14 @@ function spell(file: string, args: readonly string[]): PackageManagerCommand {
170176
return { file, args, line: [file, ...args].map(shellQuote).join(" ") };
171177
}
172178

173-
function addSpelling(file: string): Spelling["install"] {
174-
return (packages, dev) =>
175-
spell(file, ["add", ...(dev ? ["-D"] : []), ...packages]);
179+
function addSpelling(file: string, exactFlag: string): Spelling["install"] {
180+
return (packages, { dev, exact }) =>
181+
spell(file, [
182+
"add",
183+
...(dev ? ["-D"] : []),
184+
...(exact ? [exactFlag] : []),
185+
...packages,
186+
]);
176187
}
177188

178189
/**
@@ -181,23 +192,24 @@ function addSpelling(file: string): Spelling["install"] {
181192
*/
182193
const SPELLINGS: Readonly<Record<PackageManagerId, Spelling>> = {
183194
npm: {
184-
install: addSpelling("npm"),
195+
install: addSpelling("npm", "--save-exact"),
185196
run: (packageName, args) => spell("npx", [packageName, ...args]),
186197
},
187198
pnpm: {
188-
install: addSpelling("pnpm"),
199+
install: addSpelling("pnpm", "--save-exact"),
189200
run: (packageName, args) => spell("pnpm", ["dlx", packageName, ...args]),
190201
},
191202
yarn: {
192-
install: addSpelling("yarn"),
203+
install: addSpelling("yarn", "--exact"),
193204
run: (packageName, args) => spell("yarn", ["dlx", packageName, ...args]),
194205
},
195206
bun: {
196-
install: addSpelling("bun"),
207+
install: addSpelling("bun", "--exact"),
197208
run: (packageName, args) => spell("bunx", [packageName, ...args]),
198209
},
199210
deno: {
200-
install: (packages, dev) =>
211+
// deno writes the specifier as given and rejects --save-exact.
212+
install: (packages, { dev }) =>
201213
spell("deno", [
202214
"add",
203215
...(dev ? ["--dev"] : []),
@@ -214,9 +226,13 @@ export function installCommand(
214226
request: {
215227
readonly packages: readonly string[];
216228
readonly dev?: boolean;
229+
readonly exact?: boolean;
217230
},
218231
): PackageManagerCommand {
219-
return SPELLINGS[manager].install(request.packages, request.dev ?? false);
232+
return SPELLINGS[manager].install(request.packages, {
233+
dev: request.dev ?? false,
234+
exact: request.exact ?? false,
235+
});
220236
}
221237

222238
/** Runs a package's bin once, without adding a dependency. */

‎packages/cli-engine/tests/package-manager.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,89 @@ describe("installCommand", () => {
199199
});
200200
});
201201

202+
const EXACT = ["prisma@8.0.0-rc.28", "@prisma/client@8.0.0-rc.28"];
203+
204+
describe("installCommand with exact", () => {
205+
test.each<SpellingCase>([
206+
{
207+
manager: "npm",
208+
file: "npm",
209+
args: ["add", "--save-exact", ...EXACT],
210+
line: "npm add --save-exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
211+
},
212+
{
213+
manager: "pnpm",
214+
file: "pnpm",
215+
args: ["add", "--save-exact", ...EXACT],
216+
line: "pnpm add --save-exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
217+
},
218+
{
219+
manager: "yarn",
220+
file: "yarn",
221+
args: ["add", "--exact", ...EXACT],
222+
line: "yarn add --exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
223+
},
224+
{
225+
manager: "bun",
226+
file: "bun",
227+
args: ["add", "--exact", ...EXACT],
228+
line: "bun add --exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
229+
},
230+
{
231+
manager: "deno",
232+
file: "deno",
233+
args: ["add", "npm:prisma@8.0.0-rc.28", "npm:@prisma/client@8.0.0-rc.28"],
234+
line: "deno add npm:prisma@8.0.0-rc.28 npm:@prisma/client@8.0.0-rc.28",
235+
},
236+
])("$manager saves the version as given", ({ manager, ...expected }) => {
237+
expect(installCommand(manager, { packages: EXACT, exact: true })).toEqual(
238+
expected,
239+
);
240+
});
241+
242+
test.each<SpellingCase>([
243+
{
244+
manager: "npm",
245+
file: "npm",
246+
args: ["add", "-D", "--save-exact", ...EXACT],
247+
line: "npm add -D --save-exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
248+
},
249+
{
250+
manager: "pnpm",
251+
file: "pnpm",
252+
args: ["add", "-D", "--save-exact", ...EXACT],
253+
line: "pnpm add -D --save-exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
254+
},
255+
{
256+
manager: "yarn",
257+
file: "yarn",
258+
args: ["add", "-D", "--exact", ...EXACT],
259+
line: "yarn add -D --exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
260+
},
261+
{
262+
manager: "bun",
263+
file: "bun",
264+
args: ["add", "-D", "--exact", ...EXACT],
265+
line: "bun add -D --exact prisma@8.0.0-rc.28 @prisma/client@8.0.0-rc.28",
266+
},
267+
{
268+
manager: "deno",
269+
file: "deno",
270+
args: [
271+
"add",
272+
"--dev",
273+
"npm:prisma@8.0.0-rc.28",
274+
"npm:@prisma/client@8.0.0-rc.28",
275+
],
276+
line: "deno add --dev npm:prisma@8.0.0-rc.28 npm:@prisma/client@8.0.0-rc.28",
277+
},
278+
])("$manager saves dev dependencies as given", ({ manager, ...expected }) => {
279+
expect(
280+
installCommand(manager, { packages: EXACT, dev: true, exact: true }),
281+
).toEqual(expected);
282+
});
283+
});
284+
202285
describe("runCommand", () => {
203286
test.each<SpellingCase>([
204287
{

‎packages/cli-engine/tests/package-operations.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,51 @@ describe("ctx.packages.install", () => {
342342
{ file: "npm", args: ["add", "prisma"], cwd: "/elsewhere" },
343343
]);
344344
});
345+
346+
test("exact reaches the spawned args, the announced step and the remedy", async () => {
347+
const { runs, runner } = recorder({ exitCode: 1, stderr: "boom" });
348+
const cli = createTestCli({
349+
commands: {
350+
toy: installer((packages) =>
351+
packages.install({ packages: ["prisma@8.0.0-rc.28"], exact: true }),
352+
),
353+
},
354+
packageManager: "npm",
355+
packageManagerRunner: runner,
356+
});
357+
358+
const { events, json } = await cli.run(["toy", "--json"], {
359+
cwd: "/project",
360+
});
361+
362+
const line = "npm add --save-exact prisma@8.0.0-rc.28";
363+
expect(runs).toEqual([
364+
{
365+
file: "npm",
366+
args: ["add", "--save-exact", "prisma@8.0.0-rc.28"],
367+
cwd: "/project",
368+
},
369+
]);
370+
expect(events).toEqual<EngineEvent[]>([
371+
{ kind: "step-started", step: line },
372+
{ kind: "step-finished", step: line, outcome: "failed" },
373+
]);
374+
expect(json.find((frame) => frame.kind === "result")).toMatchObject({
375+
envelope: {
376+
ok: false,
377+
error: {
378+
meta: { command: line },
379+
nextActions: [
380+
{
381+
kind: "run-command",
382+
label: "Run the install yourself",
383+
command: line,
384+
},
385+
],
386+
},
387+
},
388+
});
389+
});
345390
});
346391

347392
describe("ctx.packages.run", () => {

‎packages/cli/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@manypkg/tools": "^2.1.2",
52-
"@prisma/cli-engine": "workspace:0.10.4",
52+
"@prisma/cli-engine": "workspace:0.10.5",
5353
"@prisma/composer": "0.41.2",
5454
"@prisma/composer-cli": "0.41.2",
5555
"@prisma/composer-prisma-cloud": "0.41.2",

‎packages/cli/scripts/conformance.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,28 @@ async function tarball(): Promise<readonly Finding[]> {
122122
shellPackage: "@prisma/cli",
123123
enginePackage: "@prisma/cli-engine",
124124
familyPackages: ["@prisma/composer-cli", "@prisma/orm-toolchain"],
125-
exceptions: [],
125+
exceptions: [
126+
{
127+
familyPackage: "@prisma/composer-cli",
128+
familyPin: "0.10.4",
129+
shellPin: "0.10.5",
130+
channel: "dev",
131+
reason:
132+
"engine 0.10.5 adds the exact option to ctx.packages.install; composer-cli still peers 0.10.4",
133+
removeWhen:
134+
"composer-cli releases peering 0.10.5 and the follow-up bump PR pins that release",
135+
},
136+
{
137+
familyPackage: "@prisma/orm-toolchain",
138+
familyPin: "0.10.4",
139+
shellPin: "0.10.5",
140+
channel: "dev",
141+
reason:
142+
"engine 0.10.5 adds the exact option to ctx.packages.install; orm-toolchain still peers 0.10.4",
143+
removeWhen:
144+
"orm-toolchain releases peering 0.10.5 and the follow-up bump PR pins that release",
145+
},
146+
],
126147
channel: CHANNEL,
127148
sandboxDir: join(WORK_DIR, "sandbox"),
128149
},

‎packages/prisma/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"dependencies": {
5252
"@manypkg/tools": "^2.1.2",
53-
"@prisma/cli-engine": "workspace:0.10.4",
53+
"@prisma/cli-engine": "workspace:0.10.5",
5454
"@prisma/composer": "0.41.2",
5555
"@prisma/composer-cli": "0.41.2",
5656
"@prisma/composer-prisma-cloud": "0.41.2",

‎pnpm-lock.yaml‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)