Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog.d/gazelle-data-files-stop-at-other-packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Fixed

- **Gazelle no longer lists another Bazel package's files as a package's
`srcs`.** A package's `srcs` take every regular file under its directory that
no deeper TypeScript package owns, so a Go `go_binary` in a subdirectory had
its `.go`, `go.mod` and `go.sum` files written into the TypeScript package
above it as `cmd/tool/main.go`, and an empty `BUILD.bazel` beside `.bzl`
files had them written as `bzl/defs.bzl`. Bazel refuses those labels: they
reach into a subpackage. A directory whose BUILD file has no rules on disk, or
holds a rule other than those a run withdraws from a directory that is no
package (its `ts_compile`, `ts_test`, `ts_dev_server`, `ts_config` and config
`filegroup`s, under any kind map), is a Bazel package of its own, so neither
it nor anything below it adds files to the package above.
41 changes: 38 additions & 3 deletions gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,13 @@ func programCandidate(name string) bool {
}

// dataFiles is every regular file under pkg's tree no listing decides; not a
// deeper package's, a ts_codegen's, a BUILD file, the ts_config's own src or
// the package's editor wrapper (tsconfig.json, tsconfig.editor.json).
// deeper package's, another language's Bazel package's, a ts_codegen's, a BUILD
// file, the ts_config's own src or the package's editor wrapper (tsconfig.json,
// tsconfig.editor.json).
func (s *programStore) dataFiles(pkg string, tc *tsConfig) []string {
var out []string
for _, dir := range s.walkedDirs(pkg) {
if s.nearestPackage(dir) != pkg {
if s.nearestPackage(dir) != pkg || s.inForeignBuild(pkg, dir) {
Comment thread
aronstrandberg marked this conversation as resolved.
Resolved
continue
}
s.importFacts.enumerated(dir)
Expand All @@ -595,6 +596,40 @@ func (s *programStore) dataFiles(pkg string, tc *tsConfig) []string {
return out
}

// inForeignBuild is whether a directory below pkg, at or above dir, has a BUILD
// file that stays a Bazel package whatever this run writes: one on disk with no
// rules, or one with a rule other than those a run withdraws from a directory
// that is no package (ownedRuleNames). No label of pkg can name a file there.
func (s *programStore) inForeignBuild(pkg, dir string) bool {
if s.emission == nil {
return false
}
for ; dir != pkg && dir != ""; dir = parentDir(dir) {
f := s.emission.files[dir]
if f == nil {
continue
Comment thread
aronstrandberg marked this conversation as resolved.
Resolved
}
// Generate records a directory without a BUILD file as one with no syntax tree.
if len(f.Rules) == 0 && f.File != nil {
return true
}
if len(f.Rules) == 0 {
continue
}
withdrawable := map[[2]string]bool{}
for _, r := range ownedRuleNames(dir) {
withdrawable[[2]string{r.Kind(), r.Name()}] = true
}
c := s.buildConfig(dir)
for _, r := range f.Rules {
if !withdrawable[[2]string{canonicalRule(c, r).Kind(), r.Name()}] {
return true
}
Comment thread
aronstrandberg marked this conversation as resolved.
Resolved
}
Comment thread
aronstrandberg marked this conversation as resolved.
Resolved
}
return false
}

// nearestPackage is the package at or above dir; "" when there is none.
func (s *programStore) nearestPackage(dir string) string {
for ; ; dir = parentDir(dir) {
Expand Down
55 changes: 55 additions & 0 deletions gazelle/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,61 @@ ts_codegen(
}
}

// A directory below a package whose BUILD file holds another language's rules
// is a Bazel package whatever this run writes, so its files are no src: a label
// of the package above cannot name them.
func TestGenerate_AnotherLanguagesBazelPackageKeepsItsFiles(t *testing.T) {
g := generateAll(t, writeTree(t, map[string]string{
"package.json": rootManifest,
"pkg/package.json": `{"name":"pkg"}` + "\n",
"pkg/tsconfig.json": includeTs,
"pkg/read.ts": "export const read = 1;\n",
"pkg/notes.md": "notes\n",
"pkg/cmd/tool/go.mod": "module example.com/tool\n",
"pkg/cmd/tool/main.go": "package main\n",
"pkg/cmd/tool/sub/x.go": "package sub\n",
"pkg/cmd/tool/BUILD.bazel": `load("@rules_go//go:def.bzl", "go_binary")

go_binary(
name = "tool",
srcs = ["main.go"],
)
`,
}))

compile := mustRule(t, g.results["pkg"], "ts_compile", "pkg")
wantStrings(t, "ts_compile pkg srcs", compile.AttrStrings("srcs"), []string{"notes.md", "read.ts"})
}

// A BUILD file with no rules is a Bazel package too, and so is one holding a
// filegroup this language writes by kind but not by name, and one whose mapped
// TypeScript rules a run withdraws, since Gazelle leaves the file on disk.
func TestGenerate_ABuildFileWithNoRulesKeepsItsFiles(t *testing.T) {
requireTsgo(t)
root := writeTree(t, map[string]string{
"MODULE.bazel": "module(name = \"foreign_packages\")\n",
"BUILD.bazel": "# gazelle:map_kind ts_compile custom_compile //:defs.bzl\n",
"package.json": rootManifest,
"pkg/package.json": `{"name":"pkg"}` + "\n",
"pkg/tsconfig.json": includeTs,
"pkg/read.ts": "export const read = 1;\n",
"pkg/notes.md": "notes\n",
"pkg/bzl/BUILD.bazel": "",
"pkg/bzl/defs.bzl": "X = 1\n",
"pkg/mapped/BUILD.bazel": "load(\"//:defs.bzl\", \"custom_compile\")\n\n" +
"custom_compile(\n name = \"mapped\",\n srcs = [\"m.txt\"],\n)\n",
"pkg/mapped/m.txt": "m\n",
"pkg/data/BUILD.bazel": "filegroup(\n name = \"data\",\n srcs = [\"d.txt\"],\n)\n",
"pkg/data/d.txt": "d\n",
})
if output, err := protoGazelle(t, root); err != nil {
t.Fatalf("gazelle: %v\n%s", err, output)
}

compile := onDiskRule(t, root, "pkg", "custom_compile", "pkg")
wantStrings(t, "custom_compile pkg srcs", compile.AttrStrings("srcs"), []string{"notes.md", "read.ts"})
}

// A program with no inputs is no package: its directory's files belong to the
// package above, and a ts_config an earlier run wrote there is withdrawn.
func TestGenerate_AProgramWithNoInputsIsNoPackage(t *testing.T) {
Expand Down
Loading