Skip to content

Fix map_kind for generated rules so renamed kinds keep deps/data#67

Open
firestreaker wants to merge 1 commit into
benchsci:mainfrom
firestreaker:fix-map-kind-generated-rules
Open

Fix map_kind for generated rules so renamed kinds keep deps/data#67
firestreaker wants to merge 1 commit into
benchsci:mainfrom
firestreaker:fix-map-kind-generated-rules

Conversation

@firestreaker

Copy link
Copy Markdown

I'm adding Vitest into my Bazel project, but gazelle hardcodes the generated test target to jest_test which is kinda misleading since we don't use Jest. Tried map_kind

# gazelle:map_kind jest_test vitest_test //build:vitest_test.bzl

but the renamed rule comes out with no deps, no data, and no load(). Getting by with a self-map (# gazelle:map_kind jest_test jest_test //build:vitest_test.bzl and jest_test = vitest_test in build/vitest_test.bzl) for now, but would like a cleaner fix that doesn't leave the BUILD files reading jest_test.

This change has the plugin emit builtin kinds and lets gazelle do the renaming, and adds an unmapKind helper for the few spots that read existing on-disk rules so a rule already renamed on a previous run is still recognized and not churned

Using `# gazelle:map_kind` to rename a kind this extension generates (e.g.
`map_kind jest_test vitest_test //build:vitest_test.bzl`) produced a rule with
no deps, no data, and no load() statement, breaking dependency management.
Self-maps (X X //file) worked, which is why this went unnoticed.

Root cause: the extension pre-applied map_kind itself at generation time via a
getKind() helper, writing the mapped kind to disk directly. That bypassed
gazelle core's rename handling:
  - Core never saw a builtin rule to record/remember, so it never injected the
    load() for the mapped kind.
  - Core's inverseMapKindResolver resets a renamed rule back to its builtin kind
    before calling Imports/Resolve, but resolve.go compared r.Kind() against the
    mapped name (getKind), so the comparison failed and the dep/data block was
    skipped.

Let gazelle core own map_kind instead:
  - generate.go: emit builtin kinds at rule creation; add unmapKind() (the
    inverse of KindMap) and use it only where the code inspects on-disk kinds
    (readExistingRules, pruneManagedRules, the collect-all collision check) so a
    rule already renamed on disk is still recognized, merged, and pruned across
    re-runs.
  - resolve.go: compare r.Kind() against builtin literals (consistent with the
    existing ts_project checks).

Add a golden fixture (tests/map_kind_test) covering both a fresh rename and a
re-run over an existing renamed rule. Document map_kind on the test rule in the
README.

Backward-compatible: with no map_kind (or a self-map), KindMap is empty/identity
and output is byte-identical, so the existing golden suite is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@firestreaker firestreaker force-pushed the fix-map-kind-generated-rules branch from deb3840 to 70257fa Compare June 24, 2026 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant