Guard missing require.extensions in match path resolution#289
Open
wojtekmaj wants to merge 1 commit intojonaskello:masterfrom
Open
Guard missing require.extensions in match path resolution#289wojtekmaj wants to merge 1 commit intojonaskello:masterfrom
wojtekmaj wants to merge 1 commit intojonaskello:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates tsconfig-paths match-path resolution to tolerate runtimes where require.extensions is unavailable (e.g., some Node + ESM-loader/PnP scenarios), by falling back to standard Node extensions and adding regression tests.
Changes:
- Add a safe fallback extension list (
[".js", ".json", ".node"]) whenrequire.extensionsis missing/empty in sync and async match-path resolution. - Add regression tests ensuring sync and async match-path creation/invocation does not throw/fail when
require.extensionsis unavailable.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/match-path-sync.ts |
Guard default extensions resolution with a fallback list when require.extensions is missing/empty. |
src/match-path-async.ts |
Same fallback logic for async match-path resolution. |
src/__tests__/match-path-sync.test.ts |
Add regression coverage for missing require.extensions in sync path resolution. |
src/__tests__/match-path-async.test.ts |
Add regression coverage for missing require.extensions in async path resolution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+35
to
+46
| const matchPath = createMatchPathAsync("/base", {}); | ||
| matchPath( | ||
| "missing-module", | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| (err, result) => { | ||
| expect(err).toBeUndefined(); | ||
| expect(result).toBeUndefined(); | ||
| done(); | ||
| } | ||
| ); |
Comment on lines
+73
to
+75
| extensions: Array<string> = Object.keys(require.extensions || {}).length | ||
| ? Object.keys(require.extensions || {}) | ||
| : defaultExtensions, |
Comment on lines
+6
to
+7
| const defaultExtensions = [".js", ".json", ".node"]; | ||
|
|
Comment on lines
+67
to
+70
| extensions: ReadonlyArray<string> = Object.keys(require.extensions || {}) | ||
| .length | ||
| ? Object.keys(require.extensions || {}) | ||
| : defaultExtensions, |
|
|
||
| try { | ||
| const matchPath = createMatchPath("/base", {}); | ||
| expect(() => matchPath("missing-module")).not.toThrow(); |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This makes match path resolution tolerate environments where
require.extensionsis unavailable.In our case, this surfaced under Node.js 24.15.0 with Yarn 4.14.1 Plug'n'Play, where some CommonJS code paths executed through the ESM loader no longer expose
require.extensions.tsconfig-pathsthen throws while building the default extension list, even though falling back to the standard Node extensions is sufficient.This change:
[".js", ".json", ".node"]whenrequire.extensionsis missing or emptyI drafted this patch and PR with assistance from OpenAI Codex, then reviewed and validated the changes locally before opening the PR.