Skip to content

Yarn virtual packages/workspaces not watched in pnp #15809

Description

@mrginglymus

Describe the bug

I have a project set up as a monorepo using yarn workspaces and pnp .

c:\dev\package.json
-------vite.config.js
-------packages\react\package.json
---------------------\src\button.tsx

Because some of the workspaces have peer dependencies, files from that workspace will apear as a PnP virtual. That is, when a change is made to button it will come through as something like

c:/dev/.yarn/__virtual__/@react-virtual-12345abcd/packages/react/button.tsx

However, the chokidar watcher is initialised like this:

? (chokidar.watch(
// config file dependencies and env file might be outside of root
[
root,
...config.configFileDependencies,
...getEnvFilesForMode(config.mode, config.envDir),
],
resolvedWatchOptions,
) as FSWatcher)

ie, with the project root; in this case c:/dev. The __virtual__ dir is not picked up by this, and file change events come in under the physical file location (c:/dev/packages/react/button.tsx) which isn't watched and thus the changes are ignored.

The problem appears to be here:

// only need to watch if out of root
!file.startsWith(withTrailingSlash(root)) &&

in ensureWatchedFile, which is supposed to add any used files to the watcher that aren't already there.

Because the __virtual__ dir is located in the project root, this bit of code assumes that it is already watched and ignores.

Reproduction

https://github.com/mrginglymus/vite-pnp

Steps to reproduce

Check out the linked reproduction and install with yarn.

Run yarn dev and attempt to make changes to button.jsx - observe that they do not trigger hmr.

Edite vite.config.js to have a root of demo and run yarn dev again.

Make changes to button.jsx - observe that they do now trigger hmr.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (24) x64 Intel(R) Core(TM) i9-10920X CPU @ 3.50GHz
    Memory: 33.56 GB / 63.80 GB
  Binaries:
    Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 4.1.0 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.83)
    Internet Explorer: 11.0.19041.3636

Used Package Manager

yarn

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions