Skip to content

Commit 11dd368

Browse files
authored
do not report file lookups if containing folder is known to be missing (#13187)
* added missing '.' at the end of message * do not report lookups if containing folder is known to be absent
1 parent 77a3dfb commit 11dd368

File tree

52 files changed

+286
-890
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+286
-890
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,7 +2757,7 @@
27572757
"category": "Message",
27582758
"code": 6094
27592759
},
2760-
"Loading module as file / folder, candidate module location '{0}'.": {
2760+
"Loading module as file / folder, candidate module location '{0}', target file type '{1}'.": {
27612761
"category": "Message",
27622762
"code": 6095
27632763
},
@@ -2769,7 +2769,7 @@
27692769
"category": "Message",
27702770
"code": 6097
27712771
},
2772-
"Loading module '{0}' from 'node_modules' folder.": {
2772+
"Loading module '{0}' from 'node_modules' folder, target file type '{1}'.": {
27732773
"category": "Message",
27742774
"code": 6098
27752775
},
@@ -2965,10 +2965,14 @@
29652965
"category": "Message",
29662966
"code": 6146
29672967
},
2968-
"Resolution for module '{0}' was found in cache": {
2968+
"Resolution for module '{0}' was found in cache.": {
29692969
"category": "Message",
29702970
"code": 6147
29712971
},
2972+
"Directory '{0}' does not exist, skipping all lookups in it.": {
2973+
"category": "Message",
2974+
"code": 6148
2975+
},
29722976
"Variable '{0}' implicitly has an '{1}' type.": {
29732977
"category": "Error",
29742978
"code": 7005

src/compiler/moduleNameResolver.ts

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace ts {
3232
* Kinds of file that we are currently looking for.
3333
* Typically there is one pass with Extensions.TypeScript, then a second pass with Extensions.JavaScript.
3434
*/
35-
const enum Extensions {
35+
enum Extensions {
3636
TypeScript, /** '.ts', '.tsx', or '.d.ts' */
3737
JavaScript, /** '.js' or '.jsx' */
3838
DtsOnly /** Only '.d.ts' */
@@ -217,9 +217,13 @@ namespace ts {
217217
return forEach(typeRoots, typeRoot => {
218218
const candidate = combinePaths(typeRoot, typeReferenceDirectiveName);
219219
const candidateDirectory = getDirectoryPath(candidate);
220+
const directoryExists = directoryProbablyExists(candidateDirectory, host);
221+
if (!directoryExists && traceEnabled) {
222+
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, candidateDirectory);
223+
}
220224
return resolvedTypeScriptOnly(
221225
loadNodeModuleFromDirectory(Extensions.DtsOnly, candidate, failedLookupLocations,
222-
!directoryProbablyExists(candidateDirectory, host), moduleResolutionState));
226+
!directoryExists, moduleResolutionState));
223227
});
224228
}
225229
else {
@@ -700,7 +704,7 @@ namespace ts {
700704

701705
if (moduleHasNonRelativeName(moduleName)) {
702706
if (traceEnabled) {
703-
trace(host, Diagnostics.Loading_module_0_from_node_modules_folder, moduleName);
707+
trace(host, Diagnostics.Loading_module_0_from_node_modules_folder_target_file_type_1, moduleName, Extensions[extensions]);
704708
}
705709
const resolved = loadModuleFromNodeModules(extensions, moduleName, containingDirectory, failedLookupLocations, state, cache);
706710
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
@@ -728,11 +732,33 @@ namespace ts {
728732

729733
function nodeLoadModuleByRelativeName(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
730734
if (state.traceEnabled) {
731-
trace(state.host, Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate);
735+
trace(state.host, Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0_target_file_type_1, candidate, Extensions[extensions]);
732736
}
733-
734-
const resolvedFromFile = !pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(extensions, candidate, failedLookupLocations, onlyRecordFailures, state);
735-
return resolvedFromFile || loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state);
737+
if (!pathEndsWithDirectorySeparator(candidate)) {
738+
if (!onlyRecordFailures) {
739+
const parentOfCandidate = getDirectoryPath(candidate);
740+
if (!directoryProbablyExists(parentOfCandidate, state.host)) {
741+
if (state.traceEnabled) {
742+
trace(state.host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, parentOfCandidate);
743+
}
744+
onlyRecordFailures = true;
745+
}
746+
}
747+
const resolvedFromFile = loadModuleFromFile(extensions, candidate, failedLookupLocations, onlyRecordFailures, state);
748+
if (resolvedFromFile) {
749+
return resolvedFromFile;
750+
}
751+
}
752+
if (!onlyRecordFailures) {
753+
const candidateExists = directoryProbablyExists(candidate, state.host);
754+
if (!candidateExists) {
755+
if (state.traceEnabled) {
756+
trace(state.host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, candidate);
757+
}
758+
onlyRecordFailures = true;
759+
}
760+
}
761+
return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state);
736762
}
737763

738764
/* @internal */
@@ -791,19 +817,21 @@ namespace ts {
791817

792818
/** Return the file if it exists. */
793819
function tryFile(fileName: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
794-
if (!onlyRecordFailures && state.host.fileExists(fileName)) {
795-
if (state.traceEnabled) {
796-
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
820+
if (!onlyRecordFailures) {
821+
if (state.host.fileExists(fileName)) {
822+
if (state.traceEnabled) {
823+
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
824+
}
825+
return fileName;
797826
}
798-
return fileName;
799-
}
800-
else {
801-
if (state.traceEnabled) {
802-
trace(state.host, Diagnostics.File_0_does_not_exist, fileName);
827+
else {
828+
if (state.traceEnabled) {
829+
trace(state.host, Diagnostics.File_0_does_not_exist, fileName);
830+
}
803831
}
804-
failedLookupLocations.push(fileName);
805-
return undefined;
806832
}
833+
failedLookupLocations.push(fileName);
834+
return undefined;
807835
}
808836

809837
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined {
@@ -840,7 +868,7 @@ namespace ts {
840868
}
841869
}
842870
else {
843-
if (state.traceEnabled) {
871+
if (directoryExists && state.traceEnabled) {
844872
trace(state.host, Diagnostics.File_0_does_not_exist, packageJsonPath);
845873
}
846874
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
@@ -872,9 +900,7 @@ namespace ts {
872900
return combinePaths(directory, "package.json");
873901
}
874902

875-
function loadModuleFromNodeModulesFolder(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
876-
const nodeModulesFolder = combinePaths(directory, "node_modules");
877-
const nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host);
903+
function loadModuleFromNodeModulesFolder(extensions: Extensions, moduleName: string, nodeModulesFolder: string, nodeModulesFolderExists: boolean, failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
878904
const candidate = normalizePath(combinePaths(nodeModulesFolder, moduleName));
879905

880906
return loadModuleFromFile(extensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state) ||
@@ -904,12 +930,26 @@ namespace ts {
904930

905931
/** Load a module from a single node_modules directory, but not from any ancestors' node_modules directories. */
906932
function loadModuleFromNodeModulesOneLevel(extensions: Extensions, moduleName: string, directory: string, failedLookupLocations: Push<string>, state: ModuleResolutionState, typesOnly = false): Resolved | undefined {
907-
const packageResult = typesOnly ? undefined : loadModuleFromNodeModulesFolder(extensions, moduleName, directory, failedLookupLocations, state);
933+
const nodeModulesFolder = combinePaths(directory, "node_modules");
934+
const nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host);
935+
if (!nodeModulesFolderExists && state.traceEnabled) {
936+
trace(state.host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, nodeModulesFolder);
937+
}
938+
939+
const packageResult = typesOnly ? undefined : loadModuleFromNodeModulesFolder(extensions, moduleName, nodeModulesFolder, nodeModulesFolderExists, failedLookupLocations, state);
908940
if (packageResult) {
909941
return packageResult;
910942
}
911943
if (extensions !== Extensions.JavaScript) {
912-
return loadModuleFromNodeModulesFolder(Extensions.DtsOnly, combinePaths("@types", moduleName), directory, failedLookupLocations, state);
944+
const nodeModulesAtTypes = combinePaths(nodeModulesFolder, "@types");
945+
let nodeModulesAtTypesExists = nodeModulesFolderExists;
946+
if (nodeModulesFolderExists && !directoryProbablyExists(nodeModulesAtTypes, state.host)) {
947+
if (state.traceEnabled) {
948+
trace(state.host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, nodeModulesAtTypes);
949+
}
950+
nodeModulesAtTypesExists = false;
951+
}
952+
return loadModuleFromNodeModulesFolder(Extensions.DtsOnly, moduleName, nodeModulesAtTypes, nodeModulesAtTypesExists, failedLookupLocations, state);
913953
}
914954
}
915955

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,58 +2069,17 @@ namespace ts.projectSystem {
20692069
assert.deepEqual(resolutionTrace, [
20702070
"======== Resolving module 'lib' from '/a/b/app.js'. ========",
20712071
"Module resolution kind is not specified, using 'NodeJs'.",
2072-
"Loading module 'lib' from 'node_modules' folder.",
2073-
"File '/a/b/node_modules/lib.ts' does not exist.",
2074-
"File '/a/b/node_modules/lib.tsx' does not exist.",
2075-
"File '/a/b/node_modules/lib.d.ts' does not exist.",
2076-
"File '/a/b/node_modules/lib/package.json' does not exist.",
2077-
"File '/a/b/node_modules/lib/index.ts' does not exist.",
2078-
"File '/a/b/node_modules/lib/index.tsx' does not exist.",
2079-
"File '/a/b/node_modules/lib/index.d.ts' does not exist.",
2080-
"File '/a/b/node_modules/@types/lib.d.ts' does not exist.",
2081-
"File '/a/b/node_modules/@types/lib/package.json' does not exist.",
2082-
"File '/a/b/node_modules/@types/lib/index.d.ts' does not exist.",
2083-
"File '/a/node_modules/lib.ts' does not exist.",
2084-
"File '/a/node_modules/lib.tsx' does not exist.",
2085-
"File '/a/node_modules/lib.d.ts' does not exist.",
2086-
"File '/a/node_modules/lib/package.json' does not exist.",
2087-
"File '/a/node_modules/lib/index.ts' does not exist.",
2088-
"File '/a/node_modules/lib/index.tsx' does not exist.",
2089-
"File '/a/node_modules/lib/index.d.ts' does not exist.",
2090-
"File '/a/node_modules/@types/lib.d.ts' does not exist.",
2091-
"File '/a/node_modules/@types/lib/package.json' does not exist.",
2092-
"File '/a/node_modules/@types/lib/index.d.ts' does not exist.",
2093-
"File '/node_modules/lib.ts' does not exist.",
2094-
"File '/node_modules/lib.tsx' does not exist.",
2095-
"File '/node_modules/lib.d.ts' does not exist.",
2096-
"File '/node_modules/lib/package.json' does not exist.",
2097-
"File '/node_modules/lib/index.ts' does not exist.",
2098-
"File '/node_modules/lib/index.tsx' does not exist.",
2099-
"File '/node_modules/lib/index.d.ts' does not exist.",
2100-
"File '/node_modules/@types/lib.d.ts' does not exist.",
2101-
"File '/node_modules/@types/lib/package.json' does not exist.",
2102-
"File '/node_modules/@types/lib/index.d.ts' does not exist.",
2103-
"Loading module 'lib' from 'node_modules' folder.",
2104-
"File '/a/b/node_modules/lib.js' does not exist.",
2105-
"File '/a/b/node_modules/lib.jsx' does not exist.",
2106-
"File '/a/b/node_modules/lib/package.json' does not exist.",
2107-
"File '/a/b/node_modules/lib/index.js' does not exist.",
2108-
"File '/a/b/node_modules/lib/index.jsx' does not exist.",
2109-
"File '/a/node_modules/lib.js' does not exist.",
2110-
"File '/a/node_modules/lib.jsx' does not exist.",
2111-
"File '/a/node_modules/lib/package.json' does not exist.",
2112-
"File '/a/node_modules/lib/index.js' does not exist.",
2113-
"File '/a/node_modules/lib/index.jsx' does not exist.",
2114-
"File '/node_modules/lib.js' does not exist.",
2115-
"File '/node_modules/lib.jsx' does not exist.",
2116-
"File '/node_modules/lib/package.json' does not exist.",
2117-
"File '/node_modules/lib/index.js' does not exist.",
2118-
"File '/node_modules/lib/index.jsx' does not exist.",
2072+
"Loading module 'lib' from 'node_modules' folder, target file type 'TypeScript'.",
2073+
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
2074+
"Directory '/a/node_modules' does not exist, skipping all lookups in it.",
2075+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
2076+
"Loading module 'lib' from 'node_modules' folder, target file type 'JavaScript'.",
2077+
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
2078+
"Directory '/a/node_modules' does not exist, skipping all lookups in it.",
2079+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
21192080
"======== Module name 'lib' was not resolved. ========",
21202081
`Auto discovery for typings is enabled in project '${proj.getProjectName()}'. Running extra resolution pass for module 'lib' using cache location '/a/cache'.`,
21212082
"File '/a/cache/node_modules/lib.d.ts' does not exist.",
2122-
"File '/a/cache/node_modules/lib/package.json' does not exist.",
2123-
"File '/a/cache/node_modules/lib/index.d.ts' does not exist.",
21242083
"File '/a/cache/node_modules/@types/lib.d.ts' does not exist.",
21252084
"File '/a/cache/node_modules/@types/lib/package.json' does not exist.",
21262085
"File '/a/cache/node_modules/@types/lib/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/cacheResolutions.trace.json

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,10 @@
1313
"File '/tslib.ts' does not exist.",
1414
"File '/tslib.tsx' does not exist.",
1515
"File '/tslib.d.ts' does not exist.",
16-
"File '/a/b/c/node_modules/@types/tslib.d.ts' does not exist.",
17-
"File '/a/b/c/node_modules/@types/tslib/package.json' does not exist.",
18-
"File '/a/b/c/node_modules/@types/tslib/index.d.ts' does not exist.",
19-
"File '/a/b/node_modules/@types/tslib.d.ts' does not exist.",
20-
"File '/a/b/node_modules/@types/tslib/package.json' does not exist.",
21-
"File '/a/b/node_modules/@types/tslib/index.d.ts' does not exist.",
22-
"File '/a/node_modules/@types/tslib.d.ts' does not exist.",
23-
"File '/a/node_modules/@types/tslib/package.json' does not exist.",
24-
"File '/a/node_modules/@types/tslib/index.d.ts' does not exist.",
25-
"File '/node_modules/@types/tslib.d.ts' does not exist.",
26-
"File '/node_modules/@types/tslib/package.json' does not exist.",
27-
"File '/node_modules/@types/tslib/index.d.ts' does not exist.",
16+
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
17+
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
18+
"Directory '/a/node_modules' does not exist, skipping all lookups in it.",
19+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
2820
"File '/a/b/c/tslib.js' does not exist.",
2921
"File '/a/b/c/tslib.jsx' does not exist.",
3022
"File '/a/b/tslib.js' does not exist.",
@@ -35,9 +27,9 @@
3527
"File '/tslib.jsx' does not exist.",
3628
"======== Module name 'tslib' was not resolved. ========",
3729
"======== Resolving module 'tslib' from '/a/b/c/lib1.ts'. ========",
38-
"Resolution for module 'tslib' was found in cache",
30+
"Resolution for module 'tslib' was found in cache.",
3931
"======== Module name 'tslib' was not resolved. ========",
4032
"======== Resolving module 'tslib' from '/a/b/c/lib2.ts'. ========",
41-
"Resolution for module 'tslib' was found in cache",
33+
"Resolution for module 'tslib' was found in cache.",
4234
"======== Module name 'tslib' was not resolved. ========"
4335
]

0 commit comments

Comments
 (0)