Skip to content

fix(cli): exit lint with the refusal code and summarize a clean file - #642

Open
Kiran01bm wants to merge 1 commit into
mainfrom
kiran01bm/lint-exit-codes
Open

Kiran01bm wants to merge 1 commit into
mainfrom
kiran01bm/lint-exit-codes

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

pg-sprite lint now exits 2 when a script holds a statement pg-sprite would refuse, keeps exit 1 for a missing or unparsable file, and prints a summary for a clean file instead of nothing.

Why

lint checks a DDL script offline, so teams run it as a CI gate before a schema change reaches a database. It exited 1 both when a statement in the script was one pg-sprite would refuse and when the file was missing or not valid SQL, so a gate could not tell "fix your DDL" from "your job is broken", while every other command already exits 2 for a refusal. This changes the CLI exit-code contract for lint: a gate that checks for exactly 1 on a refused script must now check for 2 (gating on any non-zero status is unaffected). A clean file also printed nothing, which reads the same as a run that checked nothing.

What

  • Error-severity findings return verdict.ErrRefused (still wrapping ErrLintFindings), so lint exits 2 with the report and no extra error line; read, parse, and usage failures keep exit 1, matching migrate for a statement that does not parse.
  • A clean script, and an admitted lint --desired file, prints the lint: summary <file> — no findings; --json keeps printing the empty report.
  • lint --desired keeps exit 2 for a refused file.
  • README exit-code table, docs/cli-output-examples.md#exit-codes (plus refused and clean lint examples), docs/cli.md, docs/lint-report.md, and the reference doc's "Lint exit" column cover lint.
  • demo/tour.sh asserts exit 2 for lint risky.sql.

Before / after

Only the refused script's exit code and the clean file's output change; a missing file stays exit 1 with the error on stderr, and the refused report itself is unchanged.

                    BEFORE                                  AFTER
                    ──────                                  ─────
lint bad.sql        report: error[unsupported-operation]    report: error[unsupported-operation]
(one EXCLUDE        "lint: bad.sql — 1 finding, 1 error"    "lint: bad.sql — 1 finding, 1 error"
 constraint)        stderr: "lint found errors: 1"          stderr: (nothing; the report says why)
                    exit 1  ◀── same as a broken job        exit 2  ◀── the refusal code every
                                                                       command shares

lint nope.sql       stderr: usage + "no such file"          stderr: usage + "no such file"
(file missing)      exit 1                                  exit 1  (unchanged)

lint ok.sql         stdout: (nothing at all)                stdout: "lint:"
(one CREATE INDEX                                                   "  ok.sql — no findings"
 CONCURRENTLY)      exit 0                                  exit 0

lint exited 1 for both a statement pg-sprite would refuse and a missing or
unparsable file, so a CI gate could not tell a refused script from a broken
job. Error findings now exit 2 like every other refusal; a clean file says so.
@Kiran01bm
Kiran01bm marked this pull request as ready for review November 30, 2026 04:31
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-30T04:31:28.474703Z
We couldn’t complete this review. Comment @codex review to try again.
1c0dd9c Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@aparajon

Copy link
Copy Markdown
Collaborator

🤖 Review 1/2 (correctness) of 1c0dd9c: 0 blocking, 1 non-blocking.

