bazelbuild / rules_go

Go rules for Bazel
Apache License 2.0
1.37k stars 654 forks source link

go_repository_tools fails due to missing go executable #2377

Closed bimargulies-google closed 4 years ago

bimargulies-google commented 4 years ago

Running on an Alpine Docker image,

In WORKSPACE, we've got io_bazel_rules_go as follows. It may not be the root of the problem, since the line number of the error points golang/protobuf, also copied below. In any case, the actual failure is external/go_sdk/bin/go not found, as shown below.

http_archive(
    name = "io_bazel_rules_go",
    urls = [
        "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz",
    ],
    sha256 = "e88471aea3a3a4f19ec1310a55ba94772d087e9ce46e41ae38ecebe17935de7b",
)
go_repository(
    name = "com_github_golang_protobuf",
    tag="v1.3.0",
    importpath = "github.com/golang/protobuf"
)

Builds fail with

Step #1: INFO: Call stack for the definition of repository 'bazel_gazelle_go_repository_tools' which is a go_repository_tools (rule definition at /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/bazel_gazelle/internal/go_repository_tools.bzl:116:23):
Step #1:  - /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/bazel_gazelle/deps.bzl:72:5
Step #1:  - /src/WORKSPACE:191:1

Step #1: INFO: Call stack for the definition of repository 'bazel_gazelle_go_repository_tools' which is a go_repository_tools (rule definition at /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/bazel_gazelle/internal/go_repository_tools.bzl:116:23):
Step #1:  - /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/bazel_gazelle/deps.bzl:72:5
Step #1:  - /src/WORKSPACE:191:1
Step #1: ERROR: An error occurred during the fetch of repository 'bazel_gazelle_go_repository_tools':
Step #1:    list_repository_tools_srcs: env: can't execute '/root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/go_sdk/bin/go': No such file or directory
jayconrod commented 4 years ago

Could you post a larger part of your WORKSPACE file, including everything related to rules_go and Gazelle?

I suspect that either go_register_toolchains is not called or the Go toolchain is downloaded with a name other than go_sdk. That's fine, but gazelle_dependencies needs to be called with the actual name:

gazelle_dependencies(go_sdk = "actual_go_sdk")
bimargulies-google commented 4 years ago
workspace(name = "redacted")

################################################################################
# Generic Bazel Support                                                        #
################################################################################

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

http_archive(
    name = "bazel_skylib",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
    ],
    sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
    name = "com_github_bazelbuild_buildtools",
    strip_prefix = "buildtools-bf564b4925ab5876a3f64d8b90fab7f769013d42",
    url = "https://github.com/bazelbuild/buildtools/archive/bf564b4925ab5876a3f64d8b90fab7f769013d42.zip",
    sha256 = "b5d7dbc6832f11b6468328a376de05959a1a9e4e9f5622499d3bab509c26b46a"
)

load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

################################################################################
# Google APIs protos                                                           #
################################################################################

http_archive(
    name="com_google_googleapis",
    url = "https://github.com/googleapis/googleapis/archive/eba3897fff7c49ed85d3c47fc96fe96e47f6f684.zip",
    strip_prefix="googleapis-eba3897fff7c49ed85d3c47fc96fe96e47f6f684",
    # Patches missing C++ build rules for Spanner and IAM protos
    patches = ["@//bazel:googleapis.patch"],
    sha256 = "e12e955d937682a01dbceed657ce79f412e374347e23883bc559fadc1af39b10",
)

load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")

switched_rules_by_language(
    name = "com_google_googleapis_imports",
    cc = True,
    go = True,
    grpc = True,
)

################################################################################
# C++ Libraries                                                                #
################################################################################

http_archive(
    name = "com_googlesource_code_re2",
    strip_prefix = "re2-d1394506654e0a19a92f3d8921e26f7c3f4de969",
    url = "https://github.com/google/re2/archive/d1394506654e0a19a92f3d8921e26f7c3f4de969.tar.gz",
    sha256 = "ac855fb93dfa6878f88bc1c399b9a2743fdfcb3dc24b94ea9a568a1c990b1212",
)

http_archive(
    name = "com_google_protobuf",
    strip_prefix = "protobuf-3.11.2",
    url = "https://github.com/protocolbuffers/protobuf/archive/v3.11.2.tar.gz",
    sha256 = "e8c7601439dbd4489fe5069c33d374804990a56c2f710e00227ee5d8fd650e67"
)

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

