Skip to content

Commit 781935f

Browse files
authored
fix(gazelle): delete invalid py_library and use correct NonEmptyAttrs for py_* (bazel-contrib#1887)
Before gazelle would leave generated py_library targets aroundeven when no files in a directory exist because X. With this change gazelle correctly handles this case.
1 parent df55823 commit 781935f

File tree

8 files changed

+70
-23
lines changed

8 files changed

+70
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ A brief description of the categories of changes:
2525

2626
### Fixed
2727

28+
* (gazelle) Remove `visibility` from `NonEmptyAttr`.
29+
Now empty(have no `deps/main/srcs/imports` attr) `py_library/test/binary` rules will
30+
be automatically deleted correctly. For example, if `python_generation_mode`
31+
is set to package, when `__init__.py` is deleted, the `py_library` generated
32+
for this package before will be deleted automatically.
33+
2834
### Added
2935

3036
## [0.32.2] - 2024-05-14

gazelle/python/generate.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
270270
}
271271
}
272272

273-
// If we're doing per-file generation, srcs could be empty at this point, meaning we shouldn't make a py_library.
274-
if srcs.Empty() {
275-
return
276-
}
277-
278273
// Check if a target with the same name we are generating already
279274
// exists, and if it is of a different kind from the one we are
280275
// generating. If so, we have to throw an error since Gazelle won't
@@ -295,8 +290,12 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
295290
generateImportsAttribute().
296291
build()
297292

298-
result.Gen = append(result.Gen, pyLibrary)
299-
result.Imports = append(result.Imports, pyLibrary.PrivateAttr(config.GazelleImportsKey))
293+
if pyLibrary.IsEmpty(py.Kinds()[pyLibrary.Kind()]) {
294+
result.Empty = append(result.Gen, pyLibrary)
295+
} else {
296+
result.Gen = append(result.Gen, pyLibrary)
297+
result.Imports = append(result.Imports, pyLibrary.PrivateAttr(config.GazelleImportsKey))
298+
}
300299
}
301300
if cfg.PerFileGeneration() {
302301
hasInit, nonEmptyInit := hasLibraryEntrypointFile(args.Dir)
@@ -311,7 +310,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
311310
}
312311
appendPyLibrary(srcs, pyLibraryTargetName)
313312
})
314-
} else if !pyLibraryFilenames.Empty() {
313+
} else {
315314
appendPyLibrary(pyLibraryFilenames, cfg.RenderLibraryName(packageName))
316315
}
317316

@@ -411,7 +410,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
411410
// the file exists on disk.
412411
pyTestFilenames.Add(pyTestEntrypointFilename)
413412
}
414-
if (hasPyTestEntryPointTarget || !pyTestFilenames.Empty()) {
413+
if hasPyTestEntryPointTarget || !pyTestFilenames.Empty() {
415414
pyTestTargetName := cfg.RenderTestName(packageName)
416415
pyTestTarget := newPyTestTargetBuilder(pyTestFilenames, pyTestTargetName)
417416

gazelle/python/kinds.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ var pyKinds = map[string]rule.KindInfo{
3434
pyBinaryKind: {
3535
MatchAny: true,
3636
NonEmptyAttrs: map[string]bool{
37-
"deps": true,
38-
"main": true,
39-
"srcs": true,
40-
"imports": true,
41-
"visibility": true,
37+
"deps": true,
38+
"main": true,
39+
"srcs": true,
40+
"imports": true,
4241
},
4342
SubstituteAttrs: map[string]bool{},
4443
MergeableAttrs: map[string]bool{
@@ -52,10 +51,9 @@ var pyKinds = map[string]rule.KindInfo{
5251
MatchAny: false,
5352
MatchAttrs: []string{"srcs"},
5453
NonEmptyAttrs: map[string]bool{
55-
"deps": true,
56-
"srcs": true,
57-
"imports": true,
58-
"visibility": true,
54+
"deps": true,
55+
"srcs": true,
56+
"imports": true,
5957
},
6058
SubstituteAttrs: map[string]bool{},
6159
MergeableAttrs: map[string]bool{
@@ -68,11 +66,10 @@ var pyKinds = map[string]rule.KindInfo{
6866
pyTestKind: {
6967
MatchAny: false,
7068
NonEmptyAttrs: map[string]bool{
71-
"deps": true,
72-
"main": true,
73-
"srcs": true,
74-
"imports": true,
75-
"visibility": true,
69+
"deps": true,
70+
"main": true,
71+
"srcs": true,
72+
"imports": true,
7673
},
7774
SubstituteAttrs: map[string]bool{},
7875
MergeableAttrs: map[string]bool{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "remove_invalid_library",
5+
srcs = ["__init__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
8+
9+
py_library(
10+
name = "deps_with_no_srcs_library",
11+
deps = [
12+
"//:remove_invalid_library",
13+
"@pypi//bar",
14+
"@pypi//foo",
15+
],
16+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "deps_with_no_srcs_library",
5+
deps = [
6+
"//:remove_invalid_library",
7+
"@pypi//bar",
8+
"@pypi//foo",
9+
],
10+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Remove invalid
2+
3+
This test case asserts that `py_library` should be deleted if invalid.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---

0 commit comments

Comments
 (0)