Skip to content

Commit 69a9920

Browse files
authored
fix: support gazelle generation_mode:update_only (#2708)
This just fixes a crash when `generation_mode: update_only` causes `GenerateRules` to not be invoked for 100% of directories. Fix #2707
1 parent 537fc4b commit 69a9920

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

gazelle/pythonconfig/pythonconfig.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222

2323
"github.com/emirpasic/gods/lists/singlylinkedlist"
2424

25-
"github.com/bazelbuild/bazel-gazelle/label"
2625
"github.com/bazel-contrib/rules_python/gazelle/manifest"
26+
"github.com/bazelbuild/bazel-gazelle/label"
2727
)
2828

2929
// Directives
@@ -125,21 +125,28 @@ const (
125125

126126
// defaultIgnoreFiles is the list of default values used in the
127127
// python_ignore_files option.
128-
var defaultIgnoreFiles = map[string]struct{}{
129-
}
128+
var defaultIgnoreFiles = map[string]struct{}{}
130129

131130
// Configs is an extension of map[string]*Config. It provides finding methods
132131
// on top of the mapping.
133132
type Configs map[string]*Config
134133

135134
// ParentForPackage returns the parent Config for the given Bazel package.
136-
func (c *Configs) ParentForPackage(pkg string) *Config {
137-
dir := path.Dir(pkg)
138-
if dir == "." {
139-
dir = ""
135+
func (c Configs) ParentForPackage(pkg string) *Config {
136+
for {
137+
dir := path.Dir(pkg)
138+
if dir == "." {
139+
dir = ""
140+
}
141+
parent := (map[string]*Config)(c)[dir]
142+
if parent != nil {
143+
return parent
144+
}
145+
if dir == "" {
146+
return nil
147+
}
148+
pkg = dir
140149
}
141-
parent := (map[string]*Config)(*c)[dir]
142-
return parent
143150
}
144151

145152
// Config represents a config extension for a specific Bazel package.

gazelle/pythonconfig/pythonconfig_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,35 @@ func TestFormatThirdPartyDependency(t *testing.T) {
248248
})
249249
}
250250
}
251+
252+
func TestConfigsMap(t *testing.T) {
253+
t.Run("only root", func(t *testing.T) {
254+
configs := Configs{"": New("root/dir", "")}
255+
256+
if configs.ParentForPackage("") == nil {
257+
t.Fatal("expected non-nil for root config")
258+
}
259+
260+
if configs.ParentForPackage("a/b/c") != configs[""] {
261+
t.Fatal("expected root for subpackage")
262+
}
263+
})
264+
265+
t.Run("sparse child configs", func(t *testing.T) {
266+
configs := Configs{"": New("root/dir", "")}
267+
configs["a"] = configs[""].NewChild()
268+
configs["a/b/c"] = configs["a"].NewChild()
269+
270+
if configs.ParentForPackage("a/b/c/d") != configs["a/b/c"] {
271+
t.Fatal("child should match direct parent")
272+
}
273+
274+
if configs.ParentForPackage("a/b/c/d/e") != configs["a/b/c"] {
275+
t.Fatal("grandchild should match first parant")
276+
}
277+
278+
if configs.ParentForPackage("other/root/path") != configs[""] {
279+
t.Fatal("non-configured subpackage should match root")
280+
}
281+
})
282+
}

0 commit comments

Comments
 (0)