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

ENOENT when setting fs.watch #246

Closed
cburroughs opened this issue Aug 14, 2018 · 4 comments
Closed

ENOENT when setting fs.watch #246

cburroughs opened this issue Aug 14, 2018 · 4 comments

Comments

@cburroughs
Copy link

'use strict';

const fs = require('fs');
const mockfs = require('mock-fs');

mockfs({'/foo': {'a': '', 'b': '', 'c': ''}});
console.log('dir contents',  fs.readdirSync('/foo'));
fs.watch('/foo', {}, function _onDirEvent(evt) {
    console.log('evt', evt);
});
$ node foo.js 
dir contents [ 'a', 'b', 'c' ]
fs.js:1236
    throw error;
    ^

Error: watch /foo ENOENT

I could not find fs.watch defined anywhere in mock-fs, but also saw that #207 removed a caveat about fs.watch not being implemented. I'm not sure if I'm missing something obvious or if fs.watch is unsupported.

@maxwellgerber
Copy link
Contributor

Nope, not supported yet.
fs.watch uses FSEvent Event Emitters, which look like they might be out of scope of this lib.
fs.watchFile uses StatWatchers - which haven't been implemented yet :

mock-fs/lib/binding.js

Lines 1214 to 1218 in 28d65a7

/**
* Not yet implemented.
* @type {function()}
*/
Binding.prototype.StatWatcher = notImplemented;

@tschaub
Copy link
Owner

tschaub commented Sep 11, 2018

Yes. Not yet implemented. This is the first I've seen of a request for it, so it is hard to say how widely used it would be. But if anyone is up for implementing it, a well tested pull request would be welcome.

@tschaub tschaub closed this as completed Sep 11, 2018
@dpryden
Copy link

dpryden commented Jun 22, 2020

For what it's worth, I had a use case for fs.watch, so I took a stab at putting together a pull request. It turns out to be much more complicated than it first appears, since the FSEvent class is in a separate binding,fs_event_wrap. The way this binding is loaded is different in different versions of Node. In Node 10+ it's lazily loaded so it is possible to replace it with a proxy that can be mocked/unmocked like the other binding dependencies. However, in earlier Node versions, the FSEvent function object is imported eagerly by the fs module and I don't see an easy way to replace it, so perhaps we would additionally need to monkeypatch fs.watch on earlier Node versions?

In any case, I've decided not to invest any more time in this right now, unfortunately. I'm going to rewrite the affected tests in my project instead.

However, if anyone finds it useful, I've pushed up a PR with my changes, which do work on Node 10 and Node 12 (and have a couple of small tests demonstrating that). See #300 (Edit: new PR is #308 )

@BadIdeaException
Copy link

Any news on this? At least as an optional functionality on Node 10+?

On a related note, might want to add the caveat about it being unsupported back into the readme. It took me a good while to find out that this is unsupported, and not just me being too dumb to use it right. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants