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

fix(gazelle): _actually_ don't write narrow dependencies. #1683

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 0 additions & 5 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

parser := newPython3Parser(args.Config.RepoRoot, args.Rel, cfg.IgnoresDependency)
visibility := fmt.Sprintf("//%s:__subpackages__", pythonProjectRoot)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, this is desirable in usecases where you have a monorepo, where you have many requirements.txt files that you don't want to mix. This change would be a regression in that front.

The go plugin for gazelle has directives to control the visibility as is documented in https://github.com/bazelbuild/bazel-gazelle, maybe introducing a directive would be useful here as well?

Choose a reason for hiding this comment

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

With this PR you can still get the old behavior by adding this to your Python root BUILD file:

package(default_visibility = ["//" + package_name() + ":__subpackages__"])

There's some more context in this previous PR that accidentally included the wrong change but the right PR description.


var result language.GenerateResult
result.Gen = make([]*rule.Rule, 0)
Expand Down Expand Up @@ -247,7 +246,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
continue
}
pyBinary := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
addVisibility(visibility).
addSrc(filename).
addModuleDependencies(mainModules[filename]).
generateImportsAttribute().build()
Expand All @@ -274,7 +272,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}

pyLibrary := newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
addVisibility(visibility).
addSrcs(srcs).
addModuleDependencies(allDeps).
generateImportsAttribute().
Expand Down Expand Up @@ -322,7 +319,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes

