bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.2k stars 380 forks source link

Gazelle fails to compile when go toolchain has nogo enabled #1397

Open lopopolo opened 1 year ago

lopopolo commented 1 year ago

What version of gazelle are you using?

v0.28.0

What version of rules_go are you using?

v0.37.0

What version of Bazel are you using?

This issue reproduces with both bazel 5.4.0 and bazel 6.0.0

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

yes

What operating system and processor architecture are you using?

$ sw_vers
ProductName:            macOS
ProductVersion:         13.1
BuildVersion:           22C65
$ uname -m
x86_64

What did you do?

WORKSPACE

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "56d8c5a5c91e1af73eca71a6fab2ced959b67c86d12ba37feedb0a2dfea441a6",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.37.0/rules_go-v0.37.0.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.37.0/rules_go-v0.37.0.zip",
    ],
)

http_archive(
    name = "bazel_gazelle",
    sha256 = "448e37e0dbf61d6fa8f00aaa12d191745e14f07c31cabfa731f0c8e8a4f41b97",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
    ],
)

# http_archive(
#     name = "com_google_protobuf",
#     sha256 = "29b0f6b6d5714f212b8549cd0cb6fc531672630e41fb99d445421bc4d1bbb9cd",
#     strip_prefix = "protobuf-21.12",
#     urls = [
#         "https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protobuf-all-21.12.zip",
#     ],
# )

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(
    nogo = "@io_bazel_rules_go//:tools_nogo",
    version = "1.19.3",
)

# Protobuf setup
# See https://github.com/bazelbuild/rules_go#protobuf-and-grpc

# load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
#
# protobuf_deps()

# Gazelle setup
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

BUILD.bazel

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

gazelle(
    name = "gazelle",
)

then run:

$ bazel clean --expunge
$ USE_BAZEL_VERSION=5.4.0 bazel run //:gazelle

and

$ bazel clean --expunge
$ USE_BAZEL_VERSION=6.0.0 bazel run //:gazelle

What did you expect to see?

No compilation errors.

What did you see instead?

$ bazel clean --expunge
Starting local Bazel server and connecting to it...
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
[19:36] [dev/repos/nogo-proto] took 4s
$ USE_BAZEL_VERSION=5.4.0 bazel run //:gazelle
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:gazelle (134 packages loaded, 10454 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_lopopolo/7088fa30d3587f1c28a18f8cf1cd9036/external/org_golang_x_tools/cmd/goyacc/BUILD.bazel:13:10: GoCompilePkg external/org_golang_x_tools/cmd/goyacc/goyacc.a failed: (Exit 1): builder failed: error executing command bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_amd64 -src external/org_golang_x_tools/cmd/goyacc/doc.go -src ... (remaining 23 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
compilepkg: nogo: errors found by nogo during build-time code analysis:
external/org_golang_x_tools/cmd/goyacc/yacc.go:425:5: declaration of "t" shadows declaration at line 400 (shadow)
external/org_golang_x_tools/cmd/goyacc/yacc.go:2787:7: declaration of "j" shadows declaration at line 2772 (shadow)
Target //:gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 30.112s, Critical Path: 4.31s
INFO: 91 processes: 20 internal, 71 darwin-sandbox.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully

and

$ USE_BAZEL_VERSION=6.0.0 bazel run //:gazelle
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:gazelle (133 packages loaded, 10104 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_lopopolo/7088fa30d3587f1c28a18f8cf1cd9036/external/org_golang_x_tools/cmd/goyacc/BUILD.bazel:13:10: GoCompilePkg external/org_golang_x_tools/cmd/goyacc/goyacc.a [for tool] failed: (Exit 1): builder failed: error executing command (from target @org_golang_x_tools//cmd/goyacc:goyacc) bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_amd64 -src external/org_golang_x_tools/cmd/goyacc/doc.go -src ... (remaining 23 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
compilepkg: nogo: errors found by nogo during build-time code analysis:
external/org_golang_x_tools/cmd/goyacc/yacc.go:425:5: declaration of "t" shadows declaration at line 400 (shadow)
external/org_golang_x_tools/cmd/goyacc/yacc.go:2787:7: declaration of "j" shadows declaration at line 2772 (shadow)
Target //:gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 30.090s, Critical Path: 4.38s
INFO: 87 processes: 20 internal, 67 darwin-sandbox.
FAILED: Build did NOT complete successfully
ERROR: Build failed. Not running target
cotocisternas commented 1 year ago

You need to exclude that lib from that linter in the nogo.json file

{
  "shadow": {
    "description": "Shadow config for all analyzers.",
    "exclude_files": {
      "external/org_golang_x_tools/": "exclude golang_x_tools"
    }
  }
}

Full example here nogo-docs