Skip to content

Commit bb8c485

Browse files
authored
feat: stop generating imports when not necessary (#1335)
When gazelle:python_root is not set or is at the root of the repo, we don't need to set imports for python rules, because that's the Bazel's default. This would reduce unnecessary verbosity.
1 parent 0642390 commit bb8c485

File tree

29 files changed

+5
-38
lines changed

29 files changed

+5
-38
lines changed

gazelle/python/target.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ func (t *targetBuilder) setTestonly() *targetBuilder {
122122
// case, the value we add is on Bazel sub-packages to be able to perform imports
123123
// relative to the root project package.
124124
func (t *targetBuilder) generateImportsAttribute() *targetBuilder {
125+
if t.pythonProjectRoot == "" {
126+
// When gazelle:python_root is not set or is at the root of the repo, we don't need
127+
// to set imports, because that's the Bazel's default.
128+
return t
129+
}
125130
p, _ := filepath.Rel(t.bzlPackage, t.pythonProjectRoot)
126131
p = filepath.Clean(p)
127132
if p == "." {

gazelle/python/testdata/dependency_resolution_order/bar/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "bar",
55
srcs = ["__init__.py"],
6-
imports = [".."],
76
visibility = ["//:__subpackages__"],
87
)

gazelle/python/testdata/dependency_resolution_order/baz/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "baz",
55
srcs = ["__init__.py"],
6-
imports = [".."],
76
visibility = ["//:__subpackages__"],
87
)

gazelle/python/testdata/dependency_resolution_order/foo/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "foo",
55
srcs = ["__init__.py"],
6-
imports = [".."],
76
visibility = ["//:__subpackages__"],
87
)

gazelle/python/testdata/dependency_resolution_order/somewhere/bar/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "bar",
55
srcs = ["__init__.py"],
6-
imports = ["../.."],
76
visibility = ["//:__subpackages__"],
87
)

gazelle/python/testdata/first_party_file_and_directory_modules/foo/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ py_library(
66
"__init__.py",
77
"bar.py",
88
],
9-
imports = [".."],
109
visibility = ["//:__subpackages__"],
1110
deps = ["//one"],
1211
)

gazelle/python/testdata/first_party_file_and_directory_modules/one/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ py_library(
66
"__init__.py",
77
"two.py",
88
],
9-
imports = [".."],
109
visibility = ["//:__subpackages__"],
1110
)

gazelle/python/testdata/from_imports/foo/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "foo",
55
srcs = ["__init__.py"],
6-
imports = [".."],
76
visibility = ["//:__subpackages__"],
87
)

gazelle/python/testdata/from_imports/import_from_init_py/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "import_from_init_py",
55
srcs = ["__init__.py"],
6-
imports = [".."],
76
visibility = ["//:__subpackages__"],
87
deps = ["//foo/bar"],
98
)

gazelle/python/testdata/from_imports/import_from_multiple/BUILD.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "import_from_multiple",
55
srcs = ["__init__.py"],
6-
imports = [".."],
76
visibility = ["//:__subpackages__"],
87
deps = [
98
"//foo/bar",

0 commit comments

Comments
 (0)