Skip to content

Commit 7fc7962

Browse files
authored
feat(gazelle): pure golang helper (#1895)
Remove gazelle plugin's python deps and make it hermetic. No more relying on the system interpreter. Use TreeSitter to parse Python code and use https://github.com/pypi/stdlib-list to determine whether a module is in std lib. Fixes #1825 Fixes #1599 Related #1315
1 parent 1036a4d commit 7fc7962

24 files changed

+748
-592
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ A brief description of the categories of changes:
3030
marked as `reproducible` and will not include any lock file entries from now
3131
on.
3232

33+
* (gazelle): Remove gazelle plugin's python deps and make it hermetic.
34+
Introduced a new Go-based helper leveraging tree-sitter for syntax analysis.
35+
Implemented the use of `pypi/stdlib-list` for standard library module verification.
36+
3337
### Fixed
3438
* (gazelle) Remove `visibility` from `NonEmptyAttr`.
3539
Now empty(have no `deps/main/srcs/imports` attr) `py_library/test/binary` rules will

gazelle/BUILD.bazel

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
1+
load("@bazel_gazelle//:def.bzl", "gazelle")
22

33
# Gazelle configuration options.
44
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
55
# gazelle:prefix github.com/bazelbuild/rules_python/gazelle
66
# gazelle:exclude bazel-out
77
gazelle(
88
name = "gazelle",
9-
gazelle = ":gazelle_binary",
10-
)
11-
12-
gazelle_binary(
13-
name = "gazelle_binary",
14-
languages = DEFAULT_LANGUAGES + ["//python"],
159
)
1610

1711
gazelle(
1812
name = "gazelle_update_repos",
1913
args = [
2014
"-from_file=go.mod",
21-
"-to_macro=deps.bzl%gazelle_deps",
15+
"-to_macro=deps.bzl%go_deps",
2216
"-prune",
2317
],
2418
command = "update-repos",

gazelle/MODULE.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ bazel_dep(name = "rules_python", version = "0.18.0")
99
bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
1010
bazel_dep(name = "gazelle", version = "0.33.0", repo_name = "bazel_gazelle")
1111

12+
local_path_override(
13+
module_name = "rules_python",
14+
path = "..",
15+
)
16+
1217
go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
1318
go_deps.from_file(go_mod = "//:go.mod")
1419
use_repo(
@@ -17,5 +22,18 @@ use_repo(
1722
"com_github_bmatcuk_doublestar_v4",
1823
"com_github_emirpasic_gods",
1924
"com_github_ghodss_yaml",
25+
"com_github_smacker_go_tree_sitter",
26+
"com_github_stretchr_testify",
2027
"in_gopkg_yaml_v2",
28+
"org_golang_x_sync",
29+
)
30+
31+
python_stdlib_list = use_extension("//python:extensions.bzl", "python_stdlib_list")
32+
use_repo(
33+
python_stdlib_list,
34+
"python_stdlib_list_3_10",
35+
"python_stdlib_list_3_11",
36+
"python_stdlib_list_3_12",
37+
"python_stdlib_list_3_8",
38+
"python_stdlib_list_3_9",
2139
)

gazelle/WORKSPACE

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,11 @@ local_repository(
3434
path = "..",
3535
)
3636

37-
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
37+
load("@rules_python//python:repositories.bzl", "py_repositories")
3838

3939
py_repositories()
4040

41-
python_register_toolchains(
42-
name = "python_3_11",
43-
python_version = "3.11",
44-
)
45-
4641
load("//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
4742

48-
# gazelle:repository_macro deps.bzl%gazelle_deps
43+
# gazelle:repository_macro deps.bzl%go_deps
4944
_py_gazelle_deps()

gazelle/deps.bzl

Lines changed: 121 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,80 @@
1414

1515
"This file managed by `bazel run //:gazelle_update_repos`"
1616

17-
load("@bazel_gazelle//:deps.bzl", _go_repository = "go_repository")
17+
load(
18+
"@bazel_gazelle//:deps.bzl",
19+
_go_repository = "go_repository",
20+
)
21+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
1822

1923
def go_repository(name, **kwargs):
2024
if name not in native.existing_rules():
2125
_go_repository(name = name, **kwargs)
2226

27+
def python_stdlib_list_deps():
28+
"Fetch python stdlib list dependencies"
29+
http_file(
30+
name = "python_stdlib_list_3_8",
31+
sha256 = "ee6dc367011ff298b906dbaab408940aa57086d5f8f47278f4b7523b9aa13ae3",
32+
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.8.txt",
33+
downloaded_file_path = "3.8.txt",
34+
)
35+
http_file(
36+
name = "python_stdlib_list_3_9",
37+
sha256 = "a4340e5ffe2e75bb18f548028cef6e6ac15384c44ae0a776e04dd869da1d1fd7",
38+
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.9.txt",
39+
downloaded_file_path = "3.9.txt",
40+
)
41+
http_file(
42+
name = "python_stdlib_list_3_10",
43+
sha256 = "0b867738b78ac98944237de2600093a1c6ef259d1810017e46f01a29f3d199e7",
44+
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.10.txt",
45+
downloaded_file_path = "3.10.txt",
46+
)
47+
http_file(
48+
name = "python_stdlib_list_3_11",
49+
sha256 = "3c1dbf991b17178d6ed3772f4fa8f64302feaf9c3385fef328a0c7ab736a79b1",
50+
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.11.txt",
51+
downloaded_file_path = "3.11.txt",
52+
)
53+
http_file(
54+
name = "python_stdlib_list_3_12",
55+
sha256 = "6d3d53194218b43ee1d04bf9a4f0b6a9309bb59cdcaddede7d9cfe8b6835d34a",
56+
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.12.txt",
57+
downloaded_file_path = "3.12.txt",
58+
)
59+
2360
def gazelle_deps():
61+
go_deps()
62+
python_stdlib_list_deps()
63+
64+
def go_deps():
2465
"Fetch go dependencies"
2566
go_repository(
2667
name = "co_honnef_go_tools",
2768
importpath = "honnef.co/go/tools",
2869
sum = "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=",
2970
version = "v0.0.0-20190523083050-ea95bdfd59fc",
3071
)
72+
go_repository(
73+
name = "com_github_bazelbuild_bazel_gazelle",
74+
importpath = "github.com/bazelbuild/bazel-gazelle",
75+
sum = "h1:ROyUyUHzoEdvoOs1e0haxJx1l5EjZX6AOqiKdVlaBbg=",
76+
version = "v0.31.1",
77+
)
3178

3279
go_repository(
3380
name = "com_github_bazelbuild_buildtools",
3481
build_naming_convention = "go_default_library",
3582
importpath = "github.com/bazelbuild/buildtools",
36-
sum = "h1:jhiMzJ+8unnLRtV8rpbWBFE9pFNzIqgUTyZU5aA++w8=",
37-
version = "v0.0.0-20221004120235-7186f635531b",
83+
sum = "h1:HTepWP/jhtWTC1gvK0RnvKCgjh4gLqiwaOwGozAXcbw=",
84+
version = "v0.0.0-20231103205921-433ea8554e82",
85+
)
86+
go_repository(
87+
name = "com_github_bazelbuild_rules_go",
88+
importpath = "github.com/bazelbuild/rules_go",
89+
sum = "h1:JzlRxsFNhlX+g4drDRPhIaU5H5LnI978wdMJ0vK4I+k=",
90+
version = "v0.41.0",
3891
)
3992

4093
go_repository(
@@ -80,6 +133,13 @@ def gazelle_deps():
80133
sum = "h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=",
81134
version = "v0.3.4",
82135
)
136+
go_repository(
137+
name = "com_github_davecgh_go_spew",
138+
importpath = "github.com/davecgh/go-spew",
139+
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
140+
version = "v1.1.1",
141+
)
142+
83143
go_repository(
84144
name = "com_github_emirpasic_gods",
85145
importpath = "github.com/emirpasic/gods",
@@ -98,6 +158,12 @@ def gazelle_deps():
98158
sum = "h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=",
99159
version = "v0.1.0",
100160
)
161+
go_repository(
162+
name = "com_github_fsnotify_fsnotify",
163+
importpath = "github.com/fsnotify/fsnotify",
164+
sum = "h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=",
165+
version = "v1.6.0",
166+
)
101167

102168
go_repository(
103169
name = "com_github_ghodss_yaml",
@@ -114,28 +180,53 @@ def gazelle_deps():
114180
go_repository(
115181
name = "com_github_golang_mock",
116182
importpath = "github.com/golang/mock",
117-
sum = "h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=",
118-
version = "v1.1.1",
183+
sum = "h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=",
184+
version = "v1.6.0",
119185
)
120186
go_repository(
121187
name = "com_github_golang_protobuf",
122188
importpath = "github.com/golang/protobuf",
123-
sum = "h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=",
124-
version = "v1.4.3",
189+
sum = "h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=",
190+
version = "v1.5.2",
125191
)
126192
go_repository(
127193
name = "com_github_google_go_cmp",
128194
importpath = "github.com/google/go-cmp",
129195
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
130196
version = "v0.5.9",
131197
)
198+
go_repository(
199+
name = "com_github_pmezard_go_difflib",
200+
importpath = "github.com/pmezard/go-difflib",
201+
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
202+
version = "v1.0.0",
203+
)
132204

133205
go_repository(
134206
name = "com_github_prometheus_client_model",
135207
importpath = "github.com/prometheus/client_model",
136208
sum = "h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=",
137209
version = "v0.0.0-20190812154241-14fe0d1b01d4",
138210
)
211+
go_repository(
212+
name = "com_github_smacker_go_tree_sitter",
213+
importpath = "github.com/smacker/go-tree-sitter",
214+
sum = "h1:7QZKUmQfnxncZIJGyvX8M8YeMfn8kM10j3J/2KwVTN4=",
215+
version = "v0.0.0-20240422154435-0628b34cbf9c",
216+
)
217+
go_repository(
218+
name = "com_github_stretchr_objx",
219+
importpath = "github.com/stretchr/objx",
220+
sum = "h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=",
221+
version = "v0.5.2",
222+
)
223+
go_repository(
224+
name = "com_github_stretchr_testify",
225+
importpath = "github.com/stretchr/testify",
226+
sum = "h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=",
227+
version = "v1.9.0",
228+
)
229+
139230
go_repository(
140231
name = "com_github_yuin_goldmark",
141232
importpath = "github.com/yuin/goldmark",
@@ -160,6 +251,13 @@ def gazelle_deps():
160251
sum = "h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=",
161252
version = "v2.4.0",
162253
)
254+
go_repository(
255+
name = "in_gopkg_yaml_v3",
256+
importpath = "gopkg.in/yaml.v3",
257+
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
258+
version = "v3.0.1",
259+
)
260+
163261
go_repository(
164262
name = "net_starlark_go",
165263
importpath = "go.starlark.net",
@@ -181,14 +279,14 @@ def gazelle_deps():
181279
go_repository(
182280
name = "org_golang_google_grpc",
183281
importpath = "google.golang.org/grpc",
184-
sum = "h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg=",
185-
version = "v1.27.0",
282+
sum = "h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU=",
283+
version = "v1.50.0",
186284
)
187285
go_repository(
188286
name = "org_golang_google_protobuf",
189287
importpath = "google.golang.org/protobuf",
190-
sum = "h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=",
191-
version = "v1.25.0",
288+
sum = "h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=",
289+
version = "v1.28.0",
192290
)
193291
go_repository(
194292
name = "org_golang_x_crypto",
@@ -211,14 +309,14 @@ def gazelle_deps():
211309
go_repository(
212310
name = "org_golang_x_mod",
213311
importpath = "golang.org/x/mod",
214-
sum = "h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=",
215-
version = "v0.6.0-dev.0.20220419223038-86c51ed26bb4",
312+
sum = "h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=",
313+
version = "v0.10.0",
216314
)
217315
go_repository(
218316
name = "org_golang_x_net",
219317
importpath = "golang.org/x/net",
220-
sum = "h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=",
221-
version = "v0.0.0-20220722155237-a158d28d115b",
318+
sum = "h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=",
319+
version = "v0.10.0",
222320
)
223321
go_repository(
224322
name = "org_golang_x_oauth2",
@@ -229,29 +327,29 @@ def gazelle_deps():
229327
go_repository(
230328
name = "org_golang_x_sync",
231329
importpath = "golang.org/x/sync",
232-
sum = "h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=",
233-
version = "v0.0.0-20220722155255-886fb9371eb4",
330+
sum = "h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=",
331+
version = "v0.2.0",
234332
)
235333
go_repository(
236334
name = "org_golang_x_sys",
237335
importpath = "golang.org/x/sys",
238-
sum = "h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=",
239-
version = "v0.0.0-20221010170243-090e33056c14",
336+
sum = "h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=",
337+
version = "v0.8.0",
240338
)
241339
go_repository(
242340
name = "org_golang_x_text",
243341
importpath = "golang.org/x/text",
244-
sum = "h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=",
245-
version = "v0.3.7",
342+
sum = "h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=",
343+
version = "v0.3.3",
246344
)
247345
go_repository(
248346
name = "org_golang_x_tools",
249347
build_directives = [
250348
"gazelle:exclude **/testdata/**/*",
251349
],
252350
importpath = "golang.org/x/tools",
253-
sum = "h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=",
254-
version = "v0.1.12",
351+
sum = "h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=",
352+
version = "v0.9.1",
255353
)
256354
go_repository(
257355
name = "org_golang_x_xerrors",

gazelle/go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ go 1.19
44

55
require (
66
github.com/bazelbuild/bazel-gazelle v0.31.1
7-
github.com/bazelbuild/buildtools v0.0.0-20230510134650-37bd1811516d
7+
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
1010
github.com/emirpasic/gods v1.18.1
1111
github.com/ghodss/yaml v1.0.0
12+
github.com/smacker/go-tree-sitter v0.0.0-20240422154435-0628b34cbf9c
13+
github.com/stretchr/testify v1.9.0
14+
golang.org/x/sync v0.2.0
1215
gopkg.in/yaml.v2 v2.4.0
1316
)
1417

1518
require (
19+
github.com/davecgh/go-spew v1.1.1 // indirect
1620
github.com/google/go-cmp v0.5.9 // indirect
21+
github.com/pmezard/go-difflib v1.0.0 // indirect
1722
golang.org/x/mod v0.10.0 // indirect
1823
golang.org/x/sys v0.8.0 // indirect
1924
golang.org/x/tools v0.9.1 // indirect
25+
gopkg.in/yaml.v3 v3.0.1 // indirect
2026
)

0 commit comments

Comments
 (0)