Skip to content

Commit 841cf00

Browse files
committed
Fixes isExternalLibraryImport detection for PnP users
1 parent bba19af commit 841cf00

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

Diff for: src/compiler/moduleNameResolver.ts

+34-8
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,11 @@ namespace ts {
285285
currentDirectory = sys.resolvePath(currentDirectory);
286286

287287
const pnpapi = getPnpApi();
288-
const locator = pnpapi.findPackageLocator(`${currentDirectory}/`);
289-
const {packageDependencies} = pnpapi.getPackageInformation(locator);
288+
289+
const currentPackage = pnpapi.findPackageLocator(`${currentDirectory}/`);
290+
Debug.assert(currentPackage);
291+
292+
const {packageDependencies} = pnpapi.getPackageInformation(currentPackage);
290293

291294
const typeRoots: string[] = [];
292295
for (const [name, referencish] of Array.from<any>(packageDependencies.entries())) {
@@ -993,13 +996,19 @@ namespace ts {
993996
}
994997

995998
let resolvedValue = resolved.value;
999+
1000+
const isExternalLibraryImport = resolvedValue && isPnpAvailable()
1001+
? checkPnpExternalLibraryImport(resolvedValue)
1002+
: true;
1003+
1004+
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
9961005
if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) {
9971006
const path = realPath(resolvedValue.path, host, traceEnabled);
9981007
const originalPath = path === resolvedValue.path ? undefined : resolvedValue.path;
9991008
resolvedValue = { ...resolvedValue, path, originalPath };
10001009
}
1001-
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
1002-
return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport: true } };
1010+
1011+
return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport } };
10031012
}
10041013
else {
10051014
const { path: candidate, parts } = normalizePathAndParts(combinePaths(containingDirectory, moduleName));
@@ -1578,7 +1587,13 @@ namespace ts {
15781587
}
15791588

15801589
function getPnpApi() {
1581-
return require("pnpapi");
1590+
return require("pnpapi") as {
1591+
findPackageLocator: (path: string) => ({name: string, reference: string}) | null,
1592+
resolveToUnqualified: (request: string, issuer: string, opts: {considerBuiltins: boolean}) => string,
1593+
getLocator: (name: string, reference: string) => ({name: string, reference: string}),
1594+
getPackageInformation: (locator: {name: string, reference: string}) => any,
1595+
getDependencyTreeRoots: () => any[],
1596+
};
15821597
}
15831598

15841599
function loadPnpPackageResolution(packageName: string, containingDirectory: string) {
@@ -1618,8 +1633,19 @@ namespace ts {
16181633
}
16191634
}
16201635

1621-
if (resolved) {
1622-
return toSearchResult(resolved);
1623-
}
1636+
return toSearchResult(resolved);
1637+
}
1638+
1639+
function checkPnpExternalLibraryImport(resolvedValue: Resolved) {
1640+
const pnpApi = getPnpApi();
1641+
1642+
const ownerPackage = pnpApi.findPackageLocator(resolvedValue.path);
1643+
Debug.assert(ownerPackage);
1644+
1645+
const rootLocators = pnpApi.getDependencyTreeRoots();
1646+
1647+
return rootLocators.some(root => {
1648+
return root.name === ownerPackage.name && root.reference === ownerPackage.reference;
1649+
});
16241650
}
16251651
}

0 commit comments

Comments
 (0)