Skip to content

Commit

Permalink
Fix use of //go with Bzlmod by removing dependency on `@org_golang_…
Browse files Browse the repository at this point in the history
…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: [email protected]/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 [email protected], but got [email protected] 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 <[email protected]>
  • Loading branch information
2 people authored and linzhp committed Apr 20, 2023
1 parent c807c7e commit 753b4ff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
11 changes: 3 additions & 8 deletions go/tools/go_bin_runner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions go/tools/go_bin_runner/process.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !unix

package main

import (
Expand Down
11 changes: 0 additions & 11 deletions go/tools/go_bin_runner/process_unix.go

This file was deleted.

8 changes: 8 additions & 0 deletions tests/bcr/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
19 changes: 19 additions & 0 deletions tests/bcr/go_version_test.sh
Original file line number Diff line number Diff line change
@@ -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 ]]

0 comments on commit 753b4ff

Please sign in to comment.