-
Notifications
You must be signed in to change notification settings - Fork 399
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
ESM loader in v11 does not work with got package, after installing the latest import-in-the-middle #1920
Comments
What version of the agent are you using? What version of |
zintus/newrelic-node-examples@c209a0d
Node v21.3.0 |
|
Thank you. I have narrowed down the issue and submitted a PR upstream (nodejs/import-in-the-middle#53). |
You can't export default multiple times, so it's possible this is a node loader bug. |
Thank you @ljharb. It's a problem with IITM. I wrote a test for core and it passes: Core Testdiff --git a/test/es-module/test-esm-initialization.mjs b/test/es-module/test-esm-initialization.mjs
index f03a47d5d3..e7882e0839 100644
--- a/test/es-module/test-esm-initialization.mjs
+++ b/test/es-module/test-esm-initialization.mjs
@@ -28,3 +28,19 @@ describe('ESM: ensure initialization happens only once', { concurrency: true },
assert.strictEqual(code, 0);
});
});
+
+describe('ESM: nested default exports are handled correctly', () => {
+ it(async() => {
+ const { code, stderr, stdout } = await spawnPromisified(execPath, [
+ '--experimental-import-meta-resolve',
+ '--loader',
+ fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'),
+ '--no-warnings',
+ fixtures.path('es-modules', 'nested-default', 'index.mjs'),
+ ]);
+
+ assert.strictEqual(stderr, '');
+ assert.strictEqual(stdout.match(/resolve passthru/g)?.length, 3);
+ assert.strictEqual(code, 0);
+ });
+});
diff --git a/test/fixtures/es-modules/nested-default/a.mjs b/test/fixtures/es-modules/nested-default/a.mjs
new file mode 100644
index 0000000000..dc07e2033d
--- /dev/null
+++ b/test/fixtures/es-modules/nested-default/a.mjs
@@ -0,0 +1,3 @@
+const a = 'a'
+export default a
+export * from './b.mjs'
diff --git a/test/fixtures/es-modules/nested-default/b.mjs b/test/fixtures/es-modules/nested-default/b.mjs
new file mode 100644
index 0000000000..7f5dadf656
--- /dev/null
+++ b/test/fixtures/es-modules/nested-default/b.mjs
@@ -0,0 +1,2 @@
+const b = 'b'
+export default b
diff --git a/test/fixtures/es-modules/nested-default/index.mjs b/test/fixtures/es-modules/nested-default/index.mjs
new file mode 100644
index 0000000000..755d201fc8
--- /dev/null
+++ b/test/fixtures/es-modules/nested-default/index.mjs
@@ -0,0 +1 @@
+export * from './a.mjs' |
I can confirm your fix helps with my repro too. Thanks for the investigation @jsumners-nr |
I've tried your fix and the error is now different, it looks like the module loading is okay, but not all the exports are re-exported correctly, it looks like the exported object is got's
Printing the default exports yields
|
Thank you for the update. Figuring out the full solution given that information will be tricky indeed. |
I have determined the underlying problem. It is going to require some significant work in IITM. |
The issue is that in https://github.com/DataDog/import-in-the-middle/blob/ae292fb72d2a3444f3c4fa77439be81df49ff8f9/hook.js#L205-L212 we merge all transitive exports into a singular namespace. Whatever was the last "default" export in derived namespaces ends up being the default exported from the shim module. Which means we need to figure out how to provide all of the required exports from the shim module while also picking the primary default and renaming the others according to their module name (e.g. './foo-bar.js' default export should be renamed and exported as I'm at a loss for how to accomplish this. I welcome any ideas. |
Sorry but I think I don't fully understand the issue here. Using the keyword /* first.mjs */
export default 1
/* second.mjs */
export default 2
/* index.mjs */
export * from './first.mjs'
export * from './second.mjs' Because if so then in |
export * from doesn’t bring the default export, so there shouldn’t ever be more than one. |
After my last update I had a brainwave and seem to have a viable solution. I'm currently working through cleaning it up and expanding my test to try and cover all bases. |
I believe commit nodejs/import-in-the-middle@1abc010 solves the problem. |
Ref: newrelic/node-newrelic#1920 <strike>👋 my last "fix" (#43) broke in a new way thanks to the helpful ESM allowance of exporting `default` multiple times. This PR resolves the issue. I suspect there could be further problems with the exports since I have no idea how exporting `default` multiple times is meant to be interpreted, but I suspect any such issues will be an extreme edge case. The only other thing I can think of to do here is to somehow track that we already have a `default` export and munge the names of any subsequent ones.</strike> ----- I have since learned that ESM doesn't actually allow exporting `default` multiple times. Transitive `default` exports get mapped to some other name for the module that has imported them, and only the directly imported `default` gets exported. Still, we have to handle figuring out which `default` is the right one and construct our shim namespace accordingly. ----- 2023-01-03: this can only be supported on Node versions where we parse the modules with an AST parser and interpret the code manually. So I have updated the test suite for this feature to only run on such versions. --------- Co-authored-by: James Sumners <git@zifbang.com>
I think we should see a release soon, but in the meantime, if you'd like to try the latest main branch of |
Hey, thanks for the fix. I've just checked with the new version of In the meantime it still fails with another import in our project. When I try to:
I get:
Also seems problem is specificly with |
This is because of the re-export issue. Only We've been waiting for some time for this to be able to move to version 11 and it looks like they finally merged the 6 month old PR. |
IITM 1.7.3 has been published and resolves the original issue described here. Please update your dependencies to get the fix. If there are other problems, please open a new issue. |
Hey, do you have plans/approximate dates to update iitm version in node-newrelic itself? |
The update is covered by the semver range qualifier specified in the Line 203 in f414564
Updating your dependencies will net the fix. |
Thanks! |
Thanks for working on this! @jsumners-nr My repro i've linked before still fails with same steps. Please advise
|
There have been a few new releases of |
Description
Attempting to use v11 in an ESM project that includes got causes the node process to crash.
In Node v18 you will get a silent exit code 13. In Node v20, it will throw a SyntaxError before exit code 1.
Expected Behavior
Running
node --loader newrelic/esm-loader.mjs
should work without crashing node.NOTE: # ( Tell us what you expected to happen. )
Troubleshooting or NR Diag results
Steps to Reproduce
npm init -y
npm i --save got newrelic
node --loader newrelic/esm-loader.mjs index.mjs
Your Environment
Additional context
The text was updated successfully, but these errors were encountered: