Skip to content

Skip indexing py_binary rules if a corresponding py_library rule contains the same srcs #2822

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (*Resolver) Name() string { return languageName }
// If nil is returned, the rule will not be indexed. If any non-nil slice is
// returned, including an empty slice, the rule will be indexed.
func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
if !indexPyBinaryImport(r, f) {
return nil
}
cfgs := c.Exts[languageName].(pythonconfig.Configs)
cfg := cfgs[f.Pkg]
srcs := r.AttrStrings("srcs")
Expand Down Expand Up @@ -317,3 +320,29 @@ func convertDependencySetToExpr(set *treeset.Set) bzl.Expr {
}
return &bzl.ListExpr{List: deps}
}

// indexPyBinaryImport returns whether the corresponding py_binary rule need to be indexed.
// To avoid multiple labels indexing the same import,
// check if there is a corresponding py_library rule with the same srcs.
func indexPyBinaryImport(r *rule.Rule, f *rule.File) bool {
// If the rule is not a py_binary, it should be indexed.
if r.Kind() != "py_binary" {
return true
}
pyBinarySrcs := r.AttrStrings("srcs")
if len(pyBinarySrcs) == 0 {
return false
}
for _, otherRule := range f.Rules {
if otherRule.Kind() != "py_library" {
continue
}
pyLibrarySrcs := otherRule.AttrStrings("srcs")
for _, src := range pyLibrarySrcs {
if src == pyBinarySrcs[0] {
return false
}
}
}
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# gazelle:python_extension enabled
# gazelle:python_library_naming_convention py_default_library
# gazelle:python_generation_mode package
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# gazelle:python_extension enabled
# gazelle:python_library_naming_convention py_default_library
# gazelle:python_generation_mode package
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Import with same srcs for library and binary
This test case asserts a file with py_library and py_binary rules that include the same .py file in srcs will resolve to the py_library rule correctly.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "bar",
srcs = ["bar.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "bar",
srcs = ["bar.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo:py_default_library"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_python//python:defs.bzl", "py_binary", "py_library")

py_binary(
name = "script",
srcs = ["script.py"],
visibility = ["//:__subpackages__"],
)

py_library(
name = "py_default_library",
srcs = ["script.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

if __name__ == "__main__":
print("Hello, world!")
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.

---
expect:
exit_code: 0