Skip to content

fix: skip errors that occurred on virtual lines - #1226

Merged
vzaidman merged 4 commits into
react:mainfrom
EvanBacon:@evanbacon/metro/fix-errors-in-babel-code
Sep 29, 2026
Merged

vzaidman merged 4 commits into
react:mainfrom
EvanBacon:@evanbacon/metro/fix-errors-in-babel-code

Conversation

@EvanBacon

@EvanBacon EvanBacon commented Feb 21, 2024 •

Copy link
Copy Markdown
Contributor

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.

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.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Feb 21, 2024
@motiz88

motiz88 commented Feb 22, 2024

Copy link
Copy Markdown
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.

@EvanBacon

Copy link
Copy Markdown
Contributor Author

@motiz88 If you have a babel plugin which takes an empty file like "" and injects a bunch of imports:

import "missing"
import "missing2"

Then the resolver will throw an error which breaks the formatter because it attempts to read lines that don't exist on-disk.

EvanBacon and others added 3 commits March 5, 2024 15:20
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.
@robhogan

Copy link
Copy Markdown
Collaborator

@vzaidman I've rebased this and added tests to show how it's reproducible with legit transform. Good to land IMO.

(Cheers @EvanBacon ;))

@meta-codesync

meta-codesync Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D120317319.

@vzaidman

Copy link
Copy Markdown
Contributor

@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?

@robhogan

robhogan commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

@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:

TypeError: Cannot read properties of undefined (reading 'length')
    at length (packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js:377:64)
    at UnableToResolveError.refineDependencyLocation [as buildCodeFrameMessage] (.../ModuleResolution.js:344:9)
    at new buildCodeFrameMessage (.../ModuleResolution.js:316:35)
    at ModuleResolver.resolveDependency (.../ModuleResolution.js:170:15)
    at DependencyGraph.resolveDependency (.../DependencyGraph.js:290:43)

Where we'd expect to see:

UnableToResolveError: Unable to resolve module ./does-not-exist from .../build-errors/transform-injected-import.js:

Running the new Jest test with the fix reverted, the test fails as expected:

✕ reports resolution errors on locations past the end of the source
  Expected pattern: /^Unable to resolve module \.\/does-not-exist/
  Received string:  "Cannot read properties of undefined (reading 'length')"

vzaidman added a commit to vzaidman/metro that referenced this pull request Sep 29, 2026
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
vzaidman added a commit to vzaidman/metro that referenced this pull request Sep 29, 2026
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
vzaidman added a commit to vzaidman/metro that referenced this pull request Sep 29, 2026
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
@vzaidman
vzaidman self-requested a review September 29, 2026 07:25
@vzaidman
vzaidman merged commit 5d6eaea into react:main Sep 29, 2026
18 checks passed
robhogan pushed a commit that referenced this pull request Oct 15, 2026
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants