Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/architecture/adrs/0006-consent-as-a-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ These came from the ORM's first use and the reviews of this change, all in engin
- **A signal while a prompt waits cancels the prompt** with `CLI.PROMPT_CANCELLED`: exit 3 for SIGINT, as Ctrl-C at the prompt is the user declining, and 143 for SIGTERM, which ends the run as a delivered signal. Later prompts in the run are cancelled at once, and a further signal force-exits.
- **`last: true`** marks a batch as the run's final ask: values still unconsumed fail with `CLI.CONSENT_UNUSED` as soon as it is answered, before the command acts on the answers.

Added in engine 0.10.1:

- **The question is the first line of the refusal** when it is the only question nobody can answer; with several, the first line counts them and `why` lists them. So a question is one line that names what it is about and what is at stake.

## Consequences

- A product adds a consent vocabulary by declaring it on the command that uses it, and its help text with it. The engine stays free of product words.
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/error-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ The config file's `$prismaConfig` marker declares a version other than the one t

A consent prompt was reached under `--yes` or in a non-interactive session. Consent has no default answer and `--yes` does not grant it, so there is nothing for the run to assume. When the consent declares a token, the message and next action say to pass `--confirm <token>`, and the token travels in meta; without a token, the only path is running the command interactively. Meta: `consentToken` (only when the consent declares a token).

A statement prompt (`ctx.prompt.statement` or `ctx.prompt.statements`) raises the same code when no statement flag on the command line answers it. One error lists every question still unanswered, and the next actions give one flag to pass per verb, such as `--delete Legacy` or `--rename 'Legacy:<new name>'`. With one question unanswered, the summary is the command's question and `why` says that the user has to choose and that the run cannot ask (or, under `--yes`, that `--yes` does not choose). With several, the summary gives their number and `why` carries the questions. `why` also lists every given statement flag that names none of the questions' subjects, and any `--confirm` token, which answers no statement, so a misspelled flag is reported before anyone answers a prompt. Meta: `unanswered` (a list of `{ subject, verbs }`), plus `subject` and `verbs` when exactly one question is unanswered; `unmatched` (a list of `{ verb, values }`) and `confirm` (the tokens) when any.
A statement prompt (`ctx.prompt.statement` or `ctx.prompt.statements`) raises the same code when no statement flag on the command line answers it. One error lists every question still unanswered: the summary says the command requires explicit consent and did not get it (under `--yes`, that `--yes` does not give it), `why` carries the questions, and the next actions give one flag to pass per verb, such as `--delete Legacy` or `--rename 'Legacy:<new name>'`. `why` also lists every given statement flag that names none of the questions' subjects, and any `--confirm` token, which answers no statement, so a misspelled flag is reported before anyone answers a prompt. Meta: `unanswered` (a list of `{ subject, verbs }`), plus `subject` and `verbs` when exactly one question is unanswered; `unmatched` (a list of `{ verb, values }`) and `confirm` (the tokens) when any.

### CLI.CONSENT_UNUSED

Expand Down
2 changes: 1 addition & 1 deletion packages/cli-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Each occurrence of `--<verb>` takes exactly `arity` values, in argv order across
The engine answers the question in this order:

