Skip to content

Commit 1ad7f4a

Browse files
clemyanarcanis
andauthored
PnP: Fix watch mode for files required under PnP (#7077)
## What's the problem this PR addresses? When running under `node --watch`, the PnP hook only reports not-found virtual modules to the watching process, causing found virtual modules to not be watched. Fixes (partially) #6871 ## How did you fix it? Report found modules from the PnP hook to the watching process. Note that this does not fix watch mode for **`import`ed** modules, which remains broken for node 18.19+. Given that the ESM loader API hasn't stabilized I am hesitant to make any changes at this moment. ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed. --------- Co-authored-by: Maël Nison <[email protected]>
1 parent deaf916 commit 1ad7f4a

5 files changed

Lines changed: 38 additions & 8 deletions

File tree

.pnp.cjs

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/versions/c071c1e5.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-pnp": patch
4+
"@yarnpkg/pnp": patch
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-init"
12+
- "@yarnpkg/plugin-interactive-tools"
13+
- "@yarnpkg/plugin-nm"
14+
- "@yarnpkg/plugin-npm-cli"
15+
- "@yarnpkg/plugin-pack"
16+
- "@yarnpkg/plugin-patch"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"
25+
- "@yarnpkg/nm"
26+
- "@yarnpkg/pnpify"
27+
- "@yarnpkg/sdks"

packages/yarnpkg-pnp/sources/hook.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-pnp/sources/loader/makeApi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,10 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
829829
const qualifiedPath = applyNodeExtensionResolution(unqualifiedPath, candidates, {extensions});
830830

831831
if (qualifiedPath) {
832+
nodeUtils.reportRequiredFilesToWatchMode([qualifiedPath]);
832833
return ppath.normalize(qualifiedPath);
833834
} else {
834-
nodeUtils.reportRequiredFilesToWatchMode(candidates.map(candidate => npath.fromPortablePath(candidate)));
835+
nodeUtils.reportRequiredFilesToWatchMode(candidates);
835836

836837
const unqualifiedPathForDisplay = getPathForDisplay(unqualifiedPath);
837838

packages/yarnpkg-pnp/sources/loader/nodeUtils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {NativePath, npath, VirtualFS} from '@yarnpkg/fslib';
1+
import type {NativePath, PortablePath} from '@yarnpkg/fslib';
2+
import {npath, VirtualFS} from '@yarnpkg/fslib';
23
import fs from 'fs';
34
import path from 'path';
45

@@ -54,9 +55,9 @@ Instead change the require of ${basename} in ${parentPath} to a dynamic import()
5455

5556
// https://github.com/nodejs/node/pull/44366
5657
// https://github.com/nodejs/node/pull/45348
57-
export function reportRequiredFilesToWatchMode(files: Array<NativePath>) {
58+
export function reportRequiredFilesToWatchMode(paths: Array<PortablePath>) {
5859
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
59-
files = files.map(filename => npath.fromPortablePath(VirtualFS.resolveVirtual(npath.toPortablePath(filename))));
60+
const files = paths.map(filename => npath.fromPortablePath(VirtualFS.resolveVirtual(filename)));
6061
if (WATCH_MODE_MESSAGE_USES_ARRAYS) {
6162
process.send({'watch:require': files});
6263
} else {

0 commit comments

Comments
 (0)