You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue tracks known bugs in the implementation of allowOptionalDependencies in #511.
While allowOptionalDependencies is off by default in Metro, it is on by default for apps built using the React Native CLI: react-native-community/cli#1350.
Bug 1: Unresolved optional dependencies are broken at runtime, along with subsequent requires/imports in the same file.
const A = metroRequire(dependencyMap[0]);
let B;
try {
B = metroRequire(dependencyMap[1]);
} catch {}
const C = metroRequire(dependencyMap[2]);
Where dependencyMap is assumed to have an entry for each required module. But if ./b.js is unresolved at build time, dependencyMap will only have entries for A and C. Therefore the following lines behave incorrectly:
B = metroRequire(dependencyMap[1]); // B evaluates to require('./c.js') !
const C = metroRequire(dependencyMap[2]); // C evaluates to metroRequire(undefined), which throws an error
Bug 2: If an optional dependency is unresolved and later becomes resolvable (e.g. the package is installed while Metro is running), Metro will not detect this unless the origin file is also modified (or Metro is restarted).
This is because we don't always invalidate dependency resolutions correctly. In this case, we do not track the relationship between the initially-missing dependency (and the paths it might appear in) and the origin file, so Metro has no reason to mark the origin file as modified when the dependency later appears. (This is a broader problem with our resolver architecture that affects more than just optional dependencies.)
The text was updated successfully, but these errors were encountered:
This issue tracks known bugs in the implementation of
allowOptionalDependencies
in #511.While
allowOptionalDependencies
is off by default in Metro, it is on by default for apps built using the React Native CLI: react-native-community/cli#1350.Bug 1: Unresolved optional dependencies are broken at runtime, along with subsequent requires/imports in the same file.
Repro: https://github.com/motiz88/metro-optional-deps-bug-1
The above module compiles to
Where
dependencyMap
is assumed to have an entry for eachrequire
d module. But if./b.js
is unresolved at build time, dependencyMap will only have entries for A and C. Therefore the following lines behave incorrectly:Bug 2: If an optional dependency is unresolved and later becomes resolvable (e.g. the package is installed while Metro is running), Metro will not detect this unless the origin file is also modified (or Metro is restarted).
This is because we don't always invalidate dependency resolutions correctly. In this case, we do not track the relationship between the initially-missing dependency (and the paths it might appear in) and the origin file, so Metro has no reason to mark the origin file as modified when the dependency later appears. (This is a broader problem with our resolver architecture that affects more than just optional dependencies.)
The text was updated successfully, but these errors were encountered: