diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ee08167c698fd..febe72bbcb720 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -819,6 +819,7 @@ import { nodeIsPresent, nodeIsSynthesized, NodeLinks, + nodeModulesPathPart, nodeStartsNewLexicalEnvironment, NodeWithTypeArguments, NonNullChain, @@ -4772,7 +4773,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); @@ -4860,7 +4861,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; @@ -4930,25 +4931,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 node10Result = sourceFile.resolvedModules?.get(moduleReference, mode)?.node10Result; + errorInfo = node10Result ? 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_or_typings, + node10Result, + node10Result.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/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index a2d3387381009..377bc39a74d82 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -5235,6 +5235,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 or typings.": { + "category": "Message", + "code": 6278 + }, "Enable project compilation": { "category": "Message", diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index c91ae73cd6b29..0d66245a58f41 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -212,7 +212,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); @@ -232,6 +233,7 @@ function createResolvedModuleWithFailedLookupLocations( failedLookupLocations: initializeResolutionField(failedLookupLocations), affectingLocations: initializeResolutionField(affectingLocations), resolutionDiagnostics: initializeResolutionField(diagnostics), + node10Result: legacyResult, }; } function initializeResolutionField(value: T[]): T[] | undefined { @@ -1666,12 +1668,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( @@ -1680,10 +1707,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) { @@ -2279,7 +2307,7 @@ function resolvedIfExtensionMatches(extensions: Extensions, path: string, resolv } /** True if `extension` is one of the supported `extensions`. */ -function extensionIsOk(extensions: Extensions, extension: Extension): boolean { +function extensionIsOk(extensions: Extensions, extension: string): boolean { return extensions & Extensions.JavaScript && (extension === Extension.Js || extension === Extension.Jsx || extension === Extension.Mjs || extension === Extension.Cjs) || extensions & Extensions.TypeScript && (extension === Extension.Ts || extension === Extension.Tsx || extension === Extension.Mts || extension === Extension.Cts) || extensions & Extensions.Declaration && (extension === Extension.Dts || extension === Extension.Dmts || extension === Extension.Dcts) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d9384eff68e8b..80f0c539aef82 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -7599,6 +7599,12 @@ export interface ResolvedModuleWithFailedLookupLocations { affectingLocations?: string[]; /** @internal */ resolutionDiagnostics?: Diagnostic[] + /** + * @internal + * Used to issue a diagnostic if typings for a non-relative import couldn't be found + * while respecting package.json `exports`, but were found when disabling `exports`. + */ + node10Result?: string; } export interface ResolvedTypeReferenceDirective { diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt index 22d2482ced9ae..a0ed330173ed8 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).errors.txt @@ -6,7 +6,7 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J /main.cts(2,16): error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. /main.cts(5,16): error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. /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';` + 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 or typings. /main.mts(2,16): error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. /main.mts(5,16): error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. @@ -79,7 +79,7 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J import {} from "exports-and-types-versions/foo"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! 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. -!!! error TS7016: 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';` +!!! error TS7016: 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 or typings. import {} from "exports-and-types-versions/nope"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json index 8c7abd981db3c..d0aae2ef9523b 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=node16).trace.json @@ -99,6 +99,16 @@ "File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.", "File '/node_modules/exports-and-types-versions/dist/foo.js' exist - use it as a name resolution result.", "Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.", + "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/foo' 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 'foo'.", + "Module name 'foo', matched pattern 'foo'.", + "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/foo' was successfully resolved to '/node_modules/exports-and-types-versions/dist/foo.js' with Package ID 'exports-and-types-versions/dist/foo.js@1.0.0'. ========", "======== Resolving module 'exports-and-types-versions/nope' 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..a0ed330173ed8 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).errors.txt +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).errors.txt @@ -6,7 +6,7 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J /main.cts(2,16): error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. /main.cts(5,16): error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. /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';` + 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 or typings. /main.mts(2,16): error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. /main.mts(5,16): error TS2307: Cannot find module 'exports-and-types-versions/versioned-nah' or its corresponding type declarations. @@ -79,7 +79,7 @@ error TS6504: File '/node_modules/exports-and-types-versions/dist/foo.js' is a J import {} from "exports-and-types-versions/foo"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! 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. -!!! error TS7016: 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';` +!!! error TS7016: 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 or typings. import {} from "exports-and-types-versions/nope"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find module 'exports-and-types-versions/nope' or its corresponding type declarations. diff --git a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json index 017892d356c35..de93053530308 100644 --- a/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json +++ b/tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json @@ -99,6 +99,16 @@ "File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.", "File '/node_modules/exports-and-types-versions/dist/foo.js' exist - use it as a name resolution result.", "Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.", + "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/foo' 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 'foo'.", + "Module name 'foo', matched pattern 'foo'.", + "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/foo' was successfully resolved to '/node_modules/exports-and-types-versions/dist/foo.js' with Package ID 'exports-and-types-versions/dist/foo.js@1.0.0'. ========", "======== Resolving module 'exports-and-types-versions/nope' from '/main.mts'. ========", "Module resolution kind is not specified, using 'NodeNext'.", 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..fe904ca69530b --- /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 or typings. +/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 or typings. + + +!!! 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 or typings. + 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 or typings. \ 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..fe904ca69530b --- /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 or typings. +/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 or typings. + + +!!! 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 or typings. + 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 or typings. \ 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/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