Skip to content

Commit b832d50

Browse files
authored
feat: add logic from bazel-contrib#1029 back with fix (bazel-contrib#1039)
1 parent 2607797 commit b832d50

File tree

32 files changed

+188
-62
lines changed

32 files changed

+188
-62
lines changed

gazelle/python/generate.go

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
7676

7777
pyLibraryFilenames := treeset.NewWith(godsutils.StringComparator)
7878
pyTestFilenames := treeset.NewWith(godsutils.StringComparator)
79+
pyFileNames := treeset.NewWith(godsutils.StringComparator)
7980

8081
// hasPyBinary controls whether a py_binary target should be generated for
8182
// this package or not.
@@ -92,16 +93,19 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
9293
continue
9394
}
9495
ext := filepath.Ext(f)
95-
if !hasPyBinary && f == pyBinaryEntrypointFilename {
96-
hasPyBinary = true
97-
} else if !hasPyTestEntryPointFile && f == pyTestEntrypointFilename {
98-
hasPyTestEntryPointFile = true
99-
} else if f == conftestFilename {
100-
hasConftestFile = true
101-
} else if strings.HasSuffix(f, "_test.py") || (strings.HasPrefix(f, "test_") && ext == ".py") {
102-
pyTestFilenames.Add(f)
103-
} else if ext == ".py" {
104-
pyLibraryFilenames.Add(f)
96+
if ext == ".py" {
97+
pyFileNames.Add(f)
98+
if !hasPyBinary && f == pyBinaryEntrypointFilename {
99+
hasPyBinary = true
100+
} else if !hasPyTestEntryPointFile && f == pyTestEntrypointFilename {
101+
hasPyTestEntryPointFile = true
102+
} else if f == conftestFilename {
103+
hasConftestFile = true
104+
} else if strings.HasSuffix(f, "_test.py") || strings.HasPrefix(f, "test_") {
105+
pyTestFilenames.Add(f)
106+
} else {
107+
pyLibraryFilenames.Add(f)
108+
}
105109
}
106110
}
107111

@@ -223,8 +227,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
223227
}
224228
}
225229

226-
pyLibrary = newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
227-
setUUID(label.New("", args.Rel, pyLibraryTargetName).String()).
230+
pyLibrary = newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
228231
addVisibility(visibility).
229232
addSrcs(pyLibraryFilenames).
230233
addModuleDependencies(deps).
@@ -260,17 +263,13 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
260263
}
261264
}
262265

263-
pyBinaryTarget := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
266+
pyBinaryTarget := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
264267
setMain(pyBinaryEntrypointFilename).
265268
addVisibility(visibility).
266269
addSrc(pyBinaryEntrypointFilename).
267270
addModuleDependencies(deps).
268271
generateImportsAttribute()
269272

270-
if pyLibrary != nil {
271-
pyBinaryTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
272-
}
273-
274273
pyBinary := pyBinaryTarget.build()
275274

276275
result.Gen = append(result.Gen, pyBinary)
@@ -300,8 +299,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
300299
}
301300
}
302301

303-
conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
304-
setUUID(label.New("", args.Rel, conftestTargetname).String()).
302+
conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyFileNames).
305303
addSrc(conftestFilename).
306304
addModuleDependencies(deps).
307305
addVisibility(visibility).
@@ -315,8 +313,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
315313
}
316314

317315
var pyTestTargets []*targetBuilder
318-
newPyTestTargetBuilder := func(pyTestFilenames *treeset.Set, pyTestTargetName string) *targetBuilder {
319-
deps, err := parser.parse(pyTestFilenames)
316+
newPyTestTargetBuilder := func(srcs *treeset.Set, pyTestTargetName string) *targetBuilder {
317+
deps, err := parser.parse(srcs)
320318
if err != nil {
321319
log.Fatalf("ERROR: %v\n", err)
322320
}
@@ -336,8 +334,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
336334
}
337335
}
338336
}
339-
return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
340-
addSrcs(pyTestFilenames).
337+
return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel, pyFileNames).
338+
addSrcs(srcs).
341339
addModuleDependencies(deps).
342340
generateImportsAttribute()
343341
}
@@ -371,14 +369,9 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
371369
}
372370

