Commit baf28ab
Process files before adding them to
Summary:
*This is a second attempt at D42846676 (1a81060), which was backed out due to bad Haste move handling - details in test plan.*
`metro-file-map` takes raw crawl or watch results, "processes" them with a worker pool (compute hashes, read symlink targets), and makes metadata available to consumers.
Previously, files would be added to the file `Map` before processing, and then additional metadata would be set, and finally (when watching) an event would be emitted.
## Problem
This created a potential bug because files briefly exist, accessible in the `FileSystem` interface, in an intermediate state. Consider:
1. `foo.js` exists and has a SHA1 pre-calculated.
2. A change is made to `foo.js`, watcher backend emits a change event.
3. `FileSystem` is updated with the metadata of the unprocessed file, and an async worker starts processing.
4. Metro receives a bundle request for `foo.js`. It [fails hard](https://github.com/facebook/metro/blob/main/packages/metro/src/node-haste/DependencyGraph.js#L250) because the file has no calculated hash.
5. The worker completes and we populate the SHA1, and emit a 'change' event.
This also complicates `TreeFS`, because symlink targets
aren't guaranteed to be available to it.
## This diff
This diff switches things around so that the `FileSystem` implementation is only updated with ready-processed `FileMetaData`, and events are emitted to `metro-file-map` consumers synchronously with updates to `FileSystem`.
That means `FileSystem` will expose stale, complete state instead of fresher, incomplete state. Besides the fix above, this is okay and indeed preferable, because:
- Two calls to `getSha1()` for the same file are guaranteed to return the same result unless there's been a 'change' event emitted on that file in the meantime. This is much more predictable.
- In any case, any file system representation is *always* potentially stale - I/O isn't instant, OS events take time to propagate, so well behaved consumers should already be treating it as such.
## Summary
Currently we
1. Remove all deleted files from the Haste map and file map simultaneously.
2. Update new/changed files in the file map (with incomplete metadata).
3. "Process" new/changed files, updating the file map with complete metadata and adding entries to the module map.
In this diff:
1. Remove all deleted files from the Haste map and file map simultaneously.
2. Process new/changed files, filling in gaps in metadata and adding entries to the module map.
3. Update new/changed files in the file map (with complete metadata).
*(In D42846676 (1a81060), 1 and 2 were reversed)*
Changelog: **[Fix]** Race condition where a very recently modified file might have missing metadata.
Reviewed By: huntie
Differential Revision: D42930236
fbshipit-source-id: 983af37bc8829ebc8b403483177211b578905ffcFileSystem (re-land)1 parent 27c2112 commit baf28ab
4 files changed
Lines changed: 89 additions & 122 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
| |||
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 53 | | |
82 | 54 | | |
83 | 55 | | |
| |||
97 | 69 | | |
98 | 70 | | |
99 | 71 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | 72 | | |
111 | 73 | | |
112 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1530 | 1530 | | |
1531 | 1531 | | |
1532 | 1532 | | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
1533 | 1536 | | |
1534 | 1537 | | |
1535 | 1538 | | |
1536 | | - | |
| 1539 | + | |
1537 | 1540 | | |
1538 | 1541 | | |
1539 | 1542 | | |
1540 | 1543 | | |
1541 | 1544 | | |
1542 | 1545 | | |
1543 | | - | |
| 1546 | + | |
1544 | 1547 | | |
1545 | 1548 | | |
1546 | 1549 | | |
| |||
1808 | 1811 | | |
1809 | 1812 | | |
1810 | 1813 | | |
1811 | | - | |
| 1814 | + | |
1812 | 1815 | | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
1813 | 1819 | | |
1814 | 1820 | | |
1815 | 1821 | | |
| |||
1820 | 1826 | | |
1821 | 1827 | | |
1822 | 1828 | | |
1823 | | - | |
1824 | | - | |
| 1829 | + | |
| 1830 | + | |
1825 | 1831 | | |
1826 | 1832 | | |
1827 | 1833 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
165 | 164 | | |
166 | 165 | | |
167 | 166 | | |
| |||
216 | 215 | | |
217 | 216 | | |
218 | 217 | | |
219 | | - | |
220 | 218 | | |
221 | 219 | | |
222 | 220 | | |
| |||
238 | 236 | | |
239 | 237 | | |
240 | 238 | | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | 239 | | |
249 | 240 | | |
250 | 241 | | |
| |||
0 commit comments