Skip to content

Commit

Permalink
fix: set explicit tsconfig to prevent use of esbuild default
Browse files Browse the repository at this point in the history
By default esbuild uses any local tsconfig.json file. With sandboxing issues (#58)
this may unexpectedly cause `esbuild_bundle` to pickup any local tsconfig.json.
  • Loading branch information
jbedard committed Jun 7, 2023
1 parent 196eb6c commit eedf641
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion e2e/smoke/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")

esbuild(
name = "lib",
srcs = ["main.js"],
srcs = ["main.js", "tsconfig.json"],
entry_point = "main.js",
)

Expand Down
1 change: 1 addition & 0 deletions e2e/smoke/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with invalid syntax, esbuild shouldn't use this
2 changes: 1 addition & 1 deletion esbuild/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports_files(
)

exports_files(
["launcher.js"],
["launcher.js", "empty.json"],
visibility = ["//visibility:public"],
)

Expand Down
1 change: 1 addition & 0 deletions esbuild/private/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 8 additions & 0 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ See https://esbuild.github.io/api/#target for more details
TODO: show how to write a config file that depends on plugins, similar to the esbuild_config macro in rules_nodejs.
""",
),
"_tsconfig": attr.label(
mandatory = False,
allow_single_file = True,
default = Label("@aspect_rules_esbuild//esbuild/private:empty.json"),
)
}

def _bin_relative_path(ctx, file):
Expand Down Expand Up @@ -199,6 +204,7 @@ def _esbuild_impl(ctx):
# Also disable the log limit and show all logs
"logLevel": "warning",
"logLimit": 0,
"tsconfig": _bin_relative_path(ctx, ctx.file._tsconfig),
"metafile": ctx.attr.metafile,
"platform": ctx.attr.platform,
# Don't preserve symlinks since doing so breaks node_modules resolution
Expand Down Expand Up @@ -306,6 +312,8 @@ def _esbuild_impl(ctx):
# inputs.append(ctx.version_file)
# env["BAZEL_VERSION_FILE"] = ctx.version_file.path

other_inputs.append(ctx.file._tsconfig)

input_sources = depset(
copy_files_to_bin_actions(ctx, [
file
Expand Down
6 changes: 5 additions & 1 deletion esbuild/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def _esbuild_repo_impl(repository_ctx):
Label("@aspect_rules_esbuild//esbuild/private:launcher.js"),
"launcher.js",
)
repository_ctx.symlink(
Label("@aspect_rules_esbuild//esbuild/private:empty.json"),
"empty.json",
)
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,7 +56,7 @@ npm_link_package(
js_binary(
name = "launcher",
entry_point = "launcher.js",
data = [":node_modules/esbuild"],
data = [":node_modules/esbuild", ":empty.json"],
)
esbuild_toolchain(
Expand Down

0 comments on commit eedf641

Please sign in to comment.