Skip to content

Commit a2138a3

Browse files
committed
Always write the binary to the mode specific directory
and always create a symlink based on basename. This approach has the following advantages: - Reduce cache invalidation on mode changes - Have a binary (symlink) available at basename instead of the unexpected "%s_/%s" % (name, name) subfolder
1 parent cd29704 commit a2138a3

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

go/private/rules/binary.bzl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,29 @@ 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+
output = ctx.attr.out
153+
else:
154+
output = name
155+
executable = ctx.actions.declare_file(output)
156+
ctx.actions.symlink(
157+
output = executable,
158+
target_file = executable_binary,
159+
is_executable = True,
160+
)
161+
157162
providers = [
158163
archive,
159164
OutputGroupInfo(
@@ -305,15 +310,12 @@ def _go_binary_kwargs(go_cc_aspects = []):
305310
"basename": attr.string(
306311
doc = """The basename of this binary. The binary
307312
basename may also be platform-dependent: on Windows, we add an .exe extension.
313+
When not set, it will default to the label name.
308314
""",
309315
),
310316
"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.
317+
doc = """Sets the output symlink filename for the generated executable. When not set,
318+
it will default to basename.
317319
""",
318320
),
319321
"cgo": attr.bool(

0 commit comments

Comments
 (0)