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

Add bzlmod support #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
load("@gazelle//:def.bzl", "gazelle")

exports_files(
["copy_into_workspace.sh"],
visibility = ["//visibility:public"],
)

gazelle(name = "gazelle")
11 changes: 11 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module(
name = "golink",
version = "2.0.0",
)

bazel_dep(name = "rules_go", version = "0.48.1")
bazel_dep(name = "gazelle", version = "0.37.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
130 changes: 130 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 6 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,21 @@ Note that this is a hack and there will be more ideal solutions in the future. u

Follow these instructions strictly.

## Add this to your `WORKSPACE`
## Add this to your `MODULE.bazel`

```bazel
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
name = "golink",
urls = ["https://github.com/nikunjy/golink/archive/v1.1.0.tar.gz"],
sha256 = "c505a82b7180d4315bbaf05848e9b7d2683e80f1b16159af51a0ecae6fb2d54d",
strip_prefix = "golink-1.1.0",
)
bazel_dep(name = "golink", version = "2.0.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
```

## Use Gazelle

You have to use [gazelle](https://github.com/bazelbuild/bazel-gazelle). If you don't know what that means follow the link and instruction there in.

*(only if you don't use gazelle)*, you will probably add two things in `WORKSPACE`

Something like this, check me on version number

```bazel
http_archive(
name = "bazel_gazelle",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz",
],
sha256 = "d8c45ee70ec39a57e7a05e5027c32b1576cc7f16d9dd37135b0eddde45cf1b10",
)`
```

In your `BUILD` file you will add something like

```bazel
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix github.com/example/project
gazelle(name = "gazelle")
```

This will generate `BUILD.bazel` files for your golang and proto stuff when you run `bazel run //:gazelle` !!!
You have to use [gazelle](https://github.com/bazelbuild/bazel-gazelle). If you don't know what that means, follow the link and instruction there in.

## Integrate golink

In your root `BUILD.bazel`

```bazel
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle_binary")
gazelle_binary(
Expand All @@ -77,7 +47,6 @@ gazelle(

Now when you run `bazel run //:gazelle` it will generate a target of `go_proto_link` type for your protos. If you run this target you will copy the generated sources into your repo.


## Example

Here are two commits I did on my sample monorepo:
Expand Down
11 changes: 0 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "bazel_rules_go",
path = "bazel/go/",
)


load("@bazel_rules_go//:repos.bzl", "add_go_repos")
add_go_repos()
load("@bazel_rules_go//:def.bzl", "go_rules_deps")
go_rules_deps()

Empty file removed bazel/go/BUILD
Empty file.
Empty file removed bazel/go/WORKSPACE
Empty file.
31 changes: 0 additions & 31 deletions bazel/go/def.bzl

This file was deleted.

25 changes: 0 additions & 25 deletions bazel/go/repos.bzl

This file was deleted.

29 changes: 22 additions & 7 deletions gazelle/go_link/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["proto_gen.go"],
importpath = "github.com/nikunjy/go/bazel/go/gazelle/go_link",
visibility = ["//visibility:public"],
deps = [
"@bazel_gazelle//config:go_default_library",
"@bazel_gazelle//label:go_default_library",
"@bazel_gazelle//language:go_default_library",
"@bazel_gazelle//repo:go_default_library",
"@bazel_gazelle//resolve:go_default_library",
"@bazel_gazelle//rule:go_default_library",
"@gazelle//config:go_default_library",
"@gazelle//label:go_default_library",
"@gazelle//language:go_default_library",
"@gazelle//repo:go_default_library",
"@gazelle//resolve:go_default_library",
"@gazelle//rule:go_default_library",
],
)

go_library(
name = "go_link",
srcs = ["proto_gen.go"],
importpath = "golink/gazelle/go_link",
visibility = ["//visibility:public"],
deps = [
"@gazelle//config:go_default_library",
"@gazelle//label:go_default_library",
"@gazelle//language:go_default_library",
"@gazelle//repo:go_default_library",
"@gazelle//resolve:go_default_library",
"@gazelle//rule:go_default_library",
],
)
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module golink

go 1.21.4

toolchain go1.21.6

require github.com/bazelbuild/bazel-gazelle v0.37.0

require (
github.com/bazelbuild/buildtools v0.0.0-20240313121412-66c605173954 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/tools/go/vcs v0.1.0-deprecated // indirect
)
Loading