Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
66 changes: 41 additions & 25 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,38 +810,54 @@ export function resolvePackageNameToPackageJson(
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
// Use explicit type list from tsconfig.json
if (options.types) {
return options.types;
}

// Walk the primary type lookup locations
const result: string[] = [];
if (host.directoryExists && host.getDirectories) {
const typeRoots = getEffectiveTypeRoots(options, host);
if (typeRoots) {
for (const root of typeRoots) {
if (host.directoryExists(root)) {
for (const typeDirectivePath of host.getDirectories(root)) {
const normalized = normalizePath(typeDirectivePath);
const packageJsonPath = combinePaths(root, normalized, "package.json");
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
// See `createNotNeededPackageJSON` in the types-publisher` repo.
// eslint-disable-next-line no-restricted-syntax
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);
// Check if the special "*" value is present, which means "include all from typeRoots"
const hasWildcard = options.types.includes("*");
if (hasWildcard) {
// Enumerate all packages from typeRoots
const result: string[] = [];
if (host.directoryExists && host.getDirectories) {
const typeRoots = getEffectiveTypeRoots(options, host);
if (typeRoots) {
for (const root of typeRoots) {
if (host.directoryExists(root)) {
for (const typeDirectivePath of host.getDirectories(root)) {
const normalized = normalizePath(typeDirectivePath);
const packageJsonPath = combinePaths(root, normalized, "package.json");
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
// See `createNotNeededPackageJSON` in the types-publisher` repo.
// eslint-disable-next-line no-restricted-syntax
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);
}
}
}
}
}
}
}
// Add any explicitly listed types that aren't already included (and aren't the wildcard itself)
for (const type of options.types) {
if (type !== "*" && !result.includes(type)) {
result.push(type);
}
}
return result;
}
else {
return options.types;
}
}
return result;
else {
// When types is undefined (not specified in tsconfig), default to empty array
// This is a breaking change from the previous behavior which included all @types packages
return emptyArray;
}
}

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'. ========"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/a.ts(1,1): error TS2304: Cannot find name 'a'.


==== /a.ts (1 errors) ====
a;
~
!!! error TS2304: Cannot find name 'a'.

==== /node_modules/@types/.a/index.d.ts (0 errors) ====
declare const a: number;

==== /node_modules/@types/a/index.d.ts (0 errors) ====
declare const a: string;

Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//// [tests/cases/compiler/moduleResolution_automaticTypeDirectiveNames.ts] ////

=== /a.ts ===
a;
>a : Symbol(a, Decl(index.d.ts, 0, 13))

=== /node_modules/@types/a/index.d.ts ===
declare const a: string;
>a : Symbol(a, Decl(index.d.ts, 0, 13))
a;

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

=== /a.ts ===
a;
>a : string
> : ^^^^^^

=== /node_modules/@types/a/index.d.ts ===
declare const a: string;
>a : string
> : ^^^^^^
>a : any
> : ^^^

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
Loading