diff options
author | Zhongpeng Lin <zplin@uber.com> | 2023-12-17 03:38:55 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-17 11:38:55 +0000 |
commit | e1c9ad5c95fee4d6d5cfeaa9bd544577972834fe (patch) | |
tree | 4ebf99bfa9f0d64dcde251d06e7b21ea985c9f36 | |
parent | b99027fbd0a62d25c933e7efee0fff4329bfed59 (diff) | |
download | bazelbuild-rules_python-e1c9ad5c95fee4d6d5cfeaa9bd544577972834fe.tar.gz |
fix: trimming deps in py_binary (#1614)
The `py_binary` targets for main modules should only depend on the
modules that it imports, not blindly inheriting from the py_library.
6 files changed, 22 insertions, 3 deletions
diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index 8d147d9..5ab9f53 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -220,7 +220,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes collisionErrors := singlylinkedlist.New() appendPyLibrary := func(srcs *treeset.Set, pyLibraryTargetName string) { - deps, mainModules, err := parser.parse(srcs) + allDeps, mainModules, err := parser.parse(srcs) if err != nil { log.Fatalf("ERROR: %v\n", err) } @@ -248,10 +248,13 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes fqTarget.String(), actualPyBinaryKind, err) continue } + binaryDeps := allDeps.Select(func(index int, value interface{}) bool { + return value.(module).Filepath == filepath.Join(args.Rel, filename) + }) pyBinary := newTargetBuilder(pyBinaryKind, pyBinaryTargetName, pythonProjectRoot, args.Rel, pyFileNames). addVisibility(visibility). addSrc(filename). - addModuleDependencies(deps). + addModuleDependencies(binaryDeps). generateImportsAttribute().build() result.Gen = append(result.Gen, pyBinary) result.Imports = append(result.Imports, pyBinary.PrivateAttr(config.GazelleImportsKey)) @@ -261,7 +264,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes pyLibrary := newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyFileNames). addVisibility(visibility). addSrcs(srcs). - addModuleDependencies(deps). + addModuleDependencies(allDeps). generateImportsAttribute(). build() diff --git a/gazelle/python/testdata/binary_without_entrypoint/BUILD.in b/gazelle/python/testdata/binary_without_entrypoint/BUILD.in index 7aace67..1177dce 100644 --- a/gazelle/python/testdata/binary_without_entrypoint/BUILD.in +++ b/gazelle/python/testdata/binary_without_entrypoint/BUILD.in @@ -1,4 +1,6 @@ # gazelle:python_library_naming_convention py_default_library +# gazelle:resolve py numpy @pip//:numpy +# gazelle:resolve py pandas @pip//:pandas filegroup( name = "collided_main", diff --git a/gazelle/python/testdata/binary_without_entrypoint/BUILD.out b/gazelle/python/testdata/binary_without_entrypoint/BUILD.out index 9dae147..72fc670 100644 --- a/gazelle/python/testdata/binary_without_entrypoint/BUILD.out +++ b/gazelle/python/testdata/binary_without_entrypoint/BUILD.out @@ -1,6 +1,8 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") # gazelle:python_library_naming_convention py_default_library +# gazelle:resolve py numpy @pip//:numpy +# gazelle:resolve py pandas @pip//:pandas filegroup( name = "collided_main", @@ -11,12 +13,14 @@ py_binary( name = "main", srcs = ["main.py"], visibility = ["//:__subpackages__"], + deps = ["@pip//:pandas"], ) py_binary( name = "main2", srcs = ["main2.py"], visibility = ["//:__subpackages__"], + deps = [":py_default_library"], ) py_library( @@ -28,6 +32,10 @@ py_library( "main2.py", ], visibility = ["//:__subpackages__"], + deps = [ + "@pip//:numpy", + "@pip//:pandas", + ], ) py_test( diff --git a/gazelle/python/testdata/binary_without_entrypoint/collided_main.py b/gazelle/python/testdata/binary_without_entrypoint/collided_main.py index 3668fcc..3bf59c7 100644 --- a/gazelle/python/testdata/binary_without_entrypoint/collided_main.py +++ b/gazelle/python/testdata/binary_without_entrypoint/collided_main.py @@ -1,2 +1,4 @@ +import numpy + if __name__ == "__main__": run()
\ No newline at end of file diff --git a/gazelle/python/testdata/binary_without_entrypoint/main.py b/gazelle/python/testdata/binary_without_entrypoint/main.py index 3668fcc..f13cbe5 100644 --- a/gazelle/python/testdata/binary_without_entrypoint/main.py +++ b/gazelle/python/testdata/binary_without_entrypoint/main.py @@ -1,2 +1,4 @@ +import pandas + if __name__ == "__main__": run()
\ No newline at end of file diff --git a/gazelle/python/testdata/binary_without_entrypoint/main2.py b/gazelle/python/testdata/binary_without_entrypoint/main2.py index 84e642a..a82a5e6 100644 --- a/gazelle/python/testdata/binary_without_entrypoint/main2.py +++ b/gazelle/python/testdata/binary_without_entrypoint/main2.py @@ -1,2 +1,4 @@ +import collided_main + if __name__ == "__main__": run() |