@@ -404,7 +404,10 @@ namespace ts {
404404 }
405405 let result : Resolved | undefined ;
406406 if ( ! isExternalModuleNameRelative ( typeReferenceDirectiveName ) ) {
407- const searchResult = loadModuleFromNearestNodeModulesDirectory ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , moduleResolutionState , /*cache*/ undefined , /*redirectedReference*/ undefined ) ;
407+ const searchResult = isPnpAvailable ( )
408+ ? tryLoadModuleUsingPnpResolution ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , moduleResolutionState )
409+ : loadModuleFromNearestNodeModulesDirectory ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , moduleResolutionState , /*cache*/ undefined , /*redirectedReference*/ undefined ) ;
410+
408411 result = searchResult && searchResult . value ;
409412 }
410413 else {
@@ -1579,29 +1582,31 @@ namespace ts {
15791582 return require ( "pnpapi" ) ;
15801583 }
15811584
1582- function loadPnpPackageResolution ( packageName : string , issuer : string ) {
1583- return getPnpApi ( ) . resolveToUnqualified ( packageName , issuer , { considerBuiltins : false } ) ;
1585+ function loadPnpPackageResolution ( packageName : string , containingDirectory : string ) {
1586+ try {
1587+ return getPnpApi ( ) . resolveToUnqualified ( packageName , `${ containingDirectory } /` , { considerBuiltins : false } ) ;
1588+ } catch { }
15841589 }
15851590
1586- function loadPnpTypePackageResolution ( packageName : string , issuer : string ) {
1587- return loadPnpPackageResolution ( getTypesPackageName ( packageName ) , issuer ) ;
1591+ function loadPnpTypePackageResolution ( packageName : string , containingDirectory : string ) {
1592+ return loadPnpPackageResolution ( getTypesPackageName ( packageName ) , containingDirectory ) ;
15881593 }
15891594
15901595 /* @internal */
1591- export function tryLoadModuleUsingPnpResolution ( extensions : Extensions , moduleName : string , issuer : string , state : ModuleResolutionState ) {
1596+ export function tryLoadModuleUsingPnpResolution ( extensions : Extensions , moduleName : string , containingDirectory : string , state : ModuleResolutionState ) {
15921597 const { packageName, rest} = parsePackageName ( moduleName ) ;
15931598
1594- const packageResolution = loadPnpPackageResolution ( packageName , issuer ) ;
1595- const packageFullResolution = packageResolution !== null
1599+ const packageResolution = loadPnpPackageResolution ( packageName , containingDirectory ) ;
1600+ const packageFullResolution = packageResolution
15961601 ? nodeLoadModuleByRelativeName ( extensions , combinePaths ( packageResolution , rest ) , /*onlyRecordFailures*/ false , state , /*considerPackageJson*/ true )
15971602 : undefined ;
15981603
15991604 let resolved ;
16001605 if ( packageFullResolution ) {
16011606 resolved = packageFullResolution ;
16021607 } else if ( extensions === Extensions . TypeScript || extensions === Extensions . DtsOnly ) {
1603- const typePackageResolution = loadPnpTypePackageResolution ( packageName , issuer ) ;
1604- const typePackageFullResolution = typePackageResolution !== null
1608+ const typePackageResolution = loadPnpTypePackageResolution ( packageName , containingDirectory ) ;
1609+ const typePackageFullResolution = typePackageResolution
16051610 ? nodeLoadModuleByRelativeName ( Extensions . DtsOnly , combinePaths ( typePackageResolution , rest ) , /*onlyRecordFailures*/ false , state , /*considerPackageJson*/ true )
16061611 : undefined ;
16071612
0 commit comments