Skip to content

Commit af97d76

Browse files
committed
Fixes isExternalLibraryImport detection for PnP users
1 parent 5019cc6 commit af97d76

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/compiler/moduleNameResolver.ts

+34-8
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,11 @@ namespace ts {
288288
currentDirectory = sys.resolvePath(currentDirectory);
289289

290290
const pnpapi = getPnpApi();
291-
const locator = pnpapi.findPackageLocator(`${currentDirectory}/`);
292-
const {packageDependencies} = pnpapi.getPackageInformation(locator);
291+
292+
const currentPackage = pnpapi.findPackageLocator(`${currentDirectory}/`);
293+
Debug.assert(currentPackage);
294+
295+
const {packageDependencies} = pnpapi.getPackageInformation(currentPackage);
293296

294297
const typeRoots: string[] = [];
295298
for (const [name, referencish] of Array.from<any>(packageDependencies.entries())) {
@@ -1000,13 +1003,19 @@ namespace ts {
10001003
}
10011004

10021005
let resolvedValue = resolved.value;
1006+
1007+
const isExternalLibraryImport = resolvedValue && isPnpAvailable()
1008+
? checkPnpExternalLibraryImport(resolvedValue)
1009+
: true;
1010+
1011+
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
10031012
if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) {
10041013
const path = realPath(resolvedValue.path, host, traceEnabled);
10051014
const originalPath = path === resolvedValue.path ? undefined : resolvedValue.path;
10061015
resolvedValue = { ...resolvedValue, path, originalPath };
10071016
}
1008-
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
1009-
return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport: true } };
1017+
1018+
return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport } };
10101019
}
10111020
else {
10121021
const { path: candidate, parts } = normalizePathAndParts(combinePaths(containingDirectory, moduleName));
@@ -1585,7 +1594,13 @@ namespace ts {
15851594
}
15861595

15871596
function getPnpApi() {
1588-
return require("pnpapi");
1597+
return require("pnpapi") as {
1598+
findPackageLocator: (path: string) => ({name: string, reference: string}) | null,
1599+
resolveToUnqualified: (request: string, issuer: string, opts: {considerBuiltins: boolean}) => string,
1600+
getLocator: (name: string, reference: string) => ({name: string, reference: string}),
1601+
getPackageInformation: (locator: {name: string, reference: string}) => any,
1602+
getDependencyTreeRoots: () => any[],
1603+
};
15891604
}
15901605

15911606
function loadPnpPackageResolution(packageName: string, containingDirectory: string) {
@@ -1625,8 +1640,19 @@ namespace ts {
16251640
}
16261641
}
16271642

1628-
if (resolved) {
1629-
return toSearchResult(resolved);
1630-
}
1643+
return toSearchResult(resolved);
1644+
}
1645+
1646+
function checkPnpExternalLibraryImport(resolvedValue: Resolved) {
1647+
const pnpApi = getPnpApi();
1648+
1649+
const ownerPackage = pnpApi.findPackageLocator(resolvedValue.path);
1650+
Debug.assert(ownerPackage);
1651+
1652+
const rootLocators = pnpApi.getDependencyTreeRoots();
1653+
1654+
return rootLocators.some(root => {
1655+
return root.name === ownerPackage.name && root.reference === ownerPackage.reference;
1656+
});
16311657
}
16321658
}

0 commit comments

Comments
 (0)