fix: skip errors that occurred on virtual lines - #1226
Open
EvanBacon wants to merge 4 commits into
Open
Conversation
Contributor
|
Where is the bad source location coming from? This kinda looks like it masks a genuine bug elsewhere. Do you have repro steps? Also, we have tests for this code now, might be worth adding a regression test to cover the fix. |
Contributor
Author
|
@motiz88 If you have a babel plugin which takes an empty file like Then the resolver will throw an error which breaks the formatter because it attempts to read lines that don't exist on-disk. |
Brings the branch up to react/metro main at 0ff28e6. No conflicts; the branch's only difference from main remains the five-line guard in refineDependencyLocation.
This fix has had no test since the PR was opened, which is what the review asked for in 2024. Adds one at the integration level, in `build-errors-test.js`, so it exercises the real transform and resolution path rather than constructing the error directly. `injectImportPastEndOfFileTransformer.js` wraps `@react-native/metro-babel-transformer` and, for one fixture, appends `import './does-not-exist';` parsed with `startLine` one line past the end of the source — which is what a plugin that generates and appends code does. The dependency Metro collects then reports a location with no counterpart in the file on disk, so building the code frame for the resolution error walks off the end of `lines`. Without the guard, the build rejects with `TypeError: Cannot read properties of undefined (reading 'length')` in place of the resolution error. With it, the error survives and the code frame degrades to the lines that do exist. The assertion is deliberately not a snapshot: the resolver's list of candidate paths varies between versions, and what matters here is only that the resolution error survives.
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.
Summary
If you have a module that has
importstatements generated in the transformer, then it could possibly throw a resolver error on a line that won't exist in the on-disk file. Without this change, the process will throw an unhandled error. Here, we just skip the error improvement step and fallback on the default behavior.