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

Make esbuild_register_toolchains more configurable. #120

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_file_to_bin_action", "copy_files_to_bin_actions")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "JsInfo", "js_info")
load(":helpers.bzl", "desugar_entry_point_names", "filter_files", "write_args_file")
load(":helpers.bzl", "desugar_entry_point_names", "write_args_file")

_ATTRS = {
"args_file": attr.label(
Expand Down Expand Up @@ -260,7 +260,7 @@ def _esbuild_impl(ctx):

env = {
"BAZEL_BINDIR": ctx.bin_dir.path,
"ESBUILD_BINARY_PATH": "../../../" + esbuild_toolinfo.target_tool_path,
"ESBUILD_BINARY_PATH": esbuild_toolinfo.target_tool_path, #"../../../" + esbuild_toolinfo.target_tool_path,
}

if ctx.attr.max_threads > 0:
Expand Down
4 changes: 4 additions & 0 deletions esbuild/private/launcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { readFileSync, writeFileSync } = require('fs')
const { pathToFileURL } = require('url')
const { join } = require('path')
const fs = require("fs");
const esbuild = require('esbuild')

function getFlag(flag, required = true) {
Expand Down Expand Up @@ -101,6 +102,9 @@ if (!process.env.ESBUILD_BINARY_PATH) {
console.error('Expected environment variable ESBUILD_BINARY_PATH to be set')
process.exit(1)
}
if (!fs.existsSync(process.env.ESBUILD_BINARY_PATH)) {
throw new Error(`[esbuild] Bad configuration: ESBUILD_BINARY_PATH=${process.env.ESBUILD_BINARY_PATH} does not exist; likely bug in rules_esbuild or bad toolchain configuration.\n\n working directory = ${process.cwd()}`);
}

async function runOneBuild(args, userArgsFilePath, configFilePath) {
if (userArgsFilePath) {
Expand Down
8 changes: 8 additions & 0 deletions esbuild/private/versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ TOOL_VERSIONS = {
"linux-arm64": "sha512-2wv0xYDskk2+MzIm/AEprDip39a23Chptc4mL7hsHg26P0gD8RUhzmDu0KCH2vMThUI1sChXXoK9uH0KYQKaDg==",
"win32-x64": "sha512-QaQ8IH0JLacfGf5cf0HCCPnQuCTd/dAI257vXBgb/cccKGbH/6pVtI1gwhdAQ0Y48QSpTIFrh9etVyNdZY+zzw==",
},
"0.17.5": {
"npm": "sha512-Bu6WLCc9NMsNoMJUjGl3yBzTjVLXdysMltxQWiLAypP+/vQrf+3L1Xe8fCXzxaECus2cEJ9M7pk4yKatEwQMqQ==",
"darwin-x64": "sha512-ha7QCJh1fuSwwCgoegfdaljowwWozwTDjBgjD3++WAy/qwee5uUi1gvOg2WENJC6EUyHBOkcd3YmLDYSZ2TPPA==",
"darwin-arm64": "sha512-EAvaoyIySV6Iif3NQCglUNpnMfHSUgC5ugt2efl3+QDntucJe5spn0udNZjTgNi6tKVqSceOw9tQ32liNZc1Xw==",
"linux-x64": "sha512-vnxuhh9e4pbtABNLbT2ANW4uwQ/zvcHRCm1JxaYkzSehugoFd5iXyC4ci1nhXU13mxEwCnrnTIiiSGwa/uAF1g==",
"linux-arm64": "sha512-8a0bqSwu3OlLCfu2FBbDNgQyBYdPJh1B9PvNX7jMaKGC9/KopgHs37t+pQqeMLzcyRqG6z55IGNQAMSlCpBuqg==",
"win32-x64": "sha512-XgA9qWRqby7xdYXuF6KALsn37QGBMHsdhmnpjfZtYxKxbTOwfnDM6MYi2WuUku5poNaX2n9XGVr20zgT/2QwCw==",
},
}
33 changes: 26 additions & 7 deletions esbuild/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ _ATTRS = {
"platform": attr.string(mandatory = True),
# The default doesn't appear here because the value depends on the version being used.
"url": attr.string(),
"target_tool": attr.label(
doc = "If specified, the esbuild executable to use instead of a downloaded version.",
default = None,
executable = True,
cfg = "target",
),
}

def _esbuild_repo_impl(repository_ctx):
Expand All @@ -39,6 +45,15 @@ def _esbuild_repo_impl(repository_ctx):
Label("@aspect_rules_esbuild//esbuild/private:launcher.js"),
"launcher.js",
)
target_tool = """select({{
"@bazel_tools//src/conditions:host_windows": "package/esbuild.exe",
"//conditions:default": "package/bin/esbuild",
}})"""
extra_data = ""
if repository_ctx.attr.target_tool != None:
target_tool = "\"{}\"".format(str(repository_ctx.attr.target_tool))
extra_data = ", \"{}\"".format(str(repository_ctx.attr.target_tool))

build_content = """#Generated by esbuild/repositories.bzl
load("@aspect_rules_esbuild//esbuild:toolchain.bzl", "esbuild_toolchain")
load("@aspect_rules_js//js:defs.bzl", "js_binary")
Expand All @@ -52,18 +67,19 @@ npm_link_package(
js_binary(
name = "launcher",
entry_point = "launcher.js",
data = [":node_modules/esbuild"],
data = [":node_modules/esbuild"{extra_data}],
)

esbuild_toolchain(
name = "esbuild_toolchain",
launcher = ":launcher",
target_tool = select({{
"@bazel_tools//src/conditions:host_windows": "package/esbuild.exe",
"//conditions:default": "package/bin/esbuild",
}}),
target_tool = {target_tool},
)
""".format(version = repository_ctx.attr.esbuild_version)
""".format(
version = repository_ctx.attr.esbuild_version,
target_tool = target_tool,
extra_data = extra_data,
)

# Base BUILD file for this repository
repository_ctx.file("BUILD.bazel", build_content)
Expand All @@ -75,7 +91,7 @@ esbuild_repositories = repository_rule(
)

# Wrapper macro around everything above, this is the primary API
def esbuild_register_toolchains(name, esbuild_version, register = True, **kwargs):
def esbuild_register_toolchains(name, esbuild_version, target_tool = None, register = True, **kwargs):
"""Convenience macro for users which does typical setup.

- create a repository for each built-in platform like "esbuild_linux-64" -
Expand All @@ -89,6 +105,8 @@ def esbuild_register_toolchains(name, esbuild_version, register = True, **kwargs
esbuild_version: a supported version like "0.14.36"
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
target_tool: If specified, the label of the esbuild binary to use instead of
a downloaded version.
**kwargs: passed to each node_repositories call
"""
if esbuild_version not in TOOL_VERSIONS.keys():
Expand All @@ -102,6 +120,7 @@ If you need custom versions, please file an issue.""".format(esbuild_version, TO
name = name + "_" + platform,
esbuild_version = esbuild_version,
platform = platform,
target_tool = target_tool,
**kwargs
)
if register:
Expand Down