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

js_binary + ESM + hermeticity #446

Closed
paullewis opened this issue Sep 12, 2022 · 3 comments
Closed

js_binary + ESM + hermeticity #446

paullewis opened this issue Sep 12, 2022 · 3 comments

Comments

@paullewis
Copy link

paullewis commented Sep 12, 2022

Assuming something like this in a BUILD file:

load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@aspect_rules_js//js:defs.bzl", "js_binary")
load("@aspect_rules_swc//swc:defs.bzl", "swc_transpiler")
load("@bazel_skylib//lib:partial.bzl", "partial")

ts_project(
    name = "src",
    srcs = [
        "src.ts",
    ],
    declaration = True,
    transpiler = partial.make(
        swc_transpiler,
        source_maps = "true",
        swcrc = "//:swcrc_esm",
    ),
    tsconfig = "//:tsconfig",
    deps = [
        "//:node_modules/@types/node",
    ],
)

js_binary(
    name = "src_bin",
    entry_point = ":src.js",
)

And assuming that the transpilation of src.ts makes an ESM bundle in bazel-dist, running the js_binary fails because the generated src.js file is neither src.mjs, nor does Node know to run as "type": "module".

In other words, bazel run //src:src_bin will fail:

(node:92736) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

However, if in the source tree I update the package.json file to have "type": "module", the js_binary will now run.

I'm not sure if this is a bug or a question, but I'm surprised that a change to the package.json file in the source tree affects the js_binary. I assumed (perhaps incorrectly) that there would be a sandbox for the node process underpinning js_binary, and that it wouldn't have access to a package.json in the source tree.

Am I missing something?

@gregmagolan
Copy link
Member

Thanks for the report @paullewis . EMS requires escape the sandbox right now. Its a known issue, #362, and seems like a tricky one to fix.

@paullewis
Copy link
Author

Ah, I see. Happy to close this if it's a dupe... wdyt?

@gregmagolan
Copy link
Member

Sounds good. It will be linked in the history of #362 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants