Skip to content
Closed
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 javascript/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports_files([
"frameworks.bzl",
"vite_tool.mjs",
"web.bzl",
"windows_tool.bzl",
])

javascript_library(
Expand Down Expand Up @@ -56,6 +57,7 @@ real_source_target(
"dev_hub.bzl",
"frameworks.bzl",
"web.bzl",
"windows_tool.bzl",
],
tags = ["corpus"],
)
Expand Down
69 changes: 69 additions & 0 deletions javascript/private/windows_tool.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Windows launcher shim for js_binary build tools. Removal tracked in #13889 (bazel-lib#1296)."""

visibility(["//javascript/...", "//typescript/..."])

_SHIM = "\r\n".join([
"@echo off",
"if defined RUNFILES_MANIFEST_FILE goto run",
"set \"RUNFILES_MANIFEST_FILE=%~f0.runfiles_manifest\"",
"if exist \"%~f0.runfiles\\MANIFEST\" set \"RUNFILES_MANIFEST_FILE=%~f0.runfiles\\MANIFEST\"",
":run",
"call \"%~dp0{tool}\" %*",
"exit /b %ERRORLEVEL%",
"",
])

def _bin_relative(ctx, file):
prefix = ctx.bin_dir.path + "/"
if not file.path.startswith(prefix):
fail("{} is not in the output tree".format(file.path))
return file.path[len(prefix):]

def _windows_js_tool_shim_impl(ctx):
executable = ctx.executable.tool
shim = ctx.actions.declare_file(ctx.label.name + ".bat")
rel = "../" * _bin_relative(ctx, shim).count("/") + _bin_relative(ctx, executable)
ctx.actions.write(
output = shim,
content = _SHIM.format(tool = rel.replace("/", "\\")),
is_executable = True,
)
runfiles = ctx.attr.tool[DefaultInfo].default_runfiles.merge(ctx.runfiles(files = [executable]))
return [DefaultInfo(executable = shim, runfiles = runfiles)]

windows_js_tool_shim = rule(
implementation = _windows_js_tool_shim_impl,
doc = "Runs a js_binary tool on Windows with RUNFILES_MANIFEST_FILE set to the shim's runfiles.",
attrs = {
"tool": attr.label(
doc = "The js_binary to run.",
executable = True,
cfg = "target",
mandatory = True,
),
},
executable = True,
)

def js_build_tool(name, tool, visibility = None):
"""Defines `name` as `tool`, wrapped in `windows_js_tool_shim` on Windows.

Args:
name: Name of the alias.
tool: The js_binary to run.
visibility: Visibility of the alias.
"""
windows_js_tool_shim(
name = name + "_windows",
tool = tool,
tags = ["manual"],
visibility = visibility,
)
native.alias(
name = name,
actual = select({
"@platforms//os:windows": ":" + name + "_windows",
"//conditions:default": tool,
}),
visibility = visibility,
)
13 changes: 13 additions & 0 deletions typescript/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gazelle:ignore

load("//core/quality:fixtures.bzl", "real_source_target")
load("//javascript/private:windows_tool.bzl", "js_build_tool")

