@@ -285,8 +285,11 @@ namespace ts {
285
285
currentDirectory = sys . resolvePath ( currentDirectory ) ;
286
286
287
287
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 ) ;
290
293
291
294
const typeRoots : string [ ] = [ ] ;
292
295
for ( const [ name , referencish ] of Array . from < any > ( packageDependencies . entries ( ) ) ) {
@@ -993,13 +996,19 @@ namespace ts {
993
996
}
994
997
995
998
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.
996
1005
if ( ! compilerOptions . preserveSymlinks && resolvedValue && ! resolvedValue . originalPath ) {
997
1006
const path = realPath ( resolvedValue . path , host , traceEnabled ) ;
998
1007
const originalPath = path === resolvedValue . path ? undefined : resolvedValue . path ;
999
1008
resolvedValue = { ...resolvedValue , path, originalPath } ;
1000
1009
}
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 } } ;
1003
1012
}
1004
1013
else {
1005
1014
const { path : candidate , parts } = normalizePathAndParts ( combinePaths ( containingDirectory , moduleName ) ) ;
@@ -1578,7 +1587,13 @@ namespace ts {
1578
1587
}
1579
1588
1580
1589
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
+ } ;
1582
1597
}
1583
1598
1584
1599
function loadPnpPackageResolution ( packageName : string , containingDirectory : string ) {
@@ -1618,8 +1633,19 @@ namespace ts {
1618
1633
}
1619
1634
}
1620
1635
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
+ } ) ;
1624
1650
}
1625
1651
}
0 commit comments