Inspected the whole change at 1c0dd9c8074d6afccc3f6f92f8f02cbb22e1c17e: the refusal return (lint.go#L57-L59), the parse-error path that stays a plain error (lint.go#L44-L47), the clean summary in writeLintText (lint_text.go#L19-L25), the --desired text path (lint.go#L76-L82), the exit mapping it feeds (main.go#L79-L89), both test files, and the demo and doc edits. I also built the binary at the head and ran it against a refused, a warnings-only, a clean, a comment-only, an unparsable and a mixed (refused statement followed by a syntax error) script, with and without --json, and against --desired files.

The change does what it says, and the three properties a scripted caller leans on hold:

  • A refused lint never exits 0. report.Errors is derived from the findings themselves (lint.go#L220-L226), so a finding and a zero count cannot disagree. The report is written first and the refusal follows, so a failed report write ends in exit 1 and never in exit 0. exitStatus checks ErrRefused first, so the wrapped error maps to 2.
  • An input or parse error stays distinct. lint.Check returns the parse error before anything is rendered, so the file exits 1 with the error on stderr and nothing on stdout, under --json too. The mixed script, whose first statement is refused, exits 1 and reports no findings, which is the right reading for a job that could not finish checking. A missing path is rejected by the argument parser and exits 1.
  • The clean summary cannot appear next to findings or a failure. It is written only when the report has no findings, only after Check succeeded, and for --desired only after admission succeeded (lint.go#L77-L82). Run against the built binary, every refused or failed case prints no no findings line.

format_version stays 12 and that is right: the lint report's version covers the field shape and the closed vocabularies (lint-report.md#L10-L16), and the change does not touch the report encoding or lint.Check, so the JSON for a refused script, a clean script and a desired file keeps its shape (run against the binary: format_version: 12, findings: [] for the clean file). The new bad.sql and ok.sql examples in cli-output-examples.md match the binary's output exactly.

Invariants (docs/invariants.md): the change upholds RF-7 (no verdict.Refusal is built; the lint path only wraps the exit sentinel, so no class or owner is required of it) and upholds AB-5 (exit 3 stays accepted-blocking's alone). The closed exit mapping stated in the exitStatus comment still holds: no new code and no ExitCode() method, just one more producer of ErrRefused. No *Enforced:* line moves.

Non-blocking

  1. Nothing pins that a refused lint --desired run in text mode stays free of the clean summary. lintDesired now prints the clean summary on the admitted branch (lint.go#L80-L82), directly after the refusal branch (lint.go#L77-L79). The text-mode refusal cases in TestLintDesiredReportsTheAdmissionVerdict assert only ErrorIs(err, verdict.ErrRefused) and never look at out (lint_test.go#L334-L336); the JSON cases read the output, so they do not see a text-only regression. A change that printed lint: <file> — no findings after the refusal verdict of a desired file would exit 2 correctly and tell a reader of the log that the file was clean. That is the one combination the PR's stated purpose rules out (mutant M8 below survives every test I ran). The syntax-error subtest already asserts empty output, so the refused-by-admission cases are the gap.
Test case: a refused desired file in text mode does not print the clean summary
func TestLintDesiredRefusalDoesNotPrintTheCleanSummary(t *testing.T) {
	var out strings.Builder
	err := (&LintCmd{Desired: true}).runLint(strings.NewReader("CREATE DOMAIN account_email AS text;"), &out)
	require.ErrorIs(t, err, verdict.ErrRefused)
	assert.NotContains(t, out.String(), "no findings")
}

Passes on 1c0dd9c. Under mutant M8 (the refusal branch also calls writeLintText with the empty report in text mode) it fails: --- FAIL: TestLintDesiredRefusalDoesNotPrintTheCleanSummary (0.46s), with the output ending lint:\n <stdin> — no findings\n.

Verified

  • Ran only anchored unit tests with -timeout 300s, one per run. Before running them I checked for database use: the CLI test package has no TestMain, lint_test.go and main_test.go carry no build tag, and neither opens a connection (the one localhost:5432 in main_test.go is a flag value in a test I did not run). Passing at the head: TestLintExitCodes, TestLintCleanScriptPrintsTheCleanSummary, TestLintCleanScriptJSONIsTheEmptyReport, TestLintWarningsPassErrorsFail, TestLintParseFailureIsErrorNotFinding, TestLintDesiredReportsTheAdmissionVerdict, TestMainLintExitCodes, TestMainMapsARunErrorToExitOneWithTheErrorOnStderr. I ran no integration test.
  • CI at this head: test (PostgreSQL 16) is red on TestRunRefusesAFinishOnlyTargetOnceTheRowIsDone (save checkpoint ... canceling statement due to lock timeout), a test in pkg/schemachange that this PR does not touch and that does not call the lint command, so it reads as a flake. I did not rerun it; it needs a rerun before the merge, and all-green stays red until then.
  • Mutation table (each change applied alone, the named tests run, then git checkout):
Mutant (production change) Caught by
M1 refusal error wraps ErrRefused with %v (exit 1 instead of 2) TestLintExitCodes/refused, TestMainLintExitCodes/refused
M2 refusal error drops ErrLintFindings TestLintWarningsPassErrorsFail
M3 refuse on any finding, warnings included TestLintExitCodes/warnings_only
M4 threshold report.Errors > 1 (a single error exits 0) TestLintExitCodes/refused
M5 parse error wrapped with ErrRefused (exit 2) TestLintExitCodes/unparsable_script, TestMainMapsARunErrorToExitOneWithTheErrorOnStderr
M6 clean summary printed before returning a parse error TestLintParseFailureIsErrorNotFinding
M8 clean summary printed after a desired-file refusal, text mode survived every test I ran; killed by the test case above

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon

Copy link
Copy Markdown
Collaborator

🤖 Review 2/2 (OSS adoption and integration) of 1c0dd9c: 0 blocking, 3 non-blocking.

Read the change from two seats: a CI author who gates a pipeline on pg-sprite lint, and an importer such as SchemaBot that embeds the engine or shells out to the binary. On the library side nothing moves: ErrLintFindings and exitStatus live in the CLI package, which an importer cannot import, and pkg/lint and its report are untouched, so no Go caller sees a different API or a different format_version. What changes is the process surface (exit code, stdout, stderr), which is the surface a shell-level caller depends on. The direction is the right one: a gate can now branch on 2 for "this script would be refused" and on 1 for "the job is broken", exactly as it does for migrate, diff and pull, and the README table, cli.md, cli-output-examples.md, lint-report.md and the demo's exit-code assertions all say so.

Non-blocking

  1. A CI job that tests for exit 1 will stop gating, and the release notes will not say so. The release changelog is generated from commit subjects, with only docs:, test: and ci: filtered out (.goreleaser.yaml#L38-L44), so this change will appear as fix(cli): exit lint with the refusal code and summarize a clean file. The docs describe the new contract, but none of them says that a refused script used to exit 1. A script written against the old behavior (if [ $? -eq 1 ], or a case that maps 1 to "lint found problems") now reads a refused script as "no match" and, depending on its fallthrough, as success. Scripts that gate on any non-zero status are unaffected, which is what the docs recommend. A line in the squash commit body (for example "lint now exits 2 for error findings; 1 is for a missing or unparsable file") or a fix(cli)!: subject would carry it into the changelog, and one sentence in the cli.md paragraph that says "earlier releases exited 1" would tell a reader of the docs. The stderr line pg-sprite: error: lint found errors: N is also gone for a refused script (the report on stdout carries the count in its summary), which matters to a job that greps stderr for it.

  2. A clean text run now prints two lines where it printed none, and a comment-only file reports no findings too. The documented contract for a machine reader is the exit code and --json, and both are stable, so this is a courtesy note: a caller that tests [ -z "$(pg-sprite lint f.sql)" ] for "clean", or that counts output lines, flips with this change (lint_text.go#L19-L25). Separately, a file with no statements at all (verified with a script holding only a comment) prints <file> — no findings and exits 0, the same line a script of ten checked statements prints. The PR's stated aim is that a run that checked something never looks like one that did nothing, and an empty or truncated file in a CI artifact is the case where that matters most. Naming how many statements were checked, or saying no statements for an empty file, would keep the summary honest there. The text report is documented as not a machine surface, so this is polish and not a contract break.

  3. Two statements the PR leaves out of step with its own change.

    • postgres-online-ddl-reference.md#L963: the validate-in-transaction row now reads lint warning ... | 2 in a "Lint exit" column, while the severities table says warnings alone exit zero (lint-report.md#L141). The 2 is true for a realistic script only because the BEGIN and COMMIT that open and close the block are themselves reported as unsupported-operation errors (verified with the built binary: a block with a NOT VALID constraint and its VALIDATE CONSTRAINT reports 2 errors and 1 warning and exits 2). A reader who copies the row into a gate will believe the warning gates on its own. Saying so in the cell, or writing 0 on its own for the finding, would keep the two tables consistent.
    • verdict.go#L32-L34: ErrRefused is documented as "the sentinel the CLI returns after printing a refusal verdict". A refused lint script now returns it after printing a lint report, not a verdict, so the comment on this exported sentinel no longer covers every producer. Widening it to "after printing a refusal (a verdict, or the lint report's error findings)" keeps the one place an importer reads the sentinel's meaning accurate.

What reads well for adopters

  • The exit mapping stays closed and public (0, 1, 2, 3): lint reuses ErrRefused instead of adding a code, so a gate written for migrate works for lint unchanged.
  • The refusal prints no pg-sprite: error: line, so stderr stays empty for a script that was checked and refused, and stderr output means a broken job. That split is what lets a CI step annotate the two cases differently.
  • The new docs examples (bad.sql refused, ok.sql clean) match the binary's output character for character, and demo/tour.sh asserts the new code on --json, as the demo rule asks.

This review was generated by Claude Code (claude-sonnet-5-5).

@aparajon aparajon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Stamping with comments: 0 blocking, 4 non-blocking (see the review comments above).

This stamp was left by Claude Code (claude-sonnet-5-5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants