Skip to content

Commit 9fc7cfa

Browse files
authored
Resolving sibling modules with absolute imports (bazel-contrib#1029)
* Resolving sibling modules with absolute imports * unconditionally importing conftest * handle from statements * adding tests * adding readme for the new test case
1 parent b122f3a commit 9fc7cfa

File tree

26 files changed

+66
-48
lines changed

26 files changed

+66
-48
lines changed

gazelle/python/generate.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
224224
}
225225

226226
pyLibrary = newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
227-
setUUID(label.New("", args.Rel, pyLibraryTargetName).String()).
228227
addVisibility(visibility).
229228
addSrcs(pyLibraryFilenames).
230229
addModuleDependencies(deps).
@@ -267,10 +266,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
267266
addModuleDependencies(deps).
268267
generateImportsAttribute()
269268

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

276271
result.Gen = append(result.Gen, pyBinary)
@@ -301,7 +296,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
301296
}
302297

303298
conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
304-
setUUID(label.New("", args.Rel, conftestTargetname).String()).
305299
addSrc(conftestFilename).
306300
addModuleDependencies(deps).
307301
addVisibility(visibility).
@@ -315,8 +309,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
315309
}
316310

317311
var pyTestTargets []*targetBuilder
318-
newPyTestTargetBuilder := func(pyTestFilenames *treeset.Set, pyTestTargetName string) *targetBuilder {
319-
deps, err := parser.parse(pyTestFilenames)
312+
newPyTestTargetBuilder := func(srcs *treeset.Set, pyTestTargetName string) *targetBuilder {
313+
deps, err := parser.parse(srcs)
320314
if err != nil {
321315
log.Fatalf("ERROR: %v\n", err)
322316
}
@@ -337,7 +331,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
337331
}
338332
}
339333
return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
340-
addSrcs(pyTestFilenames).
334+
addSrcs(srcs).
341335
addModuleDependencies(deps).
342336
generateImportsAttribute()
343337
}
@@ -371,14 +365,9 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
371365
}
372366

373367
for _, pyTestTarget := range pyTestTargets {
374-
if pyLibrary != nil {
375-
pyTestTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
376-
}
377-
378368
if conftest != nil {
379-
pyTestTarget.addModuleDependency(module{Name: conftest.PrivateAttr(uuidKey).(string)})
369+
pyTestTarget.addModuleDependency(module{Name: strings.TrimSuffix(conftestFilename, ".py")})
380370
}
381-
382371
pyTest := pyTestTarget.build()
383372

384373
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