Fix inconsistent filesystem state after a Haste collision #1399
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
Prior to D45033364 / f443e5d, Haste collisions arising after startup (in watch mode) would log a warning to console, but would otherwise be fully processed. This was true even if
throwOnModuleCollisions
was true, because_watch()
mutatesthis._options.throwOnModuleCollision
:metro/packages/metro-file-map/src/index.js
Lines 872 to 874 in 462cc9d
In D45033364, this behaviour unintentionally regressed, because
MutableHasteMap
, newly responsible for warning/throwing, is passedthrowOnModuleCollision
(by its original value) in its constructor, and so the mutation in_watch
had no effect:metro/packages/metro-file-map/src/index.js
Lines 415 to 420 in 462cc9d
Weirdly, I apparently did realise this at the time, because I gave
MutableHasteMap
asetThrowsOnModuleCollision
method that has never been used, maybe the call got lost in a merge or something 🤷♂️:metro/packages/metro-file-map/src/lib/MutableHasteMap.js
Lines 287 to 289 in 462cc9d
The throw during watch does not actually kill Metro, it just gets logged:
metro/packages/metro-file-map/src/index.js
Lines 1071 to 1075 in 462cc9d
But critically, a throw like this during
_processFile
prevents items being added to thefileSystem
(TreeFS
) and prevents change events firing:metro/packages/metro-file-map/src/index.js
Lines 1032 to 1042 in 462cc9d
And worse, because the file is not in
TreeFS
, deleting it will not remove it from the Haste map (due to the early return in_removeIfExists
), so the collision cannot be resolved without restarting Metro:metro/packages/metro-file-map/src/index.js
Lines 826 to 841 in 462cc9d
This is frequently seen when rebasing/moving around revisions while Metro is running, and could cause hard-to-debug errors (missing or ambiguous Haste resolution errors) that mysteriously resolve themselves by restarting Metro.
This restores the old behaviour in the simplest way by calling the setter at the same time as mutating
_options
, as well as adding a regression test. We'll refactor this a bit later.Changelog:
Test plan
Before (with new test)
After
Tests pass