package(default_visibility = [
"//:__pkg__",
Expand All @@ -13,6 +14,18 @@ exports_files([
"kwargs.bzl",
])

js_build_tool(
name = "tsc",
tool = "@npm_typescript//:tsc",
visibility = ["//visibility:public"],
)

js_build_tool(
name = "validator",
tool = "@npm_typescript//:validator",
visibility = ["//visibility:public"],
)

real_source_target(
name = "corpus_starlark",
starlark_srcs = [
Expand Down
4 changes: 2 additions & 2 deletions typescript/private/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ load(":proto.bzl", "ts_proto_srcs")
visibility(["//..."])

_TS_COMPILER = {
"tsc": Label("@npm_typescript//:tsc"),
"tsc": Label("//typescript/private:tsc"),
"tsc_worker": Label("@npm_typescript//:tsc_worker"),
"validator": Label("@npm_typescript//:validator"),
"validator": Label("//typescript/private:validator"),
}

def _dx_ts_project(**kwargs):
Expand Down
2 changes: 1 addition & 1 deletion typescript/private/quality/tsc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ tsc_aspect = aspect(
allow_single_file = True,
),
"_tsc": attr.label(
default = "@npm_typescript//:tsc",
default = "//typescript/private:tsc",
executable = True,
cfg = "exec",
),
Expand Down
4 changes: 4 additions & 0 deletions typescript/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load("//typescript/private:kwargs.bzl", "typescript_forward_kwargs", "typescript
load("//typescript/private/playwright:closure.bzl", "CLOSURE_DIR")
load("//typescript/private/playwright:pins.bzl", "CHROMIUM_REVISION", "CHROMIUM_VERSION", "PLAYWRIGHT_VERSION")
load(":playwright_tests.bzl", "playwright_compatibility_tests")
load(":windows_tool_tests.bzl", "windows_tool_tests")

package(
default_testonly = True,
Expand All @@ -17,6 +18,8 @@ package(

playwright_compatibility_tests(name = "playwright_compatibility_tests")

windows_tool_tests(name = "windows_tool_tests")

javascript_test(
name = "playwright_pins_test",
size = "small",
Expand Down Expand Up @@ -101,6 +104,7 @@ real_source_target(
starlark_srcs = [
"BUILD.bazel",
"playwright_tests.bzl",
"windows_tool_tests.bzl",
],
tags = ["corpus"],
)
Expand Down
59 changes: 59 additions & 0 deletions typescript/tests/windows_tool_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Analysis tests for the Windows tsc shim."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

def _bin_relative(ctx, file):
return file.path[len(ctx.bin_dir.path) + 1:]

def _shim_impl(ctx):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)
shim = target[DefaultInfo].files_to_run.executable
tool = ctx.executable.tool
writes = [a for a in analysistest.target_actions(env) if a.mnemonic == "FileWrite" and shim in a.outputs.to_list()]
asserts.equals(env, 1, len(writes))
content = writes[0].content
for line in [
"if defined RUNFILES_MANIFEST_FILE goto run\r\n",
"set \"RUNFILES_MANIFEST_FILE=%~f0.runfiles_manifest\"\r\n",
"if exist \"%~f0.runfiles\\MANIFEST\" set \"RUNFILES_MANIFEST_FILE=%~f0.runfiles\\MANIFEST\"\r\n",
]:
asserts.true(env, line in content, "missing line: " + line)
rel = "../" * _bin_relative(ctx, shim).count("/") + _bin_relative(ctx, tool)
call = "call \"%~dp0" + rel.replace("/", "\\") + "\" %*\r\n"
asserts.true(env, call in content, "expected %r in %r" % (call, content))
asserts.true(env, tool in target[DefaultInfo].default_runfiles.files.to_list(), "shim runfiles lack " + tool.path)
return analysistest.end(env)

shim_test = analysistest.make(
_shim_impl,
attrs = {"tool": attr.label(default = "@npm_typescript//:tsc", executable = True, cfg = "target")},
)

def _windows_select_impl(ctx):
env = analysistest.begin(ctx)
executable = analysistest.target_under_test(env)[DefaultInfo].files_to_run.executable
asserts.equals(env, "tsc_windows.bat", executable.basename)
return analysistest.end(env)

windows_select_test = analysistest.make(
_windows_select_impl,
config_settings = {"//command_line_option:platforms": str(Label("//tools/platforms:windows_x86_64"))},
)

def _default_select_impl(ctx):
env = analysistest.begin(ctx)
executable = analysistest.target_under_test(env)[DefaultInfo].files_to_run.executable
asserts.equals(env, ctx.executable.tool, executable)
return analysistest.end(env)

default_select_test = analysistest.make(
_default_select_impl,
attrs = {"tool": attr.label(default = "@npm_typescript//:tsc", executable = True, cfg = "target")},
)

def windows_tool_tests(name):
shim_test(name = name + "_shim", target_under_test = "//typescript/private:tsc_windows", size = "small")
windows_select_test(name = name + "_windows_select", target_under_test = "//typescript/private:tsc", size = "small")
default_select_test(name = name + "_default_select", target_under_test = "//typescript/private:tsc", size = "small")
native.test_suite(name = name, tests = [name + "_shim", name + "_windows_select", name + "_default_select"])
Loading