forked from facebook/metro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP/RFC: Include import stacks in resolution errors
Summary: Adds infrastructure for reporting *import stacks* as part of resolution errors in Metro. An import stack is the (inverse) chain of dependencies in the graph that led to a resolution error. This can help users understand and fix build errors that occur inside third-party packages (e.g. because of configuration errors). See expo/expo#23551 for more motivation. ## Caveats Ideally, we'd always be able to print the full chain of dependencies from an entry point to the module containing the error.¹ However, since we update the graph incrementally in response to filesystem changes, and produce import stacks while there are still pending changes to process, we can only safely print edges that we have *just* traversed². Otherwise, it's possible that a dependency's metadata (e.g. source location) will be stale, or indeed that an edge we print doesn't exist anymore. ¹ Also, for UX reasons, we'd want to print the *shortest path* between those two modules. ² Our main traversal is depth-first, so it's also not guaranteed to take the shortest possible path to each module. As a result, **there is an observable difference between** (the import stacks printed by) **initial and incremental builds**. * An error in an initial build (no existing Graph instance) will contain the full import stack, from the module containing the error down to the entry point. * An error in an incremental build (e.g. full reload / Fast Refresh of a bundle that previously built successfully) will contain a *partial* import stack - from the module containing the error down to some ancestor module *M*, where M is in the set of modules added/changed/invalidated since the previous successful build. * In this case we can still reliably tell the user that the dependency chain connects *somehow* to the bundle's entry point (since otherwise the module wouldn't be in the graph). Changelog: * **[Feature]** Include import stacks in resolution errors. --- TODO: * See how this renders in LogBox/RedBox and adjust accordingly. * Decide whether to bake the import stack into the error message or fully rely on external error reporters for presentation. * Probably print project-relative paths rather than absolute paths. * Pretty-print the paths of context modules rather than expose the generated, virtual paths. Differential Revision: D47548085 fbshipit-source-id: 781e3c4c4fc17a4e216e9adea1ab4c0cd708aff0
- Loading branch information
1 parent
9da2425
commit 2e85033
Showing
12 changed files
with
321 additions
and
5 deletions.
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
27 changes: 27 additions & 0 deletions
27
packages/metro/src/DeltaBundler/__tests__/__snapshots__/Graph-test.js.snap
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,70 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import type {TransformResultDependency} from './types.flow'; | ||
|
||
export type DependencyStackNode = $ReadOnly<{ | ||
// If `dependency` is null, this is a dependency whose origin isn't known | ||
// at the time we produced this stack. This happens in most incremental | ||
// traversals: since we can throw before we've finished processing | ||
// updates, all dependencies outside the immediate call stack are | ||
// potentially out of date, so we don't try to return them. | ||
dependency: null | TransformResultDependency, | ||
// Absolute path of the origin module if this is a concrete dependency, | ||
// or of the next known module in the chain if dependency is `null`. | ||
absolutePath: null | string, | ||
// The parent node in the dependency tree. | ||
parent: null | DependencyStackNode, | ||
}>; | ||
|
||
export function addImportStackToError( | ||
dependencyStack: DependencyStackNode, | ||
error: Error, | ||
) { | ||
let importStackString = ''; | ||
let node: ?DependencyStackNode = dependencyStack; | ||
let printedEllipsis = false; | ||
while (node) { | ||
if (node.dependency == null || node.absolutePath == null) { | ||
// Make sure we don't print multiple consecutive ellipses. | ||
if (!printedEllipsis) { | ||
if (importStackString !== '') { | ||
importStackString += '\n'; | ||
} | ||
importStackString += ' (...)'; | ||
printedEllipsis = true; | ||
} | ||
} | ||
|
||
if (node.absolutePath != null) { | ||
if (importStackString !== '') { | ||
importStackString += '\n'; | ||
} | ||
const loc = node.dependency?.data.locs[0]; | ||
|
||
importStackString += | ||
// TODO: @nocommit Absolute paths are too verbose - relativize them (here / outside of Graph?) | ||
' at ' + | ||
node.absolutePath + | ||
(loc ? `:${loc.start.line}:${loc.start.column + 1}` : ''); | ||
printedEllipsis = false; | ||
} | ||
node = node.parent; | ||
} | ||
try { | ||
error.message += '\nImport stack:\n' + importStackString; | ||
|
||
// $FlowIgnore | ||
error.metroImportStack = importStackString; | ||
} catch { | ||
// Mutating the error object might fail, so swallow any inner errors. | ||
} | ||
} |
Oops, something went wrong.