Skip to content

Ignoring outDir in Chokidar watcher introduced regression in >=5.1.0, not entirely fixed by #15979 #16248

@jedso

Description

@jedso

Describe the bug

I have a legacy project migrated from Gulp where a Sass file lives within the same directory as the outDir. Since Vite version >=5.1.0 (specifically #15326), vite build --watch no longer re-builds upon changes to the source Sass file. This issue is closely related to the problem described in #15951, which was fixed by the following conditional check in #15979:

if (config.build.outDir) {
ignored.push(
glob.escapePath(path.resolve(config.root, config.build.outDir)) + '/**',
)
}

The difference, in my case, is that outDir is not an empty string and instead is its own separate directory. The common thread between this issue and #15951 is that both projects have emptyOutDir set to false. My suggestion to fix my issue, while still solving the issue described in #15951, would be to test for emptyOutDir instead:

  if (config.build.emptyOutDir) { ... }

The thinking here is if the outDir is being emptied out for every build, there is no point watching for changes within. On the other hand, if the directory isn't being emptied, it is possible that source files may be present and need to be watched for. If there's agreement around this solution, I would be more than happy to open a PR with this change and update the docs.

Reproduction

https://stackblitz.com/edit/vitejs-vite-spjjif?file=vite.config.ts

Steps to reproduce

  1. Run npm run build (the script has --watch appended to vite build)
  2. Edit outDir/styles.scss and observe how saved changes do not trigger a re-build
  3. Downgrade Vite to pre-5.1.0 (e.g. npm install -D vite@5.0.12)
  4. Run npm run build again
  5. Observe how saved changes to outDir/styles.scss trigger a re-build

System Info

System:
    OS: Linux 5.15 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (4) x64 Intel(R) Core(TM) i5-6600K CPU @ 3.50GHz
    Memory: 12.39 GB / 15.49 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 20.11.1 - /usr/bin/node
    npm: 10.2.4 - /usr/bin/npm
  npmPackages:
    vite: ^5.2.4 => 5.2.4

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-to-be-discussedEnhancement under consideration (priority)

    Type

    No type

    Projects

    Status

    P2 - 4

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions