Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import {
emptyArray,
endsWith,
ensureTrailingDirectorySeparator,
equateValues,
every,
Extension,
extensionIsTS,
fileExtensionIs,
fileExtensionIsOneOf,
filter,
firstDefined,
flatten,
forEach,
forEachAncestorDirectory,
formatMessage,
Expand Down Expand Up @@ -808,13 +810,17 @@ export function resolvePackageNameToPackageJson(
* this list is only the set of defaults that are implicitly included.
*/
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
// Use explicit type list from tsconfig.json
if (options.types) {
// Default to [] if nothing specified
if (options.types === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are error messages in getCannotFindNameDiagnosticForName in checker.ts that needs to be fixed to always suggest adding type name to types array ?

return emptyArray;
}

if (!options.types.includes("*")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to update updateTypeRootsWatch to reflect this change

// No wildcard, no need to iterate anything
return options.types;
}

// Walk the primary type lookup locations
const result: string[] = [];
const wildcardMatches: string[] = [];
if (host.directoryExists && host.getDirectories) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to fix FileIncludeKind.AutomaticTypeDirectiveFile for explainFiles and fileIncludeReasonToRelatedInformation

const typeRoots = getEffectiveTypeRoots(options, host);
if (typeRoots) {
Expand All @@ -829,19 +835,19 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
if (!isNotNeededPackage) {
const baseFileName = getBaseFileName(normalized);

// At this stage, skip results with leading dot.
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
// Return just the type directive names
result.push(baseFileName);
wildcardMatches.push(baseFileName);
}
}
}
}
}
}
}
return result;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discoverTypings figures out packages to look for, would we need change to the condition to find if it includes wildcard?

// Order potentially matters in program construction, so substitute
// in the wildcard in the position it was specified in the types array
return deduplicate(flatten(options.types.map(t => t === "*" ? wildcardMatches : t)), equateValues);
}

