Repository navigation
fix: skip errors that occurred on virtual lines - #1226
Conversation
|
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. |
|
@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.
|
@vzaidman I've rebased this and added tests to show how it's reproducible with legit transform. Good to land IMO. (Cheers @EvanBacon ;)) |
|
@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D120317319. |
|
@robhogan can you please add a comment in tests or in the code or a screenshot on this issue, demonstrating how the thrown error would look like? |
@vzaidman sure - the basic recipe to repro is a resolution error in a generated import, which is handled fine (an actionable resolution error, pointing to the user's source or falling back gracefully) except where the generated import happens to be beyond the length of the untransformed source, which crashes - that's arbitrary, as there's no reason the transformed source should be shorter than the original - in fact it's usually longer. We've got away with this largely because imports are usually near the top of the file, so it's rare that a resolution failure blames to a high line number. That's obviously pretty fragile! The error thrown in the terminal is: Where we'd expect to see: Running the new Jest test with the fix reverted, the test fails as expected: |
Summary: When Metro cannot find a module being imported, it shows an error with a small preview of your code around that import. Some build tools automatically add imports while transforming your code. Those added imports can point to a line number that does not exist in your saved file. Before this change, Metro crashed while building that preview, which hid the real "Unable to resolve module" error. Now Metro skips lines that do not exist in the file and still shows the real error with a preview of the lines that do exist. Changes: - ModuleResolution.js: skip missing lines when searching for the import location, falling back to the default location. - Adds an integration test with a Babel plugin that appends an unresolvable import past the end of the file. Original contribution from react#1226 by evanbacon. Changelog: [Fix] Fixed a crash when building the error preview for an unresolvable import pointing past the end of the file (for example, imports added automatically during the build). Metro now skips the missing lines and reports the real "Unable to resolve module" error. ([react#1226](react#1226) by [evanbacon](https://github.com/evanbacon)) Reviewed By: cipolleschi Differential Revision: D120317319
Summary: When Metro cannot find a module being imported, it shows an error with a small preview of your code around that import. Some build tools automatically add imports while transforming your code. Those added imports can point to a line number that does not exist in your saved file. Before this change, Metro crashed while building that preview, which hid the real "Unable to resolve module" error. Now Metro skips lines that do not exist in the file and still shows the real error with a preview of the lines that do exist. Changes: - ModuleResolution.js: skip missing lines when searching for the import location, falling back to the default location. - Adds an integration test with a Babel plugin that appends an unresolvable import past the end of the file. Original contribution from react#1226 by evanbacon. Changelog: [Fix] Fixed a crash when building the error preview for an unresolvable import pointing past the end of the file (for example, imports added automatically during the build). Metro now skips the missing lines and reports the real "Unable to resolve module" error. ([react#1226](react#1226) by [evanbacon](https://github.com/evanbacon)) Reviewed By: cipolleschi Differential Revision: D120317319
Summary: Pull Request resolved: react#1993 When Metro cannot find a module being imported, it shows an error with a small preview of your code around that import. Some build tools automatically add imports while transforming your code. Those added imports can point to a line number that does not exist in your saved file. Before this change, Metro crashed while building that preview, which hid the real "Unable to resolve module" error. Now Metro skips lines that do not exist in the file and still shows the real error with a preview of the lines that do exist. Changes: - ModuleResolution.js: skip missing lines when searching for the import location, falling back to the default location. - Adds an integration test with a Babel plugin that appends an unresolvable import past the end of the file. Original contribution from react#1226 by evanbacon. Changelog: [Fix] Fixed a crash when building the error preview for an unresolvable import pointing past the end of the file (for example, imports added automatically during the build). Metro now skips the missing lines and reports the real "Unable to resolve module" error. ([react#1226](react#1226) by [evanbacon](https://github.com/evanbacon)) Reviewed By: cipolleschi Differential Revision: D120317319
* skip errors that occurred on virtual lines * Update ModuleResolution.js * Add regression test for resolution errors past the end of the source 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. Changelog: [Internal] --------- Co-authored-by: Rob Hogan <2590098+robhogan@users.noreply.github.com> (cherry picked from commit 5d6eaea)
Summary
When Metro cannot find a module being imported, it shows an error with a small preview of your code around that import. Some build tools automatically add imports while transforming your code. Those added imports can point to a line number that does not exist in your saved file. Before this change, Metro crashed while building that preview, which hid the real "Unable to resolve module" error. Now Metro skips lines that do not exist in the file and still shows the real error with a preview of the lines that do exist.
Changes
Test Plan
Added integration test "reports resolution errors on locations past the end of the source" in build-errors-test.js. It uses a test transformer that appends import "./does-not-exist" at a line past the end of the file, then verifies the build still fails with "Unable to resolve module ./does-not-exist" and shows the existing code instead of crashing.
Changelog
[Fix] Fixed a crash when building the error preview for an unresolvable import pointing past the end of the file (for example, imports added automatically during the build). Metro now skips the missing lines and reports the real "Unable to resolve module" error.
Original contribution from #1226 by evanbacon.