Skip to content

Commit

Permalink
respond to comments, attempt to incorporate into launcher.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vpanta committed Sep 20, 2023
1 parent 89fef6c commit c160e57
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion esbuild/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports_files(
)

exports_files(
["launcher.js"],
["launcher.js", "plugins/sandbox.js"],
visibility = ["//visibility:public"],
)

Expand Down
10 changes: 10 additions & 0 deletions esbuild/private/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { readFileSync, writeFileSync } = require('fs')
const { pathToFileURL } = require('url')
const { join } = require('path')
const esbuild = require('esbuild')
const { sandboxPlugin } = require('./plugins/sandbox.js')

function getFlag(flag, required = true) {
const argvFlag = process.argv.find((arg) => arg.startsWith(`${flag}=`))
Expand Down Expand Up @@ -117,6 +118,15 @@ async function runOneBuild(args, userArgsFilePath, configFilePath) {
}
}

const plugins = [
// onResolve plugin, must be first to occur.
sandboxPlugin()
]
if (args.plugins !== undefined) {
plugins.push(...args.plugins)
}
args.plugins = plugins

try {
const result = await esbuild.build(args)
if (result.metafile) {
Expand Down
43 changes: 22 additions & 21 deletions helpers/plugins/sandbox.js → esbuild/private/plugins/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const process = require('process')
// Regex matching any non-relative import path
const pkgImport = /^[^.]/

const bindir = process.env.BAZEL_BINDIR
const sandboxRoot = process.env.JS_BINARY__EXECROOT

// Under Bazel, esbuild likes to leave the sandbox, so make sure it
// stays inside the sandbox by using a separate resolver.
// https://github.com/aspect-build/rules_esbuild/issues/58
export function sandboxPlugin() {
const bindir = process.env['BAZEL_BINDIR']
const sandboxRoot = path.join(process.cwd().split(bindir)[0], bindir)
return {
name: 'sandbox',
setup(build) {
Expand All @@ -28,29 +29,29 @@ export function sandboxPlugin() {
// Prevent us from loading different forms of a module (CJS vs ESM).
if (pkgImport.test(importPath)) {
if (!moduleCache.has(importPath)) {
moduleCache.set(importPath, resolveInSandbox())
moduleCache.set(importPath, resolveInSandbox(build, importPath, otherOptions))
}
return await moduleCache.get(importPath)
}
return await resolveInSandbox()

async function resolveInSandbox() {
const result = await build.resolve(importPath, otherOptions)

// If esbuild attempts to leave the sandbox, map the path back into the sandbox.
if (!result.path.startsWith(sandboxRoot)) {
const pathPieces = result.path.split(bindir)
if (pathPieces.length != 2) {
// If it tried to leave bazel-bin, error out completely.
throw new Error(
`Error: esbuild resolved a path outside of BAZEL_BINDIR: ${result.path}`,
)
}
result.path = path.join(sandboxRoot, pathPieces[1])
}
return result
}
return await resolveInSandbox(build, importPath, otherOptions)
})
},
}
}

async function resolveInSandbox(build, importPath, otherOptions) {
const result = await build.resolve(importPath, otherOptions)

// If esbuild attempts to leave the sandbox, map the path back into the sandbox.
if (!result.path.startsWith(sandboxRoot)) {
const pathPieces = result.path.split(bindir)
if (pathPieces.length != 2) {
// If it tried to leave bazel-bin, error out completely.
throw new Error(
`Error: esbuild resolved a path outside of BAZEL_BINDIR: ${result.path}`,
)
}
result.path = path.join(sandboxRoot, pathPieces[1])
}
return result
}
9 changes: 8 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:plugins/sandbox.js"),
"plugins/sandbox.js",
)
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,10 @@ npm_link_package(
js_binary(
name = "launcher",
entry_point = "launcher.js",
data = [":node_modules/esbuild"],
data = [
":plugins/sandbox.js",
":node_modules/esbuild",
],
)
esbuild_toolchain(
Expand Down

0 comments on commit c160e57

Please sign in to comment.