coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Node: use fs.watch api instead

Open tylermumford opened this issue 15 years ago • 40 comments

If this issue has already been reported, I apologize for duplicating it. But I haven't found anything in the CoffeeScript community to indicate that this is known, so I'm going to report it here and hope that I won't get schooled.

Using the latest builds of Node.js for Windows (single .exe files, currently on v0.5.10), I get an error every time I try to tell the compiler to watch and compile my file. Specifically:

fs.js:696
    throw new Error('use fs.watch api instead');
          ^
Error: use fs.watch api instead
    at Object.watchFile (fs.js:696:11)
    at C:\Program Files\CoffeeScript\lib\coffee-script\command.js:199:15
    at Object.oncomplete (C:\Program Files\CoffeeScript\lib\coffee-script\comman
d.js:119:52)

It appears based on this error (and this similar issue on node-supervisor) that the fs.watchFile API no longer functions in these latest versions of Node.js.

Whether or not this issue is isolated to my usage on Windows 7, I would be happy to try to rewrite the compiler to use the new API. But if I'm not the only one with this problem, then I probably shouldn't be the one to submit the fix, as I'm not the cleanest programmer out there, and I don't have very much free time since school is in full swing.

With humility, Tyler Wayne

tylermumford avatar Oct 25 '11 07:10 tylermumford

+1 for the port to the new fs.watch api, because it is officially supported on Windows starting with node.js 5.9 (ReadDirectoryChangesW-API). I didn't know that fs.watchFile was supposed to work under windows, as a few people reported that the watch option of the coffeescript compiler doesn't work in windows

fbuchinger avatar Oct 26 '11 12:10 fbuchinger

Unfortunately, fs.watch doesn't exist in Node 0.4.x, which means we have a painful set of options:

  1. Switch from fs.watchFile to fs.watch and drop Node 0.4.x support
  2. Rewrite command.coffee to use fs.watch when available and fs.watchFile otherwise
  3. Change nothing, forcing Windows users to use something other than coffee to watch and recompile their CoffeeScript files

Option 2 would certainly be best if it were as simple as

watchFunc = fs.watch ? fs.watchFile

but looking at the docs (on Node master, because the ones on the site don't yet say anything about fs.watch), they're actually quite different. Let me see if I can come up with a mapping that works...

TrevorBurnham avatar Oct 27 '11 15:10 TrevorBurnham

Also, let's keep a good eye on what the latest "stable" version of Node is -- that's what we want to be targeting. 0.5.x support is more of a nice-to-have.

jashkenas avatar Oct 27 '11 15:10 jashkenas

@jashkenas Right, but with the announcement that 0.6 is coming next week, I'm assuming that the current Node master strongly reflects what will soon be the latest stable release.

TrevorBurnham avatar Oct 27 '11 15:10 TrevorBurnham

Worth adding that "fs.watchFile is replaced by fs.watch" is in the list of API changes from Node 0.4 to 0.6.

So maybe we should be leaving Node 0.4 behind relatively soon (CoffeeScript 1.2?). It looks like a lot of other projects are going to.

TrevorBurnham avatar Oct 27 '11 15:10 TrevorBurnham

Sure -- of course, you can always install an older version of CoffeeScript if you need it. They're all archived on npm and Github.

jashkenas avatar Oct 27 '11 15:10 jashkenas

mmhh... the situation with node.js/npm/coffeescript is quite unsatisfying for windows users at the moment. There are no official win32 binaries for node.js 0.4, so I had to use v0.5.10, and ran into the error described by tylerwayne.

I also have the impression that node.js is too much in flux so that its surrounding ecosystem can't catch up with the weekly API changes. I for my part decided to drop node.js until v0.5 is stable and will use Alisey's coffeescript compiler meanwhile

fbuchinger avatar Oct 27 '11 15:10 fbuchinger

@fbuchinger Right, Windows support is a goal for Node 0.6, an 0.5 is the unstable branch leading up to 0.6. But 0.6 is supposed to come next week, and when it does, there's going to be a lot more movement among Node libs to fully support Windows. So, better times are a-comin'. :)

TrevorBurnham avatar Oct 27 '11 17:10 TrevorBurnham

For what it's worth, 0.6 is now released http://blog.nodejs.org/2011/11/05/node-v0-6-0/

balupton avatar Nov 06 '11 12:11 balupton

Working on this now...

TrevorBurnham avatar Nov 08 '11 08:11 TrevorBurnham

Oh hey, never mind:

$ node -v
0.6.0
$ coffee -e "console.log require('fs').watchFile"
[Function]

And it's still in the docs as well. Looks like it got a stay of execution.

TrevorBurnham avatar Nov 08 '11 08:11 TrevorBurnham

$ node -v
v0.6.0

$ node -e 'console.log(require("fs").watchFile+"")'
function (filename) {
  if (isWindows) {
    throw new Error('use fs.watch api instead');
  }
...

satyr avatar Nov 08 '11 13:11 satyr

Seems like the sort of thing Node ought to mention in its docs. grumble, grumble Thanks, satyr.

I'll put together a pull request. Best to hold off on that 1.1.3 tag after all, @jashkenas...

TrevorBurnham avatar Nov 08 '11 14:11 TrevorBurnham

I'm curious, though: @tylerwayne and @fbuchinger, how were you even using command.coffee under Windows? Since npm is such a pain, and bin/coffee isn't going to work, I take it you're downloading the repo and running the command

node path/to/lib/coffee-script/command.js

Is that correct?

We'll probably want to figure out a way to make npm install -g coffee-script install a coffee executable under Windows, once npm finally works under Windows for realsies.

TrevorBurnham avatar Nov 08 '11 14:11 TrevorBurnham

Yes -- this is the hold up for a 1.1.3 ... but it shouldn't be too hard to switch to the new API.

jashkenas avatar Nov 08 '11 15:11 jashkenas

@jashkenas I wrote a patch to use fs.watch instead of fs.watchFile if available, but in testing it I found that fs.watch always fires twice on my system. Now trying to figure out if there's a way to ignore the duplicate events...

TrevorBurnham avatar Nov 08 '11 15:11 TrevorBurnham

I couldn't figure out a workaround (probably a kink that'll be worked out in Node 0.6.1), so I've submitted a pull request that uses fs.watch under Windows only. I haven't tested that it actually works under Windows, though, so someone else should try it out before we merge it...

TrevorBurnham avatar Nov 08 '11 15:11 TrevorBurnham

The above commit switches to the fs.watch API by stat-ing the file after a change has been triggered, and comparing the file size and mtime with the previous stat.

The neat thing about this new API is that it's effectively instant, as compared to the old one's slight delay.

jashkenas avatar Nov 08 '11 21:11 jashkenas

That commit broke --watch on my Mac (OS X 10.6.8) with node 0.6.0. fs.watch reports rename on the first change, twice, and fails to report subsequent changes.

satyr avatar Nov 08 '11 21:11 satyr

@satyr: It's working just fine for me on Mac OS 10.6.8 and Node.js v0.6.0.

Can you tell me what exact command you're running where you're seeing an error?

jashkenas avatar Nov 08 '11 21:11 jashkenas