From 77732baeab63f8329d41f45991761b2278b77617 Mon Sep 17 00:00:00 2001 From: Jimmy Tanner Date: Wed, 19 Feb 2025 19:33:21 -0800 Subject: [PATCH 1/4] MRE for gazelle bug with partial py_binary update --- .../BUILD.in | 9 +++++++++ .../BUILD.out | 15 +++++++++++++++ .../README.md | 3 +++ .../WORKSPACE | 1 + .../a.py | 3 +++ .../b.py | 2 ++ .../test.yaml | 17 +++++++++++++++++ 7 files changed, 50 insertions(+) create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.out create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/README.md create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/WORKSPACE create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/b.py create mode 100644 gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in new file mode 100644 index 0000000000..609341a5f3 --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in @@ -0,0 +1,9 @@ +load("@rules_python//python:defs.bzl", "py_binary") + +# gazelle:python_generation_mode file + +py_binary( + name = "a", + srcs = ["a.py"], + visibility = ["//:__subpackages__"], +) \ No newline at end of file diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.out b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.out new file mode 100644 index 0000000000..8f49cccd9f --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.out @@ -0,0 +1,15 @@ +load("@rules_python//python:defs.bzl", "py_binary") + +# gazelle:python_generation_mode file + +py_binary( + name = "a", + srcs = ["a.py"], + visibility = ["//:__subpackages__"], +) + +py_binary( + name = "b", + srcs = ["b.py"], + visibility = ["//:__subpackages__"], +) diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/README.md b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/README.md new file mode 100644 index 0000000000..5aa499f4ad --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/README.md @@ -0,0 +1,3 @@ +# Partial update with multiple per-file binaries + +This test case asserts that when there are multiple binaries in a package, and no __main__.py, and the BUILD file already includes a py_binary for one of the files, a py_binary is generated for the other file. diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/WORKSPACE b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/WORKSPACE new file mode 100644 index 0000000000..faff6af87a --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/WORKSPACE @@ -0,0 +1 @@ +# This is a Bazel workspace for the Gazelle test data. diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py new file mode 100644 index 0000000000..b9388b0557 --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py @@ -0,0 +1,3 @@ +if __name__ == "__main__": + print("Hello, world!") + diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/b.py b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/b.py new file mode 100644 index 0000000000..9c97da4809 --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/b.py @@ -0,0 +1,2 @@ +if __name__ == "__main__": + print("Hello, world!") diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml new file mode 100644 index 0000000000..2410223e59 --- /dev/null +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml @@ -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 From 1c4e608285e14a9e99115282bb5d8515c95d8133 Mon Sep 17 00:00:00 2001 From: Jimmy Tanner Date: Wed, 19 Feb 2025 19:41:48 -0800 Subject: [PATCH 2/4] Fix missing eof newline --- .../BUILD.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in index 609341a5f3..63b547f0b3 100644 --- a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/BUILD.in @@ -6,4 +6,4 @@ py_binary( name = "a", srcs = ["a.py"], visibility = ["//:__subpackages__"], -) \ No newline at end of file +) From 58ba13b141f94bc9be1ad3b33bc50a56e9f52c74 Mon Sep 17 00:00:00 2001 From: Jimmy Tanner Date: Sat, 22 Feb 2025 07:56:31 -0800 Subject: [PATCH 3/4] Fix bug --- gazelle/python/kinds.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazelle/python/kinds.go b/gazelle/python/kinds.go index a9483372e2..e1b03d6308 100644 --- a/gazelle/python/kinds.go +++ b/gazelle/python/kinds.go @@ -32,7 +32,7 @@ func (*Python) Kinds() map[string]rule.KindInfo { var pyKinds = map[string]rule.KindInfo{ pyBinaryKind: { - MatchAny: true, + MatchAny: false, NonEmptyAttrs: map[string]bool{ "deps": true, "main": true, From 7e7a6de21e28ce04d456f5d8a0c7c60bb861d4e6 Mon Sep 17 00:00:00 2001 From: Jimmy Tanner Date: Mon, 24 Feb 2025 21:18:38 -0800 Subject: [PATCH 4/4] Address comments --- CHANGELOG.md | 2 ++ gazelle/python/kinds.go | 1 + .../a.py | 1 - .../test.yaml | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a62ab7840..1c075af80b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,8 @@ Unreleased changes template. {#v0-0-0-fixed} ### Fixed * (pypi) The `ppc64le` is now pointing to the right target in the `platforms` package. +* (gazelle) No longer incorrectly merge `py_binary` targets during partial updates in + `file` generation mode. Fixed in [#2619](https://github.com/bazelbuild/rules_python/pull/2619). {#v0-0-0-added} ### Added diff --git a/gazelle/python/kinds.go b/gazelle/python/kinds.go index e1b03d6308..7a0639abd3 100644 --- a/gazelle/python/kinds.go +++ b/gazelle/python/kinds.go @@ -33,6 +33,7 @@ func (*Python) Kinds() map[string]rule.KindInfo { var pyKinds = map[string]rule.KindInfo{ pyBinaryKind: { MatchAny: false, + MatchAttrs: []string{"srcs"}, NonEmptyAttrs: map[string]bool{ "deps": true, "main": true, diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py index b9388b0557..9c97da4809 100644 --- a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/a.py @@ -1,3 +1,2 @@ if __name__ == "__main__": print("Hello, world!") - diff --git a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml index 2410223e59..346ecd7ae8 100644 --- a/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml +++ b/gazelle/python/testdata/binary_without_entrypoint_per_file_generation_partial_update/test.yaml @@ -1,4 +1,4 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. +# Copyright 2025 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.