Skip to content

CLI exit codes and JSON

Audience: CI authors and anyone scripting the strukts CLI · about 10 minutes · Prerequisites: The strukts CLI.

What it is

Every strukts run ends with an exit code (what your build tool looks at) and prints one JSON document on standard output (what your scripts parse). Progress lines such as [strukts] check hello (3/56) go to standard error and never mix into the JSON, so strukts check --all > report.json always yields valid JSON.

Exit codes

Code Name When it happens
0 OK The verb finished and nothing it checks for went wrong. Also help, list and info. Skipped projects in a sweep do not make a run fail
1 Internal or environment error A project folder or model failed to load, a model's language could not be resolved or loaded, a project has its own *.lang.js and --trust-project-code was not given, no test language is registered, or the launcher itself crashed
2 Usage error Unknown verb, missing target (check requires --project <id>, --all, or --project-dir <dir>), unknown project id, --fail-on other than error or warning, --timeout that is not a positive number, --format other than json or matcher (or matcher with a verb other than check and verify), or --project-dir given with no folder
3 check found errors check (or the check step of package) found at least one error; with --fail-on warning, at least one warning
4 Generation or packaging failed lionweb export exported no model at all (no namespace, no roots), or generate or package hit a real failure (a generator threw, a root did not parse, a language has no output extension), or you asked for one project by name and it had nothing to generate (it is marked "generatable": false, or it is a plan language and you did not pass --plans)
5 Tests failed test ran at least one failing assertion, or a model's test run was cancelled by --timeout
6 Migration needed or refused migrate found a root saved by a newer language version than installed, or a migration step failed, or (with --check) a root is still pending
8 A run failed run executed a program that raised an exception, exited non-zero or timed out, or the chosen Python (--python host) was not found

When one run has several kinds of problem, the first applicable code wins, in this order: 1, then 3, then 4, then 5. So a sweep with one unloadable project and several check findings exits 1.

verify runs all three parts and exits with the code of the first failing one in the order a person fixes them: 3 (check errors), then 5 (failing tests), then 4 (generator failure); 1 and 2 as above.

The codes are the same for the standalone bundle and for npm run strukts.

Reproducing each code

These were produced with the repository's own samples:

Command Exit code
strukts check --project hello 0
strukts check --project-dir /nonexistent/folder 1 (a STRUKT-LOAD error)
strukts check --project-dir <folder with a *.lang.js> 1 (STRUKT-LANG-UNTRUSTED)
strukts check --project nope 2
strukts check 2
strukts check --project hello --fail-on bogus 2
strukts check --project python-tour 3
strukts package --project python-tour 3 (nothing is packaged)
strukts generate --project ruff-lint-demo 4 (a not-generatable project, asked for by name)
strukts test --project-dir <decision-table copy with a wrong expected value> 5

The JSON shape

One project (--project or --project-dir)

The document is a single result. These fields appear in every result:

