webpack / watchpack

Wrapper library for directory and file watching.
MIT License
377 stars 105 forks source link

Add support for watching symlinks that resolve to paths outside the watch folder #231

Open bartvandenende-wm opened 10 months ago

bartvandenende-wm commented 10 months ago

Feature request

Watchpack followSymlinks option always resolves the directory / file paths to final link target. This can create unwanted side-effects if the target is outside of the watched directory and/or if ignored filters.

It would be great to have an option that would transparently follow symlinks without resolving to the target path.

What is the expected behavior?

I would expect the below to work:

Example 1
  1. Create a directory structure like below
    ./ app
    file1.js
    ./ src 
    file2.js
    app -> ../app
  2. Watch the ./src directory
  3. Trigger changes on both ./src/file2.js and ./src/app/file1.js

Observe that only files in src are monitored, but not files in src/app as Watchpack resolves it to ./app/file1.js.

Changing the watch directory to the project root ./ fixes this, but is not always an option.

Example 2

A similar example can be presented for the ignore filter.

  1. Create a directory structure like below
    ./ app
    file1.js
    ./ src 
    file2.js
    app -> ../app
  2. Watch the ./ directory and set an ignore filter to ignore everything except files / folders starting with the path ./src
  3. Trigger changes on both ./src/file2.js and ./src/app/file1.js

Observe that only files in src are monitored, but not files in src/app as the Watchpack 'ignored' filter exposes the pathname as the resolved path of ./app/file1.js instead of ./src/app/file1.js

What is motivation or use case for adding/changing the behavior?

Users should be able to use watchpack with symlinks without having to depend on detailed knownledge of the final symlink resolve path.

How should this be implemented in your opinion?

A potential non-breaking direction is to providing an new option followSymlinksPreservePath that would not use the LinkResolver but instead changes fs.lstat to fs.stat in DirectoryWatcher.js to transparently resolve symlinks and watch nested files without changing the paths: https://github.com/webpack/watchpack/blob/dc690bbaea140820f1d9c7c2ec4dff8902798ff9/lib/DirectoryWatcher.js#L395

Are you willing to work on this yourself?

yes

bartvandenende-wm commented 10 months ago

find a sample project to demonstrate the issue below: https://github.com/bartvandenende-wm/watchpack-directory-symlink-issue

bartvandenende-wm commented 10 months ago

Related issue https://github.com/webpack/watchpack/issues/190#issue-864025773

alexander-akait commented 10 months ago

@bartvandenende-wm Sounds good for me, do you want to send a PR?