diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a1c17d8b8..518a855163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,7 @@ Unreleased changes template. Other changes: * (python_repository) Start honoring the `strip_prefix` field for `zstd` archives. * (pypi) {bzl:obj}`pip_parse.extra_hub_aliases` now works in WORKSPACE files. +* (gazelle): Update to latest version of go-tree-sitter which supports python 3.12 syntax. {#v0-0-0-fixed} ### Fixed diff --git a/gazelle/MODULE.bazel b/gazelle/MODULE.bazel index 0418b39036..b57396e77f 100644 --- a/gazelle/MODULE.bazel +++ b/gazelle/MODULE.bazel @@ -22,7 +22,6 @@ use_repo( "com_github_bmatcuk_doublestar_v4", "com_github_emirpasic_gods", "com_github_ghodss_yaml", - "com_github_smacker_go_tree_sitter", "com_github_stretchr_testify", "in_gopkg_yaml_v2", "org_golang_x_sync", diff --git a/gazelle/deps.bzl b/gazelle/deps.bzl index 948d61e5ae..b0f6b6b9f4 100644 --- a/gazelle/deps.bzl +++ b/gazelle/deps.bzl @@ -188,8 +188,10 @@ def go_deps(): go_repository( name = "com_github_smacker_go_tree_sitter", importpath = "github.com/smacker/go-tree-sitter", - sum = "h1:7QZKUmQfnxncZIJGyvX8M8YeMfn8kM10j3J/2KwVTN4=", - version = "v0.0.0-20240422154435-0628b34cbf9c", + patch_args = ["-p1"], + patches = ["//:patches/0001-go-tree-sitter-build.patch"], + sum = "h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=", + version = "v0.0.0-20240827094217-dd81d9e9be82", ) go_repository( name = "com_github_stretchr_objx", diff --git a/gazelle/go.mod b/gazelle/go.mod index 4b65e71d67..7f1cdb1a38 100644 --- a/gazelle/go.mod +++ b/gazelle/go.mod @@ -1,6 +1,6 @@ module github.com/bazelbuild/rules_python/gazelle -go 1.19 +go 1.23 require ( github.com/bazelbuild/bazel-gazelle v0.31.1 @@ -9,7 +9,6 @@ require ( github.com/bmatcuk/doublestar/v4 v4.6.1 github.com/emirpasic/gods v1.18.1 github.com/ghodss/yaml v1.0.0 - github.com/smacker/go-tree-sitter v0.0.0-20240422154435-0628b34cbf9c github.com/stretchr/testify v1.9.0 golang.org/x/sync v0.2.0 gopkg.in/yaml.v2 v2.4.0 diff --git a/gazelle/patches/0001-go-tree-sitter-build.patch b/gazelle/patches/0001-go-tree-sitter-build.patch new file mode 100644 index 0000000000..c484c038d5 --- /dev/null +++ b/gazelle/patches/0001-go-tree-sitter-build.patch @@ -0,0 +1,26 @@ +diff --git a/BUILD.bazel b/BUILD.bazel +index 6aa29a3..155129f 100644 +--- a/BUILD.bazel ++++ b/BUILD.bazel +@@ -3,3 +3,9 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") ++filegroup( ++ name = "common", ++ srcs = ["alloc.h", "api.h", "array.h"], ++ visibility = [":__subpackages__"], ++) ++ + go_library( + name = "go-tree-sitter", + srcs = [ +diff --git a/python/BUILD.bazel b/python/BUILD.bazel +index 04c7312..b184ac8 100644 +--- a/python/BUILD.bazel ++++ b/python/BUILD.bazel +@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + go_library( + name = "python", + srcs = [ ++ "//:common", + "binding.go", + "parser.c", + "parser.h", diff --git a/gazelle/python/file_parser.go b/gazelle/python/file_parser.go index 9101621639..ecee8713fa 100644 --- a/gazelle/python/file_parser.go +++ b/gazelle/python/file_parser.go @@ -106,8 +106,6 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool { if a.Type() == sitterNodeTypeIdentifier && a.Content(p.code) == "__name__" && // at github.com/smacker/go-tree-sitter@latest (after v0.0.0-20240422154435-0628b34cbf9c we used) // "__main__" is the second child of b. But now, it isn't. - // we cannot use the latest go-tree-sitter because of the top level reference in scanner.c. - // https://github.com/smacker/go-tree-sitter/blob/04d6b33fe138a98075210f5b770482ded024dc0f/python/scanner.c#L1 b.Type() == sitterNodeTypeString && string(p.code[b.StartByte()+1:b.EndByte()-1]) == "__main__" { return true } diff --git a/gazelle/python/testdata/py312_syntax/BUILD.in b/gazelle/python/testdata/py312_syntax/BUILD.in new file mode 100644 index 0000000000..af2c2cea4b --- /dev/null +++ b/gazelle/python/testdata/py312_syntax/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_generation_mode file diff --git a/gazelle/python/testdata/py312_syntax/BUILD.out b/gazelle/python/testdata/py312_syntax/BUILD.out new file mode 100644 index 0000000000..7457f335a7 --- /dev/null +++ b/gazelle/python/testdata/py312_syntax/BUILD.out @@ -0,0 +1,16 @@ +load("@rules_python//python:defs.bzl", "py_binary", "py_library") + +# gazelle:python_generation_mode file + +py_library( + name = "_other_module", + srcs = ["_other_module.py"], + visibility = ["//:__subpackages__"], +) + +py_binary( + name = "pep_695_type_parameter", + srcs = ["pep_695_type_parameter.py"], + visibility = ["//:__subpackages__"], + deps = [":_other_module"], +) diff --git a/gazelle/python/testdata/py312_syntax/README.md b/gazelle/python/testdata/py312_syntax/README.md new file mode 100644 index 0000000000..854a0a3aa6 --- /dev/null +++ b/gazelle/python/testdata/py312_syntax/README.md @@ -0,0 +1,4 @@ +# py312 syntax + +This test case checks that we properly parse certain python 3.12 syntax, such +as pep 695 type parameters, with go-tree-sitter. diff --git a/gazelle/python/testdata/py312_syntax/WORKSPACE b/gazelle/python/testdata/py312_syntax/WORKSPACE new file mode 100644 index 0000000000..faff6af87a --- /dev/null +++ b/gazelle/python/testdata/py312_syntax/WORKSPACE @@ -0,0 +1 @@ +# This is a Bazel workspace for the Gazelle test data. diff --git a/gazelle/python/testdata/py312_syntax/__init__.py b/gazelle/python/testdata/py312_syntax/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/py312_syntax/_other_module.py b/gazelle/python/testdata/py312_syntax/_other_module.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py b/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py new file mode 100644 index 0000000000..eff06de5a7 --- /dev/null +++ b/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py @@ -0,0 +1,22 @@ +def search_one_more_level[T]( + graph: dict[T, set[T]], seen: set[T], routes: list[list[T]], target: T +) -> list[T] | None: + """This function fails to parse with older versions of go-tree-sitter. + + Args: + graph: The graph to search as input. + seen: The nodes that have been visited as input/output. + routes: The current routes in the breadth-first search as input/output. + target: The target to search in this extra search level. + + Returns: + a route if it ends on the target, or None if no route reaches the + target. + """ + + +import _other_module + + +if __name__ == "__main__": + pass diff --git a/gazelle/python/testdata/py312_syntax/test.yaml b/gazelle/python/testdata/py312_syntax/test.yaml new file mode 100644 index 0000000000..ed97d539c0 --- /dev/null +++ b/gazelle/python/testdata/py312_syntax/test.yaml @@ -0,0 +1 @@ +---