Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for windows symlinked bins #3977

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go/private/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ bzl_library(
"//go/private:mode",
"//go/private:platforms",
"//go/private:providers",
"//go/private/tools:copy_cmd",
],
)

Expand All @@ -160,6 +161,7 @@ bzl_library(
deps = [
"//go/private:providers",
"//go/private/rules:transition",
"//go/private/tools:copy_cmd",
],
)

Expand Down
8 changes: 7 additions & 1 deletion go/private/rules/cross.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ load(
"//go/private/rules:transition.bzl",
"go_cross_transition",
)
load("//go/private/tools:copy_cmd.bzl", "copy_cmd")

def _is_windows(ctx):
return ctx.configuration.host_path_separator == ";"
Expand Down Expand Up @@ -58,8 +59,13 @@ def _go_cross_impl(ctx):
if old_executable:
# Bazel requires executable rules to created the executable themselves,
# so we create a symlink in this rule so that it appears this rule created its executable.
# On windows symlinks are not reliable when using remote cache so we copy the binary instead.
# See https://github.com/bazelbuild/bazel/issues/21747
new_executable = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.symlink(output = new_executable, target_file = old_executable)
if _is_windows(ctx):
copy_cmd(ctx, old_executable, new_executable)
else:
ctx.actions.symlink(output = new_executable, target_file = old_executable)
new_default_info = DefaultInfo(
files = depset([new_executable]),
runfiles = old_default_info.default_runfiles,
Expand Down
19 changes: 14 additions & 5 deletions go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ load(
"GoLibrary",
"GoSource",
)
load("//go/private/tools:copy_cmd.bzl", "copy_cmd")

# A list of rules_go settings that are possibly set by go_transition.
# Keep their package name in sync with the implementation of
Expand All @@ -46,6 +47,9 @@ TRANSITIONED_GO_SETTING_KEYS = [
"//go/config:pgoprofile",
]

def _is_windows(ctx):
return ctx.configuration.host_path_separator == ";"

def _deduped_and_sorted(strs):
return sorted({s: None for s in strs}.keys())

Expand Down Expand Up @@ -283,12 +287,17 @@ def _go_reset_target_impl(ctx):
# In order for the symlink to have the same basename as the original
# executable (important in the case of proto plugins), put it in a
# subdirectory named after the label to prevent collisions.
# On windows symlinks are not reliable when using remote cache so we copy the binary instead.
# See https://github.com/bazelbuild/bazel/issues/21747
new_executable = ctx.actions.declare_file(paths.join(ctx.label.name, original_executable.basename))
ctx.actions.symlink(
output = new_executable,
target_file = original_executable,
is_executable = True,
)
if _is_windows(ctx):
copy_cmd(ctx, original_executable, new_executable)
else:
ctx.actions.symlink(
output = new_executable,
target_file = original_executable,
is_executable = True,
)
default_runfiles = default_runfiles.merge(ctx.runfiles([new_executable]))

providers.append(
Expand Down
6 changes: 6 additions & 0 deletions go/private/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ bzl_library(
"//go/private:providers",
],
)

bzl_library(
name = "copy_cmd",
srcs = ["copy_cmd.bzl"],
visibility = ["//go:__subpackages__"],
)
45 changes: 45 additions & 0 deletions go/private/tools/copy_cmd.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2019 The Bazel 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.

# `copy_cmd` from https://github.com/bazelbuild/bazel-skylib@6126842e3db2ec4986752f6dfc0860ca922997f1
def copy_cmd(ctx, src, dst):
# Most Windows binaries built with MSVC use a certain argument quoting
# scheme. Bazel uses that scheme too to quote arguments. However,
# cmd.exe uses different semantics, so Bazel's quoting is wrong here.
# To fix that we write the command to a .bat file so no command line
# quoting or escaping is required.
bat = ctx.actions.declare_file(ctx.label.name + "-cmd.bat")
ctx.actions.write(
output = bat,
# Do not use lib/shell.bzl's shell.quote() method, because that uses
# Bash quoting syntax, which is different from cmd.exe's syntax.
content = "@copy /Y \"%s\" \"%s\" >NUL" % (
src.path.replace("/", "\\"),
dst.path.replace("/", "\\"),
),
is_executable = True,
)
ctx.actions.run(
inputs = [src, bat],
outputs = [dst],
executable = "cmd.exe",
arguments = ["/C", bat.path.replace("/", "\\")],
mnemonic = "CopyFile",
progress_message = "Copying files",
use_default_shell_env = True,
execution_requirements = {
"no-remote": "1",
"no-cache": "1",
},
)
13 changes: 12 additions & 1 deletion tests/core/go_binary/linkmode.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("//go/private/tools:copy_cmd.bzl", "copy_cmd")

_LINKMODE_SETTING = "//go/config:linkmode"

def _linkmode_pie_transition_impl(settings, attr):
Expand All @@ -11,10 +13,19 @@ _linkmode_pie_transition = transition(
outputs = [_LINKMODE_SETTING],
)

def _is_windows(ctx):
return ctx.configuration.host_path_separator == ";"

def _linkmode_pie_wrapper(ctx):
in_binary = ctx.attr.target[0][DefaultInfo].files.to_list()[0]
out_binary = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.symlink(output = out_binary, target_file = in_binary)

# On windows symlinks are not reliable when using remote cache so we copy the binary instead.
# See https://github.com/bazelbuild/bazel/issues/21747
if _is_windows(ctx):
copy_cmd(ctx, in_binary, out_binary)
else:
ctx.actions.symlink(output = out_binary, target_file = in_binary)
return [
DefaultInfo(
files = depset([out_binary]),
Expand Down