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

Chokidar will trigger for unmodified files on Windows when LastAccessTime is changed #750

Closed
mikemaccana opened this issue Jul 16, 2018 · 8 comments

Comments

@mikemaccana
Copy link

mikemaccana commented Jul 16, 2018

Given the following in Windows:

var watcher = chokidar.watch(dir, {
    ignored: ignored,
    persistent: true,
    usePolling: config.options.legacyWatch || false,
    interval: config.options.pollingInterval,
});

When a file is opened but not modified, Chokidar will trigger a change as LastAccessTime is modified. All other attributes remain unchanged. See remy/nodemon#1208

Versions (please complete the following information):

  • Chokidar version: 170
  • Node version: v8.11.2
  • OS version: Microsoft Windows NT 10.0.17713.0

To Reproduce
Just open a file (changing LastAccessTime on a watched file. LMK if you want a demo app.

Expected behavior
No changes. Access Time is not considered a change. It is reading, not writing.

@mikemaccana mikemaccana changed the title Chokidar will trigger for unmodified fils on windows when LastAccessTime is changed Chokidar will trigger for unmodified files on windows when LastAccessTime is changed Jul 16, 2018
@mikemaccana mikemaccana changed the title Chokidar will trigger for unmodified files on windows when LastAccessTime is changed Chokidar will trigger for unmodified files on Windows when LastAccessTime is changed Jul 16, 2018
@dariocravero
Copy link

dariocravero commented Jul 31, 2018

I can confirm that this is happening on Mac too.

I wrote this script to try to work out what's going on:

const childProcess = require('child_process')
const chokidar = require('chokidar')
const fs = require('fs-extra')

async function run() {
  await fs.unlink('file')
  await fs.writeFile('file', 'content')

  setTimeout(() => {
    const watcher = chokidar.watch('file', {
      // we need the stat
      alwaysStat: true,

      // doesn't do it
      // ignore(path, stat) {
      //   return (stat.atimeMs > stat.mtimeMs)
      // },
      ignoreInitial: true,
    })

    let changes = 0
    watcher.on('change', (file, stat = {}) => {
      // this does it
      if (stat.atimeMs > stat.mtimeMs) return

      changes++
    })

    watcher.on('ready', async () => {
      setTimeout(() => childProcess.spawnSync('open', ['file']), 250)

      setTimeout(() => fs.writeFile('file', 'more content'), 1500)

      setTimeout(() => {
        watcher.close()

        console.log('registered one change', changes === 1)
      }, 3000)
    })
  }, 1000)
}

run()

We can bail on change if the access time is newer than the modified time. I tried doing it on the ignore option but it doesn't do what I thought it did :).

Is this something that chokidar should handle internally?

@goser
Copy link

goser commented Aug 8, 2018

Adding

if (event === 'change' && val1.atime && val1.mtime && val1.atime > val1.mtime) {
    return this;
}

to FSWatcher.prototype._emit worked for me as a hotfix but I am not sure if it would break other projects.

@mikemaccana
Copy link
Author

@goser Send it as a PR and mark it as 'fixes #750'. It'll get more attention and someone will review it.

@markablov
Copy link
Contributor

#762

@straussdd
Copy link

I'm running nodemon in a Visual Studio Code task and this behavior is really annoying. VS Code will sporadically cause the access time of multiple files to be updated whenever its git autorefresh feature runs in the background. So I really appreciate your PR, @markablov

@johannreboulleau
Copy link

johannreboulleau commented Oct 3, 2018

Hello,
We had the same problem, and we fix it thanks to the solution of @svogal

@7iomka
Copy link

7iomka commented Nov 20, 2018

Hello,
We had the same problem, and we fix it thanks to the solution of @svogal

Thank you!

@mikemaccana
Copy link
Author

Looks like this is in 2.1.0, see https://www.npmjs.com/package/chokidar/v/2.1.0

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

No branches or pull requests

8 participants