1. A verb flag whose first value names the subject: the value is the subject, or starts with `<subject>:`. `--delete Legacy` and `--rename Legacy:Archive` both name `Legacy`. Within one batch, a value equal to a subject answers that subject first, and any other value goes to the longest subject it names, so `--delete A:B` answers the question about `A:B`, not the one about `A`. Ask questions whose subjects are prefixes of one another in one batch. A value `validate` rejects fails the run with `CLI.PROMPT_INVALID`.
2. With no such value, a non-interactive run, or one under `--yes`, fails with `CLI.CONSENT_REQUIRED`. When one question is unanswered, the error's first line is that question word for word, so write the question to name what it is about. Its next actions give one flag to pass per verb, safe to paste into a shell: a value with anything but plain characters is single-quoted, and a value starting with `-` is written `--<verb>=<value>`, and its `meta` carries `subject`, `verbs` and `unanswered`.
2. With no such value, a non-interactive run, or one under `--yes`, fails with `CLI.CONSENT_REQUIRED`. Its next actions give one flag to pass per verb, safe to paste into a shell: a value with anything but plain characters is single-quoted, and a value starting with `-` is written `--<verb>=<value>`, and its `meta` carries `subject`, `verbs` and `unanswered`.
3. Otherwise the user is asked, and answers `<verb> <text>`, or `<verb>` alone to mean the subject. A verb with an arity above 1 takes its values from the text, separated by whitespace, and `text` is them joined by one space, as for a flag. A rejected answer is asked again on a terminal; with scripted or piped input it fails with `CLI.PROMPT_INVALID`.

