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

Fix duplicate default exports #53

Merged
merged 6 commits into from
Jan 8, 2024
Merged

Conversation

jsumners-nr
Copy link
Contributor

@jsumners-nr jsumners-nr commented Dec 21, 2023

Ref: newrelic/node-newrelic#1920

👋 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.


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.

@jsumners-nr
Copy link
Contributor Author

Moving back to draft as the issue isn't quite solved. It is possible the wrong default gets exported. In the case of got, it is possible the "request" default gets exported instead of the main got API.

@jsumners-nr jsumners-nr marked this pull request as draft December 27, 2023 12:38
@jsumners-nr jsumners-nr marked this pull request as ready for review December 28, 2023 19:53
@jsumners-nr jsumners-nr marked this pull request as draft January 3, 2024 14:40
@jsumners-nr jsumners-nr marked this pull request as ready for review January 3, 2024 17:53
@jsumners-nr jsumners-nr marked this pull request as draft January 8, 2024 15:50
@jsumners-nr jsumners-nr force-pushed the default-crash branch 5 times, most recently from b5df7bb to 449dd2b Compare January 8, 2024 19:02
@jsumners-nr jsumners-nr marked this pull request as ready for review January 8, 2024 19:14
if (matches === null) return
return `_['${matches[1]}'] = ${matches[2]}.default`
})
.filter(s => s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably want to eventually rework this for performance, but for now, can you can you add a double-exclamation to make it clear that it's filtering on truthiness, and therefore eliminating undefined holes?

Suggested change
.filter(s => s)
.filter(s => !!s)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, the !! construct is very difficult to read. Would a comment suffice?

@bengl bengl merged commit 3a3cd49 into nodejs:main Jan 8, 2024
@jsumners-nr jsumners-nr deleted the default-crash branch January 8, 2024 20:48
david-luna pushed a commit to elastic/apm-agent-nodejs that referenced this pull request Jan 22, 2024
PeterEinberger pushed a commit to fpm-git/apm-agent-nodejs that referenced this pull request Aug 20, 2024
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

Successfully merging this pull request may close these issues.

3 participants