iffyio / golink

bazel hack for linking go generated srcs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

golink

Bazel hack for linking go generated srcs. Find more detials on this post I wrote here

You can use it for copying over golang generated proto files into your source directory. This is important if you want to make your IDE work because you project needs to be go build compliant.

Note that this is a hack and there will be more ideal solutions in the future unfortunately no solution exists for the problem stated above just yet.

Installation

Follow these instructions strictly.

Add this to your WORKSPACE

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

http_archive(
    name = "golink",
    urls = ["https://github.com/nikunjy/golink/archive/v1.0.0.tar.gz"],
    sha256 = "ea728cfc9cb6e2ae024e1d5fbff185224592bbd4dad6516f3cc96d5155b69f0d",
    strip_prefix = "golink-1.0.0",
)

Use Gazelle

You have to use gazelle. If you don't know what that means follow the link and instruction there in.

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

Something like this, check me on version number

  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

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 !!!

Integrate golink

load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle_binary")
gazelle_binary(
     name = "gazelle_binary",
     languages = DEFAULT_LANGUAGES + ["@golink//gazelle/go_link:go_default_library"],
     visibility = ["//visibility:public"],
 )

and change your gazelle target to use the above binary

# gazelle:prefix github.com/nikunjy/go
gazelle(
    name = "gazelle",
    gazelle = "//:gazelle_binary",
)

Now when you run bazel run //:gazelle it will gnerate 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 sammpe monorepo

About

bazel hack for linking go generated srcs

License:MIT License


Languages

Language:Starlark 72.6%Language:Go 26.3%Language:Shell 1.0%