Skip to content

Commit

Permalink
Fix matching between Starlark and query labels in gopackagesdriver
Browse files Browse the repository at this point in the history
This relies on the query flag `--consistent_labels`, which is available
in Bazel 6.4.0.
  • Loading branch information
fmeum committed Sep 18, 2023
1 parent 09a206c commit 19b2d98
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions go/private/BUILD.sdk.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ go_sdk(
go_tool_binary(
name = "builder",
srcs = ["@io_bazel_rules_go//go/tools/builders:builder_srcs"],
ldflags = "-X main.rulesGoStdlibPrefix={rules_go_stdlib_prefix}",
sdk = ":go_sdk",
)

Expand Down
9 changes: 7 additions & 2 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,13 @@ def _go_tool_binary_impl(ctx):
if sdk.goos == "windows":
gopath = ctx.actions.declare_directory("gopath")
gocache = ctx.actions.declare_directory("gocache")
cmd = "@echo off\nset GOMAXPROCS=1\nset GOCACHE=%cd%\\{gocache}\nset GOPATH=%cd%\\{gopath}\n{go} build -o {out} -trimpath {srcs}".format(
cmd = "@echo off\nset GOMAXPROCS=1\nset GOCACHE=%cd%\\{gocache}\nset GOPATH=%cd%\\{gopath}\n{go} build -o {out} -trimpath -ldflags \"{ldflags}\" {srcs}".format(
gopath = gopath.path,
gocache = gocache.path,
go = sdk.go.path.replace("/", "\\"),
out = out.path,
srcs = " ".join([f.path for f in ctx.files.srcs]),
ldflags = ctx.attr.ldflags,
)
bat = ctx.actions.declare_file(name + ".bat")
ctx.actions.write(
Expand All @@ -461,10 +462,11 @@ def _go_tool_binary_impl(ctx):
)
else:
# Note: GOPATH is needed for Go 1.16.
cmd = "GOMAXPROCS=1 GOCACHE=$(mktemp -d) GOPATH=$(mktemp -d) {go} build -o {out} -trimpath {srcs}".format(
cmd = "GOMAXPROCS=1 GOCACHE=$(mktemp -d) GOPATH=$(mktemp -d) {go} build -o {out} -trimpath -ldflags '{ldflags}' {srcs}".format(
go = sdk.go.path,
out = out.path,
srcs = " ".join([f.path for f in ctx.files.srcs]),
ldflags = ctx.attr.ldflags,
)
ctx.actions.run_shell(
command = cmd,
Expand All @@ -490,6 +492,9 @@ go_tool_binary = rule(
providers = [GoSDK],
doc = "The SDK containing tools and libraries to build this binary",
),
"ldflags": attr.string(
doc = "Raw value to pass to go build via -ldflags without tokenization",
),
},
executable = True,
doc = """Used instead of go_binary for executables used in the toolchain.
Expand Down
2 changes: 2 additions & 0 deletions go/private/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ def _sdk_build_file(ctx, platform, version, experiments):
if not "nocoverageredesign" in experiments and not "coverageredesign" in experiments:
experiments = experiments + ["nocoverageredesign"]

rules_go_stdlib_prefix = str(Label("//stdlib:unused"))[:-len(":unused")]
ctx.template(
"BUILD.bazel",
ctx.path(ctx.attr._sdk_build_file),
Expand All @@ -500,6 +501,7 @@ def _sdk_build_file(ctx, platform, version, experiments):
"{exe}": ".exe" if goos == "windows" else "",
"{version}": version,
"{experiments}": repr(experiments),
"{rules_go_stdlib_prefix}": rules_go_stdlib_prefix,
},
)

Expand Down
12 changes: 10 additions & 2 deletions go/tools/builders/stdliblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ type goListPackage struct {
DepsErrors []*flatPackagesError // errors loading dependencies
}

var rulesGoStdlibPrefix string

func init() {
if rulesGoStdlibPrefix == "" {
panic("rulesGoStdlibPrefix should have been set via -X")
}
}

func stdlibPackageID(importPath string) string {
return "@io_bazel_rules_go//stdlib:" + importPath
return rulesGoStdlibPrefix + importPath
}

// outputBasePath replace the cloneBase with output base label
Expand Down Expand Up @@ -280,7 +288,7 @@ func stdliblist(args []string) error {

encoder := json.NewEncoder(jsonFile)
decoder := json.NewDecoder(jsonData)
pathReplaceFn := func (s string) string {
pathReplaceFn := func(s string) string {
if strings.HasPrefix(s, absCachePath) {
return strings.Replace(s, absCachePath, filepath.Join("__BAZEL_EXECROOT__", *cachePath), 1)
}
Expand Down
18 changes: 5 additions & 13 deletions go/tools/gopackagesdriver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//go:def.bzl", "go_binary", "go_library")
load(":aspect.bzl", "bazel_supports_canonical_label_literals")
load(":constants.bzl", "RULES_GO_REPOSITORY_NAME")

go_library(
name = "gopackagesdriver_lib",
Expand All @@ -21,19 +22,10 @@ go_library(
go_binary(
name = "gopackagesdriver",
embed = [":gopackagesdriver_lib"],
visibility = ["//visibility:public"],
x_defs = {
# Determine the name of the rules_go repository as we need to specify it when invoking the
# aspect.
# If canonical label literals are supported, we can use a canonical label literal (starting
# with @@) to pass the repository_name() through repo mapping unchanged.
# If canonical label literals are not supported, then bzlmod is certainly not enabled and
# we can assume that the repository name is not affected by repo mappings.
# If run in the rules_go repo itself, repository_name() returns "@", which is equivalent to
# "@io_bazel_rules_go" since Bazel 6:
# https://github.com/bazelbuild/bazel/commit/7694cf75e6366b92e3905c2ad60234cda57627ee
# TODO: Once we drop support for Bazel 5, we can remove the feature detection logic and
# use "@" + repository_name().
"rulesGoRepositoryName": "@" + repository_name() if bazel_supports_canonical_label_literals() else repository_name(),
# Determine the repository part of labels pointing into the rules_go repo. This is needed
# both to specify the aspect and to match labels in query output.
"rulesGoRepositoryName": RULES_GO_REPOSITORY_NAME,
},
visibility = ["//visibility:public"],
)
7 changes: 6 additions & 1 deletion go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ func (b *BazelJSONBuilder) outputGroupsForMode(mode LoadMode) string {
}

func (b *BazelJSONBuilder) query(ctx context.Context, query string) ([]string, error) {
queryArgs := concatStringsArrays(bazelQueryFlags, []string{
var bzlmodQueryFlags []string
if strings.HasPrefix(rulesGoRepositoryName, "@@") {
// Requires Bazel 6.4.0.
bzlmodQueryFlags = []string{"--consistent_labels"}
}
queryArgs := concatStringsArrays(bazelQueryFlags, bzlmodQueryFlags, []string{
"--ui_event_filters=-info,-stderr",
"--noshow_progress",
"--order_output=no",
Expand Down
15 changes: 15 additions & 0 deletions go/tools/gopackagesdriver/constants.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Go Rules Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

RULES_GO_REPOSITORY_NAME = str(Label("//:unused"))[:-len("//:unused")]

0 comments on commit 19b2d98

Please sign in to comment.