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 8, 2023
1 parent 196eb6c commit 8cd261c
Show file tree
Hide file tree
Showing 18 changed files with 140 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: 2 additions & 0 deletions e2e/tsconfig/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build --enable_runfiles
common:bzlmod --enable_bzlmod
39 changes: 39 additions & 0 deletions e2e/tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")

SRCS = ["main.js", "libs/a.js", "libs/b.js"]
ENTRY = "main.js"

esbuild(
name = "target-a",
srcs = SRCS,
entry_point = ENTRY,
tsconfig = ":copy-a",
output = "a.js",
)

esbuild(
name = "target-b",
srcs = SRCS,
entry_point = ENTRY,
tsconfig = ":copy-b",
output = "b.js",
)

copy_to_bin(
name = "copy-a",
srcs = ["tsconfig-a.json"]
)
copy_to_bin(
name = "copy-b",
srcs = ["tsconfig-b.json"]
)

write_source_files(
name = "test",
files = {
"expected-a.js": "a.js",
"expected-b.js": "b.js",
}
)
7 changes: 7 additions & 0 deletions e2e/tsconfig/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"Bazel dependencies"
bazel_dep(name = "aspect_rules_esbuild", dev_dependency = True, version = "0.0.0")

local_path_override(
module_name = "aspect_rules_esbuild",
path = "../..",
)
36 changes: 36 additions & 0 deletions e2e/tsconfig/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Override http_archive for local testing
local_repository(
name = "aspect_rules_esbuild",
path = "../..",
)

#---SNIP--- Below here is re-used in the workspace snippet published on releases

######################
# rules_esbuild setup #
######################

# Fetches the rules_esbuild dependencies.
# If you want to have a different version of some dependency,
# you should fetch it *before* calling this.
# Alternatively, you can skip calling this function, so long as you've
# already fetched all the dependencies.
load("@aspect_rules_esbuild//esbuild:dependencies.bzl", "rules_esbuild_dependencies")

rules_esbuild_dependencies()

# If you didn't already register a toolchain providing nodejs, do that:
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "node",
node_version = DEFAULT_NODE_VERSION,
)

# Register a toolchain containing esbuild npm package and native bindings
load("@aspect_rules_esbuild//esbuild:repositories.bzl", "LATEST_ESBUILD_VERSION", "esbuild_register_toolchains")

esbuild_register_toolchains(
name = "esbuild",
esbuild_version = LATEST_ESBUILD_VERSION,
)
Empty file added e2e/tsconfig/WORKSPACE.bzlmod
Empty file.
8 changes: 8 additions & 0 deletions e2e/tsconfig/expected-a.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions e2e/tsconfig/expected-b.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/tsconfig/libs/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ANSWER = "this is library a!!!"
1 change: 1 addition & 0 deletions e2e/tsconfig/libs/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ANSWER = "the B library"
3 changes: 3 additions & 0 deletions e2e/tsconfig/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ANSWER } from "var-lib"

console.log( ANSWER)
7 changes: 7 additions & 0 deletions e2e/tsconfig/tsconfig-a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"var-lib": ["./libs/a.js"]
}
}
}
7 changes: 7 additions & 0 deletions e2e/tsconfig/tsconfig-b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"var-lib": ["./libs/b.js"]
}
}
}
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 @@
{}
12 changes: 12 additions & 0 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ 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"),
doc = """TypeScript configuration file used by esbuild. Default to an empty file with no configuration.
See https://esbuild.github.io/api/#tsconfig for more details
"""
)
}

def _bin_relative_path(ctx, file):
Expand Down Expand Up @@ -199,6 +208,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 @@ -281,6 +291,8 @@ def _esbuild_impl(ctx):
other_inputs.append(args_file)
launcher_args.add("--esbuild_args=%s" % _bin_relative_path(ctx, args_file))

other_inputs.append(ctx.file.tsconfig)

if ctx.attr.metafile:
# add metafile
meta_file = ctx.actions.declare_file("%s_metadata.json" % ctx.attr.name)
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 8cd261c

Please sign in to comment.