Skip to content

Commit

Permalink
feat(gazelle): pure golang helper (#1895)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hunshcn authored May 20, 2024
1 parent 1036a4d commit 7fc7962
Show file tree
Hide file tree
Showing 24 changed files with 748 additions and 592 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ A brief description of the categories of changes:
marked as `reproducible` and will not include any lock file entries from now
on.

* (gazelle): Remove gazelle plugin's python deps and make it hermetic.
Introduced a new Go-based helper leveraging tree-sitter for syntax analysis.
Implemented the use of `pypi/stdlib-list` for standard library module verification.

### Fixed
* (gazelle) Remove `visibility` from `NonEmptyAttr`.
Now empty(have no `deps/main/srcs/imports` attr) `py_library/test/binary` rules will
Expand Down
10 changes: 2 additions & 8 deletions gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
load("@bazel_gazelle//:def.bzl", "gazelle")

# Gazelle configuration options.
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
# gazelle:prefix github.com/bazelbuild/rules_python/gazelle
# gazelle:exclude bazel-out
gazelle(
name = "gazelle",
gazelle = ":gazelle_binary",
)

gazelle_binary(
name = "gazelle_binary",
languages = DEFAULT_LANGUAGES + ["//python"],
)

gazelle(
name = "gazelle_update_repos",
args = [
"-from_file=go.mod",
"-to_macro=deps.bzl%gazelle_deps",
"-to_macro=deps.bzl%go_deps",
"-prune",
],
command = "update-repos",
Expand Down
18 changes: 18 additions & 0 deletions gazelle/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ bazel_dep(name = "rules_python", version = "0.18.0")
bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.33.0", repo_name = "bazel_gazelle")

local_path_override(
module_name = "rules_python",
path = "..",
)

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
Expand All @@ -17,5 +22,18 @@ 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",
)

python_stdlib_list = use_extension("//python:extensions.bzl", "python_stdlib_list")
use_repo(
python_stdlib_list,
"python_stdlib_list_3_10",
"python_stdlib_list_3_11",
"python_stdlib_list_3_12",
"python_stdlib_list_3_8",
"python_stdlib_list_3_9",
)
9 changes: 2 additions & 7 deletions gazelle/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ local_repository(
path = "..",
)

load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

python_register_toolchains(
name = "python_3_11",
python_version = "3.11",
)

load("//:deps.bzl", _py_gazelle_deps = "gazelle_deps")

# gazelle:repository_macro deps.bzl%gazelle_deps
# gazelle:repository_macro deps.bzl%go_deps
_py_gazelle_deps()
144 changes: 121 additions & 23 deletions gazelle/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,80 @@

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

load("@bazel_gazelle//:deps.bzl", _go_repository = "go_repository")
load(
"@bazel_gazelle//:deps.bzl",
_go_repository = "go_repository",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")

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

def python_stdlib_list_deps():
"Fetch python stdlib list dependencies"
http_file(
name = "python_stdlib_list_3_8",
sha256 = "ee6dc367011ff298b906dbaab408940aa57086d5f8f47278f4b7523b9aa13ae3",
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.8.txt",
downloaded_file_path = "3.8.txt",
)
http_file(
name = "python_stdlib_list_3_9",
sha256 = "a4340e5ffe2e75bb18f548028cef6e6ac15384c44ae0a776e04dd869da1d1fd7",
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.9.txt",
downloaded_file_path = "3.9.txt",
)
http_file(
name = "python_stdlib_list_3_10",
sha256 = "0b867738b78ac98944237de2600093a1c6ef259d1810017e46f01a29f3d199e7",
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.10.txt",
downloaded_file_path = "3.10.txt",
)
http_file(
name = "python_stdlib_list_3_11",
sha256 = "3c1dbf991b17178d6ed3772f4fa8f64302feaf9c3385fef328a0c7ab736a79b1",
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.11.txt",
downloaded_file_path = "3.11.txt",
)
http_file(
name = "python_stdlib_list_3_12",
sha256 = "6d3d53194218b43ee1d04bf9a4f0b6a9309bb59cdcaddede7d9cfe8b6835d34a",
url = "https://raw.githubusercontent.com/pypi/stdlib-list/8cbc2067a4a0f9eee57fb541e4cd7727724b7db4/stdlib_list/lists/3.12.txt",
downloaded_file_path = "3.12.txt",
)

def gazelle_deps():
go_deps()
python_stdlib_list_deps()

def go_deps():
"Fetch go dependencies"
go_repository(
name = "co_honnef_go_tools",
importpath = "honnef.co/go/tools",
sum = "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=",
version = "v0.0.0-20190523083050-ea95bdfd59fc",
)
go_repository(
name = "com_github_bazelbuild_bazel_gazelle",
importpath = "github.com/bazelbuild/bazel-gazelle",
sum = "h1:ROyUyUHzoEdvoOs1e0haxJx1l5EjZX6AOqiKdVlaBbg=",
version = "v0.31.1",
)

go_repository(
name = "com_github_bazelbuild_buildtools",
build_naming_convention = "go_default_library",
importpath = "github.com/bazelbuild/buildtools",
sum = "h1:jhiMzJ+8unnLRtV8rpbWBFE9pFNzIqgUTyZU5aA++w8=",
version = "v0.0.0-20221004120235-7186f635531b",
sum = "h1:HTepWP/jhtWTC1gvK0RnvKCgjh4gLqiwaOwGozAXcbw=",
version = "v0.0.0-20231103205921-433ea8554e82",
)
go_repository(
name = "com_github_bazelbuild_rules_go",
importpath = "github.com/bazelbuild/rules_go",
sum = "h1:JzlRxsFNhlX+g4drDRPhIaU5H5LnI978wdMJ0vK4I+k=",
version = "v0.41.0",
)

go_repository(
Expand Down Expand Up @@ -80,6 +133,13 @@ def gazelle_deps():
sum = "h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=",
version = "v0.3.4",
)
go_repository(
name = "com_github_davecgh_go_spew",
importpath = "github.com/davecgh/go-spew",
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
version = "v1.1.1",
)

go_repository(
name = "com_github_emirpasic_gods",
importpath = "github.com/emirpasic/gods",
Expand All @@ -98,6 +158,12 @@ def gazelle_deps():
sum = "h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=",
version = "v0.1.0",
)
go_repository(
name = "com_github_fsnotify_fsnotify",
importpath = "github.com/fsnotify/fsnotify",
sum = "h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=",
version = "v1.6.0",
)

go_repository(
name = "com_github_ghodss_yaml",
Expand All @@ -114,28 +180,53 @@ def gazelle_deps():
go_repository(
name = "com_github_golang_mock",
importpath = "github.com/golang/mock",
sum = "h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=",
version = "v1.1.1",
sum = "h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=",
version = "v1.6.0",
)
go_repository(
name = "com_github_golang_protobuf",
importpath = "github.com/golang/protobuf",
sum = "h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=",
version = "v1.4.3",
sum = "h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=",
version = "v1.5.2",
)
go_repository(
name = "com_github_google_go_cmp",
importpath = "github.com/google/go-cmp",
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
version = "v0.5.9",
)
go_repository(
name = "com_github_pmezard_go_difflib",
importpath = "github.com/pmezard/go-difflib",
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
version = "v1.0.0",
)

go_repository(
name = "com_github_prometheus_client_model",
importpath = "github.com/prometheus/client_model",
sum = "h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=",
version = "v0.0.0-20190812154241-14fe0d1b01d4",
)
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",
)
go_repository(
name = "com_github_stretchr_objx",
importpath = "github.com/stretchr/objx",
sum = "h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=",
version = "v0.5.2",
)
go_repository(
name = "com_github_stretchr_testify",
importpath = "github.com/stretchr/testify",
sum = "h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=",
version = "v1.9.0",
)

go_repository(
name = "com_github_yuin_goldmark",
importpath = "github.com/yuin/goldmark",
Expand All @@ -160,6 +251,13 @@ def gazelle_deps():
sum = "h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=",
version = "v2.4.0",
)
go_repository(
name = "in_gopkg_yaml_v3",
importpath = "gopkg.in/yaml.v3",
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
version = "v3.0.1",
)

go_repository(
name = "net_starlark_go",
importpath = "go.starlark.net",
Expand All @@ -181,14 +279,14 @@ def gazelle_deps():
go_repository(
name = "org_golang_google_grpc",
importpath = "google.golang.org/grpc",
sum = "h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg=",
version = "v1.27.0",
sum = "h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU=",
version = "v1.50.0",
)
go_repository(
name = "org_golang_google_protobuf",
importpath = "google.golang.org/protobuf",
sum = "h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=",
version = "v1.25.0",
sum = "h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=",
version = "v1.28.0",
)
go_repository(
name = "org_golang_x_crypto",
Expand All @@ -211,14 +309,14 @@ def gazelle_deps():
go_repository(
name = "org_golang_x_mod",
importpath = "golang.org/x/mod",
sum = "h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=",
version = "v0.6.0-dev.0.20220419223038-86c51ed26bb4",
sum = "h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=",
version = "v0.10.0",
)
go_repository(
name = "org_golang_x_net",
importpath = "golang.org/x/net",
sum = "h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=",
version = "v0.0.0-20220722155237-a158d28d115b",
sum = "h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=",
version = "v0.10.0",
)
go_repository(
name = "org_golang_x_oauth2",
Expand All @@ -229,29 +327,29 @@ def gazelle_deps():
go_repository(
name = "org_golang_x_sync",
importpath = "golang.org/x/sync",
sum = "h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=",
version = "v0.0.0-20220722155255-886fb9371eb4",
sum = "h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=",
version = "v0.2.0",
)
go_repository(
name = "org_golang_x_sys",
importpath = "golang.org/x/sys",
sum = "h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=",
version = "v0.0.0-20221010170243-090e33056c14",
sum = "h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=",
version = "v0.8.0",
)
go_repository(
name = "org_golang_x_text",
importpath = "golang.org/x/text",
sum = "h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=",
version = "v0.3.7",
sum = "h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=",
version = "v0.3.3",
)
go_repository(
name = "org_golang_x_tools",
build_directives = [
"gazelle:exclude **/testdata/**/*",
],
importpath = "golang.org/x/tools",
sum = "h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=",
version = "v0.1.12",
sum = "h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=",
version = "v0.9.1",
)
go_repository(
name = "org_golang_x_xerrors",
Expand Down
8 changes: 7 additions & 1 deletion gazelle/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ go 1.19

require (
github.com/bazelbuild/bazel-gazelle v0.31.1
github.com/bazelbuild/buildtools v0.0.0-20230510134650-37bd1811516d
github.com/bazelbuild/buildtools v0.0.0-20231103205921-433ea8554e82
github.com/bazelbuild/rules_go v0.41.0
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
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 7fc7962

Please sign in to comment.