Skip to content

Commit 7f6784a

Browse files
authored
support installation via Bzlmod or WORKSPACE in addition to go.mod (#1)
While this repo was previously consumable as a go dependency (which is in fact how our internal monorepo consumes it), it is also useful to allow installation via Bzlmod or WORKSPACE for repos which otherwise don't require a go environment. Support for these is added here, although Bzlmod currently requires an `--override_module` flag as we aren't published to the Bazel Central Registry. The two main bits of complexity here are: 1. Making sure the requisite go module dependencies get installed. Bzlmod handles this for us, but WORKSPACE requires that we set up Gazelle to generate a macro users can call to install them. 2. Solving the patching situation with go-tree-sitter. There is an [unreleased fix](bazel-contrib/rules_go#4298) merged to rules_go which this PR uses to remove the need for patching in this repo. However, we still require the patch being available for downstream users who might not have the rules_go fix available. In the WORKSPACE setup this is handled for them by the `scala_gazelle_deps` macro, but Bzlmod users will have to add the `go_deps.module_override` manually as it is only callable from their repo's root module.
1 parent fd9ef55 commit 7f6784a

File tree

5 files changed

+232
-8
lines changed

5 files changed

+232
-8
lines changed

BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
load("@bazel_gazelle//:def.bzl", "gazelle")
22

3+
# gazelle:prefix github.com/foursquare/scala-gazelle
4+
# gazelle:exclude bazel-bin
5+
# gazelle:exclude bazel-out
6+
# gazelle:exclude bazel-scala-gazelle
7+
# gazelle:exclude bazel-testlogs
38
gazelle(name = "gazelle")
9+
10+
gazelle(
11+
name = "gazelle_update_repos",
12+
args = [
13+
"-from_file=go.mod",
14+
"-to_macro=deps.bzl%scala_gazelle_deps",
15+
"-prune",
16+
],
17+
command = "update-repos",
18+
)

MODULE.bazel

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
module(
2+
name = "scala_gazelle",
3+
version = "0.0.0",
4+
compatibility_level = 0,
5+
)
6+
17
bazel_dep(name = "gazelle", version = "0.43.0", repo_name = "bazel_gazelle")
28
bazel_dep(name = "rules_go", version = "0.54.0", repo_name = "io_bazel_rules_go")
39

10+
# Picks up a fix to have rules_go propagate header files as transitive deps, which
11+
# removes the need to patch go-tree-sitter's generated BUILD files in order to compile.
12+
# Once this change lands in a mainline rules_go release (presumably in either 0.54.2 or
13+
# 0.55.0), we can upgrade and this override can go away.
14+
#
15+
# See https://github.com/bazel-contrib/bazel-gazelle/issues/2059 for details.
16+
archive_override(
17+
module_name = "rules_go",
18+
integrity = "sha256-fd1dlvf3B/50VNJ2f1y8sF0QZMH45n2DPIZRtdQvdTc=",
19+
strip_prefix = "rules_go-393faea31d43012fbb7887dfbddb869cb5cf58f5",
20+
urls = [
21+
"https://github.com/bazel-contrib/rules_go/archive/393faea31d43012fbb7887dfbddb869cb5cf58f5.zip",
22+
],
23+
)
24+
425
go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
526
go_deps.from_file(go_mod = "//:go.mod")
627
use_repo(
728
go_deps,
829
"com_github_emirpasic_gods",
930
"com_github_smacker_go_tree_sitter",
1031
)
11-
12-
# Patch in a cc_library for some header files needed to build go-tree-sitter; see
13-
# https://github.com/bazelbuild/rules_python/issues/2573 for details.
14-
go_deps.module_override(
15-
patches = ["//:tree-sitter_cdeps.patch"],
16-
path = "github.com/smacker/go-tree-sitter",
17-
)

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,73 @@ A Scala code parser and [Gazelle](https://github.com/bazelbuild/bazel-gazelle) p
55

66
TODO
77

8+
### Installation instructions
9+
10+
Create new `gazelle_binary` and `gazelle` targets in your root `BUILD`/`BUILD.bazel` file (or add the Scala language
11+
plugin to an existing `gazelle_binary` if you wish):
12+
13+
```starlark
14+
gazelle_binary(
15+
name = "gazelle_bin",
16+
languages = [
17+
"@scala_gazelle//scala",
18+
],
19+
)
20+
21+
gazelle(
22+
name = "gazelle",
23+
args = [
24+
# "-scala_rules_scala_repo_name=io_bazel_rules_scala", # required with older versions of rules_scala
25+
# "-scala_parsing_cache_file=...", # beneficial for large repos; specify a .json or .json.gz file path
26+
],
27+
gazelle = ":gazelle_bin",
28+
)
29+
```
30+
31+
#### Bzlmod
32+
33+
TODO: scala_gazelle is not currently published to the Bazel Central Registry. In the meantime, Bzlmod can still be used
34+
via the --override_module flag.
35+
36+
```starlark
37+
bazel_dep(name = "scala_gazelle", version = "0.0.0")
38+
```
39+
40+
Note that building scala_gazelle requires either a rules_go version later than 54.1, or a patch to the go-tree-sitter
41+
module. If upgrading rules_go is not an option for you, you will additionally require the following override in your
42+
`MODULE.bazel` file:
43+
44+
```starlark
45+
go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
46+
go_deps.module_override(
47+
patches = ["@scala_gazelle//:tree-sitter_cdeps.patch"],
48+
path = "github.com/smacker/go-tree-sitter",
49+
)
50+
```
51+
52+
#### WORKSPACE
53+
54+
```starlark
55+
SCALA_GAZELLE_VERSION = "fd9ef55674f961f05f339ca576027f706b0f3859"
56+
57+
SCALA_GAZELLE_SHA = "b45ce08742058431f20326cbfe59d240a1f4a644733bfd273f4c35865013b795"
58+
59+
http_archive(
60+
name = "scala_gazelle",
61+
strip_prefix = "scala-gazelle-{}".format(SCALA_GAZELLE_VERSION),
62+
sha256 = SCALA_GAZELLE_SHA,
63+
url = "https://github.com/foursquare/scala-gazelle/archive/{}.zip".format(SCALA_GAZELLE_VERSION),
64+
)
65+
66+
load("@scala_gazelle//:deps.bzl", "scala_gazelle_deps")
67+
68+
scala_gazelle_deps()
69+
```
70+
71+
Note that with `WORKSPACE` being order dependent, if you get errors building the gazelle binary you may need to move
72+
`scala_gazelle_deps()` earlier in the file to ensure the proper dependency versioning, especially if you use other
73+
Gazelle language plugins.
74+
875
## Maintainer notes
976

1077
### Managing go dependencies
@@ -13,7 +80,8 @@ TL;DR:
1380
1. `go.mod` may be updated by hand, or preferably via `bazel run @io_bazel_rules_go//go get example.com/pkg@version`.
1481
2. Run `bazel run @io_bazel_rules_go//go -- mod tidy` to update indirect deps and `go.sum`.
1582
3. Run `bazel mod tidy` to ensure any changes are reflected in the `go_deps` `use_repo` declaration in `MODULE.bazel`.
16-
4. Run `bazel run //:gazelle` to update `BUILD` files as needed.
83+
4. Run `bazel run //:gazelle_update_repos` to update the `scala_gazelle_deps` macro in `deps.bzl`
84+
5. Run `bazel run //:gazelle` to update `BUILD` files as needed.
1785

1886
For more details, see the upstream docs from `rules_go` [here](https://github.com/bazel-contrib/rules_go/blob/v0.54.0/docs/go/core/bzlmod.md).
1987

WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Unused, but required by `bazel run //:gazelle_update_repos`

deps.bzl

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
load("@bazel_gazelle//:deps.bzl", "go_repository")
2+
3+
# This file is managed by `bazel run //:gazelle_update_repos`
4+
5+
def scala_gazelle_deps():
6+
go_repository(
7+
name = "com_github_bazelbuild_bazel_gazelle",
8+
importpath = "github.com/bazelbuild/bazel-gazelle",
9+
sum = "h1:NQmf8f7+7OcecUdnAgYoPete6RzAutjEuYjNhE9LU68=",
10+
version = "v0.43.0",
11+
)
12+
go_repository(
13+
name = "com_github_bazelbuild_buildtools",
14+
importpath = "github.com/bazelbuild/buildtools",
15+
sum = "h1:FGzENZi+SX9I7h9xvMtRA3rel8hCEfyzSixteBgn7MU=",
16+
version = "v0.0.0-20240918101019-be1c24cc9a44",
17+
)
18+
go_repository(
19+
name = "com_github_bazelbuild_rules_go",
20+
importpath = "github.com/bazelbuild/rules_go",
21+
sum = "h1:/BUvuaB8MEiUA2oLPPCGtuw5V+doAYyiGTFyoSWlkrw=",
22+
version = "v0.50.1",
23+
)
24+
go_repository(
25+
name = "com_github_bmatcuk_doublestar_v4",
26+
importpath = "github.com/bmatcuk/doublestar/v4",
27+
sum = "h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q=",
28+
version = "v4.7.1",
29+
)
30+
go_repository(
31+
name = "com_github_davecgh_go_spew",
32+
importpath = "github.com/davecgh/go-spew",
33+
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
34+
version = "v1.1.1",
35+
)
36+
go_repository(
37+
name = "com_github_emirpasic_gods",
38+
importpath = "github.com/emirpasic/gods",
39+
sum = "h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=",
40+
version = "v1.18.1",
41+
)
42+
go_repository(
43+
name = "com_github_fsnotify_fsnotify",
44+
importpath = "github.com/fsnotify/fsnotify",
45+
sum = "h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=",
46+
version = "v1.7.0",
47+
)
48+
go_repository(
49+
name = "com_github_golang_protobuf",
50+
importpath = "github.com/golang/protobuf",
51+
sum = "h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=",
52+
version = "v1.5.0",
53+
)
54+
go_repository(
55+
name = "com_github_google_go_cmp",
56+
importpath = "github.com/google/go-cmp",
57+
sum = "h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=",
58+
version = "v0.6.0",
59+
)
60+
go_repository(
61+
name = "com_github_pmezard_go_difflib",
62+
importpath = "github.com/pmezard/go-difflib",
63+
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
64+
version = "v1.0.0",
65+
)
66+
go_repository(
67+
name = "com_github_smacker_go_tree_sitter",
68+
importpath = "github.com/smacker/go-tree-sitter",
69+
patches = ["@com_github_foursquare_scala_gazelle//:tree-sitter_cdeps.patch"],
70+
sum = "h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=",
71+
version = "v0.0.0-20240827094217-dd81d9e9be82",
72+
)
73+
go_repository(
74+
name = "com_github_stretchr_testify",
75+
importpath = "github.com/stretchr/testify",
76+
sum = "h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=",
77+
version = "v1.9.0",
78+
)
79+
go_repository(
80+
name = "in_gopkg_yaml_v3",
81+
importpath = "gopkg.in/yaml.v3",
82+
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
83+
version = "v3.0.1",
84+
)
85+
go_repository(
86+
name = "net_starlark_go",
87+
importpath = "go.starlark.net",
88+
sum = "h1:xwwDQW5We85NaTk2APgoN9202w/l0DVGp+GZMfsrh7s=",
89+
version = "v0.0.0-20210223155950-e043a3d3c984",
90+
)
91+
go_repository(
92+
name = "org_golang_google_protobuf",
93+
importpath = "google.golang.org/protobuf",
94+
sum = "h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=",
95+
version = "v1.33.0",
96+
)
97+
go_repository(
98+
name = "org_golang_x_mod",
99+
importpath = "golang.org/x/mod",
100+
sum = "h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=",
101+
version = "v0.20.0",
102+
)
103+
go_repository(
104+
name = "org_golang_x_sync",
105+
importpath = "golang.org/x/sync",
106+
sum = "h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=",
107+
version = "v0.8.0",
108+
)
109+
go_repository(
110+
name = "org_golang_x_sys",
111+
importpath = "golang.org/x/sys",
112+
sum = "h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=",
113+
version = "v0.25.0",
114+
)
115+
go_repository(
116+
name = "org_golang_x_tools",
117+
importpath = "golang.org/x/tools",
118+
sum = "h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=",
119+
version = "v0.13.0",
120+
)
121+
go_repository(
122+
name = "org_golang_x_tools_go_vcs",
123+
importpath = "golang.org/x/tools/go/vcs",
124+
sum = "h1:cOIJqWBl99H1dH5LWizPa+0ImeeJq3t3cJjaeOWUAL4=",
125+
version = "v0.1.0-deprecated",
126+
)

0 commit comments

Comments
 (0)