Skip to content

Commit

Permalink
fix: avoid direct requires to pnpapi
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Aug 26, 2021
1 parent 83a9f88 commit 88a1059
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
() => {
Expand Down
3 changes: 2 additions & 1 deletion src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions src/tsserver/nodeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 88a1059

Please sign in to comment.