diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 47288fdde3259..07e2f1d573711 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -814,6 +814,7 @@ import { nodeIsPresent, nodeIsSynthesized, NodeLinks, + nodeModulesPathPart, nodeStartsNewLexicalEnvironment, NodeWithTypeArguments, NonNullChain, @@ -4765,7 +4766,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (sourceFile.symbol) { if (resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) { - errorOnImplicitAnyModule(/*isError*/ false, errorNode, resolvedModule, moduleReference); + errorOnImplicitAnyModule(/*isError*/ false, errorNode, currentSourceFile, mode, resolvedModule, moduleReference); } if (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext) { const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration); @@ -4853,7 +4854,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { error(errorNode, diag, moduleReference, resolvedModule!.resolvedFileName); } else { - errorOnImplicitAnyModule(/*isError*/ noImplicitAny && !!moduleNotFoundError, errorNode, resolvedModule!, moduleReference); + errorOnImplicitAnyModule(/*isError*/ noImplicitAny && !!moduleNotFoundError, errorNode, currentSourceFile, mode, resolvedModule!, moduleReference); } // Failed imports and untyped modules are both treated in an untyped manner; only difference is whether we give a diagnostic first. return undefined; @@ -4923,25 +4924,33 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } } - function errorOnImplicitAnyModule(isError: boolean, errorNode: Node, { packageId, resolvedFileName }: ResolvedModuleFull, moduleReference: string): void { - const errorInfo = !isExternalModuleNameRelative(moduleReference) && packageId - ? typesPackageExists(packageId.name) + function errorOnImplicitAnyModule(isError: boolean, errorNode: Node, sourceFile: SourceFile, mode: ResolutionMode, { packageId, resolvedFileName }: ResolvedModuleFull, moduleReference: string): void { + let errorInfo; + if (!isExternalModuleNameRelative(moduleReference) && packageId) { + const legacyResult = sourceFile.resolvedModules?.get(moduleReference, mode)?.legacyResult; + errorInfo = legacyResult ? chainDiagnosticMessages( /*details*/ undefined, - Diagnostics.If_the_0_package_actually_exposes_this_module_consider_sending_a_pull_request_to_amend_https_Colon_Slash_Slashgithub_com_SlashDefinitelyTyped_SlashDefinitelyTyped_Slashtree_Slashmaster_Slashtypes_Slash_1, - packageId.name, mangleScopedPackageName(packageId.name)) - : packageBundlesTypes(packageId.name) + Diagnostics.There_are_types_at_0_but_this_result_could_not_be_resolved_when_respecting_package_json_exports_The_1_library_may_need_to_update_its_package_json, + legacyResult, + legacyResult.indexOf(nodeModulesPathPart + "@types/") > -1 ? `@types/${mangleScopedPackageName(packageId.name)}` : packageId.name) + : typesPackageExists(packageId.name) ? chainDiagnosticMessages( /*details*/ undefined, - Diagnostics.If_the_0_package_actually_exposes_this_module_try_adding_a_new_declaration_d_ts_file_containing_declare_module_1, - packageId.name, - moduleReference) - : chainDiagnosticMessages( - /*details*/ undefined, - Diagnostics.Try_npm_i_save_dev_types_Slash_1_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0, - moduleReference, - mangleScopedPackageName(packageId.name)) - : undefined; + Diagnostics.If_the_0_package_actually_exposes_this_module_consider_sending_a_pull_request_to_amend_https_Colon_Slash_Slashgithub_com_SlashDefinitelyTyped_SlashDefinitelyTyped_Slashtree_Slashmaster_Slashtypes_Slash_1, + packageId.name, mangleScopedPackageName(packageId.name)) + : packageBundlesTypes(packageId.name) + ? chainDiagnosticMessages( + /*details*/ undefined, + Diagnostics.If_the_0_package_actually_exposes_this_module_try_adding_a_new_declaration_d_ts_file_containing_declare_module_1, + packageId.name, + moduleReference) + : chainDiagnosticMessages( + /*details*/ undefined, + Diagnostics.Try_npm_i_save_dev_types_Slash_1_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0, + moduleReference, + mangleScopedPackageName(packageId.name)); + } errorOrSuggestion(isError, errorNode, chainDiagnosticMessages( errorInfo, Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type, diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 1010bd171c63a..7e3f7b80f0f5f 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -964,7 +964,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [ type: new Map(Object.entries({ // N.B. The first entry specifies the value shown in `tsc --init` node10: ModuleResolutionKind.Node10, - node: ModuleResolutionKind.Node10, + node: ModuleResolutionKind.Bundler, classic: ModuleResolutionKind.Classic, node16: ModuleResolutionKind.Node16, nodenext: ModuleResolutionKind.NodeNext, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7573cbc29eaf3..85adb10def96b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -5219,6 +5219,14 @@ "category": "Message", "code": 6276 }, + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.": { + "category": "Message", + "code": 6277 + }, + "There are types at '{0}', but this result could not be resolved when respecting package.json \"exports\". The '{1}' library may need to update its package.json.": { + "category": "Message", + "code": 6278 + }, "Enable project compilation": { "category": "Message", diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index be8573b075db3..0125fa3503ce8 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -214,7 +214,8 @@ function createResolvedModuleWithFailedLookupLocations( failedLookupLocations: string[], affectingLocations: string[], diagnostics: Diagnostic[], - resultFromCache: ResolvedModuleWithFailedLookupLocations | undefined + resultFromCache: ResolvedModuleWithFailedLookupLocations | undefined, + legacyResult?: string, ): ResolvedModuleWithFailedLookupLocations { if (resultFromCache) { resultFromCache.failedLookupLocations = updateResolutionField(resultFromCache.failedLookupLocations, failedLookupLocations); @@ -234,6 +235,7 @@ function createResolvedModuleWithFailedLookupLocations( failedLookupLocations: initializeResolutionField(failedLookupLocations), affectingLocations: initializeResolutionField(affectingLocations), resolutionDiagnostics: initializeResolutionField(diagnostics), + legacyResult, }; } function initializeResolutionField(value: T[]): T[] | undefined { @@ -1665,12 +1667,37 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa const priorityExtensions = extensions & (Extensions.TypeScript | Extensions.Declaration); const secondaryExtensions = extensions & ~(Extensions.TypeScript | Extensions.Declaration); result = - priorityExtensions && tryResolve(priorityExtensions) || - secondaryExtensions && tryResolve(secondaryExtensions) || + priorityExtensions && tryResolve(priorityExtensions, state) || + secondaryExtensions && tryResolve(secondaryExtensions, state) || undefined; } else { - result = tryResolve(extensions); + result = tryResolve(extensions, state); + } + + // For non-relative names that resolved to JS but no types in modes that look up an "import" condition in package.json "exports", + // try again with "exports" disabled to try to detect if this is likely a configuration error in a dependency's package.json. + let legacyResult; + if (result?.value?.isExternalLibraryImport + && !isConfigLookup + && extensions & (Extensions.TypeScript | Extensions.Declaration) + && features & NodeResolutionFeatures.Exports + && !isExternalModuleNameRelative(moduleName) + && !extensionIsOk(Extensions.TypeScript | Extensions.Declaration, result.value.resolved.extension) + && conditions.indexOf("import") > -1 + ) { + traceIfEnabled(state, Diagnostics.Resolution_of_non_relative_name_failed_trying_with_modern_Node_resolution_features_disabled_to_see_if_npm_library_needs_configuration_update); + const diagnosticState = { + ...state, + features: state.features & ~NodeResolutionFeatures.Exports, + failedLookupLocations: [], + affectingLocations: [], + reportDiagnostic: noop, + }; + const diagnosticResult = tryResolve(extensions & (Extensions.TypeScript | Extensions.Declaration), diagnosticState); + if (diagnosticResult?.value?.isExternalLibraryImport) { + legacyResult = diagnosticResult.value.resolved.path; + } } return createResolvedModuleWithFailedLookupLocations( @@ -1679,10 +1706,11 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa failedLookupLocations, affectingLocations, diagnostics, - state.resultFromCache + state.resultFromCache, + legacyResult, ); - function tryResolve(extensions: Extensions): SearchResult<{ resolved: Resolved, isExternalLibraryImport: boolean }> { + function tryResolve(extensions: Extensions, state: ModuleResolutionState): SearchResult<{ resolved: Resolved, isExternalLibraryImport: boolean }> { const loader: ResolutionKindSpecificLoader = (extensions, candidate, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ true); const resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, state); if (resolved) { @@ -2756,27 +2784,31 @@ function loadModuleFromImmediateNodeModulesDirectory(extensions: Extensions, mod function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, moduleName: string, nodeModulesDirectory: string, nodeModulesDirectoryExists: boolean, state: ModuleResolutionState, cache: ModuleResolutionCache | undefined, redirectedReference: ResolvedProjectReference | undefined): Resolved | undefined { const candidate = normalizePath(combinePaths(nodeModulesDirectory, moduleName)); + const { packageName, rest } = parsePackageName(moduleName); + const packageDirectory = combinePaths(nodeModulesDirectory, packageName); + let rootPackageInfo: PackageJsonInfo | undefined; // First look for a nested package.json, as in `node_modules/foo/bar/package.json`. let packageInfo = getPackageJsonInfo(candidate, !nodeModulesDirectoryExists, state); // But only if we're not respecting export maps (if we are, we might redirect around this location) - if (!(state.features & NodeResolutionFeatures.Exports)) { - if (packageInfo) { - const fromFile = loadModuleFromFile(extensions, candidate, !nodeModulesDirectoryExists, state); - if (fromFile) { - return noPackageId(fromFile); - } - - const fromDirectory = loadNodeModuleFromDirectoryWorker( - extensions, - candidate, - !nodeModulesDirectoryExists, - state, - packageInfo.contents.packageJsonContent, - getVersionPathsOfPackageJsonInfo(packageInfo, state), - ); - return withPackageId(packageInfo, fromDirectory); + if (rest !== "" && packageInfo && ( + !(state.features & NodeResolutionFeatures.Exports) || + !hasProperty((rootPackageInfo = getPackageJsonInfo(packageDirectory, !nodeModulesDirectoryExists, state))?.contents.packageJsonContent ?? emptyArray, "exports") + )) { + const fromFile = loadModuleFromFile(extensions, candidate, !nodeModulesDirectoryExists, state); + if (fromFile) { + return noPackageId(fromFile); } + + const fromDirectory = loadNodeModuleFromDirectoryWorker( + extensions, + candidate, + !nodeModulesDirectoryExists, + state, + packageInfo.contents.packageJsonContent, + getVersionPathsOfPackageJsonInfo(packageInfo, state), + ); + return withPackageId(packageInfo, fromDirectory); } const loader: ResolutionKindSpecificLoader = (extensions, candidate, onlyRecordFailures, state) => { @@ -2803,11 +2835,9 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu return withPackageId(packageInfo, pathAndExtension); }; - const { packageName, rest } = parsePackageName(moduleName); - const packageDirectory = combinePaths(nodeModulesDirectory, packageName); if (rest !== "") { // Previous `packageInfo` may have been from a nested package.json; ensure we have the one from the package root now. - packageInfo = getPackageJsonInfo(packageDirectory, !nodeModulesDirectoryExists, state); + packageInfo = rootPackageInfo ?? getPackageJsonInfo(packageDirectory, !nodeModulesDirectoryExists, state); } // package exports are higher priority than file/directory/typesVersions lookups and (and, if there's exports present, blocks them) if (packageInfo && packageInfo.contents.packageJsonContent.exports && state.features & NodeResolutionFeatures.Exports) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 44a716a516f7a..2663d678c3d50 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -7605,6 +7605,8 @@ export interface ResolvedModuleWithFailedLookupLocations { affectingLocations?: string[]; /** @internal */ resolutionDiagnostics?: Diagnostic[] + /** @internal */ + legacyResult?: string; } export interface ResolvedTypeReferenceDirective { diff --git a/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=bundler).trace.json b/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=bundler).trace.json new file mode 100644 index 0000000000000..b6dd002185a28 --- /dev/null +++ b/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=bundler).trace.json @@ -0,0 +1,17 @@ +[ + "======== Resolving module '@restart/hooks/useMergedRefs' from '/main.ts'. ========", + "Explicitly specified module resolution kind: 'Bundler'.", + "File '/package.json' does not exist.", + "Loading module '@restart/hooks/useMergedRefs' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.", + "Found 'package.json' at '/node_modules/@restart/hooks/useMergedRefs/package.json'.", + "Found 'package.json' at '/node_modules/@restart/hooks/package.json'.", + "File '/node_modules/@restart/hooks/useMergedRefs.ts' does not exist.", + "File '/node_modules/@restart/hooks/useMergedRefs.tsx' does not exist.", + "File '/node_modules/@restart/hooks/useMergedRefs.d.ts' does not exist.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field '../esm/useMergedRefs.d.ts' that references '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'.", + "File '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts', result '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'.", + "======== Module name '@restart/hooks/useMergedRefs' was successfully resolved to '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'. ========" +] \ No newline at end of file diff --git a/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=node16).trace.json b/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=node16).trace.json new file mode 100644 index 0000000000000..4d8ad876095fe --- /dev/null +++ b/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=node16).trace.json @@ -0,0 +1,31 @@ +[ + "File '/node_modules/@restart/hooks/esm/package.json' does not exist.", + "Found 'package.json' at '/node_modules/@restart/hooks/package.json'.", + "File '/package.json' does not exist.", + "======== Resolving module '@restart/hooks/useMergedRefs' from '/main.ts'. ========", + "Explicitly specified module resolution kind: 'Node16'.", + "Resolving in CJS mode with conditions 'node', 'require', 'types'.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module '@restart/hooks/useMergedRefs' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.", + "Found 'package.json' at '/node_modules/@restart/hooks/useMergedRefs/package.json'.", + "File '/node_modules/@restart/hooks/package.json' exists according to earlier cached lookups.", + "File '/node_modules/@restart/hooks/useMergedRefs.ts' does not exist.", + "File '/node_modules/@restart/hooks/useMergedRefs.tsx' does not exist.", + "File '/node_modules/@restart/hooks/useMergedRefs.d.ts' does not exist.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field '../esm/useMergedRefs.d.ts' that references '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'.", + "File '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts', result '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'.", + "======== Module name '@restart/hooks/useMergedRefs' was successfully resolved to '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'. ========", + "File 'package.json' does not exist.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups." +] \ No newline at end of file diff --git a/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=nodenext).trace.json b/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=nodenext).trace.json new file mode 100644 index 0000000000000..77e7411b6622e --- /dev/null +++ b/tests/baselines/reference/nestedPackageJsonRedirect(moduleresolution=nodenext).trace.json @@ -0,0 +1,31 @@ +[ + "File '/node_modules/@restart/hooks/esm/package.json' does not exist.", + "Found 'package.json' at '/node_modules/@restart/hooks/package.json'.", + "File '/package.json' does not exist.", + "======== Resolving module '@restart/hooks/useMergedRefs' from '/main.ts'. ========", + "Explicitly specified module resolution kind: 'NodeNext'.", + "Resolving in CJS mode with conditions 'node', 'require', 'types'.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module '@restart/hooks/useMergedRefs' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.", + "Found 'package.json' at '/node_modules/@restart/hooks/useMergedRefs/package.json'.", + "File '/node_modules/@restart/hooks/package.json' exists according to earlier cached lookups.", + "File '/node_modules/@restart/hooks/useMergedRefs.ts' does not exist.", + "File '/node_modules/@restart/hooks/useMergedRefs.tsx' does not exist.", + "File '/node_modules/@restart/hooks/useMergedRefs.d.ts' does not exist.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field '../esm/useMergedRefs.d.ts' that references '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'.", + "File '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts', result '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'.", + "======== Module name '@restart/hooks/useMergedRefs' was successfully resolved to '/node_modules/@restart/hooks/esm/useMergedRefs.d.ts'. ========", + "File 'package.json' does not exist.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups." +] \ No newline at end of file diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt index 22d2482ced9ae..aa450c5fc0c6a 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt @@ -8,7 +8,9 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J /main.mts(1,16): error TS7016: Could not find a declaration file for module 'exports-and-types-versions/foo'. '/node_modules/exports-and-types-versions/dist/foo.js' implicitly has an 'any' type. If the 'exports-and-types-versions' package actually exposes this module, try adding a new declaration (.d.ts) file containing `declare module 'exports-and-types-versions/foo';` /main.mts(2,16): error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. + There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. /main.mts(5,16): error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. + There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. !!! error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? @@ -83,10 +85,12 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J import {} from "exports-and-types-versions/nope"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. +!!! error TS2307: There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. import {} from "exports-and-types-versions/yep"; import {} from "exports-and-types-versions/versioned-yep"; import {} from "exports-and-types-versions/versioned-nah"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. +!!! error TS2307: There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. import {} from "just-types-versions/foo"; \ No newline at end of file diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json index 8c7abd981db3c..ab46ab33214e3 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json @@ -110,6 +110,16 @@ "Directory '/node_modules/@types' does not exist, skipping all lookups in it.", "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", "Export specifier './nope' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'exports-and-types-versions/nope' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", + "'package.json' has a 'typesVersions' field with version-specific path mappings.", + "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'nope'.", + "Module name 'nope', matched pattern 'nope'.", + "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.", + "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.", "======== Module name 'exports-and-types-versions/nope' was not resolved. ========", "======== Resolving module 'exports-and-types-versions/yep' from '/main.mts'. ========", "Module resolution kind is not specified, using 'Node16'.", @@ -145,6 +155,15 @@ "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", "Saw non-matching condition 'types@<4'.", "Export specifier './versioned-nah' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'exports-and-types-versions/versioned-nah' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", + "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'versioned-nah'.", + "Module name 'versioned-nah', matched pattern 'versioned-nah'.", + "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.", + "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.", "======== Module name 'exports-and-types-versions/versioned-nah' was not resolved. ========", "======== Resolving module 'just-types-versions/foo' from '/main.mts'. ========", "Module resolution kind is not specified, using 'Node16'.", diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).errors.txt b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).errors.txt index 22d2482ced9ae..aa450c5fc0c6a 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).errors.txt +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).errors.txt @@ -8,7 +8,9 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J /main.mts(1,16): error TS7016: Could not find a declaration file for module 'exports-and-types-versions/foo'. '/node_modules/exports-and-types-versions/dist/foo.js' implicitly has an 'any' type. If the 'exports-and-types-versions' package actually exposes this module, try adding a new declaration (.d.ts) file containing `declare module 'exports-and-types-versions/foo';` /main.mts(2,16): error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. + There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. /main.mts(5,16): error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. + There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. !!! error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? @@ -83,10 +85,12 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J import {} from "exports-and-types-versions/nope"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. +!!! error TS2307: There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. import {} from "exports-and-types-versions/yep"; import {} from "exports-and-types-versions/versioned-yep"; import {} from "exports-and-types-versions/versioned-nah"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. +!!! error TS2307: There are types at '/node_modules/exports-and-types-versions/types/foo.d.ts', but this result could not be resolved when respecting package.json "exports". The 'exports-and-types-versions' library may need to update its package.json. import {} from "just-types-versions/foo"; \ No newline at end of file diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json index 017892d356c35..7274f2bc1a95b 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json @@ -110,6 +110,16 @@ "Directory '/node_modules/@types' does not exist, skipping all lookups in it.", "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", "Export specifier './nope' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'exports-and-types-versions/nope' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", + "'package.json' has a 'typesVersions' field with version-specific path mappings.", + "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'nope'.", + "Module name 'nope', matched pattern 'nope'.", + "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.", + "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.", "======== Module name 'exports-and-types-versions/nope' was not resolved. ========", "======== Resolving module 'exports-and-types-versions/yep' from '/main.mts'. ========", "Module resolution kind is not specified, using 'NodeNext'.", @@ -145,6 +155,15 @@ "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", "Saw non-matching condition 'types@<4'.", "Export specifier './versioned-nah' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'exports-and-types-versions/versioned-nah' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.", + "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'versioned-nah'.", + "Module name 'versioned-nah', matched pattern 'versioned-nah'.", + "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.", + "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.", "======== Module name 'exports-and-types-versions/versioned-nah' was not resolved. ========", "======== Resolving module 'just-types-versions/foo' from '/main.mts'. ========", "Module resolution kind is not specified, using 'NodeNext'.", diff --git a/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=node16).errors.txt b/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=node16).errors.txt index 568ce306b98fd..2fef133f536b8 100644 --- a/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=node16).errors.txt +++ b/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=node16).errors.txt @@ -1,5 +1,6 @@ error TS2468: Cannot find global value 'Promise'. tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations. + There are types at 'tests/cases/conformance/node/node_modules/inner/other.d.ts', but this result could not be resolved when respecting package.json "exports". The 'inner' library may need to update its package.json. tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher. tests/cases/conformance/node/index.ts(3,25): error TS2712: A dynamic import call in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your '--lib' option. @@ -10,6 +11,7 @@ tests/cases/conformance/node/index.ts(3,25): error TS2712: A dynamic import call import { Thing } from "inner/other.js"; // should fail ~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations. +!!! error TS2307: There are types at 'tests/cases/conformance/node/node_modules/inner/other.d.ts', but this result could not be resolved when respecting package.json "exports". The 'inner' library may need to update its package.json. export const a = (await import("inner")).x(); ~~~~~ !!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher. diff --git a/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=nodenext).errors.txt b/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=nodenext).errors.txt index 568ce306b98fd..2fef133f536b8 100644 --- a/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=nodenext).errors.txt +++ b/tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=nodenext).errors.txt @@ -1,5 +1,6 @@ error TS2468: Cannot find global value 'Promise'. tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations. + There are types at 'tests/cases/conformance/node/node_modules/inner/other.d.ts', but this result could not be resolved when respecting package.json "exports". The 'inner' library may need to update its package.json. tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher. tests/cases/conformance/node/index.ts(3,25): error TS2712: A dynamic import call in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your '--lib' option. @@ -10,6 +11,7 @@ tests/cases/conformance/node/index.ts(3,25): error TS2712: A dynamic import call import { Thing } from "inner/other.js"; // should fail ~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations. +!!! error TS2307: There are types at 'tests/cases/conformance/node/node_modules/inner/other.d.ts', but this result could not be resolved when respecting package.json "exports". The 'inner' library may need to update its package.json. export const a = (await import("inner")).x(); ~~~~~ !!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher. diff --git a/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=bundler).errors.txt b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=bundler).errors.txt new file mode 100644 index 0000000000000..ef8ea52f327c4 --- /dev/null +++ b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=bundler).errors.txt @@ -0,0 +1,96 @@ +error TS6504: File '/node_modules/bar/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/node_modules/bar/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/node_modules/foo/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/node_modules/foo/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +/index.mts(1,21): error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.mjs' implicitly has an 'any' type. + There are types at '/node_modules/foo/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'foo' library may need to update its package.json. +/index.mts(2,21): error TS7016: Could not find a declaration file for module 'bar'. '/node_modules/bar/index.mjs' implicitly has an 'any' type. + There are types at '/node_modules/@types/bar/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@types/bar' library may need to update its package.json. + + +!!! error TS6504: File '/node_modules/bar/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/node_modules/bar/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/node_modules/foo/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/node_modules/foo/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +==== /node_modules/foo/package.json (0 errors) ==== + { + "name": "foo", + "version": "1.0.0", + "main": "index.js", + "types": "index.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } + } + +==== /node_modules/foo/index.js (0 errors) ==== + module.exports = { foo: 1 }; + +==== /node_modules/foo/index.mjs (0 errors) ==== + export const foo = 1; + +==== /node_modules/foo/index.d.ts (0 errors) ==== + export declare const foo: number; + +==== /node_modules/@types/bar/package.json (0 errors) ==== + { + "name": "@types/bar", + "version": "1.0.0", + "types": "index.d.ts", + "exports": { + ".": { + "require": "./index.d.ts" + } + } + } + +==== /node_modules/@types/bar/index.d.ts (0 errors) ==== + export declare const bar: number; + +==== /node_modules/bar/package.json (0 errors) ==== + { + "name": "bar", + "version": "1.0.0", + "main": "index.js", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } + } + +==== /node_modules/bar/index.js (0 errors) ==== + module.exports = { bar: 1 }; + +==== /node_modules/bar/index.mjs (0 errors) ==== + export const bar = 1; + +==== /index.mts (2 errors) ==== + import { foo } from "foo"; + ~~~~~ +!!! error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.mjs' implicitly has an 'any' type. +!!! error TS7016: There are types at '/node_modules/foo/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'foo' library may need to update its package.json. + import { bar } from "bar"; + ~~~~~ +!!! error TS7016: Could not find a declaration file for module 'bar'. '/node_modules/bar/index.mjs' implicitly has an 'any' type. +!!! error TS7016: There are types at '/node_modules/@types/bar/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@types/bar' library may need to update its package.json. \ No newline at end of file diff --git a/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=bundler).trace.json b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=bundler).trace.json new file mode 100644 index 0000000000000..2859c27f86107 --- /dev/null +++ b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=bundler).trace.json @@ -0,0 +1,93 @@ +[ + "======== Resolving module 'foo' from '/index.mts'. ========", + "Explicitly specified module resolution kind: 'Bundler'.", + "File '/package.json' does not exist.", + "Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.", + "Found 'package.json' at '/node_modules/foo/package.json'.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/foo/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/foo/index.mts' does not exist.", + "File '/node_modules/foo/index.d.mts' does not exist.", + "Saw non-matching condition 'require'.", + "File '/node_modules/@types/foo.d.ts' does not exist.", + "File '/node_modules/foo/package.json' exists according to earlier cached lookups.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/foo/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/foo/index.mjs' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/foo/index.mjs', result '/node_modules/foo/index.mjs'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/foo/package.json' exists according to earlier cached lookups.", + "File '/node_modules/foo.ts' does not exist.", + "File '/node_modules/foo.tsx' does not exist.", + "File '/node_modules/foo.d.ts' does not exist.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field 'index.d.ts' that references '/node_modules/foo/index.d.ts'.", + "File '/node_modules/foo/index.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/foo/index.d.ts', result '/node_modules/foo/index.d.ts'.", + "======== Module name 'foo' was successfully resolved to '/node_modules/foo/index.mjs' with Package ID 'foo/index.mjs@1.0.0'. ========", + "======== Resolving module 'bar' from '/index.mts'. ========", + "Explicitly specified module resolution kind: 'Bundler'.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'bar' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.", + "Found 'package.json' at '/node_modules/bar/package.json'.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/bar/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/bar/index.mts' does not exist.", + "File '/node_modules/bar/index.d.mts' does not exist.", + "Saw non-matching condition 'require'.", + "Found 'package.json' at '/node_modules/@types/bar/package.json'.", + "Saw non-matching condition 'require'.", + "File '/node_modules/bar/package.json' exists according to earlier cached lookups.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/bar/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/bar/index.mjs' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/bar/index.mjs', result '/node_modules/bar/index.mjs'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'bar' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/bar/package.json' exists according to earlier cached lookups.", + "File '/node_modules/bar.ts' does not exist.", + "File '/node_modules/bar.tsx' does not exist.", + "File '/node_modules/bar.d.ts' does not exist.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' does not have a 'types' field.", + "'package.json' has 'main' field 'index.js' that references '/node_modules/bar/index.js'.", + "File '/node_modules/bar/index.js' exist - use it as a name resolution result.", + "File '/node_modules/bar/index.js' has an unsupported extension, so skipping it.", + "Loading module as file / folder, candidate module location '/node_modules/bar/index.js', target file types: TypeScript, Declaration.", + "File name '/node_modules/bar/index.js' has a '.js' extension - stripping it.", + "File '/node_modules/bar/index.ts' does not exist.", + "File '/node_modules/bar/index.tsx' does not exist.", + "File '/node_modules/bar/index.d.ts' does not exist.", + "File '/node_modules/bar/index.js.ts' does not exist.", + "File '/node_modules/bar/index.js.tsx' does not exist.", + "File '/node_modules/bar/index.js.d.ts' does not exist.", + "Directory '/node_modules/bar/index.js' does not exist, skipping all lookups in it.", + "File '/node_modules/bar/index.ts' does not exist.", + "File '/node_modules/bar/index.tsx' does not exist.", + "File '/node_modules/bar/index.d.ts' does not exist.", + "File '/node_modules/@types/bar/package.json' exists according to earlier cached lookups.", + "File '/node_modules/@types/bar.d.ts' does not exist.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/bar/index.d.ts'.", + "File '/node_modules/@types/bar/index.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/bar/index.d.ts', result '/node_modules/@types/bar/index.d.ts'.", + "======== Module name 'bar' was successfully resolved to '/node_modules/bar/index.mjs' with Package ID 'bar/index.mjs@1.0.0'. ========", + "======== Resolving type reference directive 'bar', containing file '__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'.", + "File '/node_modules/@types/bar/package.json' exists according to earlier cached lookups.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/bar/index.d.ts'.", + "File '/node_modules/@types/bar/index.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/bar/index.d.ts', result '/node_modules/@types/bar/index.d.ts'.", + "======== Type reference directive 'bar' was successfully resolved to '/node_modules/@types/bar/index.d.ts' with Package ID '@types/bar/index.d.ts@1.0.0', primary: true. ========" +] \ No newline at end of file diff --git a/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=node16).errors.txt b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=node16).errors.txt new file mode 100644 index 0000000000000..ef8ea52f327c4 --- /dev/null +++ b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=node16).errors.txt @@ -0,0 +1,96 @@ +error TS6504: File '/node_modules/bar/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/node_modules/bar/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/node_modules/foo/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +error TS6504: File '/node_modules/foo/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? + The file is in the program because: + Root file specified for compilation +/index.mts(1,21): error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.mjs' implicitly has an 'any' type. + There are types at '/node_modules/foo/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'foo' library may need to update its package.json. +/index.mts(2,21): error TS7016: Could not find a declaration file for module 'bar'. '/node_modules/bar/index.mjs' implicitly has an 'any' type. + There are types at '/node_modules/@types/bar/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@types/bar' library may need to update its package.json. + + +!!! error TS6504: File '/node_modules/bar/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/node_modules/bar/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/node_modules/foo/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +!!! error TS6504: File '/node_modules/foo/index.mjs' is a JavaScript file. Did you mean to enable the 'allowJs' option? +!!! error TS6504: The file is in the program because: +!!! error TS6504: Root file specified for compilation +==== /node_modules/foo/package.json (0 errors) ==== + { + "name": "foo", + "version": "1.0.0", + "main": "index.js", + "types": "index.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } + } + +==== /node_modules/foo/index.js (0 errors) ==== + module.exports = { foo: 1 }; + +==== /node_modules/foo/index.mjs (0 errors) ==== + export const foo = 1; + +==== /node_modules/foo/index.d.ts (0 errors) ==== + export declare const foo: number; + +==== /node_modules/@types/bar/package.json (0 errors) ==== + { + "name": "@types/bar", + "version": "1.0.0", + "types": "index.d.ts", + "exports": { + ".": { + "require": "./index.d.ts" + } + } + } + +==== /node_modules/@types/bar/index.d.ts (0 errors) ==== + export declare const bar: number; + +==== /node_modules/bar/package.json (0 errors) ==== + { + "name": "bar", + "version": "1.0.0", + "main": "index.js", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } + } + +==== /node_modules/bar/index.js (0 errors) ==== + module.exports = { bar: 1 }; + +==== /node_modules/bar/index.mjs (0 errors) ==== + export const bar = 1; + +==== /index.mts (2 errors) ==== + import { foo } from "foo"; + ~~~~~ +!!! error TS7016: Could not find a declaration file for module 'foo'. '/node_modules/foo/index.mjs' implicitly has an 'any' type. +!!! error TS7016: There are types at '/node_modules/foo/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'foo' library may need to update its package.json. + import { bar } from "bar"; + ~~~~~ +!!! error TS7016: Could not find a declaration file for module 'bar'. '/node_modules/bar/index.mjs' implicitly has an 'any' type. +!!! error TS7016: There are types at '/node_modules/@types/bar/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@types/bar' library may need to update its package.json. \ No newline at end of file diff --git a/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=node16).trace.json b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=node16).trace.json new file mode 100644 index 0000000000000..6d8b992bfbc76 --- /dev/null +++ b/tests/baselines/reference/resolvesWithoutExportsDiagnostic1(moduleresolution=node16).trace.json @@ -0,0 +1,96 @@ +[ + "Found 'package.json' at '/node_modules/foo/package.json'.", + "Found 'package.json' at '/node_modules/@types/bar/package.json'.", + "======== Resolving module 'foo' from '/index.mts'. ========", + "Explicitly specified module resolution kind: 'Node16'.", + "Resolving in ESM mode with conditions 'node', 'import', 'types'.", + "File '/package.json' does not exist.", + "Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.", + "File '/node_modules/foo/package.json' exists according to earlier cached lookups.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/foo/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/foo/index.mts' does not exist.", + "File '/node_modules/foo/index.d.mts' does not exist.", + "Saw non-matching condition 'require'.", + "File '/node_modules/foo/package.json' exists according to earlier cached lookups.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/foo/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/foo/index.mjs' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/foo/index.mjs', result '/node_modules/foo/index.mjs'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/foo/package.json' exists according to earlier cached lookups.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field 'index.d.ts' that references '/node_modules/foo/index.d.ts'.", + "File '/node_modules/foo/index.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/foo/index.d.ts', result '/node_modules/foo/index.d.ts'.", + "======== Module name 'foo' was successfully resolved to '/node_modules/foo/index.mjs' with Package ID 'foo/index.mjs@1.0.0'. ========", + "======== Resolving module 'bar' from '/index.mts'. ========", + "Explicitly specified module resolution kind: 'Node16'.", + "Resolving in ESM mode with conditions 'node', 'import', 'types'.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'bar' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.", + "Found 'package.json' at '/node_modules/bar/package.json'.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/bar/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/bar/index.mts' does not exist.", + "File '/node_modules/bar/index.d.mts' does not exist.", + "Saw non-matching condition 'require'.", + "File '/node_modules/@types/bar/package.json' exists according to earlier cached lookups.", + "Saw non-matching condition 'require'.", + "File '/node_modules/bar/package.json' exists according to earlier cached lookups.", + "Matched 'exports' condition 'import'.", + "Using 'exports' subpath '.' with target './index.mjs'.", + "File name '/node_modules/bar/index.mjs' has a '.mjs' extension - stripping it.", + "File '/node_modules/bar/index.mjs' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/bar/index.mjs', result '/node_modules/bar/index.mjs'.", + "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.", + "File '/package.json' does not exist according to earlier cached lookups.", + "Loading module 'bar' from 'node_modules' folder, target file types: TypeScript, Declaration.", + "File '/node_modules/bar/package.json' exists according to earlier cached lookups.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' does not have a 'types' field.", + "'package.json' has 'main' field 'index.js' that references '/node_modules/bar/index.js'.", + "File '/node_modules/bar/index.js' exist - use it as a name resolution result.", + "File '/node_modules/bar/index.js' has an unsupported extension, so skipping it.", + "Loading module as file / folder, candidate module location '/node_modules/bar/index.js', target file types: TypeScript, Declaration.", + "File name '/node_modules/bar/index.js' has a '.js' extension - stripping it.", + "File '/node_modules/bar/index.ts' does not exist.", + "File '/node_modules/bar/index.tsx' does not exist.", + "File '/node_modules/bar/index.d.ts' does not exist.", + "File '/node_modules/bar/index.js.ts' does not exist.", + "File '/node_modules/bar/index.js.tsx' does not exist.", + "File '/node_modules/bar/index.js.d.ts' does not exist.", + "Directory '/node_modules/bar/index.js' does not exist, skipping all lookups in it.", + "File '/node_modules/@types/bar/package.json' exists according to earlier cached lookups.", + "'package.json' does not have a 'typesVersions' field.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/bar/index.d.ts'.", + "File '/node_modules/@types/bar/index.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/bar/index.d.ts', result '/node_modules/@types/bar/index.d.ts'.", + "======== Module name 'bar' was successfully resolved to '/node_modules/bar/index.mjs' with Package ID 'bar/index.mjs@1.0.0'. ========", + "======== Resolving type reference directive 'bar', containing file '__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'.", + "File '/node_modules/@types/bar/package.json' exists according to earlier cached lookups.", + "'package.json' does not have a 'typings' field.", + "'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/bar/index.d.ts'.", + "File '/node_modules/@types/bar/index.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/bar/index.d.ts', result '/node_modules/@types/bar/index.d.ts'.", + "======== Type reference directive 'bar' was successfully resolved to '/node_modules/@types/bar/index.d.ts' with Package ID '@types/bar/index.d.ts@1.0.0', primary: true. ========", + "File 'package.json' does not exist.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups.", + "File 'package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups." +] \ No newline at end of file diff --git a/tests/cases/conformance/moduleResolution/nestedPackageJsonRedirect.ts b/tests/cases/conformance/moduleResolution/nestedPackageJsonRedirect.ts new file mode 100644 index 0000000000000..8d5804c1fd010 --- /dev/null +++ b/tests/cases/conformance/moduleResolution/nestedPackageJsonRedirect.ts @@ -0,0 +1,29 @@ +// @moduleResolution: node16,nodenext,bundler +// @noEmit: true +// @noTypesAndSymbols: true +// @traceResolution: true +// @strict: true + +// @Filename: /node_modules/@restart/hooks/package.json +{ + "name": "@restart/hooks", + "version": "0.3.25", + "main": "cjs/index.js", + "types": "cjs/index.d.ts", + "module": "esm/index.js" +} + +// @Filename: /node_modules/@restart/hooks/useMergedRefs/package.json +{ + "name": "@restart/hooks/useMergedRefs", + "private": true, + "main": "../cjs/useMergedRefs.js", + "module": "../esm/useMergedRefs.js", + "types": "../esm/useMergedRefs.d.ts" + } + +// @Filename: /node_modules/@restart/hooks/esm/useMergedRefs.d.ts +export {}; + +// @Filename: /main.ts +import {} from "@restart/hooks/useMergedRefs"; diff --git a/tests/cases/conformance/moduleResolution/resolvesWithoutExportsDiagnostic1.ts b/tests/cases/conformance/moduleResolution/resolvesWithoutExportsDiagnostic1.ts new file mode 100644 index 0000000000000..d01bb47a75ca8 --- /dev/null +++ b/tests/cases/conformance/moduleResolution/resolvesWithoutExportsDiagnostic1.ts @@ -0,0 +1,66 @@ +// @moduleResolution: bundler,node16 +// @strict: true +// @noTypesAndSymbols: true +// @noEmit: true +// @traceResolution: true + +// @Filename: /node_modules/foo/package.json +{ + "name": "foo", + "version": "1.0.0", + "main": "index.js", + "types": "index.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } +} + +// @Filename: /node_modules/foo/index.js +module.exports = { foo: 1 }; + +// @Filename: /node_modules/foo/index.mjs +export const foo = 1; + +// @Filename: /node_modules/foo/index.d.ts +export declare const foo: number; + +// @Filename: /node_modules/@types/bar/package.json +{ + "name": "@types/bar", + "version": "1.0.0", + "types": "index.d.ts", + "exports": { + ".": { + "require": "./index.d.ts" + } + } +} + +// @Filename: /node_modules/@types/bar/index.d.ts +export declare const bar: number; + +// @Filename: /node_modules/bar/package.json +{ + "name": "bar", + "version": "1.0.0", + "main": "index.js", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + } +} + +// @Filename: /node_modules/bar/index.js +module.exports = { bar: 1 }; + +// @Filename: /node_modules/bar/index.mjs +export const bar = 1; + +// @Filename: /index.mts +import { foo } from "foo"; +import { bar } from "bar"; \ No newline at end of file