Skip to content

Commit

Permalink
fix(gazelle): use Python 3.11 in tests and use absolute paths in helper
Browse files Browse the repository at this point in the history
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 moves the python
code to a different location to ensure that the top level package is
something more unique than just "python" 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.

Fixes #1589
  • Loading branch information
aignas committed Dec 2, 2023
1 parent 89be2e0 commit 72372c3
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ A brief description of the categories of changes:
is also available under bzlmod as
`pip.parse(experimental_requirement_cycles={})`.

### Fixed

* (gazelle) The gazelle plugin helper was not working with Python toolchains 3.11
and above due to a bug in the helper entrypoint import paths.

[0.XX.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.XX.0

## [0.27.0] - 2023-11-16
Expand Down
8 changes: 4 additions & 4 deletions gazelle/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ go_library(
py_binary(
name = "helper",
srcs = [
"__main__.py",
"parse.py",
"std_modules.py",
"//rules_python_gazelle_helper:__main__.py",
"//rules_python_gazelle_helper:parse.py",
"//rules_python_gazelle_helper:std_modules.py",
],
main = "__main__.py",
main = "//rules_python_gazelle_helper:__main__.py",
visibility = ["//visibility:public"],
)

Expand Down
2 changes: 1 addition & 1 deletion gazelle/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) {
cmd.Dir = workspaceRoot
helperScript, err := runfiles.Rlocation("rules_python_gazelle_plugin/python/helper")
if err != nil {
t.Fatalf("failed to initialize Python heler: %v", err)
t.Fatalf("failed to initialize Python helper: %v", err)
}
cmd.Env = append(os.Environ(), "GAZELLE_PYTHON_HELPER="+helperScript)
if err := cmd.Run(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions gazelle/rules_python_gazelle_helper/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports_files(
srcs = glob(["*.py"]),
visibility = ["//python:__pkg__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
# STDIN receives parse requests, one per line. It outputs the parsed modules and
# comments from all the files from each request.

import parse
import std_modules
import sys

# NOTE @aignas 2023-12-02: Use absolute imports with respect to WORKSPACE root.
# With Python versions other than 3.11 doing import parse import std_modules
# works fine, but with 3.11 we need to use absolute import paths, which could be
# due to differences in the bootstrap template in 3.11, which is more strict.
#
# We are also using a unique name to avoid any name clashes
from rules_python_gazelle_helper import parse, std_modules

if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit("Please provide subcommand, either print or std_modules")
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 72372c3

Please sign in to comment.