Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving sibling modules with absolute imports #1029

Merged
merged 6 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 0 additions & 14 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

pyLibrary = newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
setUUID(label.New("", args.Rel, pyLibraryTargetName).String()).
addVisibility(visibility).
addSrcs(pyLibraryFilenames).
addModuleDependencies(deps).
Expand Down Expand Up @@ -253,10 +252,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
addModuleDependencies(deps).
generateImportsAttribute()

if pyLibrary != nil {
pyBinaryTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
}

pyBinary := pyBinaryTarget.build()

result.Gen = append(result.Gen, pyBinary)
Expand Down Expand Up @@ -287,7 +282,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
setUUID(label.New("", args.Rel, conftestTargetname).String()).
addSrc(conftestFilename).
addModuleDependencies(deps).
addVisibility(visibility).
Expand Down Expand Up @@ -357,14 +351,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

for _, pyTestTarget := range pyTestTargets {
if pyLibrary != nil {
pyTestTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
}

if conftest != nil {
pyTestTarget.addModuleDependency(module{Name: conftest.PrivateAttr(uuidKey).(string)})
}

pyTest := pyTestTarget.build()

result.Gen = append(result.Gen, pyTest)
Expand Down
11 changes: 0 additions & 11 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ const (
// resolvedDepsKey is the attribute key used to pass dependencies that don't
// need to be resolved by the dependency resolver in the Resolver step.
resolvedDepsKey = "_gazelle_python_resolved_deps"
// uuidKey is the attribute key used to uniquely identify a py_library
// target that should be imported by a py_test or py_binary in the same
// Bazel package.
uuidKey = "_gazelle_python_library_uuid"
)

// Resolver satisfies the resolve.Resolver interface. It resolves dependencies
Expand Down Expand Up @@ -57,13 +53,6 @@ func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []reso
provides = append(provides, provide)
}
}
if r.PrivateAttr(uuidKey) != nil {
provide := resolve.ImportSpec{
Lang: languageName,
Imp: r.PrivateAttr(uuidKey).(string),
}
provides = append(provides, provide)
}
if len(provides) == 0 {
return nil
}
Expand Down
27 changes: 12 additions & 15 deletions gazelle/python/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package python

import (
"path/filepath"
"strings"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
Expand All @@ -15,7 +16,6 @@ type targetBuilder struct {
name string
pythonProjectRoot string
bzlPackage string
uuid string
srcs *treeset.Set
siblingSrcs *treeset.Set
deps *treeset.Set
Expand All @@ -41,15 +41,6 @@ func newTargetBuilder(kind, name, pythonProjectRoot, bzlPackage string, siblingS
}
}

// setUUID sets the given UUID for the target. It's used to index the generated
// target based on this value in addition to the other ways the targets can be
// imported. py_{binary,test} targets in the same Bazel package can add a
// virtual dependency to this UUID that gets resolved in the Resolver interface.
func (t *targetBuilder) setUUID(uuid string) *targetBuilder {
t.uuid = uuid
return t
}

// addSrc adds a single src to the target.
func (t *targetBuilder) addSrc(src string) *targetBuilder {
t.srcs.Add(src)
Expand All @@ -67,9 +58,18 @@ func (t *targetBuilder) addSrcs(srcs *treeset.Set) *targetBuilder {

// addModuleDependency adds a single module dep to the target.
func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
if dep.Name+".py" == filepath.Base(dep.Filepath) || !t.siblingSrcs.Contains(dep.Name+".py") {
t.deps.Add(dep)
fileName := dep.Name + ".py"
if t.siblingSrcs.Contains(fileName) && fileName != filepath.Base(dep.Filepath) {
if strings.HasPrefix(dep.Name, "test_") || strings.HasSuffix(dep.Name, "_test") {
// don't need to add deps when test files are importing each other, because they are in
// the same py_test target.
return t
}
// importing another module from the same package, converting to absolute imports to make
// dependency resolution easier
dep.Name = importSpecFromSrc(t.pythonProjectRoot, t.bzlPackage, fileName).Imp
}
t.deps.Add(dep)
return t
}

Expand Down Expand Up @@ -124,9 +124,6 @@ func (t *targetBuilder) generateImportsAttribute() *targetBuilder {
// build returns the assembled *rule.Rule for the target.
func (t *targetBuilder) build() *rule.Rule {
r := rule.NewRule(t.kind, t.name)
if t.uuid != "" {
r.SetPrivateAttr(uuidKey, t.uuid)
}
if !t.srcs.Empty() {
r.SetAttr("srcs", t.srcs.Values())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@ py_test(
name = "generated_test_entrypoint_test",
srcs = [":__test__"],
main = ":__test__.py",
deps = [
":__test__",
":generated_test_entrypoint",
],
deps = [":__test__"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a breaking change.

)
1 change: 1 addition & 0 deletions gazelle/python/testdata/naming_convention/__main__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
1 change: 1 addition & 0 deletions gazelle/python/testdata/naming_convention/__test__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import boto3
import __init__

_ = boto3
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import foo
5 changes: 1 addition & 4 deletions gazelle/python/testdata/simple_test_with_conftest/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@ py_test(
name = "simple_test_with_conftest_test",
srcs = ["__test__.py"],
main = "__test__.py",
deps = [
":conftest",
":simple_test_with_conftest",
],
deps = [":simple_test_with_conftest"],
)
3 changes: 1 addition & 2 deletions gazelle/python/testdata/simple_test_with_conftest/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Simple test with conftest.py

This test case asserts that a simple `py_test` is generated as expected when a
`conftest.py` is present.
This test case asserts that a simple `py_test` does not depend on conftest when it doesn't import it.
linzhp marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import foo.has_main.python.my_module
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# For test purposes only.
import foo.has_test.python.my_module
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ py_binary(
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":with_third_party_requirements"],
deps = ["@gazelle_python_test_baz//:pkg"],
)