Skip to content

Commit 74199c9

Browse files
authored
use sh_toolchain to get a truer path to bash (#4465)
<!-- Thanks for sending a PR! Before submitting: 1. If this is your first PR, please read CONTRIBUTING.md and sign the CLA first. We cannot review code without a signed CLA. 2. Please file an issue *first*. All features and most bug fixes should have an associated issue with a design discussed and decided upon. Small bug fixes and documentation improvements don't need issues. 3. New features and bug fixes must have tests. Documentation may need to be updated. If you're unsure what to update, send the PR, and we'll discuss in review. 4. Note that PRs updating dependencies and new Go versions are not accepted. Please file an issue instead. --> **What type of PR is this?** > Uncomment one line below and remove others. > > Bug fix **What does this PR do? Why is it needed?** Use the `sh_toolchain` to find the path to `bash` instead of bazel. **Which issues(s) does this PR fix?** Fixes #4352 **Other notes for review**
1 parent 56929da commit 74199c9

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

go/config/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ bool_flag(
3939
visibility = ["//visibility:public"],
4040
)
4141

42+
bool_flag(
43+
name = "experimental_use_sh_toolchain",
44+
build_setting_default = False,
45+
visibility = ["//visibility:public"],
46+
)
47+
4248
string_flag(
4349
name = "linkmode",
4450
build_setting_default = LINKMODE_NORMAL,

go/private/rules/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ load(
55
"TRANSITIONED_GO_SETTING_KEYS",
66
)
77

8-
exports_files(["library.bzl"])
8+
exports_files([
9+
"library.bzl",
10+
"binary_wrapper.sh",
11+
])
912

1013
[
1114
string_setting(

go/private/rules/binary.bzl

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1516
load(
1617
"//go/private:common.bzl",
1718
"GO_TOOLCHAIN",
@@ -574,26 +575,22 @@ exit /b %GO_EXIT_CODE%
574575
args.add(out)
575576
args.add_all(ctx.files.srcs)
576577

577-
# We do not use -a here as the cache drastically reduces the time spent
578-
# on the second go build invocation (roughly 50% faster).
579-
ctx.actions.run_shell(
580-
# The value of GOCACHE/GOPATH are determined from HOME.
581-
# We place them in the execroot to avoid dependency on `mktemp` and because we don't know
582-
# a safe scratch space on all systems. Note that HOME must be an absolute path, otherwise the
583-
# Go toolchain will write some outputs to the wrong place and the result will be uncacheable.
584-
# We include an output path of this action to prevent collisions with anything else,
585-
# including differently configured versions of the same target, under an unsandboxed strategy.
586-
command = """
587-
set -eu
588-
export HOME="$PWD/_go_tool_binary-fake-home-${{1//\\//_}}"
589-
trap "{go} clean -cache" EXIT;
590-
{go} build -trimpath -ldflags='-buildid="" {ldflags}' -o "$1" cmd/pack
591-
shift
592-
{go} build -trimpath -ldflags='-buildid="" {ldflags}' -o "$@"
593-
""".format(
594-
go = sdk.go.path,
595-
ldflags = ctx.attr.ldflags,
596-
),
578+
setting = ctx.attr._use_sh_toolchain_for_bootstrap_process_wrapper[BuildSettingInfo].value
579+
sh_toolchain = ctx.toolchains["@bazel_tools//tools/sh:toolchain_type"]
580+
binary_wrapper = ctx.file._binary_wrapper
581+
if setting and sh_toolchain:
582+
binary_wrapper = ctx.actions.declare_file(name + "_binary_wrapper.sh")
583+
ctx.actions.expand_template(
584+
template = ctx.file._binary_wrapper,
585+
output = binary_wrapper,
586+
is_executable = True,
587+
substitutions = {
588+
"#!/usr/bin/env bash": "#!{}".format(sh_toolchain.path),
589+
},
590+
)
591+
592+
ctx.actions.run(
593+
executable = binary_wrapper,
597594
arguments = [args],
598595
tools = [sdk.go],
599596
env = {
@@ -602,6 +599,8 @@ shift
602599
"GO111MODULE": "off",
603600
"GOTELEMETRY": "off",
604601
"GOENV": "off",
602+
"GO_BINARY": sdk.go.path,
603+
"LD_FLAGS": ctx.attr.ldflags,
605604
},
606605
inputs = depset(
607606
ctx.files.srcs,
@@ -634,6 +633,13 @@ go_tool_binary = rule(
634633
doc = "Raw value to pass to go build via -ldflags without tokenization",
635634
),
636635
"out_pack": attr.output(),
636+
"_binary_wrapper": attr.label(
637+
allow_single_file = True,
638+
default = "//go/private/rules:binary_wrapper.sh",
639+
),
640+
"_use_sh_toolchain_for_bootstrap_process_wrapper": attr.label(
641+
default = Label("//go/config:experimental_use_sh_toolchain"),
642+
),
637643
},
638644
executable = True,
639645
doc = """Used instead of go_binary for executables used in the toolchain.
@@ -648,6 +654,7 @@ rules_go as well as the `pack` tool provided by the Go SDK in source form
648654
only as of Go 1.25. Combining both builds into a single action drastically
649655
reduces the overall build time due to Go's own caching mechanism.
650656
""",
657+
toolchains = [config_common.toolchain_type("@bazel_tools//tools/sh:toolchain_type", mandatory = False)],
651658
)
652659

653660
def gc_linkopts(ctx):

go/private/rules/binary_wrapper.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
# The value of GOCACHE/GOPATH are determined from HOME.
6+
# We place them in the execroot to avoid dependency on `mktemp` and because we don't know
7+
# a safe scratch space on all systems. Note that HOME must be an absolute path, otherwise the
8+
# Go toolchain will write some outputs to the wrong place and the result will be uncacheable.
9+
# We include an output path of this action to prevent collisions with anything else,
10+
# including differently configured versions of the same target, under an unsandboxed strategy.
11+
12+
export HOME="${PWD}/_go_tool_binary-fake-home-${1//\\//_}"
13+
trap "${GO_BINARY} clean -cache" EXIT
14+
15+
# We do not use -a here as the cache drastically reduces the time spent
16+
# on the second go build invocation (roughly 50% faster).
17+
18+
"${GO_BINARY}" build -trimpath -ldflags="-buildid=\"\" ${LD_FLAGS}" -o "$1" cmd/pack
19+
shift
20+
21+
"${GO_BINARY}" build -trimpath -ldflags="-buildid=\"\" ${LD_FLAGS}" -o "$@"

0 commit comments

Comments
 (0)