Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pnp): report loaded modules in watch mode #4934

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* text=auto eol=lf

# Mark generated files as binary to prevent git from trying to merge them
/.pnp.* binary linguist-generated
packages/yarnpkg-pnp/sources/hook.js binary linguist-generated
packages/yarnpkg-core/sources/worker-zip/index.js binary linguist-generated
packages/yarnpkg-libzip/sources/libzipAsync.js binary linguist-generated
packages/yarnpkg-libzip/sources/libzipSync.js binary linguist-generated
/.pnp.* binary linguist-generated
packages/yarnpkg-pnp/sources/esm-loader/built-loader.js binary linguist-generated
packages/yarnpkg-pnp/sources/hook.js binary linguist-generated
packages/yarnpkg-core/sources/worker-zip/index.js binary linguist-generated
packages/yarnpkg-libzip/sources/libzipAsync.js binary linguist-generated
packages/yarnpkg-libzip/sources/libzipSync.js binary linguist-generated

# Set the language for these files to json5 to ensure GitHub doesn't show the comments as errors
/.vscode/*.json linguist-language=JSON5
Expand Down
7 changes: 7 additions & 0 deletions .pnp.cjs

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/cd516e64.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The following changes only affect people writing Yarn plugins:
- The patched filesystem now supports `fchown`.
- PnP now handles private import mappings.
- Updates the PnP compatibility layer for TypeScript v4.8.4 and v4.9.1-beta.
- PnP now reports loaded modules when in watch mode.

### Shell

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

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/hooks/load.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {VirtualFS, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import {fileURLToPath} from 'url';
import {fileURLToPath, pathToFileURL} from 'url';

import {HAS_JSON_IMPORT_ASSERTION_REQUIREMENT} from '../loaderFlags';
import * as loaderUtils from '../loaderUtils';
Expand Down Expand Up @@ -31,6 +32,20 @@ export async function load(
throw err;
}

// https://github.com/nodejs/node/pull/44366/files#diff-f6796082f599554ec3a29c47cf026cb24fc5104884f2632e472c05fe622d778bR477-R479
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
// 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.
process.send({
'watch:import': pathToFileURL(
npath.fromPortablePath(
VirtualFS.resolveVirtual(npath.toPortablePath(filePath)),
),
).href,
});
}

return {
format,
source: await fs.promises.readFile(filePath, `utf8`),
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions packages/yarnpkg-pnp/sources/loader/applyPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ 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 @@ -167,6 +175,8 @@ export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
const module = new Module(modulePath, parent ?? undefined) as PatchedModule;
module.pnpApiPath = moduleApiPath;

reportModuleToWatchMode(modulePath);

entry.cache[modulePath] = module;

// The main module is exposed as a global variable
Expand Down