-
Notifications
You must be signed in to change notification settings - Fork 27
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 resolution of lib/register.js when using multiple loaders #76
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { register } from 'node:module' | ||
|
||
register('./typescript-loader.mjs', import.meta.url) | ||
register('../../hook.mjs', import.meta.url) | ||
|
||
await import('node:util') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* This simulates what something like `tsx` (https://github.com/privatenumber/tsx) | ||
* will do: it will try to resolve a URL with a `.js` extension to a `.ts` extension. | ||
* | ||
* Combined with the test case in the adjacent `multiple-loaders.test.mjs` file, | ||
* this forces `import-in-the-middle` into what used to be a failure state: where | ||
* `context.parentURL` is a `node:*` specifier and the `specifier` refers to a file | ||
* that does not exist. | ||
* | ||
* See https://github.com/nodejs/node/issues/52987 for more details. | ||
*/ | ||
export async function resolve (specifier, context, defaultResolve) { | ||
if (!specifier.endsWith('.js') && !specifier.endsWith('.mjs')) { | ||
return await defaultResolve(specifier, context) | ||
} | ||
|
||
try { | ||
return await defaultResolve(specifier.replace(/\.m?js/, '.ts'), context) | ||
} catch (err) { | ||
if (err.code !== 'ERR_MODULE_NOT_FOUND') { | ||
throw err | ||
} | ||
|
||
return await defaultResolve(specifier, context) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This will only work if
specifier
is an absolute URL. It hasn’t been resolved yet, so this is the string passed toimport
.What is the approach being pursued here? When IITM itself is being registered, short-circuit the chains? But this is ITTM, so this condition would only ever be true if ITTM is registered twice?
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.
Double-check what
iitmURL
actually refers to. It is nothook.js
; it islib/register.js
, which is imported from generated code here:https://github.com/DataDog/import-in-the-middle/blob/00b01fff1f5b69dd25e307593ec54d1d8abb4844/hook.js#L302
Note that in this case, the specifier templated into the generated code and passed to
import
is an absolutefile://
URL.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.
For slightly more context, the actual problem stems from the fact that
import-in-the-middle
ends up generating source fornode:*
imports viaload()
, and that this code imports absolute file URLs (lib/register.js
). That's how we end up in a situation whereparentURL
isnode:*
andspecifier
is an absolutefile://
URL.