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.
diff --git a/gazelle/python/target.go b/gazelle/python/target.go
index fdc99fc..e310405 100644
--- a/gazelle/python/target.go
+++ b/gazelle/python/target.go
@@ -122,6 +122,11 @@
// case, the value we add is on Bazel sub-packages to be able to perform imports
// relative to the root project package.
func (t *targetBuilder) generateImportsAttribute() *targetBuilder {
+ if t.pythonProjectRoot == "" {
+ // When gazelle:python_root is not set or is at the root of the repo, we don't need
+ // to set imports, because that's the Bazel's default.
+ return t
+ }
p, _ := filepath.Rel(t.bzlPackage, t.pythonProjectRoot)
p = filepath.Clean(p)
if p == "." {
diff --git a/gazelle/python/testdata/dependency_resolution_order/bar/BUILD.out b/gazelle/python/testdata/dependency_resolution_order/bar/BUILD.out
index da9915d..5291471 100644
--- a/gazelle/python/testdata/dependency_resolution_order/bar/BUILD.out
+++ b/gazelle/python/testdata/dependency_resolution_order/bar/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "bar",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/dependency_resolution_order/baz/BUILD.out b/gazelle/python/testdata/dependency_resolution_order/baz/BUILD.out
index 749fd3d..fadf5c1 100644
--- a/gazelle/python/testdata/dependency_resolution_order/baz/BUILD.out
+++ b/gazelle/python/testdata/dependency_resolution_order/baz/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "baz",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/dependency_resolution_order/foo/BUILD.out b/gazelle/python/testdata/dependency_resolution_order/foo/BUILD.out
index 4404d30..58498ee 100644
--- a/gazelle/python/testdata/dependency_resolution_order/foo/BUILD.out
+++ b/gazelle/python/testdata/dependency_resolution_order/foo/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "foo",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/dependency_resolution_order/somewhere/bar/BUILD.out b/gazelle/python/testdata/dependency_resolution_order/somewhere/bar/BUILD.out
index a0d421b..5291471 100644
--- a/gazelle/python/testdata/dependency_resolution_order/somewhere/bar/BUILD.out
+++ b/gazelle/python/testdata/dependency_resolution_order/somewhere/bar/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "bar",
srcs = ["__init__.py"],
- imports = ["../.."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/first_party_file_and_directory_modules/foo/BUILD.out b/gazelle/python/testdata/first_party_file_and_directory_modules/foo/BUILD.out
index 3decd90..8c54e3c 100644
--- a/gazelle/python/testdata/first_party_file_and_directory_modules/foo/BUILD.out
+++ b/gazelle/python/testdata/first_party_file_and_directory_modules/foo/BUILD.out
@@ -6,7 +6,6 @@
"__init__.py",
"bar.py",
],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = ["//one"],
)
diff --git a/gazelle/python/testdata/first_party_file_and_directory_modules/one/BUILD.out b/gazelle/python/testdata/first_party_file_and_directory_modules/one/BUILD.out
index 7063141..3ae64b6 100644
--- a/gazelle/python/testdata/first_party_file_and_directory_modules/one/BUILD.out
+++ b/gazelle/python/testdata/first_party_file_and_directory_modules/one/BUILD.out
@@ -6,6 +6,5 @@
"__init__.py",
"two.py",
],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/from_imports/foo/BUILD.out b/gazelle/python/testdata/from_imports/foo/BUILD.out
index 4404d30..58498ee 100644
--- a/gazelle/python/testdata/from_imports/foo/BUILD.out
+++ b/gazelle/python/testdata/from_imports/foo/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "foo",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/from_imports/import_from_init_py/BUILD.out b/gazelle/python/testdata/from_imports/import_from_init_py/BUILD.out
index 99b4861..8098aa7 100644
--- a/gazelle/python/testdata/from_imports/import_from_init_py/BUILD.out
+++ b/gazelle/python/testdata/from_imports/import_from_init_py/BUILD.out
@@ -3,7 +3,6 @@
py_library(
name = "import_from_init_py",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar"],
)
\ No newline at end of file
diff --git a/gazelle/python/testdata/from_imports/import_from_multiple/BUILD.out b/gazelle/python/testdata/from_imports/import_from_multiple/BUILD.out
index d8219bb..f5e113b 100644
--- a/gazelle/python/testdata/from_imports/import_from_multiple/BUILD.out
+++ b/gazelle/python/testdata/from_imports/import_from_multiple/BUILD.out
@@ -3,7 +3,6 @@
py_library(
name = "import_from_multiple",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = [
"//foo/bar",
diff --git a/gazelle/python/testdata/from_imports/import_nested_file/BUILD.out b/gazelle/python/testdata/from_imports/import_nested_file/BUILD.out
index 662da9c..930216b 100644
--- a/gazelle/python/testdata/from_imports/import_nested_file/BUILD.out
+++ b/gazelle/python/testdata/from_imports/import_nested_file/BUILD.out
@@ -3,7 +3,6 @@
py_library(
name = "import_nested_file",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar:baz"],
)
\ No newline at end of file
diff --git a/gazelle/python/testdata/from_imports/import_nested_module/BUILD.out b/gazelle/python/testdata/from_imports/import_nested_module/BUILD.out
index ec6da50..51d3b8c 100644
--- a/gazelle/python/testdata/from_imports/import_nested_module/BUILD.out
+++ b/gazelle/python/testdata/from_imports/import_nested_module/BUILD.out
@@ -3,7 +3,6 @@
py_library(
name = "import_nested_module",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar"],
)
\ No newline at end of file
diff --git a/gazelle/python/testdata/from_imports/import_nested_var/BUILD.out b/gazelle/python/testdata/from_imports/import_nested_var/BUILD.out
index 8ee527e..2129c32 100644
--- a/gazelle/python/testdata/from_imports/import_nested_var/BUILD.out
+++ b/gazelle/python/testdata/from_imports/import_nested_var/BUILD.out
@@ -3,7 +3,6 @@
py_library(
name = "import_nested_var",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar:baz"],
)
\ No newline at end of file
diff --git a/gazelle/python/testdata/from_imports/import_top_level_var/BUILD.out b/gazelle/python/testdata/from_imports/import_top_level_var/BUILD.out
index 6b584d7..c8ef6f4 100644
--- a/gazelle/python/testdata/from_imports/import_top_level_var/BUILD.out
+++ b/gazelle/python/testdata/from_imports/import_top_level_var/BUILD.out
@@ -3,7 +3,6 @@
py_library(
name = "import_top_level_var",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
deps = ["//foo"],
)
\ No newline at end of file
diff --git a/gazelle/python/testdata/from_imports/std_module/BUILD.out b/gazelle/python/testdata/from_imports/std_module/BUILD.out
index 4903999..b3597a9 100644
--- a/gazelle/python/testdata/from_imports/std_module/BUILD.out
+++ b/gazelle/python/testdata/from_imports/std_module/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "std_module",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
\ No newline at end of file
diff --git a/gazelle/python/testdata/naming_convention/dont_rename/BUILD.out b/gazelle/python/testdata/naming_convention/dont_rename/BUILD.out
index 4d4ead8..8d418be 100644
--- a/gazelle/python/testdata/naming_convention/dont_rename/BUILD.out
+++ b/gazelle/python/testdata/naming_convention/dont_rename/BUILD.out
@@ -3,14 +3,12 @@
py_library(
name = "dont_rename",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
py_binary(
name = "my_dont_rename_binary",
srcs = ["__main__.py"],
- imports = [".."],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":dont_rename"],
@@ -19,7 +17,6 @@
py_test(
name = "my_dont_rename_test",
srcs = ["__test__.py"],
- imports = [".."],
main = "__test__.py",
deps = [":dont_rename"],
)
diff --git a/gazelle/python/testdata/naming_convention/resolve_conflict/BUILD.out b/gazelle/python/testdata/naming_convention/resolve_conflict/BUILD.out
index 3fa5de2..e155fa6 100644
--- a/gazelle/python/testdata/naming_convention/resolve_conflict/BUILD.out
+++ b/gazelle/python/testdata/naming_convention/resolve_conflict/BUILD.out
@@ -9,14 +9,12 @@
py_library(
name = "my_resolve_conflict_library",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
py_binary(
name = "my_resolve_conflict_binary",
srcs = ["__main__.py"],
- imports = [".."],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":my_resolve_conflict_library"],
@@ -25,7 +23,6 @@
py_test(
name = "my_resolve_conflict_test",
srcs = ["__test__.py"],
- imports = [".."],
main = "__test__.py",
deps = [":my_resolve_conflict_library"],
)
diff --git a/gazelle/python/testdata/python_ignore_files_directive/bar/BUILD.out b/gazelle/python/testdata/python_ignore_files_directive/bar/BUILD.out
index af3c398..94259f9 100644
--- a/gazelle/python/testdata/python_ignore_files_directive/bar/BUILD.out
+++ b/gazelle/python/testdata/python_ignore_files_directive/bar/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "bar",
srcs = ["baz.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/relative_imports/package2/BUILD.out b/gazelle/python/testdata/relative_imports/package2/BUILD.out
index bbbc9f8..cf61691 100644
--- a/gazelle/python/testdata/relative_imports/package2/BUILD.out
+++ b/gazelle/python/testdata/relative_imports/package2/BUILD.out
@@ -8,6 +8,5 @@
"module4.py",
"subpackage1/module5.py",
],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/sibling_imports/pkg/BUILD.out b/gazelle/python/testdata/sibling_imports/pkg/BUILD.out
index edb40a8..cae6c3f 100644
--- a/gazelle/python/testdata/sibling_imports/pkg/BUILD.out
+++ b/gazelle/python/testdata/sibling_imports/pkg/BUILD.out
@@ -7,20 +7,17 @@
"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",
diff --git a/gazelle/python/testdata/simple_library_without_init/foo/BUILD.out b/gazelle/python/testdata/simple_library_without_init/foo/BUILD.out
index 2faa046..8e50095 100644
--- a/gazelle/python/testdata/simple_library_without_init/foo/BUILD.out
+++ b/gazelle/python/testdata/simple_library_without_init/foo/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "foo",
srcs = ["foo.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out b/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out
index e42c499..4a1204e 100644
--- a/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out
+++ b/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out
@@ -6,7 +6,6 @@
"__init__.py",
"bar.py",
],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
@@ -14,14 +13,12 @@
name = "conftest",
testonly = True,
srcs = ["conftest.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
py_test(
name = "bar_test",
srcs = ["__test__.py"],
- imports = [".."],
main = "__test__.py",
deps = [
":bar",
diff --git a/gazelle/python/testdata/subdir_sources/foo/BUILD.out b/gazelle/python/testdata/subdir_sources/foo/BUILD.out
index f99857d..9107d2d 100644
--- a/gazelle/python/testdata/subdir_sources/foo/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/foo/BUILD.out
@@ -8,6 +8,5 @@
"baz/baz.py",
"foo.py",
],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/subdir_sources/foo/has_build/BUILD.out b/gazelle/python/testdata/subdir_sources/foo/has_build/BUILD.out
index 0ef0cc1..d5196e5 100644
--- a/gazelle/python/testdata/subdir_sources/foo/has_build/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/foo/has_build/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "has_build",
srcs = ["python/my_module.py"],
- imports = ["../.."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/subdir_sources/foo/has_init/BUILD.out b/gazelle/python/testdata/subdir_sources/foo/has_init/BUILD.out
index ce59ee2..de61008 100644
--- a/gazelle/python/testdata/subdir_sources/foo/has_init/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/foo/has_init/BUILD.out
@@ -6,6 +6,5 @@
"__init__.py",
"python/my_module.py",
],
- imports = ["../.."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/subdir_sources/foo/has_main/BUILD.out b/gazelle/python/testdata/subdir_sources/foo/has_main/BUILD.out
index 265c08b..1c56f72 100644
--- a/gazelle/python/testdata/subdir_sources/foo/has_main/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/foo/has_main/BUILD.out
@@ -3,14 +3,12 @@
py_library(
name = "has_main",
srcs = ["python/my_module.py"],
- imports = ["../.."],
visibility = ["//:__subpackages__"],
)
py_binary(
name = "has_main_bin",
srcs = ["__main__.py"],
- imports = ["../.."],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":has_main"],
diff --git a/gazelle/python/testdata/subdir_sources/foo/has_test/BUILD.out b/gazelle/python/testdata/subdir_sources/foo/has_test/BUILD.out
index 80739d9..a99278e 100644
--- a/gazelle/python/testdata/subdir_sources/foo/has_test/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/foo/has_test/BUILD.out
@@ -3,14 +3,12 @@
py_library(
name = "has_test",
srcs = ["python/my_module.py"],
- imports = ["../.."],
visibility = ["//:__subpackages__"],
)
py_test(
name = "has_test_test",
srcs = ["__test__.py"],
- imports = ["../.."],
main = "__test__.py",
deps = [":has_test"],
)
diff --git a/gazelle/python/testdata/subdir_sources/one/BUILD.out b/gazelle/python/testdata/subdir_sources/one/BUILD.out
index f2e5745..b78b650 100644
--- a/gazelle/python/testdata/subdir_sources/one/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/one/BUILD.out
@@ -3,6 +3,5 @@
py_library(
name = "one",
srcs = ["__init__.py"],
- imports = [".."],
visibility = ["//:__subpackages__"],
)
diff --git a/gazelle/python/testdata/subdir_sources/one/two/BUILD.out b/gazelle/python/testdata/subdir_sources/one/two/BUILD.out
index f632eed..8f0ac17 100644
--- a/gazelle/python/testdata/subdir_sources/one/two/BUILD.out
+++ b/gazelle/python/testdata/subdir_sources/one/two/BUILD.out
@@ -6,7 +6,6 @@
"__init__.py",
"three.py",
],
- imports = ["../.."],
visibility = ["//:__subpackages__"],
deps = ["//foo"],
)