@@ -226,7 +226,7 @@ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
226
226
diagnostics : Diagnostic [ ] ,
227
227
state : ModuleResolutionState ,
228
228
cache : ModuleResolutionCache | NonRelativeModuleNameResolutionCache | undefined ,
229
- legacyResult ?: string ,
229
+ alternateResult ?: string ,
230
230
) : ResolvedModuleWithFailedLookupLocations {
231
231
// If this is from node_modules for non relative name, always respect preserveSymlinks
232
232
if (
@@ -248,7 +248,7 @@ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
248
248
diagnostics ,
249
249
state . resultFromCache ,
250
250
cache ,
251
- legacyResult ,
251
+ alternateResult ,
252
252
) ;
253
253
}
254
254
@@ -260,7 +260,7 @@ function createResolvedModuleWithFailedLookupLocations(
260
260
diagnostics : Diagnostic [ ] ,
261
261
resultFromCache : ResolvedModuleWithFailedLookupLocations | undefined ,
262
262
cache : ModuleResolutionCache | NonRelativeModuleNameResolutionCache | undefined ,
263
- legacyResult ?: string ,
263
+ alternateResult ?: string ,
264
264
) : ResolvedModuleWithFailedLookupLocations {
265
265
if ( resultFromCache ) {
266
266
if ( ! cache ?. isReadonly ) {
@@ -290,7 +290,7 @@ function createResolvedModuleWithFailedLookupLocations(
290
290
failedLookupLocations : initializeResolutionField ( failedLookupLocations ) ,
291
291
affectingLocations : initializeResolutionField ( affectingLocations ) ,
292
292
resolutionDiagnostics : initializeResolutionField ( diagnostics ) ,
293
- node10Result : legacyResult ,
293
+ alternateResult ,
294
294
} ;
295
295
}
296
296
function initializeResolutionField < T > ( value : T [ ] ) : T [ ] | undefined {
@@ -325,6 +325,7 @@ export interface ModuleResolutionState {
325
325
reportDiagnostic : DiagnosticReporter ;
326
326
isConfigLookup : boolean ;
327
327
candidateIsFromPackageJsonField : boolean ;
328
+ resolvedPackageDirectory : boolean ;
328
329
}
329
330
330
331
/** Just the fields that we use for module resolution.
@@ -602,6 +603,7 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
602
603
reportDiagnostic : diag => void diagnostics . push ( diag ) ,
603
604
isConfigLookup : false ,
604
605
candidateIsFromPackageJsonField : false ,
606
+ resolvedPackageDirectory : false ,
605
607
} ;
606
608
let resolved = primaryLookup ( ) ;
607
609
let primary = true ;
@@ -1839,6 +1841,7 @@ function nodeModuleNameResolverWorker(
1839
1841
reportDiagnostic : diag => void diagnostics . push ( diag ) ,
1840
1842
isConfigLookup,
1841
1843
candidateIsFromPackageJsonField : false ,
1844
+ resolvedPackageDirectory : false ,
1842
1845
} ;
1843
1846
if ( traceEnabled && moduleResolutionSupportsPackageJsonExportsAndImports ( moduleResolution ) ) {
1844
1847
trace ( host , Diagnostics . Resolving_in_0_mode_with_conditions_1 , features & NodeResolutionFeatures . EsmMode ? "ESM" : "CJS" , state . conditions . map ( c => `'${ c } '` ) . join ( ", " ) ) ;
@@ -1856,27 +1859,45 @@ function nodeModuleNameResolverWorker(
1856
1859
result = tryResolve ( extensions , state ) ;
1857
1860
}
1858
1861
1859
- // For non-relative names that resolved to JS but no types in modes that look up an "import" condition in package.json "exports",
1860
- // try again with "exports" disabled to try to detect if this is likely a configuration error in a dependency's package.json.
1861
- let legacyResult ;
1862
- if (
1863
- result ?. value ?. isExternalLibraryImport
1864
- && ! isConfigLookup
1865
- && extensions & ( Extensions . TypeScript | Extensions . Declaration )
1866
- && features & NodeResolutionFeatures . Exports
1867
- && ! isExternalModuleNameRelative ( moduleName )
1868
- && ! extensionIsOk ( Extensions . TypeScript | Extensions . Declaration , result . value . resolved . extension )
1869
- && conditions ?. includes ( "import" )
1870
- ) {
1871
- traceIfEnabled ( state , Diagnostics . Resolution_of_non_relative_name_failed_trying_with_modern_Node_resolution_features_disabled_to_see_if_npm_library_needs_configuration_update ) ;
1872
- const diagnosticState = {
1873
- ...state ,
1874
- features : state . features & ~ NodeResolutionFeatures . Exports ,
1875
- reportDiagnostic : noop ,
1876
- } ;
1877
- const diagnosticResult = tryResolve ( extensions & ( Extensions . TypeScript | Extensions . Declaration ) , diagnosticState ) ;
1878
- if ( diagnosticResult ?. value ?. isExternalLibraryImport ) {
1879
- legacyResult = diagnosticResult . value . resolved . path ;
1862
+ let alternateResult ;
1863
+ if ( state . resolvedPackageDirectory && ! isConfigLookup && ! isExternalModuleNameRelative ( moduleName ) ) {
1864
+ const wantedTypesButGotJs = result ?. value
1865
+ && extensions & ( Extensions . TypeScript | Extensions . Declaration )
1866
+ && ! extensionIsOk ( Extensions . TypeScript | Extensions . Declaration , result . value . resolved . extension ) ;
1867
+ if (
1868
+ result ?. value ?. isExternalLibraryImport
1869
+ && wantedTypesButGotJs
1870
+ && features & NodeResolutionFeatures . Exports
1871
+ && conditions ?. includes ( "import" )
1872
+ ) {
1873
+ traceIfEnabled ( state , Diagnostics . Resolution_of_non_relative_name_failed_trying_with_modern_Node_resolution_features_disabled_to_see_if_npm_library_needs_configuration_update ) ;
1874
+ const diagnosticState = {
1875
+ ...state ,
1876
+ features : state . features & ~ NodeResolutionFeatures . Exports ,
1877
+ reportDiagnostic : noop ,
1878
+ } ;
1879
+ const diagnosticResult = tryResolve ( extensions & ( Extensions . TypeScript | Extensions . Declaration ) , diagnosticState ) ;
1880
+ if ( diagnosticResult ?. value ?. isExternalLibraryImport ) {
1881
+ alternateResult = diagnosticResult . value . resolved . path ;
1882
+ }
1883
+ }
1884
+ else if (
1885
+ ( ! result ?. value || wantedTypesButGotJs )
1886
+ && moduleResolution === ModuleResolutionKind . Node10
1887
+ ) {
1888
+ traceIfEnabled ( state , Diagnostics . Resolution_of_non_relative_name_failed_trying_with_moduleResolution_bundler_to_see_if_project_may_need_configuration_update ) ;
1889
+ const diagnosticsCompilerOptions = { ...state . compilerOptions , moduleResolution : ModuleResolutionKind . Bundler } ;
1890
+ const diagnosticState = {
1891
+ ...state ,
1892
+ compilerOptions : diagnosticsCompilerOptions ,
1893
+ features : NodeResolutionFeatures . BundlerDefault ,
1894
+ conditions : getConditions ( diagnosticsCompilerOptions ) ,
1895
+ reportDiagnostic : noop ,
1896
+ } ;
1897
+ const diagnosticResult = tryResolve ( extensions & ( Extensions . TypeScript | Extensions . Declaration ) , diagnosticState ) ;
1898
+ if ( diagnosticResult ?. value ?. isExternalLibraryImport ) {
1899
+ alternateResult = diagnosticResult . value . resolved . path ;
1900
+ }
1880
1901
}
1881
1902
}
1882
1903
@@ -1889,7 +1910,7 @@ function nodeModuleNameResolverWorker(
1889
1910
diagnostics ,
1890
1911
state ,
1891
1912
cache ,
1892
- legacyResult ,
1913
+ alternateResult ,
1893
1914
) ;
1894
1915
1895
1916
function tryResolve ( extensions : Extensions , state : ModuleResolutionState ) : SearchResult < { resolved : Resolved ; isExternalLibraryImport : boolean ; } > {
@@ -2350,6 +2371,7 @@ export function getTemporaryModuleResolutionState(packageJsonInfoCache: PackageJ
2350
2371
reportDiagnostic : noop ,
2351
2372
isConfigLookup : false ,
2352
2373
candidateIsFromPackageJsonField : false ,
2374
+ resolvedPackageDirectory : false ,
2353
2375
} ;
2354
2376
}
2355
2377
@@ -3072,6 +3094,9 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
3072
3094
// Previous `packageInfo` may have been from a nested package.json; ensure we have the one from the package root now.
3073
3095
packageInfo = rootPackageInfo ?? getPackageJsonInfo ( packageDirectory , ! nodeModulesDirectoryExists , state ) ;
3074
3096
}
3097
+ if ( packageInfo ) {
3098
+ state . resolvedPackageDirectory = true ;
3099
+ }
3075
3100
// package exports are higher priority than file/directory/typesVersions lookups and (and, if there's exports present, blocks them)
3076
3101
if ( packageInfo && packageInfo . contents . packageJsonContent . exports && state . features & NodeResolutionFeatures . Exports ) {
3077
3102
return loadModuleFromExports ( packageInfo , extensions , combinePaths ( "." , rest ) , state , cache , redirectedReference ) ?. value ;
@@ -3202,6 +3227,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
3202
3227
reportDiagnostic : diag => void diagnostics . push ( diag ) ,
3203
3228
isConfigLookup : false ,
3204
3229
candidateIsFromPackageJsonField : false ,
3230
+ resolvedPackageDirectory : false ,
3205
3231
} ;
3206
3232
3207
3233
const resolved = tryResolve ( Extensions . TypeScript | Extensions . Declaration ) ||
@@ -3302,6 +3328,7 @@ export function loadModuleFromGlobalCache(moduleName: string, projectName: strin
3302
3328
reportDiagnostic : diag => void diagnostics . push ( diag ) ,
3303
3329
isConfigLookup : false ,
3304
3330
candidateIsFromPackageJsonField : false ,
3331
+ resolvedPackageDirectory : false ,
3305
3332
} ;
3306
3333
const resolved = loadModuleFromImmediateNodeModulesDirectory ( Extensions . Declaration , moduleName , globalCache , state , /*typesScopeOnly*/ false , /*cache*/ undefined , /*redirectedReference*/ undefined ) ;
3307
3334
return createResolvedModuleWithFailedLookupLocations (
0 commit comments