From 88a10595c32309fc26eb4a7f5a858d98e883dae0 Mon Sep 17 00:00:00 2001 From: merceyz Date: Wed, 25 Aug 2021 13:19:42 +0200 Subject: [PATCH] fix: avoid direct requires to `pnpapi` --- src/compiler/moduleSpecifiers.ts | 3 ++- src/server/editorServices.ts | 3 ++- src/server/project.ts | 3 ++- src/tsserver/nodeServer.ts | 6 ++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index a1ba77f0a0500..3a2a44895e5c5 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -558,7 +558,8 @@ namespace ts.moduleSpecifiers { let packageName: string | undefined; if (!parts && typeof process.versions.pnp !== "undefined") { - const pnpApi = require("pnpapi"); + const {findPnpApi} = require("module"); + const pnpApi = findPnpApi(path); const locator = pnpApi.findPackageLocator(path); // eslint-disable-next-line no-null/no-null if (locator !== null) { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 5049c9b8c61e5..85754ad6249ce 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -4121,7 +4121,8 @@ namespace ts.server { if (typeof process.versions.pnp === "undefined") { return; } - const pnpFileName = require.resolve("pnpapi"); + const {findPnpApi} = require("module"); + const pnpFileName = findPnpApi(__filename).resolveRequest('pnpapi', null); return this.watchFactory.watchFile( pnpFileName, () => { diff --git a/src/server/project.ts b/src/server/project.ts index 74ef968ff10cc..e314f4c790690 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -2248,11 +2248,12 @@ namespace ts.server { // the PnP API. This way users don't have to know the virtual paths, // but we still support them just fine even through references. - const pnpApi = require("pnpapi"); const basePath = this.getCurrentDirectory(); + const {findPnpApi} = require("module"); const getPnpPath = (path: string) => { try { + const pnpApi = findPnpApi(`${path}/`); const targetLocator = pnpApi.findPackageLocator(`${path}/`); const {packageLocation} = pnpApi.getPackageInformation(targetLocator); const request = combinePaths(targetLocator.name, getRelativePathFromDirectory(packageLocation, path, false)); diff --git a/src/tsserver/nodeServer.ts b/src/tsserver/nodeServer.ts index 38ebd10e32119..ca8e10fd70e61 100644 --- a/src/tsserver/nodeServer.ts +++ b/src/tsserver/nodeServer.ts @@ -196,7 +196,8 @@ namespace ts.server { try { const args = [combinePaths(__dirname, "watchGuard.js"), path]; if (typeof process.versions.pnp !== "undefined") { - args.unshift('-r', require.resolve('pnpapi')); + const {findPnpApi} = require("module"); + args.unshift('-r', findPnpApi(__filename).resolveRequest('pnpapi', null)); } if (logger.hasLevel(LogLevel.verbose)) { logger.info(`Starting ${process.execPath} with args:${stringifyIndented(args)}`); @@ -511,7 +512,8 @@ namespace ts.server { } if (typeof process.versions.pnp !== "undefined") { - execArgv.unshift('-r', require.resolve('pnpapi')); + const {findPnpApi} = require("module"); + execArgv.unshift('-r', findPnpApi(__filename).resolveRequest('pnpapi', null)); } this.installer = childProcess.fork(combinePaths(__dirname, "typingsInstaller.js"), args, { execArgv });