Skip to content

Commit 50d2b7f

Browse files
committed
Always write the binary to the mode specific directory
and create a symlink if "out" is specified. This reduces cache invalidation on mode changes.
1 parent cd29704 commit 50d2b7f

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

go/private/rules/binary.bzl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,31 @@ def _go_binary_impl(ctx):
136136
name = ctx.attr.basename
137137
if not name:
138138
name = ctx.label.name
139-
executable = None
140-
if ctx.attr.out:
141-
# Use declare_file instead of attr.output(). When users set output files
142-
# directly, Bazel warns them not to use the same name as the rule, which is
143-
# the common case with go_binary.
144-
executable = ctx.actions.declare_file(ctx.attr.out)
145-
archive, executable, runfiles = go.binary(
139+
archive, executable_binary, runfiles = go.binary(
146140
go,
147141
name = name,
148142
source = go_info,
149143
gc_linkopts = gc_linkopts(ctx),
150144
version_file = ctx.version_file,
151145
info_file = ctx.info_file,
152-
executable = executable,
146+
executable = None,
153147
)
154148
validation_output = archive.data._validation_output
155149
nogo_diagnostics = archive.data._nogo_diagnostics
156150

151+
if ctx.attr.out:
152+
# Use declare_file instead of attr.output(). When users set output files
153+
# directly, Bazel warns them not to use the same name as the rule, which is
154+
# the common case with go_binary.
155+
executable = ctx.actions.declare_file(ctx.attr.out)
156+
ctx.actions.symlink(
157+
output = executable,
158+
target_file = executable_binary,
159+
is_executable = True,
160+
)
161+
else:
162+
executable = executable_binary
163+
157164
providers = [
158165
archive,
159166
OutputGroupInfo(
@@ -305,15 +312,11 @@ def _go_binary_kwargs(go_cc_aspects = []):
305312
"basename": attr.string(
306313
doc = """The basename of this binary. The binary
307314
basename may also be platform-dependent: on Windows, we add an .exe extension.
315+
When not set, it will default to the label name.
308316
""",
309317
),
310318
"out": attr.string(
311-
doc = """Sets the output filename for the generated executable. When set, `go_binary`
312-
will write this file without mode-specific directory prefixes, without
313-
linkmode-specific prefixes like "lib", and without platform-specific suffixes
314-
like ".exe". Note that without a mode-specific directory prefix, the
315-
output file (but not its dependencies) will be invalidated in Bazel's cache
316-
when changing configurations.
319+
doc = """Sets the output symlink filename for the generated executable.
317320
""",
318321
),
319322
"cgo": attr.bool(

0 commit comments

Comments
 (0)