Skip to content

fix(gazelle): keep another language's Bazel package out of a package's srcs - #418

Merged
aronstrandberg merged 3 commits into
mainfrom
aronstrandberg/data-files-stop-at-bazel-packages
Dec 2, 2026
Merged

aronstrandberg merged 3 commits into
mainfrom
aronstrandberg/data-files-stop-at-bazel-packages

Conversation

@aronstrandberg

@aronstrandberg aronstrandberg commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Stop Gazelle from listing another language's files as a TypeScript package's srcs. Today a Go package in a subdirectory of a TypeScript package gets its .go, go.mod and go.sum files written into the package above, as labels such as cmd/tool/main.go that Bazel refuses because they reach into a subpackage. In the lovable monorepo this blocks any Go code under infra/buildkite, which is itself a TypeScript package.

Claude Opus 5.5, high

Cause. dataFiles takes every regular file under a package's tree that no listing decides, and skips only directories that belong to a deeper TypeScript package (nearestPackage). A directory whose BUILD file holds Go rules is no TypeScript package, so its files land in the package above.

Change. dataFiles also skips a directory when it, or a directory between it and the package, has a BUILD file that stays a Bazel package whatever this run writes: one on disk with no rules, or one with any rule other than those a run withdraws from a directory without a program (ownedRuleNames, matched by kind under that directory's own kind map, and by name). No label of the package above can name a file in it or below it. A BUILD file that holds only such rules is still no boundary while it holds them; a hand-written filegroup or exports_files, or a ts_codegen, is one.

One consequence to check: when a run withdraws all of a directory's TypeScript rules, Gazelle leaves its BUILD file on disk, empty, so the next read treats it as a package and its files leave the parent's srcs. Before this change they went into the parent, where Bazel refuses them because the empty BUILD file is still a package. TestGenerate_AProgramWithNoInputsIsNoPackage still passes, because its harness does not write files back.

Not covered: a .ts file inside such a foreign Bazel package that a program above it lists is still claimed through owner. That needs its own decision, since the program does list it.

Tests. TestGenerate_AnotherLanguagesBazelPackageKeepsItsFiles puts a go_binary under a TypeScript package; without the change srcs held cmd/tool/go.mod cmd/tool/main.go cmd/tool/sub/x.go. TestGenerate_ABuildFileWithNoRulesKeepsItsFiles runs the real binary over an empty BUILD.bazel beside a .bzl file, a hand-written filegroup and a withdrawn mapped ts_compile; without the change srcs held bzl/defs.bzl data/d.txt mapped/m.txt. Both pass with it.

Not covered either: a run where the other language's BUILD file does not exist yet when this extension reads the tree (a binary that lists TypeScript first, or a TypeScript-only run before the other language's). In the lovable monorepo the Go Gazelle runs first. On my machine TestProgram_ExitCodePolicy and TestProgram_TheTraceIsDecodedWhileTsgoRuns fail on main and with this change alike (tsgo exits 127 there), so //gazelle:typescript_test fails 4 of 8 shards either way; I have not looked into why.

In the monorepo. With the change applied on the current pin (1583e19a) through --override_module, //:gazelle_typescript writes srcs = ["read.ts"] for infra/buildkite/packages/secrets next to a Gazelle-written Go package in packages/secrets/cmd/bk-secrets, and both build and test.

…s srcs

A package's srcs take every regular file under its directory that no deeper
TypeScript package owns. A Go go_binary in a subdirectory therefore had its
.go, go.mod and go.sum files written into the TypeScript package above it,
as labels Bazel refuses because they reach into a subpackage. A directory
whose BUILD file holds a rule of a kind this extension does not write stays
a Bazel package whatever a run writes, so it and its subtree add no files
to the package above. A BUILD file that holds only this extension's rules
still does not, since a run can withdraw them all.
@aronstrandberg
aronstrandberg requested a review from mikn as a code owner December 1, 2026 20:57
Comment thread gazelle/generate.go Resolved
Comment thread gazelle/generate.go Resolved
Comment thread gazelle/generate.go Resolved
… under the directory's kind map

Read each BUILD file's rules with its own directory's configuration, so a
mapped or aliased TypeScript kind counts as this extension's. A BUILD file on
disk with no rules stays a Bazel package too, including one whose rules a run
withdrew.
@aronstrandberg

Copy link
Copy Markdown
Member Author

@claude review always

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread gazelle/generate.go Resolved
Comment thread gazelle/generate.go Resolved
…dary

A hand-written filegroup or exports_files, or any rule of this extension's
kinds that a run does not withdraw from a directory without a program, keeps
its directory a Bazel package, so its files stay out of the package above.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No new blocking issues. 1 optional suggestion (a nit or a note on pre-existing code) was found and not posted. Nothing in this review needs a push before merging.

@aronstrandberg
aronstrandberg merged commit dd6ac82 into main Dec 2, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants