Skip to content

Commit

Permalink
fix(pnp): report missing files in watch mode (#5132)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Dec 12, 2022
1 parent 0aadf4f commit 9c1bcc8
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 41 deletions.
22 changes: 16 additions & 6 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions .pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .yarn/versions/371a1b00.yml
@@ -0,0 +1,27 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -80,6 +80,7 @@ The following changes only affect people writing Yarn plugins:

- Updates the PnP compatibility layer for TypeScript v4.9.4.
- The patched filesystem now supports `FileHandle.readLines`.
- PnP now reports missing files when in watch mode.

## 3.3.0

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions packages/yarnpkg-pnp/sources/esm-loader/hooks/load.ts
@@ -1,9 +1,9 @@
import {VirtualFS, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import {fileURLToPath, pathToFileURL} from 'url';
import {VirtualFS, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import {fileURLToPath, pathToFileURL} from 'url';

import {HAS_JSON_IMPORT_ASSERTION_REQUIREMENT} from '../loaderFlags';
import * as loaderUtils from '../loaderUtils';
import {HAS_JSON_IMPORT_ASSERTION_REQUIREMENT, WATCH_MODE_MESSAGE_USES_ARRAYS} from '../loaderFlags';
import * as loaderUtils from '../loaderUtils';

// The default `load` doesn't support reading from zip files
export async function load(
Expand Down Expand Up @@ -37,12 +37,13 @@ export async function load(
// At the time of writing Node.js reports all loaded URLs itself so
// we technically only need to do this for virtual files but in the
// event that ever changes we report everything.
const pathToSend = pathToFileURL(
npath.fromPortablePath(
VirtualFS.resolveVirtual(npath.toPortablePath(filePath)),
),
).href;
process.send({
'watch:import': pathToFileURL(
npath.fromPortablePath(
VirtualFS.resolveVirtual(npath.toPortablePath(filePath)),
),
).href,
'watch:import': WATCH_MODE_MESSAGE_USES_ARRAYS ? [pathToSend] : pathToSend,
});
}

Expand Down
3 changes: 3 additions & 0 deletions packages/yarnpkg-pnp/sources/esm-loader/loaderFlags.ts
Expand Up @@ -8,3 +8,6 @@ export const HAS_UNFLAGGED_JSON_MODULES = major > 17 || (major === 17 && minor >

// JSON modules requires import assertions after https://github.com/nodejs/node/pull/40250
export const HAS_JSON_IMPORT_ASSERTION_REQUIREMENT = major > 17 || (major === 17 && minor >= 1) || (major === 16 && minor > 14);

// The message switched to using an array in https://github.com/nodejs/node/pull/45348
export const WATCH_MODE_MESSAGE_USES_ARRAYS = major > 19 || (major === 19 && minor >= 2);
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions packages/yarnpkg-pnp/sources/loader/applyPatch.ts
Expand Up @@ -27,14 +27,6 @@ declare global {
}
}

// https://github.com/nodejs/node/pull/44366
const shouldReportRequiredModules = process.env.WATCH_REPORT_DEPENDENCIES;
function reportModuleToWatchMode(filename: NativePath) {
if (shouldReportRequiredModules && process.send) {
process.send({'watch:require': npath.fromPortablePath(VirtualFS.resolveVirtual(npath.toPortablePath(filename)))});
}
}

export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
/**
* The cache that will be used for all accesses occurring outside of a PnP context.
Expand Down Expand Up @@ -175,7 +167,7 @@ export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
const module = new Module(modulePath, parent ?? undefined) as PatchedModule;
module.pnpApiPath = moduleApiPath;

reportModuleToWatchMode(modulePath);
nodeUtils.reportRequiredFilesToWatchMode([modulePath]);

entry.cache[modulePath] = module;

Expand Down
2 changes: 2 additions & 0 deletions packages/yarnpkg-pnp/sources/loader/makeApi.ts
Expand Up @@ -830,6 +830,8 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
if (qualifiedPath) {
return ppath.normalize(qualifiedPath);
} else {
nodeUtils.reportRequiredFilesToWatchMode(candidates.map(candidate => npath.fromPortablePath(candidate)));

const unqualifiedPathForDisplay = getPathForDisplay(unqualifiedPath);

const containingPackage = findPackageLocator(unqualifiedPath);
Expand Down
25 changes: 21 additions & 4 deletions packages/yarnpkg-pnp/sources/loader/nodeUtils.ts
@@ -1,7 +1,9 @@
import {NativePath, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import {Module} from 'module';
import path from 'path';
import {NativePath, npath, VirtualFS} from '@yarnpkg/fslib';
import fs from 'fs';
import {Module} from 'module';
import path from 'path';

import {WATCH_MODE_MESSAGE_USES_ARRAYS} from '../esm-loader/loaderFlags';

// @ts-expect-error
const builtinModules = new Set(Module.builtinModules || Object.keys(process.binding(`natives`)));
Expand Down Expand Up @@ -55,3 +57,18 @@ Instead change the require of ${basename} in ${parentPath} to a dynamic import()
err.code = `ERR_REQUIRE_ESM`;
return err;
}

// https://github.com/nodejs/node/pull/44366
// https://github.com/nodejs/node/pull/45348
export function reportRequiredFilesToWatchMode(files: Array<NativePath>) {
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
files = files.map(filename => npath.fromPortablePath(VirtualFS.resolveVirtual(npath.toPortablePath(filename))));
if (WATCH_MODE_MESSAGE_USES_ARRAYS) {
process.send({'watch:require': files});
} else {
for (const filename of files) {
process.send({'watch:require': filename});
}
}
}
}

0 comments on commit 9c1bcc8

Please sign in to comment.