Skip to content

Commit 15cc0b3

Browse files
dougthor42maffooaignas
authored
fix(gazelle): Support parsing files that use Python3.12 PEP 695 (Type Parameter Syntax) by using dougthor42's fork of go-tree-sitter (#2496)
Replaces #2413. Fixes #2396. This updates the `go-tree-sitter` dependency to use my fork that includes `BUILD.bazel` files. Specifically, the `BUILD.bazel` files in the fork include references to top-level code like `array.h` which the original Gazelle-generated files for `go-tree-sitter` were not able to handle. I also include the test cases that @maffoo created in #2413 and verified that they (a) fail before the fix and (b) pass after the fix. The fork is: https://github.com/dougthor42/go-tree-sitter The branch that includes all changes is: https://github.com/dougthor42/go-tree-sitter/tree/for-rules-python-gazelle-plugin A couple notes: + I have a PR open to get `go-tree-sitter` into BCR [here](bazelbuild/bazel-central-registry#3366). However: 1. I'm having trouble getting tests to pass and to get things running locally to validate it 2. Using BCR would not fix things for people who still use WORKSPACE (right?) + The fork is _mostly_ [autogenerated BUILD.bazel files from gazelle](smacker/go-tree-sitter@cfa9bdf) but also contains: + [manual updates so that build files reference the toplevel `array.h` and other files](smacker/go-tree-sitter@63f89cd) + [replace all `smacker` with `dougthor42` so that `go build` works](smacker/go-tree-sitter@8a73cbd) + various other more minor things. + I was unable to get `go mod edit -replace` to work, so I've just manually updated `go.mod` and whatnot everywhere. If someone with more go knowledge has a suggestion I'm happy to hear it. --------- Co-authored-by: Matthew Neeley <[email protected]> Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent 94c77e2 commit 15cc0b3

15 files changed

+65
-22
lines changed

