Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

dx generate

bazel run @rules_dx//:dx -- generate
bazel run @rules_dx//:dx -- generate --apply //...
bazel run @rules_dx//:dx -- generate --here
bazel run @rules_dx//:dx -- generate --output json //pkg/...
bazel run @rules_dx//:dx -- generate --apply //... -- --jobs=4

Updates BUILD declarations from source with Gazelle over the scope. To materialize declared generated source, use dx codegen. No scope means the whole repo. Use --here for the current dir tree. Checks by default and reports stale files without writing. Pass --apply to write them. Bazel options go after --.

dx generate [--here] [--check] [--apply] [scope...] [-- bazel-options...]
  • --check: fail if files are stale instead of writing them. Same as the default.
  • --apply: write the updated BUILD files. Without it files stay untouched.
  • --fail-on, --report, --affected: rejected as usage errors (exit 2).
  • --output text|diff|json: result shape. diff prints one unified patch per stale file, or per file --apply wrote or did not apply.

Exit codes: 0 success, 2 usage or scope errors, 1 stale files with --check or the default, a file --apply did not write, incomplete results, a missing or invalid generation result (invalid_result), or a patch that fails to render (diff_failed). Bazel failures keep Bazel's code.

Output

Text starts with Running generate for <scope>. --quiet hides it. Then it prints Created <path> or Modified <path> for each written file, or each stale file with --check, sorted by path. Each ignored import prints Ignored import '<import>' in <path> [<language>]: Static import intentionally contributes no Bazel dependency. Files that were not written go to stderr as Not applied: <path> (<reason>). An incomplete --apply run still prints the files it wrote and the Not applied lines. An incomplete --check run, or a Bazel failure that leaves no result, prints no file lines.

JSON starts with command_started with mode check, or default with --apply. Then one change per file with path, kind (create or modify), edit_count, and effect (proposed, applied, or refused). A refused change has a reason: write_mismatch, missing_file, or unreadable_file. Each ignored import emits a notice with code ignored_import, level warning, path, language, and import. A Bazel failure with --apply, or one that leaves results incomplete, emits a bazel_failed error. So does a Gazelle failure with --check, including a nonzero exit that does not come with a Gazelle diff. command_finished ends the stream with results_complete, exit_code, and changes with create and modify counts, plus applied and refused with --apply. An incomplete --apply run still emits its change events and changes with applied and refused. An incomplete --check run, or a Bazel failure that leaves no result, emits no change events and no changes.

Directives

# gazelle:dx_ignore_import <language> <import> skips one import in this directory and below. dx generate fails when no literal reference matches it, when the language is unknown, or when the value is malformed.

Accepted languages: astro, cc, csharp, fsharp, gleam, go, java, javascript, kotlin, mdx, python, ruby, rust, scala, svelte, typescript, vue, zig.

# gazelle:dx_cuda_archs <spec> <spec>... sets the archs of a generated cuda_library. It applies to this directory and below, and a deeper directive replaces it. Separate specs with spaces, because a spec can contain commas, as in compute_89:compute_89,sm_89. The cc language generates a <dir>_cuda target for .cu and .cuh files that no hand-written CUDA rule lists, and it fails when the directive is missing. Gazelle never touches hand-written cuda_library, cuda_binary, cuda_test or cuda_gpu_test targets. It sets archs only when it creates the target, so a hand-edited value survives later runs. Sources named *_test.cu and sources that define main are not generated, and main fails the run.

# gazelle:lang must list dx_dispatch (for example # gazelle:lang go,dx_dispatch), or dx generate fails.

# gazelle:resolve <language> <import> <label> maps one import to an exact label.

# gazelle:dx_ignore_import python requests
# gazelle:resolve python yaml @pypi//pyyaml

For java, kotlin and scala, a resolve mapping names the fully qualified import or the import's own package, and a dx_ignore_import names the fully qualified import. A wildcard import such as kotlinx.coroutines.* resolves through its package. A bare class name such as ImmutableList no longer matches, so a stale bare-name entry fails the run.

# gazelle:resolve kotlin kotlinx.coroutines @maven//:org_jetbrains_kotlinx_kotlinx_coroutines_core
# gazelle:resolve kotlin com.google.common.collect.ImmutableList @maven//:com_google_guava_guava