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

Sourcemaps are broken for @swc-node/register/esm #719

Closed
jwalton opened this issue Jul 28, 2023 · 3 comments
Closed

Sourcemaps are broken for @swc-node/register/esm #719

jwalton opened this issue Jul 28, 2023 · 3 comments

Comments

@jwalton
Copy link

jwalton commented Jul 28, 2023

index.ts:

function main() {

    // Some whitespace for swc to optimize away...


    console.log('hello world');
    throw new Error('foo');
}

main()

@swc-node/register gives us sourcemap support:

$ node -r @swc-node/register ./index.ts
hello world
/Users/jwalton/t/index.ts:4
    throw new Error('foo');
    ^

Error: foo
    at main (/Users/jwalton/t/index.ts:7:11)

But @swc-node/register/esm does not:

node --loader @swc-node/register/esm ./index.ts
(node:81567) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
hello world
file:///Users/jwalton/t/index.ts.mjs:3
    throw new Error('foo');
          ^

Error: foo
    at main (file:///Users/jwalton/t/index.ts.mjs:3:11)
@arimgibson
Copy link

arimgibson commented Aug 2, 2023

This also impacts debugging through VSCode's built in debugger, which relies on sourcemaps.

@Xinkai
Copy link
Contributor

Xinkai commented Oct 31, 2023

This finally bothered me enough so I took a look.

Finding:
Unlike

installSourceMapSupport()
, esm.mts does not call the installSourceMapSupport to enable source map support.

I then tried adding the following in esm.mts, as documented at https://nodejs.org/docs/latest-v20.x/api/module.html#initialize. Sadly, it does not work.

export async function initialize() {
    console.log("This function is indeed called without error, but source map does not apply");
    installSourceMapSupport();
}

Solution:
I finally found this to be working (tested on Node 20). https://nodejs.org/docs/latest-v20.x/api/module.html#source-map-v3-support.

$ node --enable-source-maps --loader @swc-node/register/esm index.ts

@slavafomin
Copy link

Yep, stumbled upon this too.

@Xinkai thank you for a great workaround.

Happy new year :)

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

No branches or pull requests

5 participants