protobuf_deps()

http_archive(
    name = "com_google_absl",
    strip_prefix = "abseil-cpp-1de0166368e2ae67347f92099d6dca3ab3a4a496",
    url = "https://github.com/abseil/abseil-cpp/archive/1de0166368e2ae67347f92099d6dca3ab3a4a496.tar.gz",
    sha256 = "c632ae484401789cea79ba9c4e625bc121020e81fc90ca3620e3ba69e9b26feb",
)

http_archive(
    name = "com_google_googletest",
    strip_prefix = "googletest-release-1.10.0",
    urls = ["https://github.com/google/googletest/archive/release-1.10.0.tar.gz"],
    sha256 = "9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb",
)

http_archive(
    name = "com_github_grpc_grpc",
    urls = ["https://github.com/grpc/grpc/archive/v1.26.0.tar.gz"],
    strip_prefix = "grpc-1.26.0",
    sha256 = "2fcb7f1ab160d6fd3aaade64520be3e5446fc4c6fa7ba6581afdc4e26094bd81",
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

http_archive(
    name = "com_google_zetasql",
    url = "https://github.com/google/zetasql/archive/8fa97e0a57513fe38b5f9037b6fc3de1b6340ab7.zip",
    strip_prefix = "zetasql-8fa97e0a57513fe38b5f9037b6fc3de1b6340ab7",
    # Patches applied:
    # - Give visibility to ZetaSQL's base library to reuse some utilities
    # - Allow implicit conversion of grpc::Status to zetasql_base::Status
    # - Patches for flex, m4, and icu to work on MacOS
    patches = ["//bazel:zetasql.patch"],
    sha256 = "9f818406ddd74a78d6a965cf9148bf718c0760b46427514c5ad20cfb3fec2391",
)

load("@com_google_zetasql//bazel:zetasql_deps_step_1.bzl", "zetasql_deps_step_1")

zetasql_deps_step_1()

load("@com_google_zetasql//bazel:zetasql_deps_step_2.bzl", "zetasql_deps_step_2")

zetasql_deps_step_2()

load("@com_google_zetasql//bazel:zetasql_deps_step_3.bzl", "zetasql_deps_step_3")

zetasql_deps_step_3()

load("@com_google_zetasql//bazel:zetasql_deps_step_4.bzl", "zetasql_deps_step_4")

zetasql_deps_step_4()

http_archive(
    name = "com_github_googleapis_google_cloud_cpp_spanner",
    url = "https://github.com/googleapis/google-cloud-cpp-spanner/archive/5a03fc23b85af214ed8e5954c50f351c20afe814.tar.gz",
    strip_prefix = "google-cloud-cpp-spanner-5a03fc23b85af214ed8e5954c50f351c20afe814",
    sha256 = "4132228b8e64bb3dac2e13132f97534780f42a4e4ba472d88e85dc2d6488a6c0",
)

load("@com_github_googleapis_google_cloud_cpp_spanner//bazel:google_cloud_cpp_spanner_deps.bzl", "google_cloud_cpp_spanner_deps")

google_cloud_cpp_spanner_deps()

load("@com_github_googleapis_google_cloud_cpp_common//bazel:google_cloud_cpp_common_deps.bzl", "google_cloud_cpp_common_deps")

google_cloud_cpp_common_deps()

################################################################################
# Go Build Support                                                             #
################################################################################

http_archive(
    name = "io_bazel_rules_go",
    urls = [
        "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz",
    ],
    sha256 = "e88471aea3a3a4f19ec1310a55ba94772d087e9ce46e41ae38ecebe17935de7b",
)

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

go_rules_dependencies()

go_register_toolchains()

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

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

gazelle_dependencies()

################################################################################
# GRPC Gateway                                                                 #
################################################################################

http_archive(
    name = "grpc_ecosystem_grpc_gateway",
    strip_prefix="grpc-gateway-1.12.1",
    urls = ["https://github.com/grpc-ecosystem/grpc-gateway/archive/v1.12.1.zip"],
    sha256 = "7566d466d1b7319b73a13395def529ce5a04f9298e1c8fabbc151dd89987ce58",
)

go_repository(
    name = "com_github_golang_protobuf",
    tag="v1.3.0",
    importpath = "github.com/golang/protobuf"
)

go_repository(
        name = "in_gopkg_yaml_v2",
        importpath = "gopkg.in/yaml.v2",
        sum = "h1:+t9dhfO+GNOIGJof6kPOAenx7YgrZMTdRPV+EsnPabk=",
        version = "v2.0.0-20170812160011-eb3733d160e7",
)

go_repository(
        name = "com_github_ghodss_yaml",
        importpath = "github.com/ghodss/yaml",
        sum = "h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=",
        version = "v1.0.0",
)

go_repository(
        name = "com_github_golang_glog",
        importpath = "github.com/golang/glog",
        sum = "h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=",
        version = "v0.0.0-20160126235308-23def4e6c14b",
)

go_repository(
    name = "org_golang_google_grpc",
    importpath = "google.golang.org/grpc",
    sum = "h1:cfg4PD8YEdSFnm7qLV4++93WcmhH2nIUhMjhdCvl3j8=",
    version = "v1.19.0",
)

go_repository(
    name = "org_golang_x_net",
    importpath = "golang.org/x/net",
    sum = "h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=",
    version = "v0.0.0-20181220203305-927f97764cc3",
)

go_repository(
    name = "org_golang_x_text",
    importpath = "golang.org/x/text",
    sum = "h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=",
    version = "v0.3.0",
)

################################################################################
# Python Libraries                                                             #
################################################################################

http_archive(
    name = "org_python_pypi_portpicker",
    urls = [
        "https://pypi.python.org/packages/d9/f4/0188bc07d38b5f9dd192b8329c5e098e3b23552c01a96fd08973dba9e315/portpicker-1.3.1.tar.gz",
    ],
    sha256 = "d2cdc776873635ed421315c4d22e63280042456bbfa07397817e687b142b9667",
    strip_prefix = "portpicker-1.3.1/src",
    build_file = "//bazel:portpicker.BUILD",
)

################################################################################
# Java Libraries                                                               #
################################################################################

RULES_JVM_EXTERNAL_TAG = "3.0"
RULES_JVM_EXTERNAL_SHA = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a"

http_archive(
    name = "rules_jvm_external",
    strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
    sha256 = RULES_JVM_EXTERNAL_SHA,
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    artifacts = [
        "net.java.dev.javacc:javacc:jar:6.1.2",
    ],
    repositories = [
        "https://repo1.maven.org/maven2",
    ],
)
bimargulies-google commented 4 years ago

Changing the gazelle rule to as you suggested leads to

The repository '@actual_go_sdk' could not be resolved

jayconrod commented 4 years ago

Sorry, #2378 actually resolved #2376, not this issue.

jayconrod commented 4 years ago

I was able to reproduce this with a more minimal Dockerfile. It reproduces with a WORKSPACE file with the rules_go boilerplate code and a trivial go_binary.

# This image is based on alpine and includes nix. (https://github.com/NixOS/docker)
FROM nixos/nix
RUN apk add build-base go tzdata git patch
RUN apk add bazel --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
# nix version of patch works with googleapis.
RUN nix-env --install patch-2.7.6
# minimal WORKSPACE
COPY src /src

The error is misleading. The loader is missing, not the go binary.

# ls external/go_sdk/bin/go
external/go_sdk/bin/go

# ldd external/go_sdk/bin/go
    /lib64/ld-linux-x86-64.so.2 (0x7f063db02000)
    libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f063db02000)
    libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f063db02000)

# ls /lib64/ld-linux-x86-64.so.2
ls: /lib64/ld-linux-x86-64.so.2: No such file or directory

This was reported earlier in #1376. Unfortunately, I can't provide support for NixOS or other distributions where the Go toolchain downloaded from golang.org doesn't work. rules_go may still work if you install the Go toolchain outside the workspace, then register it using:

go_register_toolchains(go_version = "host")

Some more changes may be needed. I tried this briefly, but it looks like the system Go toolchain might not work either:

ERROR: /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/go_sdk/BUILD.bazel:43:1: error executing shell command: '/bin/bash -c 'external/go_sdk/bin/go' tool compile -o 'bazel-out/host/bin/external/go_sdk/builder'.a -I 'external/go_sdk' -trimpath=$PWD $@ && 'external/go_sdk/bin/go' tool link -o 'bazel-out/host/...' failed (Exit 1)
2020/02/22 14:21:14 cannot handle R_TLS_IE (sym sync/atomic.(*Value).Store) when linking internally