Skip to content

Commit

Permalink
docs: update WSL2 watch limitation explanation (#8939)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
sapphi-red and bluwy authored Aug 13, 2022
1 parent f4b4405 commit afbb87d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 27 deletions.
7 changes: 7 additions & 0 deletions docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,10 @@ Limit for chunk size warnings (in kbs).
- **Default:** `null`

Set to `{}` to enable rollup watcher. This is mostly used in cases that involve build-only plugins or integrations processes.

::: warning Using Vite on Windows Subsystem for Linux (WSL) 2

There are cases that file system watching does not work with WSL2.
See [`server.watch`](./server-options.md#server-watch) for more details.

:::
15 changes: 13 additions & 2 deletions docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ The error that appears in the Browser when the fallback happens can be ignored.

File system watcher options to pass on to [chokidar](https://github.com/paulmillr/chokidar#api).

When running Vite on Windows Subsystem for Linux (WSL) 2, if the project folder resides in a Windows filesystem, you'll need to set this option to `{ usePolling: true }`. This is due to [a WSL2 limitation](https://github.com/microsoft/WSL/issues/4739) with the Windows filesystem.

The Vite server watcher skips `.git/` and `node_modules/` directories by default. If you want to watch a package inside `node_modules/`, you can pass a negated glob pattern to `server.watch.ignored`. That is:

```js
Expand All @@ -188,6 +186,19 @@ export default defineConfig({
})
```

::: warning Using Vite on Windows Subsystem for Linux (WSL) 2

When running Vite on WSL2, file system watching does not work when a file is edited by Windows applications (non-WSL2 process). This is due to [a WSL2 limitation](https://github.com/microsoft/WSL/issues/4739). This also applies to running on Docker with a WSL2 backend.

To fix it, you could either:

- **Recommended**: Use WSL2 applications to edit your files.
- It is also recommended to move the project folder outside of a Windows filesystem. Accessing Windows filesystem from WSL2 is slow. Removing that overhead will improve performance.
- Set `{ usePolling: true }`.
- Note that [`usePolling` leads to high CPU utilization](https://github.com/paulmillr/chokidar#performance).

:::

## server.middlewareMode

- **Type:** `boolean`
Expand Down
19 changes: 7 additions & 12 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import type { PackageData } from './packages'
import { watchPackageDataPlugin } from './packages'
import { ensureWatchPlugin } from './plugins/ensureWatch'
import { ESBUILD_MODULES_TARGET, VERSION } from './constants'
import { resolveChokidarOptions } from './watch'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -504,23 +505,17 @@ async function doBuild(
output.push(buildOutputOptions(outputs))
}

const watcherOptions = config.build.watch
const resolvedChokidarOptions = resolveChokidarOptions(
config.build.watch.chokidar
)

const { watch } = await import('rollup')
const watcher = watch({
...rollupOptions,
output,
watch: {
...watcherOptions,
chokidar: {
ignoreInitial: true,
ignorePermissionErrors: true,
...watcherOptions.chokidar,
ignored: [
'**/node_modules/**',
'**/.git/**',
...(watcherOptions?.chokidar?.ignored || [])
]
}
...config.build.watch,
chokidar: resolvedChokidarOptions
}
})

Expand Down
23 changes: 10 additions & 13 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { CLIENT_DIR } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { invalidatePackageData } from '../packages'
import { resolveChokidarOptions } from '../watch'
import type { PluginContainer } from './pluginContainer'
import { createPluginContainer } from './pluginContainer'
import type { WebSocketServer } from './ws'
Expand Down Expand Up @@ -300,6 +301,11 @@ export async function createServer(
)
const { middlewareMode } = serverConfig

const resolvedWatchOptions = resolveChokidarOptions({
disableGlobbing: true,
...serverConfig.watch
})

const middlewares = connect() as Connect.Server
const httpServer = middlewareMode
? null
Expand All @@ -310,19 +316,10 @@ export async function createServer(
setClientErrorHandler(httpServer, config.logger)
}

const { ignored = [], ...watchOptions } = serverConfig.watch || {}
const watcher = chokidar.watch(path.resolve(root), {
ignored: [
'**/.git/**',
'**/node_modules/**',
'**/test-results/**', // Playwright
...(Array.isArray(ignored) ? ignored : [ignored])
],
ignoreInitial: true,
ignorePermissionErrors: true,
disableGlobbing: true,
...watchOptions
}) as FSWatcher
const watcher = chokidar.watch(
path.resolve(root),
resolvedWatchOptions
) as FSWatcher

const moduleGraph: ModuleGraph = new ModuleGraph((url, ssr) =>
container.resolveId(url, undefined, { ssr })
Expand Down
21 changes: 21 additions & 0 deletions packages/vite/src/node/watch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { WatchOptions } from 'types/chokidar'

export function resolveChokidarOptions(
options: WatchOptions | undefined
): WatchOptions {
const { ignored = [], ...otherOptions } = options ?? {}

const resolvedWatchOptions: WatchOptions = {
ignored: [
'**/.git/**',
'**/node_modules/**',
'**/test-results/**', // Playwright
...(Array.isArray(ignored) ? ignored : [ignored])
],
ignoreInitial: true,
ignorePermissionErrors: true,
...otherOptions
}

return resolvedWatchOptions
}

0 comments on commit afbb87d

Please sign in to comment.