dx check
Use dx check to find and fix generated-file drift, formatting, lint and type errors in one run before you push.
dx check --affected
dx check --affected --coverage --report lcov=lcov.info
dx check --apply --affected
dx check --apply //pkg/...
dx check --diff //pkg/...
dx check --output json --report sarif=out.sarif //pkg/...
dx check runs dx generate, dx format, dx lint and dx typecheck in that order. By default it only reports. --apply writes the fixes, then checks again without writing.
Usage
dx check [--here] [--coverage] [--affected [--since <ref>|--base <ref>] [--lint-scope direct|closure]] [--no-retry] [--apply] [--diff] [--unsafe] [--include-edits] [--max-findings <n>] [--fail-on info|warning|error] [--report sarif|lcov|sources=<dest>] [scope...] [-- bazel-options...]
Args after -- go to Bazel.
Flags
| Flag | What it does |
|---|---|
--apply | Write fixes, then verify with a read-only check. |
--diff | Print the edits as a patch. |
--unsafe | With --apply or --diff, also include fixes the tools mark unsafe. Without either, exits 2. The read-only check after --apply runs without it. |
--here | Use the current directory tree: //path/..., or //... at the workspace root. |
--affected | Use only the targets your git changes reach, and run only their tests in the last phase. |
--coverage | With --affected or the whole //... scope, run the tests that take coverage under coverage. With a narrower scope, --here or --apply, exits 2. |
--since <ref> | With --affected: compare against this git ref instead of the merge-base with the default branch. Without --affected, exits 2. |
--base <ref> | With --affected: take the merge-base with this branch instead of the default branch. Not with --since. |
--lint-scope direct|closure | With --affected: the lint phase adds the direct dependents (direct, the default) or every dependent (closure). Without --affected, exits 2. |
--fail-on info|warning|error | Fail on findings of this severity or worse. Applies to format, lint and typecheck. |
--include-edits | Add full edits to JSON change records. Needs --output json. |
--max-findings <n> | Print at most n findings per tool in JSON output. Default 50. 0 means no limit. Needs --output json. |
--report sarif=<dest> | Merge the lint and typecheck findings into one SARIF file. Repeatable. - (stdout) is rejected. |
--report lcov=<dest> | With --coverage, write the lcov report of the coverage phase. Without --coverage, exits 2. |
--report sources=<dest> | With --coverage, write the coverage sources report of the coverage phase. Without --coverage, exits 2. |
-o, --output text|json | Choose the output format. |
--workspace <dir> | Use this workspace directory. |
-q, --quiet | Hide summaries. |
-v, --verbose | Show more logs. |
--color auto|always|never | Set color output. |
--build-slots <auto|n|off> | Set build slots: auto, a count, or off. |
--build-slots-wait <duration> | Give up waiting for a build slot after this long. 0 fails at once. |
--bazel-startup-option <token> | Pass one Bazel startup option. Repeatable. |
Scopes
A scope picks what to check. Pass a label such as //pkg:target (one Bazel target), a pattern such as //pkg/... (every target in pkg and below), or a workspace file or directory. With no scope, dx checks //..., every target in the workspace.
--hereuses the current directory tree.--affecteduses the targets changed since the merge-base with the default branch (or with--base <ref>), or since--since <ref>, plus the targets that depend on them. The format phase uses only the targets that read a changed file. The lint phase adds the targets that depend on them directly. It adds every target that depends on them with--lint-scope closureor when a dependency version changes. The format, lint and typecheck phases leave outfilegroup,genquery,genrule,starlark_doc_extractandtest_suitetargets, which report no findings. A format, lint or typecheck phase with no affected targets printsno affected targetsand runs nothing.--heredoes not combine with scopes or--affected.--affectedwith scopes keeps only the affected targets inside them.- With nothing affected, dx prints
no affected targets, runs no phase, and exits0. With--diff, that line goes to stderr and stdout stays empty. - The generate phase writes only the
BUILDfiles of packages in scope. A file or//pkg:targetis its own package.//pkg/...or a directory is the tree under it.--affectedis the directories of the changed files, without those.bazelignoreor a# gazelle:excludedirective leaves out, unless the change is in a lock set’s directory, whose lock is still checked; when none is left, the phase is skipped. - With
--affected, the other phases write only the changed files and theBUILDfile of each one’s package. A fix to any other file is refused asoutside_scope.
Read Scope defaults for details.
Phases
- Every phase runs, even after an earlier phase reports findings. The run still exits nonzero.
generatecompares each lock that a rules_dx lock step writes in a package the scope reaches with its manifest, offline:<ecosystem>.lock.json,pubspec.lockand a dx-writtenmaven_install.json. Amaven_install.jsonthat rules_jvm_external pins is skipped. Apubspec.lockormaven_install.jsonwithout the dx marker is stale until onedx generate --apply. A stale lock printsdx: lock of <set> is stale; run dx generate --applyand fails the run. When the scope reaches the workspace root,generatealso runsbazel mod deps --lockfile_mode=updateand comparesMODULE.bazel.lockwith the result. A staleMODULE.bazel.lockprintsdx: MODULE.bazel.lock is stale; run dx generate --apply, fails the run, and is left as it was.--applywrites the refreshed lock. With a currentMODULE.bazel.lock,generatethen runsbazel mod tidy: without--applyaMODULE.bazelthat it would rewrite printsdx: MODULE.bazel is not tidy: bazel mod tidy rewrites it; run dx generate --apply, fails the run, and is restored;--applykeeps the tidied file and then refreshes the lock. Last,generaterunsbazel mod deps --lockfile_mode=error --repo_env=CARGO_BAZEL_DIGEST_CHECK=1, which fails the run when acargo-bazel-lock.jsonno longer matches its inputs; repin it withCARGO_BAZEL_REPIN=1. A lock step that cannot run on your platform is skipped withdx: lock of <set> not checked: <step> is incompatible with this platform.- Without
--apply,format,lintandtypecheckshare one Bazel build, also with--affected. Each phase still reports only its own targets, findings and exit code. - The run stops when the scope is bad,
generatefails, Bazel cannot run, or you cancel. - With
--apply, the run also stops when a phase fails to compile. The remaining phases are skipped. - With
--apply, dx runsgenerateagain aftertypecheckwhen a phase wrote a file. - A check of
//...ends with atestphase that runs every test.--affectednarrows it to the affected tests. A check of a narrower scope has notestphase.--applyskips it. When the changes select//...,dx.toml [affected] whole_repo_testsreplaces those tests and skips thecoveragephase. See Scope defaults. - With
--coverage, acoveragephase runs each affected test, or with//...every test, that takes coverage once, under coverage. Thetestphase runs only the tests taggedno-coverage,dx-no-coverage-collector,manualorresources:nested_bazel:1. - The
coveragephase applies the minimums indx.toml [coverage], the same gatedx coverageapplies. The changed-line minimum compares against the same base as--affected. See Coverage minimums. - A
testorcoveragephase with no tests is skipped. - The
testandcoveragephases print a failed test as soon as it finishes. They printdx: <phase>: <finished>/<total> tests finished, <failed> failed, <elapsed>on stderr every 60 seconds. - When you cancel a
testorcoveragephase, dx prints the count line anddx: <phase>: not finished: <labels>on stderr. - A failed run ends with a
dx: phases:line on stderr.
Commit messages
Set this in dx.toml to lint commit subjects:
[check]
commits = "conventional"
Then dx check --affected checks every commit subject since the merge-base. Each subject that is not a conventional commit is a warning with its sha, and the run exits 1. Merge commits are skipped. The check runs even when nothing is affected. It does not run under --apply. Without the key, it is off.
Released patch fixes
In a workspace with patches/upstream.py, as in rules_dx itself, dx check --affected reads the Upstream: line of each patch of a single_version_override whose version changed since the merge-base. It runs //patches:upstream for them. A patch whose upstream fixes are all in a release at or below the new version is a warning, and the run exits 1. When //patches:upstream cannot run, dx prints a warning and goes on. It does not run under --apply.
Build output check
Before any phase, dx check fails when your change adds build output or a large file:
- a new file under
target/,node_modules/,__pycache__/,.gradle/, or a top-levelbazel-*directory; - a new file under a
build/directory that has noBUILD,BUILD.bazel,pom.xml,CMakeLists.txtorbuild.gradlein it; - a changed file over 1 MiB. Lock files such as
MODULE.bazel.lockare exempt.
To keep such a path, list it in dx.toml. An entry ending in / keeps everything under that directory:
[large_files]
allow = ["assets/video.mp4", "testdata/big/"]
Outside a Git checkout, this check is skipped.
Output
Text prints one line per finding, such as src/lib.rs:12:5: warning: indexing may panic [clippy/clippy::indexing_slicing]: the path, line, column, severity, message, and [tool/rule]. Coding agents get JSON by default; pass --output text to see these lines.
Text also prints N file(s) need changes. Apply them with: dx check --apply ... when a phase finds changes. It ends with dx: <total> actions, <executed> executed, <cached> cached on stderr. <executed> counts processes Bazel ran, locally or remotely.
JSON has one command_started and one command_finished event. Each phase sits between phase_started and phase_finished. Its events carry phase and a correlation such as check/format. The verify phases after --apply use verify/<phase>. JSON streams events while the command runs.
Reports
--report sarif=<dest> writes one SARIF file with the lint and typecheck findings. Results carry partialFingerprints, ruleIndex and fixes. A phase that ran but left no readable SARIF is a collection_failed error and marks the report incomplete.
--report lcov=<dest> and --report sources=<dest> write the reports of the coverage phase, as in dx coverage. Without a coverage phase, they are not written.
Exit codes
| Code | Meaning |
|---|---|
0 | Success. |
1 | A phase failed or found changes, a capture was lost, verification failed, a commit subject failed, or a report could not be written. |
2 | Usage or scope error. |
| Bazel’s code | The phase that stopped the run keeps Bazel’s code. |
The test and coverage phases keep Bazel’s code:
3with complete results means tests failed.3or1with incomplete results means the phase is incomplete.2and4mean the scope selected no runnable tests.