Skip to content

Commit bbfb74c

Browse files
authored
fix: fix host platform detection on all copy actions which always run locally (#241)
1 parent aa42a92 commit bbfb74c

File tree

13 files changed

+114
-41
lines changed

13 files changed

+114
-41
lines changed

WORKSPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ go_rules_dependencies()
3535
go_register_toolchains(version = "1.17.2")
3636

3737
gazelle_dependencies()
38+
39+
# buildifier: disable=bzl-visibility
40+
load("//lib/private:local_config_platform.bzl", "local_config_platform")
41+
42+
local_config_platform(
43+
name = "local_config_platform",
44+
)

docs/copy_directory.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/copy_file.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/copy_to_bin.md

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/copy_to_directory.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/private/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ exports_files(
88
)
99

1010
exports_files(
11-
["diff_test_tmpl.sh", "diff_test_tmpl.bat", "parse_status_file.jq"],
11+
[
12+
"diff_test_tmpl.sh",
13+
"diff_test_tmpl.bat",
14+
"parse_status_file.jq",
15+
],
1216
visibility = ["//visibility:public"],
1317
)
1418

1519
bzl_library(
1620
name = "copy_common",
1721
srcs = ["copy_common.bzl"],
22+
deps = ["@local_config_platform//:constraints"],
1823
)
1924

2025
bzl_library(

lib/private/copy_common.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"Helpers for copy rules"
22

3+
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
4+
35
# Hints for Bazel spawn strategy
46
COPY_EXECUTION_REQUIREMENTS = {
57
# ----------------+-----------------------------------------------------------------------------
@@ -57,3 +59,6 @@ COPY_EXECUTION_REQUIREMENTS = {
5759
"no-sandbox": "1",
5860
"local": "1",
5961
}
62+
63+
def is_windows_host():
64+
return "@platforms//os:windows" in HOST_CONSTRAINTS

lib/private/copy_directory.bzl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule copies a directory to another location using Bash (on Linux/macOS) or
44
cmd.exe (on Windows).
55
"""
66

7-
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
7+
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
88

99
def _copy_cmd(ctx, src, dst):
1010
# Most Windows binaries built with MSVC use a certain argument quoting
@@ -61,7 +61,7 @@ def _copy_bash(ctx, src, dst):
6161
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
6262
)
6363

64-
def copy_directory_action(ctx, src, dst, is_windows = False):
64+
def copy_directory_action(ctx, src, dst, is_windows = None):
6565
"""Helper function that creates an action to copy a directory from src to dst.
6666
6767
This helper is used by copy_directory. It is exposed as a public API so it can be used within
@@ -71,22 +71,27 @@ def copy_directory_action(ctx, src, dst, is_windows = False):
7171
ctx: The rule context.
7272
src: The directory to make a copy of. Can be a source directory or TreeArtifact.
7373
dst: The directory to copy to. Must be a TreeArtifact.
74-
is_windows: If true, an cmd.exe action is created so there is no bash dependency.
74+
is_windows: Deprecated and unused
7575
"""
76+
77+
# TODO(2.0): remove depcreated & unused is_windows parameter
7678
if not src.is_source and not dst.is_directory:
7779
fail("src must be a source directory or TreeArtifact")
7880
if dst.is_source or not dst.is_directory:
7981
fail("dst must be a TreeArtifact")
82+
83+
# Because copy actions have "local" execution requirements, we can safely assume
84+
# the execution is the same as the host platform and generate different actions for Windows
85+
# and non-Windows host platforms
86+
is_windows = _is_windows_host()
8087
if is_windows:
8188
_copy_cmd(ctx, src, dst)
8289
else:
8390
_copy_bash(ctx, src, dst)
8491

8592
def _copy_directory_impl(ctx):
86-
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
87-
8893
dst = ctx.actions.declare_directory(ctx.attr.out)
89-
copy_directory_action(ctx, ctx.file.src, dst, is_windows)
94+
copy_directory_action(ctx, ctx.file.src, dst)
9095

9196
files = depset(direct = [dst])
9297
runfiles = ctx.runfiles(files = [dst])
@@ -101,7 +106,6 @@ _copy_directory = rule(
101106
# Cannot declare out as an output here, because there's no API for declaring
102107
# TreeArtifact outputs.
103108
"out": attr.string(mandatory = True),
104-
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
105109
},
106110
)
107111

lib/private/copy_file.bzl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cmd.exe (on Windows). `_copy_xfile` marks the resulting file executable,
2424
`_copy_file` does not.
2525
"""
2626

27-
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
27+
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
2828
load(":directory_path.bzl", "DirectoryPathInfo")
2929

3030
def _copy_cmd(ctx, src, src_path, dst):
@@ -83,7 +83,7 @@ def _copy_bash(ctx, src, src_path, dst):
8383
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
8484
)
8585

86-
def copy_file_action(ctx, src, dst, dir_path = None, is_windows = False):
86+
def copy_file_action(ctx, src, dst, dir_path = None, is_windows = None):
8787
"""Helper function that creates an action to copy a file from src to dst.
8888
8989
If src is a TreeArtifact, dir_path must be specified as the path within
@@ -97,8 +97,10 @@ def copy_file_action(ctx, src, dst, dir_path = None, is_windows = False):
9797
src: The source file to copy or TreeArtifact to copy a single file out of.
9898
dst: The destination file.
9999
dir_path: If src is a TreeArtifact, the path within the TreeArtifact to the file to copy.
100-
is_windows: If true, an cmd.exe action is created so there is no bash dependency.
100+
is_windows: Deprecated and unused
101101
"""
102+
103+
# TODO(2.0): remove depcreated & unused is_windows parameter
102104
if dst.is_directory:
103105
fail("dst must not be a TreeArtifact")
104106
if src.is_directory:
@@ -107,14 +109,17 @@ def copy_file_action(ctx, src, dst, dir_path = None, is_windows = False):
107109
src_path = "/".join([src.path, dir_path])
108110
else:
109111
src_path = src.path
112+
113+
# Because copy actions have "local" execution requirements, we can safely assume
114+
# the execution is the same as the host platform and generate different actions for Windows
115+
# and non-Windows host platforms
116+
is_windows = _is_windows_host()
110117
if is_windows:
111118
_copy_cmd(ctx, src, src_path, dst)
112119
else:
113120
_copy_bash(ctx, src, src_path, dst)
114121

115122
def _copy_file_impl(ctx):
116-
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
117-
118123
if ctx.attr.allow_symlink:
119124
if len(ctx.files.src) != 1:
120125
fail("src must be a single file when allow_symlink is True")
@@ -131,14 +136,13 @@ def _copy_file_impl(ctx):
131136
ctx.attr.src[DirectoryPathInfo].directory,
132137
ctx.outputs.out,
133138
dir_path = ctx.attr.src[DirectoryPathInfo].path,
134-
is_windows = is_windows,
135139
)
136140
else:
137141
if len(ctx.files.src) != 1:
138142
fail("src must be a single file or a target that provides a DirectoryPathInfo")
139143
if ctx.files.src[0].is_directory:
140144
fail("cannot use copy_file on a directory; try copy_directory instead")
141-
copy_file_action(ctx, ctx.files.src[0], ctx.outputs.out, is_windows = is_windows)
145+
copy_file_action(ctx, ctx.files.src[0], ctx.outputs.out)
142146

143147
files = depset(direct = [ctx.outputs.out])
144148
runfiles = ctx.runfiles(files = [ctx.outputs.out])
@@ -152,7 +156,6 @@ _ATTRS = {
152156
"is_executable": attr.bool(mandatory = True),
153157
"allow_symlink": attr.bool(mandatory = True),
154158
"out": attr.output(mandatory = True),
155-
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
156159
}
157160

158161
_copy_file = rule(

lib/private/copy_to_bin.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
load("@bazel_skylib//lib:paths.bzl", "paths")
1818
load(":copy_file.bzl", "copy_file_action")
1919

20-
def copy_file_to_bin_action(ctx, file, is_windows = False):
20+
def copy_file_to_bin_action(ctx, file, is_windows = None):
2121
"""Helper function that creates an action to copy a file to the output tree.
2222
2323
File are copied to the same workspace-relative path. The resulting files is
@@ -29,11 +29,13 @@ def copy_file_to_bin_action(ctx, file, is_windows = False):
2929
Args:
3030
ctx: The rule context.
3131
file: The file to copy.
32-
is_windows: If true, an cmd.exe action is created so there is no bash dependency.
32+
is_windows: Deprecated and unused
3333
3434
Returns:
3535
A File in the output tree.
3636
"""
37+
38+
# TODO(2.0): remove depcreated & unused is_windows parameter
3739
if not file.is_source:
3840
return file
3941
if ctx.label.workspace_name != file.owner.workspace_name:
@@ -89,7 +91,7 @@ target to {file_package} using:
8991
package = "%s//%s" % (curr_package_label.workspace_name, curr_package_label.package),
9092
)
9193

92-
def copy_files_to_bin_actions(ctx, files, is_windows = False):
94+
def copy_files_to_bin_actions(ctx, files, is_windows = None):
9395
"""Helper function that creates actions to copy files to the output tree.
9496
9597
Files are copied to the same workspace-relative path. The resulting list of
@@ -101,17 +103,17 @@ def copy_files_to_bin_actions(ctx, files, is_windows = False):
101103
Args:
102104
ctx: The rule context.
103105
files: List of File objects.
104-
is_windows: If true, an cmd.exe action is created so there is no bash dependency.
106+
is_windows: Deprecated and unused
105107
106108
Returns:
107109
List of File objects in the output tree.
108110
"""
111+
112+
# TODO(2.0): remove depcreated & unused is_windows parameter
109113
return [copy_file_to_bin_action(ctx, file, is_windows = is_windows) for file in files]
110114

111115
def _impl(ctx):
112-
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
113-
114-
files = copy_files_to_bin_actions(ctx, ctx.files.srcs, is_windows = is_windows)
116+
files = copy_files_to_bin_actions(ctx, ctx.files.srcs)
115117
return DefaultInfo(
116118
files = depset(files),
117119
runfiles = ctx.runfiles(files = files),
@@ -122,7 +124,6 @@ _copy_to_bin = rule(
122124
provides = [DefaultInfo],
123125
attrs = {
124126
"srcs": attr.label_list(mandatory = True, allow_files = True),
125-
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
126127
},
127128
)
128129

lib/private/copy_to_directory.bzl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"copy_to_directory implementation"
22

33
load("@bazel_skylib//lib:paths.bzl", skylib_paths = "paths")
4-
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
4+
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
55
load(":paths.bzl", "paths")
66
load(":directory_path.bzl", "DirectoryPathInfo")
77
load(":glob_match.bzl", "glob_match")
@@ -279,7 +279,6 @@ _copy_to_directory_attr = {
279279
280280
This setting has no effect on Windows where overwrites are always allowed.""",
281281
),
282-
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
283282
}
284283

285284
def _any_globs_match(exprs, path):
@@ -580,11 +579,10 @@ if exist "{src}\\*" (
580579
mnemonic = "CopyToDirectory",
581580
progress_message = "Copying files to directory",
582581
use_default_shell_env = True,
582+
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
583583
)
584584

585585
def _copy_to_directory_impl(ctx):
586-
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
587-
588586
dst = ctx.actions.declare_directory(ctx.attr.out if ctx.attr.out else ctx.attr.name)
589587

590588
copy_to_directory_action(
@@ -600,7 +598,6 @@ def _copy_to_directory_impl(ctx):
600598
exclude_prefixes = ctx.attr.exclude_prefixes,
601599
replace_prefixes = ctx.attr.replace_prefixes,
602600
allow_overwrites = ctx.attr.allow_overwrites,
603-
is_windows = is_windows,
604601
)
605602

606603
return [
@@ -640,7 +637,7 @@ def copy_to_directory_action(
640637
exclude_prefixes = [],
641638
replace_prefixes = {},
642639
allow_overwrites = False,
643-
is_windows = False):
640+
is_windows = None):
644641
"""Helper function to copy files to a directory.
645642
646643
This helper is used by copy_to_directory. It is exposed as a public API so it can be used within
@@ -689,8 +686,10 @@ def copy_to_directory_action(
689686
690687
See copy_to_directory rule documentation for more details.
691688
692-
is_windows: If true, an cmd.exe action is created so there is no bash dependency.
689+
is_windows: Deprecated and unused
693690
"""
691+
692+
# TODO(2.0): remove depcreated & unused is_windows parameter
694693
if not srcs:
695694
fail("srcs must not be empty")
696695

@@ -762,6 +761,10 @@ def copy_to_directory_action(
762761
if not copy_paths:
763762
fail("There are no files or directories to copy after applying filters. Are your 'include_srcs_patterns' and 'exclude_srcs_patterns' attributes correct?")
764763

764+
# Because copy actions have "local" execution requirements, we can safely assume
765+
# the execution is the same as the host platform and generate different actions for Windows
766+
# and non-Windows host platforms
767+
is_windows = _is_windows_host()
765768
if is_windows:
766769
_copy_to_dir_cmd(ctx, copy_paths, dst)
767770
else:

0 commit comments

Comments
 (0)