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

On windows, cannot watch files with some glob patterns #714

Closed
meteorlxy opened this issue May 10, 2018 · 6 comments
Closed

On windows, cannot watch files with some glob patterns #714

meteorlxy opened this issue May 10, 2018 · 6 comments

Comments

@meteorlxy
Copy link

meteorlxy commented May 10, 2018

const chokidar = require('chokidar')
const path = require('path')

const pagesWatcher = chokidar.watch([
  path.join(__dirname, 'watchdir/**/*.md')
], {
  ignoreInitial: true
})

const log = () => {
  console.log('triggered')
}

pagesWatcher.on('add', log)
pagesWatcher.on('unlink', log)
pagesWatcher.on('change', log)
  

Works well on Ubuntu, but logs nothing on Windows

@es128
Copy link
Contributor

es128 commented May 10, 2018

path.join(__dirname, 'watchdir/**/*.md')

Can't do that on Windows as it will introduce backslash path separators to your glob pattern, which is invalid. It's unnecessary anyway. You can just watch 'watchdir/**/*.md', or use the cwd option if you need to.

@es128 es128 closed this as completed May 10, 2018
@meteorlxy
Copy link
Author

meteorlxy commented May 10, 2018

@es128
Sorry but I want to watch some other absolute paths that should be passed in, not the __dirname indeed.

E.g:

const sourceDir = '[An absolute path that passed by other script]'
const pagesWatcher = chokidar.watch([
  path.join(sourceDir , 'watchdir/**/*.md')
], {
  ignoreInitial: true
})

@es128
Copy link
Contributor

es128 commented May 10, 2018

Then make sure sourceDir uses forward-slash path separators. You cannot combine backslash paths with globs. Backlashes are reserved in globs for escaping special characters.

@es128
Copy link
Contributor

es128 commented May 10, 2018

Also cwd: sourceDir in the options may be an easy solution.

@meteorlxy
Copy link
Author

meteorlxy commented May 10, 2018

@es128
The sourceDir is processed by path.resolve.
And the path.join will resolve the slash correctly, too.

Results like:

  • E:\Projects\meteorlxy\vuepress\docs\**\*.md

@es128
Copy link
Contributor

es128 commented May 10, 2018

notice the backslashes in the result? invalid glob.

Don't use path.join with globs.

Repository owner locked as resolved and limited conversation to collaborators May 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants