@@ -288,8 +288,11 @@ namespace ts {
288
288
currentDirectory = sys . resolvePath ( currentDirectory ) ;
289
289
290
290
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 ) ;
293
296
294
297
const typeRoots : string [ ] = [ ] ;
295
298
for ( const [ name , referencish ] of Array . from < any > ( packageDependencies . entries ( ) ) ) {
@@ -1000,13 +1003,19 @@ namespace ts {
1000
1003
}
1001
1004
1002
1005
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.
1003
1012
if ( ! compilerOptions . preserveSymlinks && resolvedValue && ! resolvedValue . originalPath ) {
1004
1013
const path = realPath ( resolvedValue . path , host , traceEnabled ) ;
1005
1014
const originalPath = path === resolvedValue . path ? undefined : resolvedValue . path ;
1006
1015
resolvedValue = { ...resolvedValue , path, originalPath } ;
1007
1016
}
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 } } ;
1010
1019
}
1011
1020
else {
1012
1021
const { path : candidate , parts } = normalizePathAndParts ( combinePaths ( containingDirectory , moduleName ) ) ;
@@ -1585,7 +1594,13 @@ namespace ts {
1585
1594
}
1586
1595
1587
1596
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
+ } ;
1589
1604
}
1590
1605
1591
1606
function loadPnpPackageResolution ( packageName : string , containingDirectory : string ) {
@@ -1625,8 +1640,19 @@ namespace ts {
1625
1640
}
1626
1641
}
1627
1642
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
+ } ) ;
1631
1657
}
1632
1658
}
0 commit comments