webpack: Watcher fails to build sometimes

I’ve been noticing that webpack’s watcher often fails to rebuild when I change a file. I thought there might have been weird issues that had to do with my vagrant situation (where I have another file watcher that copies files over). But now I’m using it on on windows 8.1 (node 0.10.29, webpack 1.5.3).

In my own file watcher that I use for vagrant, I had pretty much the same problem. I was using chokidar, and sometimes files simply weren’t being copied over when they changed. I don’t know the root cause, but I worked around it by setting up a poller that polls file contents for files that I’ve recently changed for up to 50 seconds. If the file changes within that time, it’ll copy it over.

The code I have is:

var seconds = 1000 // from milliseconds

var filesToPoll = {}
function poller() {
    setTimeout(function() {
        Fiber(function() {
            try {
                for(var file in filesToPoll) {
                    var info = filesToPoll[file]
                    if(Date.now() - info.time > 50*seconds && !info.matchFailure) {
                        delete filesToPoll[file] // stop polling it
                    } else if(Date.now() - info.time > 5*seconds && fs.readFileSync(file).toString() !== fs.readFileSync(info.destination).toString()) {
                        info.matchFailure = true
                        console.log(colors.red("File "+file+" doesn't match! Fixing the bitch"))
                        exports.copy(file, info.destination)
                    } else {
                        if(info.matchFailure) {
                            console.log(colors.blue("Match failure corrected for "+file+"!"))
                            info.matchFailure = false
                        }

                    }
                }
            } catch(e) {
                console.log(e.stack.toString().red)
            }
            poller()
        }).run()
    },1000)
}

setTimeout(function() {
    filesToPoll = {} // empty it out after its been given time to set up the watchers
    poller()
},10000)

and to add a file to the poller:

filesToPoll[filePathToCopyOver] = {time: Date.now(), destination: destinationPath}

Originally I just put in the console logs so I could see when files were failing to copy over. After I noticed just how often it was happening, I put in the extra copy logic. I haven’t had a problem with my file watcher since.

So maybe you could use this kind of plug solution if you don’t know why the watcher might not be picking up file changes.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

It took me ages to figure out why I was getting intermittent rebuilds on saving files. I figure out my issue, so in case someone else comes along with this same problem:

Another thing that causes this to happen is if your IDE/editor writes to a temporary file and then renames the temporary file. I know that vim does this and it can be changed: https://github.com/webpack/webpack/issues/781

Also, Jetbrains IDEs have a setting called Use "safe write" which can be disabled to fix this as well.