Field Meaning
project The project id (the folder name; for a folder called model, its parent's name)
status One of ok, findings, failed, skipped, error (below)
ok true when the run should be treated as a success, which is also what exit code 0 means

The remaining fields depend on the verb.

check adds diagnostics and summary:

{
  "project": "python-tour",
  "status": "findings",
  "ok": false,
  "diagnostics": [
    {
      "code": "STRUKT-CHK-no-literal-zero-divisor",
      "severity": "error",
      "message": "Division by zero: the divisor is the literal 0",
      "explain": {
        "reason": "The divisor is the number 0, and dividing by zero is an error.",
        "hint": "Use a divisor that cannot be zero.",
        "templates": { "reason": "The divisor is the number 0, and dividing by zero is an error.", "hint": "Use a divisor that cannot be zero." }
      },
      "model": "python-tour",
      "root": "python-tour.model.py",
      "file": "model/python-tour.model.py",
      "nodeId": "3207",
      "location": { "line": 5, "column": 5 }
    }
  ],
  "summary": { "errors": 8, "warnings": 9 }
}

A diagnostic has code, severity (error or warning), message, and usually model, root, file (the root's path inside the project) and location (1-based line and column). A finding whose rule has put it in plain words also has explain: a reason and usually a hint, in English, with their wording templates (templates, holes in {braces} and the vars that fill them) for a tool that translates. message is always the checker's own text and does not change; read it for logs and scripts, show explain to a person. The codes you will meet:

Code Meaning
STRUKT-CHK-<rule name> A named check or constraint rule fired; the part after the prefix is the rule's name
STRUKT-CHK-GENERIC A check with no rule name fired (for example "Unreachable code")
STRUKT-PARSE A root did not parse, or parsed with a syntax error (the unparseable part is kept as an error node)
STRUKT-LOAD A project or model file could not be read
STRUKT-CHK-CRASH A check threw an exception while running; the message says what it threw
STRUKT-LANG-* A model's language could not be loaded or resolved

nodeId is a runtime-assigned number and changes from run to run; use model + root + location to recognise the same finding in two runs.

generate adds artifacts (one per file written), and optionally skipped, problems and reason:

{
  "project": "multi-root-demo",
  "status": "ok",
  "ok": true,
  "artifacts": [
    {
      "path": "build/generated/multi-root-demo/multi-root-demo.py",
      "file": "multi-root-demo/multi-root-demo.py",
      "model": "multi-root-demo",
      "root": "multi-root-demo.model.py",
      "generator": "text",
      "sha256": "839e7d5fe7cc9f00e84569162a95c8cb32e12c55955735841723d1ec73696360",
      "bytes": 182
    }
  ]
}

generator is text, plan (a .plan.kdl file from --plans) or html-preview (a self-contained HTML document). An artifact may also carry warnings, the generator's own reports. problems lists what went wrong when status is failed; skipped lists models left out, each with a reason.

test adds summary, models and, if nothing ran, reason:

{
  "project": "fp",
  "status": "failed",
  "ok": false,
  "summary": { "passed": 1, "failed": 1 },
  "models": [
    {
      "model": "fp",
      "status": "failed",
      "passed": 1,
      "failed": 1,
      "failures": [
        { "suite": "constraints showcase", "test": "sum 1..5 evaluates to 15 — AssertEvaluatesTo", "message": "Expected 16 but got 15" }
      ]
    }
  ]
}

Each model has its own status (ok, failed, skipped, or error). A failure that needs a DOM which the bundle does not have is marked "unsupported": true; with --skip-unsupported those are counted separately in unsupported and do not fail the run. A run stopped by --timeout shows a problems entry saying cancelled after <n> ms (--timeout).

package returns a package and manifest path and a summary of { files, errors, warnings } on success. On failure it returns the same shape as the step that failed: status: "findings" with the diagnostics (and the reason check found errors; nothing was packaged (use --allow-findings to package anyway)), or the generate failure.

Errors (status error) carry an error object with a code and message; for example STRUKT-LANG-UNTRUSTED or STRUKT-TEST-NO-RUNNER. Usage errors (exit 2) and unexpected failures use a short form: { "ok": false, "error": "<message>" }.

What each status means

Status Meaning Usual exit code
ok Everything the verb checks passed 0
findings check (or package's check) found errors 3
failed generate, package or test failed 4 or 5
skipped There was nothing to do (not generatable, plan language without --plans, no test suites). In a sweep this counts as fine; for generate or package of one named project it exits 4 0 (or 4)
error The project could not be loaded or run at all 1

A sweep (--all)

With --all the document wraps the per-project results and adds a tally. A real run of check --all:

{
  "ok": false,
  "summary": { "projects": 56, "findings": 4, "ok": 52 },
  "projects": [ { "project": "python-tour", "status": "findings", "ok": false, "diagnostics": [ ] } ]
}

summary counts projects by status (only statuses that occurred appear), and projects holds the same per-project results described above. Some shipped samples are deliberately illustrative and contain findings, so a whole-checkout check --all exits 3 by design.

list and info

list returns { "schemaVersion", "cliVersion", "projects": [...] }. Each project has id, kind (sample or plugin), path, name, description and a model object (fqn, path, imports, roots, generatable). info returns the fields shown in The strukts CLI. Both always exit 0.

Using the output in a script

Read the exit code first, the JSON second:

node dist/strukts-cli/strukts.mjs check --project-dir ./my-project > check.json
status=$?
if [ "$status" -eq 3 ]; then
  echo "model has check errors, see check.json"
elif [ "$status" -ne 0 ]; then
  echo "strukts could not run (exit $status)"
fi

Extract only the errors with jq (not required, just convenient):

jq -r '.diagnostics[] | select(.severity=="error") | "\(.root):\(.location.line // 0): \(.message)"' check.json

For a project you pointed to with --project-dir the result is a single document, so .diagnostics is at the top. For a sweep, use .projects[].diagnostics[].

Gotchas

  • A skipped project is not a failure in a sweep but is one when you name it with generate or package. Use --all for "everything that can be generated", --project for "this one must produce output".
  • The JSON is pretty-printed with two-space indentation and may gain fields over time. Read fields by name and ignore ones you do not know. list carries a schemaVersion for that reason.
  • Output paths in artifacts[].path are relative to the directory you ran the command in.
  • The progress lines on standard error are for humans; do not parse them.