Skip to content

Commit

Permalink
Fix running tests in Windows (#79)
Browse files Browse the repository at this point in the history
This should resolve the toolchain registration issue from #78.

Also, it adds a batch script version of the shell script shim used to
invoke protoc for tests; however, this requires `--enable_runfiles` to
be explicitly passed or added to `.bazelrc` for a project, as Bazel on
Windows does not populate runfiles by default.

In the future, we will need a better approach to supporting Windows that
uses the runfiles manifest instead. This is tricky though, and will
require some careful thought.
  • Loading branch information
jchadwick-buf authored Jun 20, 2024
1 parent d18f127 commit 850c799
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions buf/internal/breaking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ buf_breaking_test = rule(
executable = True,
cfg = "exec",
),
"_windows_constraint": attr.label(
default = "@platforms//os:windows",
),
"targets": attr.label_list(
providers = [ProtoInfo],
doc = """`proto_library` targets to check for breaking changes""",
Expand Down
3 changes: 3 additions & 0 deletions buf/internal/lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ buf_lint_test = rule(
executable = True,
cfg = "exec",
),
"_windows_constraint": attr.label(
default = "@platforms//os:windows",
),
"targets": attr.label_list(
providers = [ProtoInfo],
mandatory = True,
Expand Down
34 changes: 28 additions & 6 deletions buf/internal/plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

"""protoc plugin based test rule"""

_BATCH_PROTOC_SHIM = """@echo off
set errorlevel=
call {} @{}
exit /b %errorlevel%
"""

def protoc_plugin_test(ctx, proto_infos, protoc, plugin, config, files_to_include = [], protoc_args = []):
"""protoc_plugin_test creates a script file for a generic protoc plugin
Expand All @@ -28,6 +34,12 @@ def protoc_plugin_test(ctx, proto_infos, protoc, plugin, config, files_to_includ
Returns:
Runfiles required to run the test
"""
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])

path_join = ':'
if is_windows:
path_join = ';'

deps = depset(
[pi.direct_descriptor_set for pi in proto_infos],
transitive = [pi.transitive_descriptor_sets for pi in proto_infos],
Expand All @@ -50,7 +62,7 @@ def protoc_plugin_test(ctx, proto_infos, protoc, plugin, config, files_to_includ
args = args.set_param_file_format("multiline")
args.add_joined(["--plugin", "protoc-gen-buf-plugin", plugin.short_path], join_with = "=")
args.add_joined(["--buf-plugin_opt", config], join_with = "=")
args.add_joined("--descriptor_set_in", deps, join_with = ":", map_each = _short_path)
args.add_joined("--descriptor_set_in", deps, join_with = path_join, map_each = _short_path)
args.add_joined(["--buf-plugin_out", "."], join_with = "=")
args.add_all(protoc_args)
args.add_all(sources)
Expand All @@ -62,11 +74,20 @@ def protoc_plugin_test(ctx, proto_infos, protoc, plugin, config, files_to_includ
is_executable = True,
)

ctx.actions.write(
output = ctx.outputs.executable,
content = "{} @{}".format(protoc.short_path, args_file.short_path),
is_executable = True,
)
if is_windows:
# TODO: This still requires `--enable_runfiles` on Windows.
executable_file = ctx.actions.declare_file("{}.bat".format(ctx.label.name))
ctx.actions.write(
output = executable_file,
content = _BATCH_PROTOC_SHIM.format(protoc.short_path, args_file.short_path),
)
else:
executable_file = ctx.actions.declare_file("{}.sh".format(ctx.label.name))
ctx.actions.write(
output = executable_file,
content = "{} @{}".format(protoc.short_path, args_file.short_path),
is_executable = True,
)

files = [protoc, plugin, args_file] + source_files + files_to_include
runfiles = ctx.runfiles(
Expand All @@ -76,6 +97,7 @@ def protoc_plugin_test(ctx, proto_infos, protoc, plugin, config, files_to_includ

return [
DefaultInfo(
executable = executable_file,
runfiles = runfiles,
),
]
Expand Down
6 changes: 3 additions & 3 deletions buf/internal/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ _buf_toolchain = rule(
def declare_buf_toolchains(os, cpu, rules_buf_repo_name):
for cmd in ["buf", "protoc-gen-buf-lint", "protoc-gen-buf-breaking"]:
ext = ""
cmd_suffix = ""
if os == "windows":
ext = ".exe"
cmd_suffix = ".exe"
toolchain_impl = cmd + "_toolchain_impl"
_buf_toolchain(
name = toolchain_impl,
cli = str(Label("//:"+ cmd)),
cli = str(Label("//:"+ cmd + cmd_suffix)),
)
native.toolchain(
name = cmd + "_toolchain",
Expand Down

0 comments on commit 850c799

Please sign in to comment.