Check code with builder validate
builder validate answers one question: is the code in the workspace aligned with its schematics?
It runs a schematic in a Dry Run, so it never writes to the workspace, and it reports the result
as an exit code. That makes it a check you can put in CI.
It works with two kinds of schematic:
- An evaluation schematic reads the workspace and returns findings.
builder validateprints each one. This page is mostly about this kind. - A generator gets a drift check:
builder validatereports what the generator would change. An empty Plan means the code is aligned.
Requirements
Section titled “Requirements”You need the builder CLI v0.19.12 and @pbuilder/sdk 0.22.0. Every output below comes from a
run with exactly those two versions in a workspace created with
builder init --package-manager npm.
Write an evaluation schematic
Section titled “Write an evaluation schematic”This section is the short version. Write an evaluation schematic covers the generated types, every field of a finding, invoking an evaluation from another schematic and the current limits.
Scaffold the schematic as usual:
builder new schematic check-hello --title "Check hello" --job "check the hello file"From CLI v0.21.0 --title and --job are required, and the CLI ignores them once the entry
declares another role. builder new schematic has no --role flag yet (cli#269), so mark it as an
evaluation in project-builder.json by adding "role": "evaluation" to its entry:
"collections": { "default": { "check-hello": { "path": "./schematics/check-hello", "role": "evaluation" } }}The factory reads files and returns { items }, a list of findings. This one checks that
hello.txt exists and says hello, and warns about a TODO in docs/notes.txt:
import { find } from "@pbuilder/sdk";import type { Input } from "./schema.generated.ts";
export default async (input: Input) => { const items = [];
const hello = await find("hello.txt").read(); if (hello === undefined) { items.push({ rule: "hello-exists", message: "hello.txt is missing", severity: "error" }); } else if (!hello.includes("hello")) { items.push({ rule: "hello-content", message: "hello.txt must say hello", severity: "error", path: "hello.txt", fix: { schematic: "default:greet" }, }); }
const notes = await find("docs/notes.txt").read(); if (notes?.includes("TODO")) { items.push({ rule: "no-todo", message: "docs/notes.txt has a TODO", severity: "warning", path: "docs/notes.txt", }); }
return { items };};A finding has a rule, a message and a severity, which is error or warning. It can also
have a path and a fix. The fix names the schematic that would fix the finding as
{ "schematic": "collection:name" }. builder validate prints it as given: default:greet in this example is never created. From CLI v0.20.1, a fix that names a schematic the Manifest does not have also adds the warning warn_fix_schematic_unresolved (a finding's fix names the schematic "default:greet", which does not resolve in the Manifest), and the finding is still reported unchanged. The SDK sets source to the schematic’s qualified name, so
the factory does not set it.
The other outputs on this page come from v0.19.12, so they do not show the warning. On v0.20.1, with no
default:greet in the Manifest, the warning prints on standard output before the findings:
dry run: nothing is written to the workspace✓ done — no changesa finding's fix names the schematic "default:greet", which does not resolve in the Manifestwarning no-todo docs/notes.txt — docs/notes.txt has a TODO [default:check-hello]error hello-content hello.txt — hello.txt must say hello (fix: default:greet) [default:check-hello]With --output=json, it is one more entry in warnings:
{"code":"warn_fix_schematic_unresolved","message":"a finding's fix names the schematic \"default:greet\", which does not resolve in the Manifest"}.
The exit code is still 6. Once the Manifest has a greet schematic in default, the warning is gone.
The role also gives the factory two inputs it does not declare in schema.json: include and
exclude. See Narrow what is checked.
An evaluation schematic never changes the workspace, and builder execute refuses it:
builder execute default:check-helloIt exits with code 2 and the error execute_schematic_not_executable (reason
schematic_role_evaluation). Run it with builder validate.
Run it
Section titled “Run it”builder validate default:check-helloWithout --output, the format is detected from the terminal. When standard output is a pipe or a
file, you get JSON. Pass --output=pretty or --output=json to choose one.
Aligned
Section titled “Aligned”With hello.txt saying hello and no TODO in the notes, the pretty output is:
dry run: nothing is written to the workspace✓ done — no changespassed: 0 finding(s), none with the severity errorThe command exits with code 0. The JSON output is:
{"result":{"role":"evaluation","findings":[]},"warnings":[{"code":"warn_dry_run","message":"dry run: nothing is written to the workspace"}]}Warnings only
Section titled “Warnings only”With a TODO in docs/notes.txt and a good hello.txt, the check still passes and exits with
code 0. Warnings are findings, but they do not fail the check:
dry run: nothing is written to the workspace✓ done — no changeswarning no-todo docs/notes.txt — docs/notes.txt has a TODO [default:check-hello]passed: 1 finding(s), none with the severity errorAn error finding
Section titled “An error finding”With hello.txt containing nope, the check fails. The findings go to standard output, and the
summary goes to standard error:
dry run: nothing is written to the workspace✓ done — no changeswarning no-todo docs/notes.txt — docs/notes.txt has a TODO [default:check-hello]error hello-content hello.txt — hello.txt must say hello (fix: default:greet) [default:check-hello]✗ [CLI] the evaluation reported 1 finding(s) with the severity error validate_findings a finding with a fix names the schematic that would fix itThe command exits with code 6. Each line reads: severity, rule, path, message, the fix when there
is one, and the source in brackets. Findings are sorted by path, which is why the warning for
docs/notes.txt comes before the error for hello.txt.
A finding without a path prints without one. With hello.txt missing, the line is:
error hello-exists — hello.txt is missing [default:check-hello]The JSON output
Section titled “The JSON output”With --output=json, standard output holds one {result, error, warnings} envelope. For an
evaluation, result is { role, findings }. A warning-severity finding is one of findings; the
envelope’s warnings hold only the CLI’s own warnings, such as warn_dry_run.
{"result":{"role":"evaluation","findings":[{"rule":"no-todo","message":"docs/notes.txt has a TODO","severity":"warning","path":"docs/notes.txt","source":"default:check-hello"},{"rule":"hello-content","message":"hello.txt must say hello","severity":"error","path":"hello.txt","fix":{"schematic":"default:greet"},"source":"default:check-hello"}]},"warnings":[{"code":"warn_dry_run","message":"dry run: nothing is written to the workspace"}]}The ✓ done — no changes line goes to standard error in this mode, so standard output stays one
JSON document.
Run every evaluation
Section titled “Run every evaluation”Without a positional, builder validate runs every evaluation schematic of the Manifest and
reports their findings together. Generators in the Manifest are not run:
builder validateOn the workspace above, it prints the same findings and exits with code 6. In a workspace with no
evaluation schematic, it exits with code 0 and {"result":{"role":"evaluation","findings":[]},"warnings":[]}.
From CLI v0.20.1, an evaluation that fails while running, for example because it returns
nothing, does not hide the others. result holds the findings of every evaluation that ran, and
error is the typed error of the first one that could not run, by qualified name, with every
failed evaluation and its own error under error.evaluations. The message names them:
✗ [ENGINE] 2 evaluations could not run: "default:silent", "default:sourced" engine_native_developer_fault · factory at schematics/silent failed: invalid Result: Result is undefinedWith one failure it reads the evaluation "default:silent" could not run: sidecar runner exited with code 1.
The outcome is an error even when a finding has the severity error, because an incomplete check
gives no verdict: the exit code is the family of that first error (5 here), never 6. With CLI
v0.20.0 and earlier, the failure hid the other findings and result was empty.
Narrow what is checked
Section titled “Narrow what is checked”Every evaluation schematic takes --include and --exclude, each a glob. The scope is what
include names, less what exclude names. A finding whose path falls outside the scope is
dropped from the report. With --include='docs/**', only the docs/notes.txt warning remains and
the command exits with code 0:
builder validate --output=json default:check-hello '--include=docs/**'With --exclude=hello.txt, the hello.txt error is dropped the same way and the exit code is 0.
Quote a glob so the shell does not expand it.
To pass several globs, repeat the flag. The CLI applies every value. With --exclude=hello.txt and
--exclude='docs/**' together, both findings are dropped and the result is
"findings":[].
Check a generator for drift
Section titled “Check a generator for drift”For a generator, builder validate runs a Dry Run and reports its Plan. A generator that would
create made.txt, with the file missing:
builder validate --output=pretty default:make-hellodry run: nothing is written to the workspacecreate (1) create made.txt✓ done — 1 create✗ [CLI] drift: the generator's Dry Run plans 1 operation(s) validate_drift the Plan is the fix: run the generator with builder execute and the same inputs to apply itThe command exits with code 6, and the file is not created. In JSON, result is
{"role":"generator","plan":[{"class":"create","path":"made.txt"}]}. After made.txt exists, the
Plan is empty, result is {"role":"generator","plan":[]}, and the exit code is 0. The
generator’s inputs follow the positional as flags, as in builder execute.
Exit codes
Section titled “Exit codes”| Exit code | When |
|---|---|
| 0 | An evaluation has no finding with the severity error, or a generator’s Plan is empty. |
| 6 | An evaluation has at least one finding with the severity error (validate_findings), or a generator’s Plan is not empty (validate_drift). |
| 2 | A usage error: an unknown schematic or collection, or --commit=always. |
| 5 | The schematic failed while running. |
When the check cannot run, builder validate exits with the code of the error and never with 6.
Errors you may see
Section titled “Errors you may see”With --output=json, a check that cannot run puts a typed error in error and leaves result
empty. The exception is builder validate with no positional on CLI v0.20.1 or later: result still
holds the findings of the evaluations that ran (see Run every evaluation).
| Error code | Exit | Cause | Fix |
|---|---|---|---|
execute_schematic_not_found |
2 | The schematic is not in the collection: schematic "nope" not found in collection "default" |
Use a name from suggestions |
execute_collection_not_found |
2 | The collection is not in project-builder.json: collection "nocoll" not found in project-builder.json |
Use a collection the Manifest lists |
invalid_input |
2 | --commit=always: builder validate never commits: --commit accepts only never |
Drop the flag. builder validate never commits |
execute_schematic_not_executable |
2 | builder execute on an evaluation schematic: an evaluation schematic cannot be executed |
Run it with builder validate |
engine_native_developer_fault |
5 | The factory returned a finding the Findings shape rejects, for example invalid Result: Result.items[0].path is undefined |
Omit path when a finding has none, rather than setting it to undefined |
execute_read_ceiling_exceeded |
5 | CLI v0.26.0 with @pbuilder/sdk 0.26.0 or later. The factory read a file over the 8 MiB read cap and did not catch ReadTooLargeError: the schematic read the file "big.txt", 9000051 bytes, over the read ceiling of 8388608 bytes, and did not catch the refusal. Older pairs report engine_native_developer_fault |
Catch ReadTooLargeError and skip or report the file; see When nothing catches the read |
Use it in CI
Section titled “Use it in CI”The exit code carries the verdict, so a CI step needs nothing more than the command. A job fails on exit code 6:
builder validateTo report findings, read the JSON from standard output. Piped output is JSON without
--output=json. This command lists the rules of the error findings:
builder validate 2>/dev/null | jq -c '[.result.findings[] | select(.severity=="error") | .rule]'On the dirty workspace above it prints ["hello-content"], and builder validate itself exits
with code 6 even when its output goes through a pipe.