export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, NonRelativeNameResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe("unittests:: tsserver:: typingsInstaller:: General functionality", () =
});
openExternalProjectForSession({
projectFileName,
options: {},
options: { types: ["*"] },
rootFiles: [toExternalFile(appJs.path)],
typeAcquisition: { enable: true, include: ["node"] },
}, session);
Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/jsDeclarationsTypeReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ module.exports = {
export const thing: Something;
import Something_1 = require("fs");
import Something = Something_1.Something;


//// [DtsFileErrors]


tests/cases/conformance/jsdoc/declarations/out/index.d.ts(2,30): error TS2307: Cannot find module 'fs' or its corresponding type declarations.


==== tests/cases/conformance/jsdoc/declarations/out/index.d.ts (1 errors) ====
export const thing: Something;
import Something_1 = require("fs");
~~~~
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations.
import Something = Something_1.Something;
Comment on lines +34 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something isn't right here; probably need to update the test as it explicitly makes a @types/node dir that should be loaded.


==== node_modules/@types/node/index.d.ts (0 errors) ====
declare module "fs" {
export class Something {}
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/jsDeclarationsTypeReferences3.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ export namespace A {
}
import Something_1 = require("fs");
import Something = Something_1.Something;


//// [DtsFileErrors]


tests/cases/conformance/jsdoc/declarations/out/index.d.ts(6,30): error TS2307: Cannot find module 'fs' or its corresponding type declarations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here.



==== tests/cases/conformance/jsdoc/declarations/out/index.d.ts (1 errors) ====
export namespace A {
namespace B {
let thing: Something;
}
}
import Something_1 = require("fs");
~~~~
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations.
import Something = Something_1.Something;

==== node_modules/@types/node/index.d.ts (0 errors) ====
declare module "fs" {
export class Something {}
}
7 changes: 0 additions & 7 deletions tests/baselines/reference/library-reference-1.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@
"File '/src/types/jquery/package.json' does not exist.",
"File '/src/types/jquery/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/src/types/jquery/index.d.ts', result '/src/types/jquery/index.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/src/types/jquery/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/.src/__inferred type names__.ts', root directory '/src/types'. ========",
"Resolving with primary search path '/src/types'.",
"File '/src/types/jquery.d.ts' does not exist.",
"File '/src/types/jquery/package.json' does not exist according to earlier cached lookups.",
"File '/src/types/jquery/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/src/types/jquery/index.d.ts', result '/src/types/jquery/index.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/src/types/jquery/index.d.ts', primary: true. ========"
]
8 changes: 0 additions & 8 deletions tests/baselines/reference/library-reference-10.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@
"'package.json' has 'typings' field 'jquery.d.ts' that references '/foo/types/jquery/jquery.d.ts'.",
"File '/foo/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/foo/types/jquery/jquery.d.ts', result '/foo/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/foo/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/.src/__inferred type names__.ts', root directory '/foo/types'. ========",
"Resolving with primary search path '/foo/types'.",
"File '/foo/types/jquery.d.ts' does not exist.",
"File '/foo/types/jquery/package.json' exists according to earlier cached lookups.",
"'package.json' has 'typings' field 'jquery.d.ts' that references '/foo/types/jquery/jquery.d.ts'.",
"File '/foo/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/foo/types/jquery/jquery.d.ts', result '/foo/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/foo/types/jquery/jquery.d.ts', primary: true. ========"
]
9 changes: 0 additions & 9 deletions tests/baselines/reference/library-reference-2.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,5 @@
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
"File '/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/types/jquery/jquery.d.ts', result '/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/test/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/jquery.d.ts' does not exist.",
"File '/types/jquery/package.json' exists according to earlier cached lookups.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
"File '/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/types/jquery/jquery.d.ts', result '/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========"
]
5 changes: 1 addition & 4 deletions tests/baselines/reference/library-reference-6.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
"File '/node_modules/@types/alpha/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/package.json' does not exist.",
"File '/node_modules/package.json' does not exist.",
"File '/package.json' does not exist.",
"======== Resolving type reference directive 'alpha', containing file '/__inferred type names__.ts'. ========",
"Resolution for type reference directive 'alpha' was found in cache from location '/'.",
"======== Type reference directive 'alpha' was successfully resolved to '/node_modules/@types/alpha/index.d.ts', primary: true. ========"
"File '/package.json' does not exist."
]
8 changes: 1 addition & 7 deletions tests/baselines/reference/library-reference-8.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,5 @@
"File '/test/types/alpha/package.json' does not exist according to earlier cached lookups.",
"File '/test/types/alpha/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/test/types/alpha/index.d.ts', result '/test/types/alpha/index.d.ts'.",
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'alpha', containing file '/test/__inferred type names__.ts'. ========",
"Resolution for type reference directive 'alpha' was found in cache from location '/test'.",
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'beta', containing file '/test/__inferred type names__.ts'. ========",
"Resolution for type reference directive 'beta' was found in cache from location '/test'.",
"======== Type reference directive 'beta' was successfully resolved to '/test/types/beta/index.d.ts', primary: true. ========"
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========"
]
15 changes: 0 additions & 15 deletions tests/baselines/reference/modulePreserve3.errors.txt

This file was deleted.

