Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.watch doesn't emit change event when a watched folder is moved or deleted on Windows #31702

Closed
ARitz-Cracker opened this issue Feb 9, 2020 · 2 comments

Comments

@ARitz-Cracker
Copy link

Hello, hello! I'm back with another fs "bug"

  • Version: v12.15.0
  • Platform: Windows 10 1909 (x64)
  • Subsystem: fs

On Linux (tested with Ubuntu 18.04 with the same NodeJS version), when you move or delete a folder watched by fs.watch, a the change event is emitted with changeType being "rename". However, on Windows, the following happens:

  • Folder move within the same partition: No event is emitted
  • Folder move to different partition: (untested)
  • Deletion: error is emitted with "Error: EPERM: operation not permitted, watch"

Sidenote: detecting folder deletions is unreliable in general as renaming a file/folder within the watched folder with the same name as the watched folder will emit the exact same event as the watched folder itself being renamed or deleted, on Linux anyway.

@himself65
Copy link
Member

himself65 commented Feb 9, 2020

Example

const fs = require('fs')
if (!fs.existsSync('dir'))
  fs.mkdirSync('dir')

fs.watch('./dir').on('error', console.log)

then delete folder dir

Error: EPERM: operation not permitted, watch
    at FSEvent.FSWatcher._handle.onchange (internal/fs/watchers.js:123:28) {
  errno: -4048,
  syscall: 'watch',
  code: 'EPERM',
  filename: null
}

in Ubuntu, not throw errors.

@bzoz
Copy link
Contributor

bzoz commented Mar 3, 2020

We can't do anything about the folder itself being changed. You can always watch the parent folder though.

As for the EPERM - internally, we set up a callback using ReadDirectoryChangesW. When the folder is deleted, the callback returns ERROR_ACCESS_DENIED which gets translated into EPERM. So again - nothing we can really do about it.

I'll open a PR to add those two things to the "caveats" section of the fs.watch documentation.

bzoz added a commit to JaneaSystems/node that referenced this issue Mar 10, 2020
Document Windows specific fs.watch caveats.

Fixes: nodejs#31702
BridgeAR pushed a commit that referenced this issue Mar 17, 2020
Document Windows specific fs.watch caveats.

Fixes: #31702

PR-URL: #32176
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Masashi Hirano <shisama07@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue Mar 24, 2020
Document Windows specific fs.watch caveats.

Fixes: #31702

PR-URL: #32176
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Masashi Hirano <shisama07@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this issue Apr 22, 2020
Document Windows specific fs.watch caveats.

Fixes: #31702

PR-URL: #32176
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Masashi Hirano <shisama07@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants