bazelbuild / rules_go

Go rules for Bazel
Apache License 2.0
1.36k stars 645 forks source link

Using TOOLS_NOGO together with vet #3592

Open michelgb opened 1 year ago

michelgb commented 1 year ago

What version of rules_go are you using?

v0.39.1

What version of gazelle are you using?

v0.30.0

What version of Bazel are you using?

v6.2.1

Does this issue reproduce with the latest releases of all the above?

yes

What operating system and processor architecture are you using?

x86_64

Any other potentially useful information about your toolchain?

Just building go code.

What did you do?

nogo(
    name = "nogo",
    config = "nogo.json",  # needed a config to exclude a few files
    visibility = ["//visibility:public"],
    deps = TOOLS_NOGO, # added all rules
    vet = True, # enabled vet
)

What did you expect to see?

I would expect Bazel to compile normally.

What did you see instead?

The build fails due to duplicated dependencies. Perhaps the rule can do a better job by deduplicating dependencies.

ERROR: /usr/.../BUILD.bazel:12:5: Label '@org_golang_x_tools//go/analysis/passes/buildtag:go_default_library' is duplicated in the 'deps' attribute of rule 'nogo_actual'
ERROR: /usr/.../BUILD.bazel:12:5: Label '@org_golang_x_tools//go/analysis/passes/atomic:go_default_library' is duplicated in the 'deps' attribute of rule 'nogo_actual'
ERROR: /usr/.../BUILD.bazel:12:5: Label '@org_golang_x_tools//go/analysis/passes/printf:go_default_library' is duplicated in the 'deps' attribute of rule 'nogo_actual'
ERROR: /usr/.../BUILD.bazel:12:5: Label '@org_golang_x_tools//go/analysis/passes/bools:go_default_library' is duplicated in the 'deps' attribute of rule 'nogo_actual'
ERROR: /usr/.../BUILD.bazel:12:5: Label '@org_golang_x_tools//go/analysis/passes/nilfunc:go_default_library' is duplicated in the 'deps' attribute of rule 'nogo_actual'
albertocavalcante commented 1 month ago

Although I agree this could be better handled, the nogo doc section at "running vet" states:

Setting vet = True is equivalent to adding the atomic, bools, buildtag, nilfunc, and printf analyzers from @org_golang_x_tools//go/analysis/passes to the deps list of your nogo rule.

And TOOLS_NOGO is a list of all the analysis/passes as per https://github.com/bazelbuild/rules_go/blob/master/go/def.bzl#L77-L120, so this should be expected to happen.

The way I take this, vet is a sugar syntax to replicate the behavior of go vet ./... but if you want to add other analyzers, you may individually pick those in deps.

nogo(
    name = "nogo",
    config = "nogo.json",  # needed a config to exclude a few files
    vet = True, # enabled vet
    visibility = ["//visibility:public"],
    deps = [
      "@org_golang_x_tools//go/analysis/passes/slog",
      "@org_golang_x_tools//go/analysis/passes/unreachable",
    ]
)

If you want to run all, you can simply remove the vet.