15 changes: 1 addition & 14 deletions tests/baselines/reference/modulePreserve3.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,5 @@
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react/jsx-runtime.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/react/jsx-runtime.d.ts', result '/node_modules/@types/react/jsx-runtime.d.ts'.",
"======== Module name 'react/jsx-runtime' was successfully resolved to '/node_modules/@types/react/jsx-runtime.d.ts'. ========",
"======== Resolving type reference directive 'react', containing file '/.src/__inferred type names__.ts', root directory '/.src/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/.src/node_modules/@types, /node_modules/@types'.",
"Directory '/.src/node_modules/@types' does not exist, skipping all lookups in it.",
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react/index.d.ts' does not exist.",
"Looking up in 'node_modules' folder, initial location '/.src'.",
"Searching all ancestor node_modules directories for preferred extensions: Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"File '/node_modules/react.d.ts' does not exist.",
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react.d.ts' does not exist.",
"File '/node_modules/@types/react/index.d.ts' does not exist.",
"======== Type reference directive 'react' was not resolved. ========"
"======== Module name 'react/jsx-runtime' was successfully resolved to '/node_modules/@types/react/jsx-runtime.d.ts'. ========"
]
14 changes: 0 additions & 14 deletions tests/baselines/reference/nodeModulesAtTypesPriority.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@
"File '/packages/a/node_modules/redux/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/packages/a/node_modules/redux/index.d.ts', result '/packages/a/node_modules/redux/index.d.ts'.",
"======== Module name 'redux' was successfully resolved to '/packages/a/node_modules/redux/index.d.ts'. ========",
"======== Resolving type reference directive 'react', containing file '/.src/__inferred type names__.ts', root directory '/.src/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/.src/node_modules/@types, /node_modules/@types'.",
"Directory '/.src/node_modules/@types' does not exist, skipping all lookups in it.",
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/react/index.d.ts', result '/node_modules/@types/react/index.d.ts'.",
"======== Type reference directive 'react' was successfully resolved to '/node_modules/@types/react/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'redux', containing file '/.src/__inferred type names__.ts', root directory '/.src/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/.src/node_modules/@types, /node_modules/@types'.",
"Directory '/.src/node_modules/@types' does not exist, skipping all lookups in it.",
"File '/node_modules/@types/redux/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/redux/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/redux/index.d.ts', result '/node_modules/@types/redux/index.d.ts'.",
"======== Type reference directive 'redux' was successfully resolved to '/node_modules/@types/redux/index.d.ts', primary: true. ========",
"File '/.ts/package.json' does not exist.",
"File '/package.json' does not exist according to earlier cached lookups.",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@
"======== Resolving module './' from '/.src/node_modules/@types/react/jsx-dev-runtime.d.ts'. ========",
"Resolution for module './' was found in cache from location '/.src/node_modules/@types/react'.",
"======== Module name './' was successfully resolved to '/.src/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
"======== Resolving type reference directive 'react', containing file '/.src/__inferred type names__.ts', root directory '/.src/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/.src/node_modules/@types, /node_modules/@types'.",
"File '/.src/node_modules/@types/react/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 '/.src/node_modules/@types/react/index.d.ts'.",
"File '/.src/node_modules/@types/react/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/.src/node_modules/@types/react/index.d.ts', result '/.src/node_modules/@types/react/index.d.ts'.",
"======== Type reference directive 'react' was successfully resolved to '/.src/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1', primary: true. ========",
"File '/.ts/package.json' does not exist.",
"File '/package.json' does not exist according to earlier cached lookups.",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@
"======== Resolving module './' from '/.src/node_modules/@types/react/jsx-dev-runtime.d.ts'. ========",
"Resolution for module './' was found in cache from location '/.src/node_modules/@types/react'.",
"======== Module name './' was successfully resolved to '/.src/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
"======== Resolving type reference directive 'react', containing file '/.src/__inferred type names__.ts', root directory '/.src/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/.src/node_modules/@types, /node_modules/@types'.",
"File '/.src/node_modules/@types/react/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 '/.src/node_modules/@types/react/index.d.ts'.",
"File '/.src/node_modules/@types/react/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/.src/node_modules/@types/react/index.d.ts', result '/.src/node_modules/@types/react/index.d.ts'.",
"======== Type reference directive 'react' was successfully resolved to '/.src/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1', primary: true. ========",
"File '/.ts/package.json' does not exist.",
"File '/package.json' does not exist.",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
error TS2688: Cannot find type definition file for 'foo'.
The file is in the program because:
Entry point for implicit type library 'foo'
error TS5107: Option 'moduleResolution=classic' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
/app.ts(1,30): error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
/app.ts(2,29): error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
/app.ts(3,30): error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?


!!! error TS2688: Cannot find type definition file for 'foo'.
!!! error TS2688: The file is in the program because:
!!! error TS2688: Entry point for implicit type library 'foo'
!!! error TS5107: Option 'moduleResolution=classic' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
==== /node_modules/@types/foo/package.json (0 errors) ====
{
Expand Down
Loading