atlassian / bazel-tools

Reusable bits for Bazel
Apache License 2.0
113 stars 36 forks source link

Golangci-lint on generated code #120

Open parabolala opened 4 years ago

parabolala commented 4 years ago

Hi, I've hacked together a small patch that allows running golangci-lint on the generated go code. It relies on @rules_go/go_path to assemble a source tree of all the dependencies and uses that as GOPATH for the golangci-lint invocation.

I've got it to work on my small repo that uses generated code for protobuf libraries and it works. The minimal patch to accept a go_path() output and wire it through GOPATH is at https://github.com/xa4a/bazel-tools/commit/3fd012ac23223a26b9f921fba5d86a82ea0d4a8c

It can be used as:

load("@io_bazel_rules_go//go:def.bzl", "go_path")
load("@com_github_atlassian_bazel_tools//golangcilint:def.bzl", "golangcilint")

go_path(
    name = "go_root",
    deps = [
      # Code entry points (go_binary, go_library) to lint. 
      # All their transitive deps are pulled in and are filtered by "prefix" below.
      "//service/cmd/server",
    ],
    include_pkg = True,
)

golangcilint(
    name = "golangcilint",
    go_path = ":go_root",
    prefix = "<prefix matching the code>",
)

Am I missing something essential? Does this look feasible? If yes, I'll be happy to flesh it out into a PR.

ash2k commented 4 years ago

GOPATH is going away within a year, so no point in supporting it. I think this rule just cannot be made to work the way it currently... works. It needs to be redone completely. See this issue for more details https://github.com/atlassian/bazel-tools/issues/118

ash2k commented 4 years ago

On the other hand, if you use this rule and it would be convenient for you to have it with the patch, feel free to submit a PR. It will keep working while Go supports GOPATH and then it will stop. 🤷