Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gazelle): Update to latest version of go-tree-sitter #2413

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion gazelle/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions gazelle/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions gazelle/go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions gazelle/patches/0001-go-tree-sitter-build.patch
Original file line number Diff line number Diff line change
@@ -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",
2 changes: 0 additions & 2 deletions gazelle/python/file_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions gazelle/python/testdata/py312_syntax/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:python_generation_mode file
16 changes: 16 additions & 0 deletions gazelle/python/testdata/py312_syntax/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
4 changes: 4 additions & 0 deletions gazelle/python/testdata/py312_syntax/README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions gazelle/python/testdata/py312_syntax/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions gazelle/python/testdata/py312_syntax/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---