373371
for _, pyTestTarget := range pyTestTargets {
374-
if pyLibrary != nil {
375-
pyTestTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
376-
}
377-
378372
if conftest != nil {
379-
pyTestTarget.addModuleDependency(module{Name: conftest.PrivateAttr(uuidKey).(string)})
373+
pyTestTarget.addModuleDependency(module{Name: strings.TrimSuffix(conftestFilename, ".py")})
380374
}
381-
382375
pyTest := pyTestTarget.build()
383376

384377
result.Gen = append(result.Gen, pyTest)

gazelle/python/resolve.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ const (
3939
// resolvedDepsKey is the attribute key used to pass dependencies that don't
4040
// need to be resolved by the dependency resolver in the Resolver step.
4141
resolvedDepsKey = "_gazelle_python_resolved_deps"
42-
// uuidKey is the attribute key used to uniquely identify a py_library
43-
// target that should be imported by a py_test or py_binary in the same
44-
// Bazel package.
45-
uuidKey = "_gazelle_python_library_uuid"
4642
)
4743

4844
// Resolver satisfies the resolve.Resolver interface. It resolves dependencies
@@ -71,13 +67,6 @@ func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []reso
7167
provides = append(provides, provide)
7268
}
7369
}
74-
if r.PrivateAttr(uuidKey) != nil {
75-
provide := resolve.ImportSpec{
76-
Lang: languageName,
77-
Imp: r.PrivateAttr(uuidKey).(string),
78-
}
79-
provides = append(provides, provide)
80-
}
8170
if len(provides) == 0 {
8271
return nil
8372
}

gazelle/python/target.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
package python
1616

1717
import (
18-
"path/filepath"
19-
2018
"github.com/bazelbuild/bazel-gazelle/config"
2119
"github.com/bazelbuild/bazel-gazelle/rule"
2220
"github.com/emirpasic/gods/sets/treeset"
2321
godsutils "github.com/emirpasic/gods/utils"
22+
"path/filepath"
2423
)
2524

2625
// targetBuilder builds targets to be generated by Gazelle.
@@ -29,7 +28,6 @@ type targetBuilder struct {
2928
name string
3029
pythonProjectRoot string
3130
bzlPackage string
32-
uuid string
3331
srcs *treeset.Set
3432
siblingSrcs *treeset.Set
3533
deps *treeset.Set
@@ -55,15 +53,6 @@ func newTargetBuilder(kind, name, pythonProjectRoot, bzlPackage string, siblingS
5553
}
5654
}
5755

58-
// setUUID sets the given UUID for the target. It's used to index the generated
59-
// target based on this value in addition to the other ways the targets can be
60-
// imported. py_{binary,test} targets in the same Bazel package can add a
61-
// virtual dependency to this UUID that gets resolved in the Resolver interface.
62-
func (t *targetBuilder) setUUID(uuid string) *targetBuilder {
63-
t.uuid = uuid
64-
return t
65-
}
66-
6756
// addSrc adds a single src to the target.
6857
func (t *targetBuilder) addSrc(src string) *targetBuilder {
6958
t.srcs.Add(src)
@@ -81,9 +70,16 @@ func (t *targetBuilder) addSrcs(srcs *treeset.Set) *targetBuilder {
8170

8271
// addModuleDependency adds a single module dep to the target.
8372
func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
84-
if dep.Name+".py" == filepath.Base(dep.Filepath) || !t.siblingSrcs.Contains(dep.Name+".py") {
85-
t.deps.Add(dep)
73+
fileName := dep.Name + ".py"
74+
if dep.From != "" {
75+
fileName = dep.From + ".py"
8676
}
77+
if t.siblingSrcs.Contains(fileName) && fileName != filepath.Base(dep.Filepath) {
78+
// importing another module from the same package, converting to absolute imports to make
79+
// dependency resolution easier
80+
dep.Name = importSpecFromSrc(t.pythonProjectRoot, t.bzlPackage, fileName).Imp
81+
}
82+
t.deps.Add(dep)
8783
return t
8884
}
8985

@@ -138,9 +134,6 @@ func (t *targetBuilder) generateImportsAttribute() *targetBuilder {
138134
// build returns the assembled *rule.Rule for the target.
139135
func (t *targetBuilder) build() *rule.Rule {
140136
r := rule.NewRule(t.kind, t.name)
141-
if t.uuid != "" {
142-
r.SetPrivateAttr(uuidKey, t.uuid)
143-
}
144137
if !t.srcs.Empty() {
145138
r.SetAttr("srcs", t.srcs.Values())
146139
}

gazelle/python/testdata/generated_test_entrypoint/BUILD.out

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@ py_test(
1717
name = "generated_test_entrypoint_test",
1818
srcs = [":__test__"],
1919
main = ":__test__.py",
20-
deps = [
21-
":__test__",
22-
":generated_test_entrypoint",
23-
],
20+
deps = [":__test__"],
2421
)

gazelle/python/testdata/naming_convention/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
# For test purposes only.
16+
import __init__

gazelle/python/testdata/naming_convention/__test__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
# For test purposes only.
16+
import __init__

gazelle/python/testdata/naming_convention/dont_rename/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
# For test purposes only.
16+
import __init__

gazelle/python/testdata/naming_convention/dont_rename/__test__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
# For test purposes only.
16+
import __init__

gazelle/python/testdata/naming_convention/resolve_conflict/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
# For test purposes only.
16+
import __init__

gazelle/python/testdata/naming_convention/resolve_conflict/__test__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
# For test purposes only.
16+
import __init__

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy