Skip to content

Commit 16c65cf

Browse files
authored
chore: Switch back to smacker/go-tree-sitter (#3069)
Finally remove the dougthor42/go-tree-sitter fork, fixing #2630. Admittedly we could have done this sooner had I figured things out sooner... but c'est la vie. Instead of using the BUILD.bazel files in dougthor42/go-tree-sitter, we basically vendor the build file via http_archive. This is different than using patches because non-root Bazel modules can still make use of the BUILD.bazel files we make. Background: The reason we migrated to dougthor42/go-tree-sitter in the first place was to support python 3.12 grammar. smacker/go-tree-sitter supported for python 3.12, but made a change to their file structure that Gazelle was unable to handle. Specifically, the python/binding.go file indirectly requires a c header file found in a parent directory, and Gazelle doesn't know how to handle that for `go_repository` (WORKSPACE) and `go_deps.from_file` (bzlmod). So dougthor42/go-tree-sitter created our own BUILD.bazel files that included the required filegroups and whatnot, thus negating the need for Gazelle to generate BUILD.bazel files. Future Work: This still doesn't resolve the issues with bumping rules_go and go seen in #2962, but it does simplify that investigation a bit as it's just one fewer thing to account for. It also doesn't address the desire to migrate to the official tree-sitter/go-tree-sitter repo, but @jbedard found some perf issues with that anyway (tree-sitter/go-tree-sitter#32).
1 parent 7685993 commit 16c65cf

File tree

8 files changed

+105
-21
lines changed

8 files changed

+105
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ END_UNRELEASED_TEMPLATE
6767
* 3.12.11
6868
* 3.14.0b3
6969
* (toolchain) Python 3.13 now references 3.13.5
70+
* (gazelle) Switched back to smacker/go-tree-sitter, fixing
71+
[#2630](https://github.com/bazel-contrib/rules_python/issues/2630)
7072

7173
{#v0-0-0-fixed}
7274
### Fixed

gazelle/MODULE.bazel

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ use_repo(
2121
go_deps,
2222
"com_github_bazelbuild_buildtools",
2323
"com_github_bmatcuk_doublestar_v4",
24-
"com_github_dougthor42_go_tree_sitter",
2524
"com_github_emirpasic_gods",
2625
"com_github_ghodss_yaml",
2726
"com_github_stretchr_testify",
2827
"in_gopkg_yaml_v2",
2928
"org_golang_x_sync",
3029
)
3130

31+
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
32+
33+
http_archive(
34+
name = "com_github_smacker_go_tree_sitter",
35+
build_file = "//:internal/smacker_BUILD.bazel",
36+
integrity = "sha256-4AkDY4Rh5Auu9Kwzhj5XYSirMLlhmd6ClMWo/r0kmu4=",
37+
strip_prefix = "go-tree-sitter-dd81d9e9be82a8cac96ed1d50c7389c5f1997c02",
38+
url = "https://github.com/smacker/go-tree-sitter/archive/dd81d9e9be82a8cac96ed1d50c7389c5f1997c02.zip",
39+
)
40+
3241
python_stdlib_list = use_extension("//python:extensions.bzl", "python_stdlib_list")
3342
use_repo(
3443
python_stdlib_list,

gazelle/deps.bzl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ def go_deps():
113113
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
114114
version = "v1.1.1",
115115
)
116-
117116
go_repository(
118117
name = "com_github_emirpasic_gods",
119118
importpath = "github.com/emirpasic/gods",
@@ -175,18 +174,18 @@ def go_deps():
175174
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
176175
version = "v1.0.0",
177176
)
178-
179177
go_repository(
180178
name = "com_github_prometheus_client_model",
181179
importpath = "github.com/prometheus/client_model",
182180
sum = "h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=",
183181
version = "v0.0.0-20190812154241-14fe0d1b01d4",
184182
)
185-
go_repository(
186-
name = "com_github_dougthor42_go_tree_sitter",
187-
importpath = "github.com/dougthor42/go-tree-sitter",
188-
sum = "h1:b9s96BulIARx0konX36sJ5oZhWvAvjQBBntxp1eUukQ=",
189-
version = "v0.0.0-20241210060307-2737e1d0de6b",
183+
http_archive(
184+
name = "com_github_smacker_go_tree_sitter",
185+
build_file = Label("//:internal/smacker_BUILD.bazel"),
186+
integrity = "sha256-4AkDY4Rh5Auu9Kwzhj5XYSirMLlhmd6ClMWo/r0kmu4=",
187+
strip_prefix = "go-tree-sitter-dd81d9e9be82a8cac96ed1d50c7389c5f1997c02",
188+
url = "https://github.com/smacker/go-tree-sitter/archive/dd81d9e9be82a8cac96ed1d50c7389c5f1997c02.zip",
190189
)
191190
go_repository(
192191
name = "com_github_stretchr_objx",

gazelle/go.mod

Lines changed: 1 addition & 1 deletion
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.7.1
10-
github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b
1110
github.com/emirpasic/gods v1.18.1
1211
github.com/ghodss/yaml v1.0.0
12+
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82
1313
github.com/stretchr/testify v1.9.0
1414
golang.org/x/sync v0.2.0
1515
gopkg.in/yaml.v2 v2.4.0

gazelle/go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82 h1:HTepWP/jh
66
github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo=
77
github.com/bazelbuild/rules_go v0.41.0 h1:JzlRxsFNhlX+g4drDRPhIaU5H5LnI978wdMJ0vK4I+k=
88
github.com/bazelbuild/rules_go v0.41.0/go.mod h1:TMHmtfpvyfsxaqfL9WnahCsXMWDMICTw7XeK9yVb+YU=
9-
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
10-
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
119
github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q=
1210
github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
1311
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -17,8 +15,6 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
1715
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1816
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1917
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
20-
github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b h1:b9s96BulIARx0konX36sJ5oZhWvAvjQBBntxp1eUukQ=
21-
github.com/dougthor42/go-tree-sitter v0.0.0-20241210060307-2737e1d0de6b/go.mod h1:87UkDyPt18bTH/FvinLc/kj587VNYOdRKZT1la4T8Hg=
2218
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
2319
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
2420
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -47,6 +43,8 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
4743
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4844
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4945
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
46+
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
47+
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw=
5048
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
5149
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
5250
go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0=

gazelle/internal/smacker_BUILD.bazel

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
filegroup(
4+
name = "common_libs",
5+
srcs = [
6+
"alloc.h",
7+
"api.h",
8+
"array.h",
9+
],
10+
visibility = [":__subpackages__"],
11+
)
12+
13+
go_library(
14+
name = "go-tree-sitter",
15+
srcs = [
16+
"alloc.c",
17+
"alloc.h",
18+
"api.h",
19+
"array.h",
20+
"atomic.h",
21+
"bindings.c",
22+
"bindings.go",
23+
"bindings.h",
24+
"bits.h",
25+
"clock.h",
26+
"error_costs.h",
27+
"get_changed_ranges.c",
28+
"get_changed_ranges.h",
29+
"host.h",
30+
"iter.go",
31+
"language.c",
32+
"language.h",
33+
"length.h",
34+
"lexer.c",
35+
"lexer.h",
36+
"node.c",
37+
"parser.c",
38+
"parser.h",
39+
"point.h",
40+
"ptypes.h",
41+
"query.c",
42+
"reduce_action.h",
43+
"reusable_node.h",
44+
"stack.c",
45+
"stack.h",
46+
"subtree.c",
47+
"subtree.h",
48+
"test_grammar.go",
49+
"tree.c",
50+
"tree.h",
51+
"tree_cursor.c",
52+
"tree_cursor.h",
53+
"umachine.h",
54+
"unicode.h",
55+
"urename.h",
56+
"utf.h",
57+
"utf16.h",
58+
"utf8.h",
59+
"wasm_store.c",
60+
"wasm_store.h",
61+
],
62+
cgo = True,
63+
importpath = "github.com/smacker/go-tree-sitter",
64+
visibility = ["//visibility:public"],
65+
)
66+
67+
go_library(
68+
name = "python",
69+
srcs = [
70+
"python/binding.go",
71+
"python/parser.c",
72+
"python/parser.h",
73+
"python/scanner.c",
74+
":common_libs",
75+
],
76+
cgo = True,
77+
importpath = "github.com/smacker/go-tree-sitter/python",
78+
visibility = ["//visibility:public"],
79+
deps = [":go-tree-sitter"],
80+
)

gazelle/python/BUILD.bazel

Lines changed: 2 additions & 2 deletions
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",
4442
"@com_github_emirpasic_gods//lists/singlylinkedlist",
4543
"@com_github_emirpasic_gods//sets/treeset",
4644
"@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
)

gazelle/python/file_parser.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"path/filepath"
2323
"strings"
2424

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

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

0 commit comments

Comments
 (0)