Skip to content

Commit

Permalink
Fix EPERM errors reading transient/locked files on Windows without Wa…
Browse files Browse the repository at this point in the history
…tchman (#1007)

Summary:
Pull Request resolved: #1007

As reported in #1001, `fs.lstat` operations on Windows can fail with `EPERM` in certain circumstances - apparently when new files are either still being written or are about to be deleted.

Currently, this bubbles up as a Watcher error, whereas it should be handled gracefully.

Reportedly the OS will follow up with another event on the file when it's ready to read, so we may safely ignore `EPERM` in `add`/`change` event processing.

NOTE: This raises the question of why we're watching build output/intermediate directories in the default React Native Metro config. We should almost certainly be ignoring them from Metro.

```
 - **[Changelog]** Fix EPERM file watching errors on Windows.
```

Reviewed By: motiz88

Differential Revision: D46803761

fbshipit-source-id: 043b55cede80edfc77ab6be5753b6808aa2f1528
  • Loading branch information
robhogan authored and facebook-github-bot committed Jun 19, 2023
1 parent 2ed01e8 commit 4b22efb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/metro-file-map/src/watchers/NodeWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ module.exports = class NodeWatcher extends EventEmitter {
}
}
} catch (error) {
if (error?.code !== 'ENOENT') {
if (!isIgnorableFileError(error)) {
this.emit('error', error);
return;
}
Expand Down Expand Up @@ -403,7 +403,11 @@ module.exports = class NodeWatcher extends EventEmitter {
function isIgnorableFileError(error: Error | {code: string}) {
return (
error.code === 'ENOENT' ||
// Workaround Windows EPERM on watched folder deletion.
// Workaround Windows EPERM on watched folder deletion, and when
// reading locked files (pending further writes or pending deletion).
// In such cases, we'll receive a subsequent event when the file is
// deleted or ready to read.
// https://github.com/facebook/metro/issues/1001
// https://github.com/nodejs/node-v0.x-archive/issues/4337
(error.code === 'EPERM' && platform === 'win32')
);
Expand Down

0 comments on commit 4b22efb

Please sign in to comment.