Skip to content

Commit e166034

Browse files
committed
Merge branch 'main' into bug/multiple-platforms-with-extras
2 parents 018f135 + d08cf53 commit e166034

File tree

22 files changed

+143
-23
lines changed

22 files changed

+143
-23
lines changed

CHANGELOG.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ BEGIN_UNRELEASED_TEMPLATE
3838
3939
{#v0-0-0-fixed}
4040
### Fixed
41-
* Nothing fixed.
41+
* (gazelle) Remove {obj}`py_binary` targets with invalid `srcs`. This includes files
42+
that are not generated or regular files.
4243
4344
{#v0-0-0-added}
4445
### Added
@@ -74,10 +75,10 @@ END_UNRELEASED_TEMPLATE
7475
* (toolchains) `py_runtime` and `PyRuntimeInfo` reject Python 2 settings.
7576
Setting `py_runtime.python_version = "PY2"` or non-None
7677
`PyRuntimeInfo.py2_runtime` is an error.
77-
* (pypi) `pipstar` flag has been flipped to be enabled by default, to turn it
78-
off use `RULES_PYTHON_ENABLE_PIPSTAR=0` environment variable. If you do, please
78+
* (pypi) `pipstar` flag has been implemented for `WORKSPACE` and can be flipped to be enabled using `RULES_PYTHON_ENABLE_PIPSTAR=1` environment variable. If you do, please
7979
add a comment to
80-
[#2949](https://github.com/bazel-contrib/rules_python/issues/2949).
80+
[#2949](https://github.com/bazel-contrib/rules_python/issues/2949) if you run into any
81+
problems.
8182
With this release we are deprecating {obj}`pip.parse.experimental_target_platforms` and
8283
{obj}`pip_repository.experimental_target_platforms`. For users using `WORKSPACE` and
8384
vendoring the `requirements.bzl` file, please re-vendor so that downstream is unaffected
@@ -112,6 +113,8 @@ END_UNRELEASED_TEMPLATE
112113
([#3339](https://github.com/bazel-contrib/rules_python/issues/3339)).
113114
* (uv) {obj}`//python/uv:lock.bzl%lock` now works with a local platform
114115
runtime.
116+
* (pypi) `linux_riscv64` is added to the platforms list in `_pip_repository_impl`,
117+
which fixes [a build issue for tensorflow on riscv64](https://github.com/bazel-contrib/rules_python/discussions/2729).
115118
* (toolchains) WORKSPACE builds now correctly register musl and freethreaded
116119
variants. Setting {obj}`--py_linux_libc=musl` and `--py_freethreaded=yes` now
117120
activate them, respectively.

gazelle/examples/bzlmod_build_file_generation/.bazelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ common --experimental_enable_bzlmod
77

88
coverage --java_runtime_version=remotejdk_11
99
common:bazel7.x --incompatible_python_disallow_native_rules
10+
11+
# NOTE: This override is specific to the development of gazelle itself
12+
# and the testing of it during its BCR release presubmits.
13+
# In development of gazelle itself, we override it to the development
14+
# rules_python code. In the BCR presubmits, this override is removed
15+
# and the bazel_dep version of rules_python is used.
16+
common --override_module=rules_python=../../../

gazelle/examples/bzlmod_build_file_generation/MODULE.bazel

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ module(
1313
# For typical setups you set the version.
1414
# See the releases page for available versions.
1515
# https://github.com/bazel-contrib/rules_python/releases
16-
bazel_dep(name = "rules_python", version = "1.0.0")
17-
18-
# NOTE: This override is removed for BCR presubmits and the version
19-
# specified by bazel_dep() is used instead.
20-
# The following loads rules_python from the file system.
21-
# For usual setups you should remove this local_path_override block.
22-
local_path_override(
23-
module_name = "rules_python",
24-
path = "../../..",
25-
)
16+
bazel_dep(name = "rules_python", version = "1.4.0")
2617

2718
# The following stanza defines the dependency rules_python_gazelle_plugin.
2819
# For typical setups you set the version.

gazelle/python/generate.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,14 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
231231
}
232232

233233
collisionErrors := singlylinkedlist.New()
234+
// Create a validFilesMap of mainModules to validate if python macros have valid srcs.
235+
validFilesMap := make(map[string]struct{})
234236

235237
appendPyLibrary := func(srcs *treeset.Set, pyLibraryTargetName string) {
236238
allDeps, mainModules, annotations, err := parser.parse(srcs)
239+
for name := range mainModules {
240+
validFilesMap[name] = struct{}{}
241+
}
237242
if err != nil {
238243
log.Fatalf("ERROR: %v\n", err)
239244
}
@@ -363,6 +368,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
363368
setAnnotations(*annotations).
364369
generateImportsAttribute()
365370

371+
366372
pyBinary := pyBinaryTarget.build()
367373

368374
result.Gen = append(result.Gen, pyBinary)
@@ -490,7 +496,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
490496
result.Gen = append(result.Gen, pyTest)
491497
result.Imports = append(result.Imports, pyTest.PrivateAttr(config.GazelleImportsKey))
492498
}
493-
499+
emptyRules := py.getRulesWithInvalidSrcs(args, validFilesMap)
500+
result.Empty = append(result.Empty, emptyRules...)
494501
if !collisionErrors.Empty() {
495502
it := collisionErrors.Iterator()
496503
for it.Next() {
@@ -502,6 +509,42 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
502509
return result
503510
}
504511

512+
// getRulesWithInvalidSrcs checks existing Python rules in the BUILD file and return the rules with invalid source files.
513+
// Invalid source files are files that do not exist or not a target.
514+
func (py *Python) getRulesWithInvalidSrcs(args language.GenerateArgs, validFilesMap map[string]struct{}) (invalidRules []*rule.Rule) {
515+
if args.File == nil {
516+
return
517+
}
518+
for _, file := range args.GenFiles {
519+
validFilesMap[file] = struct{}{}
520+
}
521+
522+
isTarget := func(src string) bool {
523+
return strings.HasPrefix(src, "@") || strings.HasPrefix(src, "//") || strings.HasPrefix(src, ":")
524+
}
525+
for _, existingRule := range args.File.Rules {
526+
actualPyBinaryKind := GetActualKindName(pyBinaryKind, args)
527+
if existingRule.Kind() != actualPyBinaryKind {
528+
continue
529+
}
530+
var hasValidSrcs bool
531+
for _, src := range existingRule.AttrStrings("srcs") {
532+
if isTarget(src) {
533+
hasValidSrcs = true
534+
break
535+
}
536+
if _, ok := validFilesMap[src]; ok {
537+
hasValidSrcs = true
538+
break
539+
}
540+
}
541+
if !hasValidSrcs {
542+
invalidRules = append(invalidRules, newTargetBuilder(pyBinaryKind, existingRule.Name(), "", "", nil, false).build())
543+
}
544+
}
545+
return invalidRules
546+
}
547+
505548
// isBazelPackage determines if the directory is a Bazel package by probing for
506549
// the existence of a known BUILD file name.
507550
func isBazelPackage(dir string) bool {

gazelle/python/kinds.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var pyKinds = map[string]rule.KindInfo{
4646
SubstituteAttrs: map[string]bool{},
4747
MergeableAttrs: map[string]bool{
4848
"srcs": true,
49+
"imports": true,
4950
},
5051
ResolveAttrs: map[string]bool{
5152
"deps": true,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
2+
3+
py_library(
4+
name = "keep_library",
5+
deps = ["//keep_binary:foo"],
6+
)
7+
py_binary(
8+
name = "remove_invalid_binary",
9+
srcs = ["__main__.py"],
10+
data = ["testdata/test.txt"],
11+
visibility = ["//:__subpackages__"],
12+
)
13+
14+
py_binary(
15+
name = "another_removed_binary",
16+
srcs = ["foo.py"], # eg a now-deleted file that used to have `if __name__` block
17+
imports = ["."],
18+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "keep_library",
5+
deps = ["//keep_binary:foo"],
6+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Remove invalid binary
2+
3+
This test case asserts that `py_binary` should be deleted if invalid (no source files).

gazelle/python/testdata/remove_invalid_binary/WORKSPACE

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
2+
3+
py_binary(
4+
name = "foo",
5+
srcs = ["foo.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
8+
9+
py_library(
10+
name = "keep_binary",
11+
srcs = ["foo.py"],
12+
visibility = ["//:__subpackages__"],
13+
)

0 commit comments

Comments
 (0)