diff options
author | Brice <33697112+bricedp@users.noreply.github.com> | 2023-04-02 10:03:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-02 19:03:11 +0200 |
commit | 941602aedce63539747d57be384af999b0e72a5f (patch) | |
tree | 412fadc44df4c2b8d39df08f26f821a7aa86b7b1 | |
parent | e558f56df9995f571e49de336c3831d407d9ff70 (diff) | |
download | bazelbuild-rules_go-941602aedce63539747d57be384af999b0e72a5f.tar.gz |
Fix use of `//go` with Bzlmod by removing dependency on `@org_golang_x_sys` (#3512)
* Fix use of `//go` by exposing `@org_golang_x_sys` when using Bzlmod
Running `bazel run //go` (or `bazel run @rules_go//go` from another module) fails when using Bzlmod because the `@org_golang_x_sys` repository is not visible. Without Bzlmod, the repository is loaded through a WORKSPACE dependency and the error does not happen.
Two-step minimal reproduction from root of this repo:
1. `touch WORKSPACE.bzlmod`
2. `bazel run --enable_bzlmod //go`
Here's the error that produces:
```shell
$ bazel run --enable_bzlmod //go
DEBUG: gazelle@0.27.0/MODULE.bazel:7:6: WARNING: The bazel_gazelle Bazel module is still highly experimental and subject to change at any time. Only use it to try out bzlmod for now.
WARNING: For repository 'com_google_protobuf', the root module requires module version protobuf@3.19.2, but got protobuf@3.19.6 in the resolved dependency graph.
ERROR: /Users/brice/Documents/Code/rules_go/go/tools/go_bin_runner/BUILD.bazel:11:11: no such package '@[unknown repo 'org_golang_x_sys' requested from @]//unix': The repository '@[unknown repo 'org_golang_x_sys' requested from @]' could not be resolved: No repository visible as '@org_golang_x_sys' from main repository and referenced by '//go/tools/go_bin_runner:go_bin_runner_lib'
ERROR: Analysis of target '//go:go' failed; build aborted:
INFO: Elapsed time: 3.043s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (45 packages loaded, 865 targets configured)
currently loading: go/platform
ERROR: Build failed. Not running target
```
Here's the point in the BUILD file where this repository is being referenced:
https://github.com/bazelbuild/rules_go/blob/e558f56df9995f571e49de336c3831d407d9ff70/go/tools/go_bin_runner/BUILD.bazel#L25
* Remove `process_unix.go` and use `process.go` for all platforms. Eliminates dependency on `@org_golang_x_sys`.
* Add Bzlmod test for //go
---------
Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
-rw-r--r-- | go/tools/go_bin_runner/BUILD.bazel | 11 | ||||
-rw-r--r-- | go/tools/go_bin_runner/process.go | 2 | ||||
-rw-r--r-- | go/tools/go_bin_runner/process_unix.go | 11 | ||||
-rw-r--r-- | tests/bcr/BUILD.bazel | 8 | ||||
-rwxr-xr-x | tests/bcr/go_version_test.sh | 19 |
5 files changed, 30 insertions, 21 deletions
diff --git a/go/tools/go_bin_runner/BUILD.bazel b/go/tools/go_bin_runner/BUILD.bazel index a0e26d65..91be016e 100644 --- a/go/tools/go_bin_runner/BUILD.bazel +++ b/go/tools/go_bin_runner/BUILD.bazel @@ -12,18 +12,13 @@ go_library( name = "go_bin_runner_lib", srcs = [ "main.go", - ] + select({ - "@platforms//os:windows": ["process.go"], - "//conditions:default": ["process_unix.go"], - }), + "process.go", + ], importpath = "github.com/bazelbuild/rules_go/go/tools/go_bin_runner", visibility = ["//visibility:private"], deps = [ "//go/runfiles", - ] + select({ - "@platforms//os:windows": [], - "//conditions:default": ["@org_golang_x_sys//unix"], - }), + ], ) go_binary( diff --git a/go/tools/go_bin_runner/process.go b/go/tools/go_bin_runner/process.go index 8b344c96..af236549 100644 --- a/go/tools/go_bin_runner/process.go +++ b/go/tools/go_bin_runner/process.go @@ -1,5 +1,3 @@ -//go:build !unix - package main import ( diff --git a/go/tools/go_bin_runner/process_unix.go b/go/tools/go_bin_runner/process_unix.go deleted file mode 100644 index f59af5d2..00000000 --- a/go/tools/go_bin_runner/process_unix.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build unix - -package main - -import ( - "golang.org/x/sys/unix" -) - -func ReplaceWithProcess(args, env []string) error { - return unix.Exec(args[0], args, env) -} diff --git a/tests/bcr/BUILD.bazel b/tests/bcr/BUILD.bazel index 813d72fe..d5c5f6f1 100644 --- a/tests/bcr/BUILD.bazel +++ b/tests/bcr/BUILD.bazel @@ -45,3 +45,11 @@ go_test( embed = [":mockable"], deps = ["@my_rules_go//extras/gomock"], ) + +sh_test( + name = "go_version_test", + srcs = ["go_version_test.sh"], + data = ["@my_rules_go//go"], + env = {"GO_TOOL_RLOCATION": "$(rlocationpath @my_rules_go//go)"}, + deps = ["@bazel_tools//tools/bash/runfiles"], +) diff --git a/tests/bcr/go_version_test.sh b/tests/bcr/go_version_test.sh new file mode 100755 index 00000000..986d91d7 --- /dev/null +++ b/tests/bcr/go_version_test.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# --- begin runfiles.bash initialization v3 --- +# Copy-pasted from the Bazel Bash runfiles library v3. +set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v3 --- + +GO_TOOL=$(rlocation "$GO_TOOL_RLOCATION") +# Set runfiles variables for subprocess. +runfiles_export_envvars +# Simulate a bazel run environment. +export BUILD_WORKING_DIRECTORY=$(pwd) +[[ "$("$GO_TOOL" version)" =~ ^go ]] |