-
Notifications
You must be signed in to change notification settings - Fork 28
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
fix: handling of default and star exports #85
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
I'm not sure, doesn't #86 ignore the |
It can't rename This PR may well fix it in another way but I'd try copying the tests across the confirm that. |
Yes, clearly they do different things so either the test isn't covering it or something else. Not sure tbh. I added the tests here and they pass on my machine. |
It turns out there are other problems with the handling of default exports. I reverted the default export changes from #53 as they stemmed from a misunderstanding that |
I've since opened #87, but it looks from your work that it can be trimmed down even more! |
|
It's a runtime error in Node.js if you do that: import { foo } from './entry.mjs'
^^^
SyntaxError: The requested module './entry.mjs' contains conflicting star exports for name 'foo' where And if it's in the same file, it's an error as well: import { foo } from './file1.mjs'
import { foo } from './file2.mjs'
console.log(foo); import { foo } from './file2.mjs'
^^^
SyntaxError: Identifier 'foo' has already been declared |
Star exports were also broken. All the tests currently pass on my machine. |
Yes its a runtime error in Node, but it should also be a runtime error when using the loader hook. If you export a See my addition of a duplicate named exports test here: That's why I used this to remove duplicates: |
Adding your test to my PR results in a runtime error as expected:
|
That's not the same runtime error, that's because the emitted iitm code contains duplicate identifiers. You need to exclude This should not error, but you get the same runtime error as above because of the duplicate identifiers: |
Okay, it seems in Node.js there is no runtime error if you do an |
@timfish Added the duplicate handling here as well. I think this PR cleans up more unnecessary code and likely fixes other issues as well (namespaces caused a bug too). What do you think? |
Assuming all the tests pass on all node version this simplifies a lot more than my PR #87 so I'd favour this. As @jsumners-nr says here though, this PR effectively reverts a PR that was supposed to fix something. I think before we can even consider landing a PR like this with huge changes, we need to improve the tests with some real-world troublesome libraries. |
The original PR had tests with it and they still pass, I only removed the tests that were checking for the wrong thing. I agree on needing some more tests. Sentry is a big consumer of this library so they might be able to test this patch too, and more thoroughly. |
Looks good! Rather than me opening another PR to add more tests, do you think it's worth adding this patch to your PR? The @jsumners-nr, do you think this would suffice to confirm import got, { Options } from 'got'
import { strictEqual } from 'assert'
import Hook from '../../index.js'
Hook((exports, name) => {
if (name === 'got' && 'Options' in exports) {
exports.Options = 'nothing'
}
})
strictEqual(typeof got, 'function')
strictEqual(typeof got.post, 'function')
strictEqual(typeof got.stream, 'function')
strictEqual(typeof got.extend, 'function')
strictEqual(Options, 'nothing') |
newrelic/node-newrelic#1920 is the issue where most of the discoveries come from. So it looks like the following should all work: import got from 'got'
typeof got.extend === 'function' import { defineScript } from 'redis'
typeof defineScript === 'function' import OpenAI from 'openai'
typeof OpenAI === 'function' |
@mohd-akram If you update you PR description to include the following, it'll auto-close them when this gets merged: |
@timfish Thanks, updated. There seems to be some problem with the |
What version of Node does the You can limit the test version by prepending a version the test file name: |
Thanks, indeed it doesn't work before 18.19. |
Fixes #77 Co-authored-by: Tim Fish <tim@timfish.uk>
Had to move the other tests too. Looks good now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay seems reasonable enough to me. I tested with some fixtures in https://github.com/nodejs/node/tree/main/test/fixtures/es-modules and tried applying the patch on some sentry internal projects and nothing crashed, let's give it a try!
$ git log --oneline --no-decorate v1.7.4..HEAD 9269d1f 1.8.0 74124e9 fix: handling of default and star exports (#85) 7fa4e9c Fix resolution of lib/register.js when using multiple loaders (#76) e7f05ca add node 22 to test matrix (#84) fb0e393 feat: Add `Hook` named export (#88) bef32f2 chore: don't create a package-lock file (#67) bf3a4fb fix: Resolve re-exports of external modules (#78) 0d9f351 fix: Handle cyclical reference to current file (#83) 1e05e4b test: skip static-import on > v21 (#81)
Closes #68, Closes #77, Closes #62, Closes #60