pyBinaryTarget := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyFileNames).
setMain(pyBinaryEntrypointFilename).
addVisibility(visibility).
addSrc(pyBinaryEntrypointFilename).
addModuleDependencies(deps).
generateImportsAttribute()
Expand Down Expand Up @@ -354,7 +350,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyFileNames).
addSrc(conftestFilename).
addModuleDependencies(deps).
addVisibility(visibility).
setTestonly().
generateImportsAttribute()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ filegroup(
py_binary(
name = "main",
srcs = ["main.py"],
visibility = ["//:__subpackages__"],
deps = [
":py_default_library",
"@pip//:pandas",
Expand All @@ -22,7 +21,6 @@ py_binary(
py_binary(
name = "main2",
srcs = ["main2.py"],
visibility = ["//:__subpackages__"],
deps = [":py_default_library"],
)

Expand All @@ -34,7 +32,6 @@ py_library(
"main.py",
"main2.py",
],
visibility = ["//:__subpackages__"],
deps = [
"@pip//:numpy",
"@pip//:pandas",
Expand All @@ -44,4 +41,4 @@ py_library(
py_test(
name = "main_test",
srcs = ["main_test.py"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library")
py_library(
name = "lib",
srcs = ["lib.py"],
visibility = ["//:__subpackages__"],
deps = [
"@pip//:numpy",
"@pip//:pandas",
Expand All @@ -18,7 +17,6 @@ py_library(
py_library(
name = "lib2",
srcs = ["lib2.py"],
visibility = ["//:__subpackages__"],
deps = [
":lib",
":lib_and_main",
Expand All @@ -28,19 +26,16 @@ py_library(
py_binary(
name = "lib_and_main",
srcs = ["lib_and_main.py"],
visibility = ["//:__subpackages__"],
)

py_binary(
name = "main",
srcs = ["main.py"],
visibility = ["//:__subpackages__"],
deps = ["@pip//:pandas"],
)

py_binary(
name = "main2",
srcs = ["main2.py"],
visibility = ["//:__subpackages__"],
deps = [":lib2"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "dependency_resolution_order",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = [
"//baz",
"//somewhere/bar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "bar",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "baz",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "foo",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "bar",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "disable_import_statements_validation",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
1 change: 0 additions & 1 deletion gazelle/python/testdata/dont_rename_target/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "my_custom_target",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ py_library(
"__init__.py",
"rest_framework.py",
],
visibility = ["//:__subpackages__"],
deps = ["@gazelle_python_test//djangorestframework"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ py_binary(
name = "one_bin",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//one:__subpackages__"],
deps = [
"//one/bar",
"//one/bar/baz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "three",
srcs = ["__init__.py"],
visibility = ["//three:__subpackages__"],
deps = [
"//one/bar",
"//one/bar/baz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "two",
srcs = ["__init__.py"],
visibility = ["//two:__subpackages__"],
deps = ["//one/foo"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ py_library(
"baz.py",
"foo.py",
],
visibility = ["//:__subpackages__"],
)

py_binary(
name = "first_party_file_and_directory_modules_bin",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [
":first_party_file_and_directory_modules",
"//foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ py_library(
"__init__.py",
"bar.py",
],
visibility = ["//:__subpackages__"],
deps = ["//one"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ py_library(
"__init__.py",
"two.py",
],
visibility = ["//:__subpackages__"],
)
1 change: 0 additions & 1 deletion gazelle/python/testdata/from_imports/foo/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "foo",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
2 changes: 1 addition & 1 deletion gazelle/python/testdata/from_imports/foo/bar/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ py_library(
],
imports = ["../.."],
visibility = ["//:__subpackages__"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "import_from_init_py",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "import_from_multiple",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = [
"//foo/bar",
"//foo/bar:baz",
],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "import_nested_file",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar:baz"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "import_nested_module",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "import_nested_var",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo/bar:baz"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "import_top_level_var",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = ["//foo"],
)
)
3 changes: 1 addition & 2 deletions gazelle/python/testdata/from_imports/std_module/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "std_module",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ py_library(
"__init__.py",
"foo.py",
],
visibility = ["//:__subpackages__"],
)

py_test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "ignored_invalid_imported_module",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = ["@gazelle_python_test//foo"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ py_library(
name = "_boundary",
srcs = ["__init__.py"],
imports = [".."],
visibility = ["//coarse_grained:__subpackages__"],
)
1 change: 0 additions & 1 deletion gazelle/python/testdata/monorepo/one/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ py_binary(
name = "one_bin",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//one:__subpackages__"],
deps = [
"//one/bar",
"//one/bar/baz:modified_name_baz",
Expand Down
1 change: 0 additions & 1 deletion gazelle/python/testdata/monorepo/three/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "three",
srcs = ["__init__.py"],
visibility = ["//three:__subpackages__"],
deps = [
"//coarse_grained",
"//one/bar",
Expand Down
1 change: 0 additions & 1 deletion gazelle/python/testdata/monorepo/two/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "two",
srcs = ["__init__.py"],
visibility = ["//two:__subpackages__"],
deps = [
"//one/foo",
"@two_pip_deps//twoboto3",
Expand Down
2 changes: 0 additions & 2 deletions gazelle/python/testdata/naming_convention/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
py_library(
name = "my_naming_convention_library",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)

py_binary(
name = "my_naming_convention_binary",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":my_naming_convention_library"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ py_binary(
name = "my_dont_rename_binary",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":dont_rename"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ go_test(name = "resolve_conflict_test")
py_library(
name = "my_resolve_conflict_library",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
)

py_binary(
name = "my_resolve_conflict_binary",
srcs = ["__main__.py"],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [":my_resolve_conflict_library"],
)

Expand Down
2 changes: 0 additions & 2 deletions gazelle/python/testdata/per_file/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ py_library(
py_library(
name = "baz",
srcs = ["baz.py"],
visibility = ["//:__subpackages__"],
)

py_library(
name = "foo",
srcs = ["foo.py"],
visibility = ["//:__subpackages__"],
deps = [":custom"],
)

Expand Down
2 changes: 0 additions & 2 deletions gazelle/python/testdata/per_file_non_empty_init/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "__init__",
srcs = ["__init__.py"],
visibility = ["//:__subpackages__"],
deps = [":foo"],
)

Expand All @@ -16,5 +15,4 @@ py_library(
"__init__.py",
"foo.py",
],
visibility = ["//:__subpackages__"],
)
Loading