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

feat: add logic from #1029 back with fix #1039

Merged
merged 4 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 22 additions & 29 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes

pyLibraryFilenames := treeset.NewWith(godsutils.StringComparator)
pyTestFilenames := treeset.NewWith(godsutils.StringComparator)
pyFileNames := treeset.NewWith(godsutils.StringComparator)

// hasPyBinary controls whether a py_binary target should be generated for
// this package or not.
Expand All @@ -92,16 +93,19 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
continue
}
ext := filepath.Ext(f)
if !hasPyBinary && f == pyBinaryEntrypointFilename {
hasPyBinary = true
} else if !hasPyTestEntryPointFile && f == pyTestEntrypointFilename {
hasPyTestEntryPointFile = true
} else if f == conftestFilename {
hasConftestFile = true
} else if strings.HasSuffix(f, "_test.py") || (strings.HasPrefix(f, "test_") && ext == ".py") {
pyTestFilenames.Add(f)
} else if ext == ".py" {
pyLibraryFilenames.Add(f)
if ext == ".py" {
pyFileNames.Add(f)
if !hasPyBinary && f == pyBinaryEntrypointFilename {
hasPyBinary = true
} else if !hasPyTestEntryPointFile && f == pyTestEntrypointFilename {
hasPyTestEntryPointFile = true
} else if f == conftestFilename {
hasConftestFile = true
} else if strings.HasSuffix(f, "_test.py") || strings.HasPrefix(f, "test_") {
pyTestFilenames.Add(f)
} else {
pyLibraryFilenames.Add(f)
}
}
}

Expand Down Expand Up @@ -223,8 +227,7 @@ 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()).
pyLibrary = newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
addVisibility(visibility).
addSrcs(pyLibraryFilenames).
addModuleDependencies(deps).
Expand Down Expand Up @@ -260,17 +263,13 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
}

pyBinaryTarget := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
pyBinaryTarget := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
setMain(pyBinaryEntrypointFilename).
addVisibility(visibility).
addSrc(pyBinaryEntrypointFilename).
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 @@ -300,8 +299,7 @@ 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()).
conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyFileNames).
addSrc(conftestFilename).
addModuleDependencies(deps).
addVisibility(visibility).
Expand All @@ -315,8 +313,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

var pyTestTargets []*targetBuilder
newPyTestTargetBuilder := func(pyTestFilenames *treeset.Set, pyTestTargetName string) *targetBuilder {
deps, err := parser.parse(pyTestFilenames)
newPyTestTargetBuilder := func(srcs *treeset.Set, pyTestTargetName string) *targetBuilder {
deps, err := parser.parse(srcs)
if err != nil {
log.Fatalf("ERROR: %v\n", err)
}
Expand All @@ -336,8 +334,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
}
}
return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
addSrcs(pyTestFilenames).
return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel, pyFileNames).
addSrcs(srcs).
addModuleDependencies(deps).
generateImportsAttribute()
}
Expand Down Expand Up @@ -371,14 +369,9 @@ 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)})
pyTestTarget.addModuleDependency(module{Name: strings.TrimSuffix(conftestFilename, ".py")})
}

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 @@ -39,10 +39,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 @@ -71,13 +67,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: 10 additions & 17 deletions gazelle/python/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
package python

import (
"path/filepath"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/emirpasic/gods/sets/treeset"
godsutils "github.com/emirpasic/gods/utils"
"path/filepath"
)

// targetBuilder builds targets to be generated by Gazelle.
Expand All @@ -29,7 +28,6 @@ type targetBuilder struct {
name string
pythonProjectRoot string
bzlPackage string
uuid string
srcs *treeset.Set
siblingSrcs *treeset.Set
deps *treeset.Set
Expand All @@ -55,15 +53,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 @@ -81,9 +70,16 @@ 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 dep.From != "" {
fileName = dep.From + ".py"
}
if t.siblingSrcs.Contains(fileName) && fileName != filepath.Base(dep.Filepath) {
// 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 @@ -138,9 +134,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__"],
)
1 change: 1 addition & 0 deletions gazelle/python/testdata/naming_convention/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# 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
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
# limitations under the License.

import boto3
import __init__

_ = boto3
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
3 changes: 3 additions & 0 deletions gazelle/python/testdata/sibling_imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sibling imports

This test case asserts that imports from sibling modules are resolved correctly. It covers 3 different types of imports in `pkg/unit_test.py`
1 change: 1 addition & 0 deletions gazelle/python/testdata/sibling_imports/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
Empty file.
29 changes: 29 additions & 0 deletions gazelle/python/testdata/sibling_imports/pkg/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

py_library(
name = "pkg",
srcs = [
"__init__.py",
"a.py",
"b.py",
],
imports = [".."],
visibility = ["//:__subpackages__"],
)

py_test(
name = "test_util",
srcs = ["test_util.py"],
imports = [".."],
)

py_test(
name = "unit_test",
srcs = ["unit_test.py"],
imports = [".."],
deps = [
":pkg",
":test_util",
],
)

Empty file.
Empty file.
2 changes: 2 additions & 0 deletions gazelle/python/testdata/sibling_imports/pkg/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def run():
pass
Empty file.
3 changes: 3 additions & 0 deletions gazelle/python/testdata/sibling_imports/pkg/unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import a
from b import run
import test_util
1 change: 1 addition & 0 deletions gazelle/python/testdata/sibling_imports/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

# For test purposes only.
import foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
load("@rules_python//python:defs.bzl", "py_library")
30 changes: 30 additions & 0 deletions gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

py_library(
name = "bar",
srcs = [
"__init__.py",
"bar.py",
],
imports = [".."],
visibility = ["//:__subpackages__"],
)

py_library(
name = "conftest",
testonly = True,
srcs = ["conftest.py"],
imports = [".."],
visibility = ["//:__subpackages__"],
)

py_test(
name = "bar_test",
srcs = ["__test__.py"],
imports = [".."],
main = "__test__.py",
deps = [
":bar",
":conftest",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from bar import bar

_ = bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from __init__ import bar


class BarTest(unittest.TestCase):
def test_bar(self):
self.assertEqual("bar", bar())


if __name__ == "__main__":
unittest.main()
Loading