Diff for: CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ Unreleased changes template.
6565
available for all of the platforms and the sdist can be built. This fix is
6666
for both WORKSPACE and `bzlmod` setups.
6767
Fixes [#2450](https://github.com/bazelbuild/rules_python/issues/2450).
68+
* (gazelle) Gazelle will now correctly parse Python3.12 files that use [PEP 695 Type
69+
Parameter Syntax][pep-695]. (#2396)
70+
71+
[pep-695]: https://peps.python.org/pep-0695/
6872

6973
{#v0-0-0-added}
7074
### Added
7175
* (gazelle) Added `include_stub_packages` flag to `modules_mapping`. When set to `True`, this
7276
automatically includes corresponding stub packages for third-party libraries
73-
that are present and used (e.g., `boto3``boto3-stubs`), improving
77+
that are present and used (e.g., `boto3``boto3-stubs`), improving
7478
type-checking support.
7579
* (pypi) Freethreaded packages are now fully supported in the
7680
{obj}`experimental_index_url` usage or the regular `pip.parse` usage.
@@ -137,7 +141,7 @@ Other changes:
137141
* (repositories): Add libs/python3.lib and pythonXY.dll to the `libpython` target
138142
defined by a repository template. This enables stable ABI builds of Python extensions
139143
on Windows (by defining Py_LIMITED_API).
140-
* (rules) `py_test` and `py_binary` targets no longer incorrectly remove the
144+
* (rules) `py_test` and `py_binary` targets no longer incorrectly remove the
141145
first `sys.path` entry when using {obj}`--bootstrap_impl=script`
142146

143147
{#v1-0-0-added}

Diff for: gazelle/MODULE.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use_repo(
2121
go_deps,
2222
"com_github_bazelbuild_buildtools",
2323
"com_github_bmatcuk_doublestar_v4",
24+
"com_github_dougthor42_go_tree_sitter",
2425
"com_github_emirpasic_gods",
2526
"com_github_ghodss_yaml",
26-
"com_github_smacker_go_tree_sitter",
2727
"com_github_stretchr_testify",
2828
"in_gopkg_yaml_v2",
2929
"org_golang_x_sync",

Diff for: gazelle/deps.bzl

+4-4
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ def go_deps():
186186
version = "v0.0.0-20190812154241-14fe0d1b01d4",
187187
)
188188
go_repository(
189-
name = "com_github_smacker_go_tree_sitter",
190-
importpath = "github.com/smacker/go-tree-sitter",
191-
sum = "h1:7QZKUmQfnxncZIJGyvX8M8YeMfn8kM10j3J/2KwVTN4=",
192-
version = "v0.0.0-20240422154435-0628b34cbf9c",
189+
name = "com_github_dougthor42_go_tree_sitter",
190+
importpath = "github.com/dougthor42/go-tree-sitter",
191+
sum = "h1:b9s96BulIARx0konX36sJ5oZhWvAvjQBBntxp1eUukQ=",
192+
version = "v0.0.0-20241210060307-2737e1d0de6b",
193193
)
194194
go_repository(
195195
name = "com_github_stretchr_objx",

Diff for: gazelle/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ require (
77
github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82
88
github.com/bazelbuild/rules_go v0.41.0
99
github.com/bmatcuk/doublestar/v4 v4.6.1
10+
github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b
1011
github.com/emirpasic/gods v1.18.1
1112
github.com/ghodss/yaml v1.0.0
12-
github.com/smacker/go-tree-sitter v0.0.0-20240422154435-0628b34cbf9c
1313
github.com/stretchr/testify v1.9.0
1414
golang.org/x/sync v0.2.0
1515
gopkg.in/yaml.v2 v2.4.0

Diff for: gazelle/go.sum

+2-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
1313
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
1414
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
1515
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
16-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1716
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1817
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
18+
github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b h1:b9s96BulIARx0konX36sJ5oZhWvAvjQBBntxp1eUukQ=
19+
github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b/go.mod h1:87UkDyPt18bTH/FvinLc/kj587VNYOdRKZT1la4T8Hg=
1920
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
2021
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
2122
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -44,12 +45,6 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
4445
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4546
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4647
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
47-
github.com/smacker/go-tree-sitter v0.0.0-20240422154435-0628b34cbf9c h1:7QZKUmQfnxncZIJGyvX8M8YeMfn8kM10j3J/2KwVTN4=
48-
github.com/smacker/go-tree-sitter v0.0.0-20240422154435-0628b34cbf9c/go.mod h1:q99oHDsbP0xRwmn7Vmob8gbSMNyvJ83OauXPSuHQuKE=
49-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
50-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
51-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
52-
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
5348
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
5449
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
5550
go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0=
@@ -105,7 +100,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
105100
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
106101
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
107102
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
108-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
109103
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
110104
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
111105
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

Diff for: gazelle/python/BUILD.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ go_library(
3939
"@bazel_gazelle//rule:go_default_library",
4040
"@com_github_bazelbuild_buildtools//build:go_default_library",
4141
"@com_github_bmatcuk_doublestar_v4//:doublestar",
42+
"@com_github_dougthor42_go_tree_sitter//:go-tree-sitter",
43+
"@com_github_dougthor42_go_tree_sitter//python",
4244
"@com_github_emirpasic_gods//lists/singlylinkedlist",
4345
"@com_github_emirpasic_gods//sets/treeset",
4446
"@com_github_emirpasic_gods//utils",
45-
"@com_github_smacker_go_tree_sitter//:go-tree-sitter",
46-
"@com_github_smacker_go_tree_sitter//python",
4747
"@org_golang_x_sync//errgroup",
4848
],
4949
)

Diff for: gazelle/python/file_parser.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"path/filepath"
2323
"strings"
2424

25-
sitter "github.com/smacker/go-tree-sitter"
26-
"github.com/smacker/go-tree-sitter/python"
25+
sitter "github.com/dougthor42/go-tree-sitter"
26+
"github.com/dougthor42/go-tree-sitter/python"
2727
)
2828

2929
const (
@@ -115,10 +115,10 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
115115
a, b = b, a
116116
}
117117
if a.Type() == sitterNodeTypeIdentifier && a.Content(p.code) == "__name__" &&
118-
// at github.com/smacker/go-tree-sitter@latest (after v0.0.0-20240422154435-0628b34cbf9c we used)
118+
// at github.com/dougthor42/go-tree-sitter@latest (after v0.0.0-20240422154435-0628b34cbf9c we used)
119119
// "__main__" is the second child of b. But now, it isn't.
120120
// we cannot use the latest go-tree-sitter because of the top level reference in scanner.c.
121-
// https://github.com/smacker/go-tree-sitter/blob/04d6b33fe138a98075210f5b770482ded024dc0f/python/scanner.c#L1
121+
// https://github.com/dougthor42/go-tree-sitter/blob/04d6b33fe138a98075210f5b770482ded024dc0f/python/scanner.c#L1
122122
b.Type() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" {
123123
return true
124124
}

Diff for: gazelle/python/testdata/py312_syntax/BUILD.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_generation_mode file

Diff for: gazelle/python/testdata/py312_syntax/BUILD.out

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
2+
3+
# gazelle:python_generation_mode file
4+
5+
py_library(
6+
name = "_other_module",
7+
srcs = ["_other_module.py"],
8+
visibility = ["//:__subpackages__"],
9+
)
10+
11+
py_binary(
12+
name = "pep_695_type_parameter",
13+
srcs = ["pep_695_type_parameter.py"],
14+
visibility = ["//:__subpackages__"],
15+
deps = [":_other_module"],
16+
)

Diff for: gazelle/python/testdata/py312_syntax/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# py312 syntax
2+
3+
This test case checks that we properly parse certain python 3.12 syntax, such
4+
as pep 695 type parameters, with go-tree-sitter.

Diff for: gazelle/python/testdata/py312_syntax/WORKSPACE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.

Diff for: gazelle/python/testdata/py312_syntax/__init__.py

Whitespace-only changes.

Diff for: gazelle/python/testdata/py312_syntax/_other_module.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def search_one_more_level[T](
2+
graph: dict[T, set[T]], seen: set[T], routes: list[list[T]], target: T
3+
) -> list[T] | None:
4+
"""This function fails to parse with older versions of go-tree-sitter.
5+
6+
Args:
7+
graph: The graph to search as input.
8+
seen: The nodes that have been visited as input/output.
9+
routes: The current routes in the breadth-first search as input/output.
10+
target: The target to search in this extra search level.
11+
12+
Returns:
13+
a route if it ends on the target, or None if no route reaches the
14+
target.
15+
"""
16+
17+
18+
import _other_module
19+
20+
21+
if __name__ == "__main__":
22+
pass

Diff for: gazelle/python/testdata/py312_syntax/test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

0 commit comments

Comments
 (0)