`ctx.prompt.statements([...])` asks several questions at once and returns the answers in order. Flags answer what they can, a refusal lists every question still unanswered, and an interactive run asks the rest one after another.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/cli-engine",
"version": "0.10.1",
"version": "0.10.2",
"description": "The execution engine of the unified Prisma CLI.",
"type": "module",
"exports": {
Expand Down
2 changes: 0 additions & 2 deletions packages/cli-engine/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ export interface StatementOptions<V extends string> {

export interface StatementQuestion<V extends string>
extends StatementOptions<V> {
/** Shown as the prompt, and as the first line of the error when it is
* the only question nobody can answer. Name what it is about. */
readonly question: string;
}

Expand Down
24 changes: 7 additions & 17 deletions packages/cli-engine/src/execution/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ function answerFromFlags<V extends string>(
function strayFlagLines(
state: RunState,
subjects: readonly string[],
verbs: readonly string[],
): readonly string[] {
const unmatched = state.statementValues.filter(
(value) =>
Expand All @@ -168,11 +167,11 @@ function strayFlagLines(
return [
...unmatched.map(
(value) =>
`${flagForm(value.verb, value.values)} does not name ${subjects.join(" or ")}.`,
`${flagForm(value.verb, value.values)} answers no question; the questions are about ${subjects.join(", ")}.`,
),
...state.confirmValues.map(
(token) =>
`${flagForm("confirm", [token])} does not choose what happens; ${verbs.map((verb) => `--${verb}`).join(" or ")} does.`,
`${flagForm("confirm", [token])} answers no consent in this run; a statement flag is what answers these questions.`,
),
];
}
Expand All @@ -182,14 +181,9 @@ function statementUnavailable<V extends string>(
subjects: readonly string[],
state: RunState,
): CliStructuredError {
const situation = state.yes
? "--yes does not choose for you"
: "this run cannot ask you";
const [only, ...others] = unanswered;
const alone = others.length === 0;
const summary = alone
? only.question
: `${unanswered.length} choices are needed, and ${situation}.`;
const summary = state.yes
? "This command requires explicit consent, and --yes does not give it."
: "This command requires explicit consent, but it was not provided.";
const listed = unanswered.map(({ subject, verbs }) => ({ subject, verbs }));
const unmatched = state.statementValues
.filter(
Expand All @@ -200,12 +194,8 @@ function statementUnavailable<V extends string>(
.map(({ verb, values }) => ({ verb, values }));
return new CliStructuredError("CLI.CONSENT_REQUIRED", summary, {
why: [
...(alone
? [`You have to choose what happens, and ${situation}.`]
: unanswered.map((question) => question.question)),
...strayFlagLines(state, subjects, [
...new Set(unanswered.flatMap((question) => question.verbs)),
]),
...unanswered.map((question) => question.question),
...strayFlagLines(state, subjects),
].join("\n"),
nextActions: unanswered.flatMap((question) =>
question.verbs.map((verb) => ({
Expand Down
28 changes: 3 additions & 25 deletions packages/cli-engine/tests/consent-leftovers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("an unconsumed --confirm token", () => {
});
});

test("is reported with a statement refusal, which names the flags that choose", async () => {
test("is reported with a statement refusal, which says a statement flag answers it", async () => {
const result = await cliWith(askBoth).run([
"probe",
"--confirm",
Expand All @@ -109,32 +109,10 @@ describe("an unconsumed --confirm token", () => {

expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
why: "What happens to Legacy?\nWhat happens to User.nickname?\n--confirm mydb does not choose what happens; --rename or --delete does.",
why: "What happens to Legacy?\nWhat happens to User.nickname?\n--confirm mydb answers no consent in this run; a statement flag is what answers these questions.",
meta: { confirm: ["mydb"] },
});
});

test("names only the flags of the questions still unanswered", async () => {
const result = await cliWith((prompt) =>
prompt.statements([
question("Legacy"),
{ ...question("User.nickname"), verbs: ["delete"] },
]),
).run([
"probe",
"--rename",
"Legacy:Archive",
"--confirm",
"mydb",
"--json",
]);

expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
summary: "What happens to User.nickname?",
why: "You have to choose what happens, and this run cannot ask you.\n--confirm mydb does not choose what happens; --delete does.",
});
});
});

describe("a statement value that matches no question", () => {
Expand All @@ -148,7 +126,7 @@ describe("a statement value that matches no question", () => {

expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
why: "What happens to Legacy?\nWhat happens to User.nickname?\n--delete Legcy does not name Legacy or User.nickname.",
why: "What happens to Legacy?\nWhat happens to User.nickname?\n--delete Legcy answers no question; the questions are about Legacy, User.nickname.",
meta: { unmatched: [{ verb: "delete", values: ["Legcy"] }] },
});
});
Expand Down
70 changes: 12 additions & 58 deletions packages/cli-engine/tests/statement-prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,16 @@ describe("a verb flag answers the statement", () => {
});

describe("without a flag, a non-interactive run refuses", () => {
test("the refusal leads with the question and gives one flag form per verb", async () => {
test("the refusal names the subject and one flag form per verb", async () => {
const result = await cliWith(askLegacy).run(["probe", "--json"]);

expect(result.exitCode).toBe(2);
expect(errorOf(result)).toEqual({
code: "CLI.CONSENT_REQUIRED",
severity: "error",
summary: LEGACY_QUESTION,
why: "You have to choose what happens, and this run cannot ask you.",
summary:
"This command requires explicit consent, but it was not provided.",
why: LEGACY_QUESTION,
nextActions: [
{
kind: "user-choice",
Expand All @@ -213,19 +214,16 @@ describe("without a flag, a non-interactive run refuses", () => {
);

expect(result.exitCode).toBe(2);
expect(errorOf(result)).toMatchObject({
summary: LEGACY_QUESTION,
why: "You have to choose what happens, and --yes does not choose for you.",
});
expect(errorOf(result)?.summary).toBe(
"This command requires explicit consent, and --yes does not give it.",
);
});

test("the human refusal prints each flag form as a next action", async () => {
const result = await cliWith(askLegacy).run(["probe", "--format", "human"]);

expect(result.exitCode).toBe(2);
expect(result.stderr).toContain(
`[CLI.CONSENT_REQUIRED] ${LEGACY_QUESTION}`,
);
expect(result.stderr).toContain("[CLI.CONSENT_REQUIRED]");
expect(result.stderr).toContain(
"Run the command again with --rename 'Legacy:<new name>'",
);
Expand All @@ -235,38 +233,6 @@ describe("without a flag, a non-interactive run refuses", () => {
});
});

describe("a refusal of one question with a flag that answers nothing", () => {
test("says which subject a statement flag does not name", async () => {
const result = await cliWith(askLegacy).run([
"probe",
"--delete",
"Legcy",
"--json",
]);

expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
summary: LEGACY_QUESTION,
why: "You have to choose what happens, and this run cannot ask you.\n--delete Legcy does not name Legacy.",
});
});

test("says which flags choose when --confirm was given", async () => {
const result = await cliWith(askLegacy).run([
"probe",
"--confirm",
"mydb",
"--json",
]);

expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
summary: LEGACY_QUESTION,
why: "You have to choose what happens, and this run cannot ask you.\n--confirm mydb does not choose what happens; --rename or --delete does.",
});
});
});

describe("an interactive run asks", () => {
test("a bare verb answers with the subject", async () => {
const result = await cliWith(askLegacy).run(["probe"], {
Expand Down Expand Up @@ -346,8 +312,8 @@ describe("prompt.statements asks several questions together", () => {
expect(result.exitCode).toBe(2);
expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
summary: USER_NAME_QUESTION,
why: "You have to choose what happens, and this run cannot ask you.",
summary:
"This command requires explicit consent, but it was not provided.",
nextActions: [
{
kind: "user-choice",
Expand All @@ -364,27 +330,15 @@ describe("prompt.statements asks several questions together", () => {
});
});

test("--yes answers none of several questions", async () => {
const result = await cliWith(askBoth).run(
["probe", "--yes", "--json"],
INTERACTIVE,
);

expect(errorOf(result)).toMatchObject({
code: "CLI.CONSENT_REQUIRED",
summary: "2 choices are needed, and --yes does not choose for you.",
why: `${LEGACY_QUESTION}\n${USER_NAME_QUESTION}`,
});
});

test("one refusal lists every unanswered question", async () => {
const result = await cliWith(askBoth).run(["probe", "--json"]);

expect(result.exitCode).toBe(2);
expect(errorOf(result)).toEqual({
code: "CLI.CONSENT_REQUIRED",
severity: "error",
summary: "2 choices are needed, and this run cannot ask you.",
summary:
"This command requires explicit consent, but it was not provided.",
why: `${LEGACY_QUESTION}\n${USER_NAME_QUESTION}`,
nextActions: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"@manypkg/tools": "^2.1.2",
"@prisma/cli-engine": "workspace:0.10.1",
"@prisma/cli-engine": "workspace:0.10.2",
"@prisma/composer-cli": "0.39.0",
"@prisma/compute-sdk": "0.43.0",
"@prisma/management-api-sdk": "1.80.0",
Expand Down
14 changes: 8 additions & 6 deletions packages/cli/scripts/conformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,22 @@ async function tarball(): Promise<readonly Finding[]> {
{
familyPackage: "@prisma/composer-cli",
familyPin: "0.10.0",
shellPin: "0.10.1",
shellPin: "0.10.2",
channel: "dev",
reason: "engine 0.10.1 must publish before composer-cli can peer it",
reason:
"engine 0.10.2 changes the first line of the statement refusal; composer-cli still peers 0.10.0",
removeWhen:
"composer-cli releases peering 0.10.1 and the follow-up bump PR pins that release",
"composer-cli releases peering 0.10.2 and the follow-up bump PR pins that release",
},
{
familyPackage: "@prisma/orm-toolchain",
familyPin: "0.10.0",
shellPin: "0.10.1",
shellPin: "0.10.2",
channel: "dev",
reason: "engine 0.10.1 must publish before orm-toolchain can peer it",
reason:
"engine 0.10.2 changes the first line of the statement refusal; orm-toolchain still peers 0.10.0",
removeWhen:
"orm-toolchain releases peering 0.10.1 and the follow-up bump PR pins that release",
"orm-toolchain releases peering 0.10.2 and the follow-up bump PR pins that release",
},
],
channel: CHANNEL,
Expand Down
2 changes: 1 addition & 1 deletion packages/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"dependencies": {
"@manypkg/tools": "^2.1.2",
"@prisma/cli-engine": "workspace:0.10.1",
"@prisma/cli-engine": "workspace:0.10.2",
"@prisma/composer-cli": "0.39.0",
"@prisma/compute-sdk": "0.43.0",
"@prisma/management-api-sdk": "1.80.0",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading