8000 fix(gazelle): ensure that gazelle helper modules are on PYTHONPATH (#… · kshramt/rules_python@bc5a0b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc5a0b0

Browse files
authored
fix(gazelle): ensure that gazelle helper modules are on PYTHONPATH (bazel-contrib#1590)
Before this change there was a bug in how the parsing helpers were being used in case we were using Python 3.11 toolchain, which is using a more strict version of the entrypoint template. This change adds `imports = ["."]` to ensure that the gazelle helper components are on PYTHONPATH and updates the non-bzlmod tests to run under 3.11. We also: * Change `.bazelrc` to use explicit `__init__.py` definition to avoid non-reproducible errors in the future. * Add a dedicated `gazelle_binary` that uses `DEFAULT_LANGUAGES` *and* `//python`. Fixes bazel-contrib#1589
1 parent 6695fe1 commit bc5a0b0

File tree

10 files changed

+45
-11
lines changed

10 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
3131
is also available under bzlmod as
3232
`pip.parse(experimental_requirement_cycles={})`.
3333

34+
### Fixed
35+
36+
* (gazelle) The gazelle plugin helper was not working with Python toolchains 3.11
37+
and above due to a bug in the helper components not being on PYTHONPATH.
38+
3439
[0.XX.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.XX.0
3540

3641
## [0.27.0] - 2023-11-16

gazelle/.bazelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ build --incompatible_default_to_explicit_init_py
1111
# Windows makes use of runfiles for some rules
1212
build --enable_runfiles
1313
startup --windows_enable_symlinks
14+
15+
# Do NOT implicitly create empty __init__.py files in the runfiles tree.
16+
# By default, these are created in every directory containing Python source code
17+
# or shared libraries, and every parent directory of those directories,
18+
# excluding the repo root directory. With this flag set, we are responsible for
19+
# creating (possibly empty) __init__.py files and adding them to the srcs of
20+
# Python targets as required.
21+
build --incompatible_default_to_explicit_init_py

gazelle/BUILD.bazel

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
load("@bazel_gazelle//:def.bzl", "gazelle")
1+
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
22

33
# Gazelle configuration options.
44
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
55
# gazelle:prefix github.com/bazelbuild/rules_python/gazelle
66
# gazelle:exclude bazel-out
7-
gazelle(name = "gazelle")
7+
gazelle(
8+
name = "gazelle",
9+
gazelle = ":gazelle_binary",
10+
)
11+
12+
gazelle_binary(
13+
name = "gazelle_binary",
14+
languages = DEFAULT_LANGUAGES + ["//python"],
15+
)
816

917
gazelle(
1018
name = "gazelle_update_repos",

gazelle/WORKSPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist
3939
py_repositories()
4040

4141
python_register_toolchains(
42-
name = "python39",
43-
python_version = "3.9",
42+
name = "python_3_11",
43+
python_version = "3.11",
4444
)
4545

4646
load("//:deps.bzl", _py_gazelle_deps = "gazelle_deps")

gazelle/modules_mapping/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
load("@rules_python//python:defs.bzl", "py_binary")
22

3+
# gazelle:exclude *.py
4+
35
py_binary(
46
name = "generator",
57
srcs = ["generator.py"],

gazelle/python/BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ go_library(
1717
"std_modules.go",
1818
"target.go",
1919
],
20-
embedsrcs = [":helper.zip"],
20+ # NOTE @aignas 2023-12-03: currently gazelle does not support embedding
21+
# generated files, but helper.zip is generated by a build rule.
22+
#
23+
# You will get a benign error like when running gazelle locally:
24+
# > 8 gazelle: .../rules_python/gazelle/python/lifecycle.go:26:3: pattern helper.zip: matched no files
25+
#
26+
# See following for more info:
27+
# https://github.com/bazelbuild/bazel-gazelle/issues/1513
28+
embedsrcs = [":helper.zip"], # keep
2129
importpath = "github.com/bazelbuild/rules_python/gazelle/python",
2230
visibility = ["//visibility:public"],
2331
deps = [
@@ -44,6 +52,8 @@ py_binary(
4452
"parse.py",
4553
"std_modules.py",
4654
],
55+
# This is to make sure that the current directory is added to PYTHONPATH
56+
imports = ["."],
4757
main = "__main__.py",
4858
visibility = ["//visibility:public"],
4959
)
@@ -54,6 +64,8 @@ filegroup(
5464
output_group = "python_zip_file",
5565
)
5666

67+
# gazelle:exclude testdata/
68+
5769
gazelle_test(
5870
name = "python_test",
5971
srcs = ["python_test.go"],

gazelle/python/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# STDIN receives parse requests, one per line. It outputs the parsed modules and
1717
# comments from all the files from each request.
1818

19+
import sys
20+
1921
import parse
2022
import std_modules
21-
import sys
2223

2324
if __name__ == "__main__":
2425
if len(sys.argv) < 2:

gazelle/python/python_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) {
162162
cmd.Dir = workspaceRoot
163163
helperScript, err := runfiles.Rlocation("rules_python_gazelle_plugin/python/helper")
164164
if err != nil {
165-
t.Fatalf("failed to initialize Python heler: %v", err)
165+
t.Fatalf("failed to initialize Python helper: %v", err)
166166
}
167167
cmd.Env = append(os.Environ(), "GAZELLE_PYTHON_HELPER="+helperScript)
168168
if err := cmd.Run(); err != nil {

gazelle/pythonconfig/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ go_library(
1818
go_test(
1919
name = "pythonconfig_test",
2020
srcs = ["pythonconfig_test.go"],
21-
deps = [":pythonconfig"],
21+
embed = [":pythonconfig"],
2222
)
2323

2424
filegroup(

gazelle/pythonconfig/pythonconfig_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package pythonconfig
22

33
import (
44
"testing"
5-
6-
"github.com/bazelbuild/rules_python/gazelle/pythonconfig"
75
)
86

97
func TestDistributionSanitizing(t *testing.T) {
@@ -19,7 +17,7 @@ func TestDistributionSanitizing(t *testing.T) {
1917

2018
for name, tc := range tests {
2119
t.Run(name, func(t *testing.T) {
22-
got := pythonconfig.SanitizeDistribution(tc.input)
20+
got := SanitizeDistribution(tc.input)
2321
if tc.want != got {
2422
t.Fatalf("expected %q, got %q", tc.want, got)
2523
}

0 commit comments

Comments
 (0)
0