Skip to content

Commit bffe354

Browse files
mshivaku99Meera Shivakumarorta
authored
Issue35876: Give better error message when Classic Module Resolution with incorrect path (microsoft#38105)
* added Error 5084 to diagnosticMessages.json * added test case errorForBareSpecifierWithImplicitModuleResolution1 to tests/cases/compiler * modified checker.ts to report error 5084 when classic resolution and incorrect path are used * added baseline changes * passes all test cases including src/testRunner/unittests/ tests * Update with feedback * Make it check whether it is the right module resolution kind * Use the right diagnostic message in tsserver tests Co-authored-by: Meera Shivakumar <[email protected]> Co-authored-by: Orta <[email protected]>
1 parent a320e1b commit bffe354

File tree

72 files changed

+290
-236
lines changed

Some content is hidden

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

72 files changed

+290
-236
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ namespace ts {
26052605
}
26062606

26072607
function getExternalModuleMember(node: ImportDeclaration | ExportDeclaration, specifier: ImportOrExportSpecifier, dontResolveAlias = false): Symbol | undefined {
2608-
const moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier!)!; // TODO: GH#18217
2608+
const moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier!)!;
26092609
const name = specifier.propertyName || specifier.name;
26102610
const suppressInteropError = name.escapedText === InternalSymbolName.Default && !!(compilerOptions.allowSyntheticDefaultImports || compilerOptions.esModuleInterop);
26112611
const targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier!, dontResolveAlias, suppressInteropError);
@@ -3112,9 +3112,12 @@ namespace ts {
31123112
}
31133113
}
31143114

3115-
31163115
function resolveExternalModuleName(location: Node, moduleReferenceExpression: Expression, ignoreErrors?: boolean): Symbol | undefined {
3117-
return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ignoreErrors ? undefined : Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations);
3116+
const isClassic = getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Classic;
3117+
const errorMessage = isClassic?
3118+
Diagnostics.Cannot_find_module_0_Did_you_mean_to_set_the_moduleResolution_option_to_node_or_to_add_aliases_to_the_paths_option
3119+
: Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations;
3120+
return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ignoreErrors ? undefined : errorMessage);
31183121
}
31193122

31203123
function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage | undefined, isForAugmentation = false): Symbol | undefined {

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,11 @@
30123012
"category": "Error",
30133013
"code": 2791
30143014
},
3015+
"Cannot find module '{0}'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?": {
3016+
"category": "Error",
3017+
"code": 2792
3018+
},
3019+
30153020

30163021
"Import declaration '{0}' is using private name '{1}'.": {
30173022
"category": "Error",

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ namespace ts.tscWatch {
267267

268268
export function getDiagnosticModuleNotFoundOfFile(program: Program, file: File, moduleName: string) {
269269
const quotedModuleName = `"${moduleName}"`;
270-
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(quotedModuleName), quotedModuleName.length, Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations, moduleName);
270+
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(quotedModuleName), quotedModuleName.length, Diagnostics.Cannot_find_module_0_Did_you_mean_to_set_the_moduleResolution_option_to_node_or_to_add_aliases_to_the_paths_option, moduleName);
271271
}
272272

273273
export function runQueuedTimeoutCallbacks(sys: WatchedSystem) {

src/testRunner/unittests/tsserver/cachingFileSystemInformation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ namespace ts.projectSystem {
240240
let diags = project.getLanguageService().getSemanticDiagnostics(root.path);
241241
assert.equal(diags.length, 1);
242242
const diag = diags[0];
243-
assert.equal(diag.code, Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations.code);
244-
assert.equal(flattenDiagnosticMessageText(diag.messageText, "\n"), "Cannot find module 'bar' or its corresponding type declarations.");
243+
assert.equal(diag.code, Diagnostics.Cannot_find_module_0_Did_you_mean_to_set_the_moduleResolution_option_to_node_or_to_add_aliases_to_the_paths_option.code);
244+
assert.equal(flattenDiagnosticMessageText(diag.messageText, "\n"), "Cannot find module 'bar'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?");
245245
callsTrackingHost.verifyCalledOn(CalledMapsWithSingleArg.fileExists, imported.path);
246246

247247

tests/baselines/reference/aliasesInSystemModule1.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/aliasesInSystemModule1.ts(1,24): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
1+
tests/cases/compiler/aliasesInSystemModule1.ts(1,24): error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
22

33

44
==== tests/cases/compiler/aliasesInSystemModule1.ts (1 errors) ====
55
import alias = require('foo');
66
~~~~~
7-
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
7+
!!! error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
88
import cls = alias.Class;
99
export import cls2 = alias.Class;
1010

tests/baselines/reference/aliasesInSystemModule2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/aliasesInSystemModule2.ts(1,21): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
1+
tests/cases/compiler/aliasesInSystemModule2.ts(1,21): error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
22

33

44
==== tests/cases/compiler/aliasesInSystemModule2.ts (1 errors) ====
55
import {alias} from "foo";
66
~~~~~
7-
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
7+
!!! error TS2792: Cannot find module 'foo'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
88
import cls = alias.Class;
99
export import cls2 = alias.Class;
1010

tests/baselines/reference/ambientExternalModuleInAnotherExternalModule.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/ambientExternalModuleInAnotherExternalModule.ts(4,16): error TS2664: Invalid module name in augmentation, module 'ext' cannot be found.
2-
tests/cases/compiler/ambientExternalModuleInAnotherExternalModule.ts(9,22): error TS2307: Cannot find module 'ext' or its corresponding type declarations.
2+
tests/cases/compiler/ambientExternalModuleInAnotherExternalModule.ts(9,22): error TS2792: Cannot find module 'ext'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
33

44

55
==== tests/cases/compiler/ambientExternalModuleInAnotherExternalModule.ts (2 errors) ====
@@ -15,5 +15,5 @@ tests/cases/compiler/ambientExternalModuleInAnotherExternalModule.ts(9,22): erro
1515
// Cannot resolve this ext module reference
1616
import ext = require("ext");
1717
~~~~~
18-
!!! error TS2307: Cannot find module 'ext' or its corresponding type declarations.
18+
!!! error TS2792: Cannot find module 'ext'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
1919
var x = ext;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/amdDependencyComment2.ts(3,21): error TS2307: Cannot find module 'm2' or its corresponding type declarations.
1+
tests/cases/compiler/amdDependencyComment2.ts(3,21): error TS2792: Cannot find module 'm2'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
22

33

44
==== tests/cases/compiler/amdDependencyComment2.ts (1 errors) ====
55
///<amd-dependency path='bar'/>
66

77
import m1 = require("m2")
88
~~~~
9-
!!! error TS2307: Cannot find module 'm2' or its corresponding type declarations.
9+
!!! error TS2792: Cannot find module 'm2'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
1010
m1.f();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/amdDependencyCommentName2.ts(3,21): error TS2307: Cannot find module 'm2' or its corresponding type declarations.
1+
tests/cases/compiler/amdDependencyCommentName2.ts(3,21): error TS2792: Cannot find module 'm2'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
22

33

44
==== tests/cases/compiler/amdDependencyCommentName2.ts (1 errors) ====
55
///<amd-dependency path='bar' name='b'/>
66

77
import m1 = require("m2")
88
~~~~
9-
!!! error TS2307: Cannot find module 'm2' or its corresponding type declarations.
9+
!!! error TS2792: Cannot find module 'm2'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
1010
m1.f();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/amdDependencyCommentName3.ts(5,21): error TS2307: Cannot find module 'm2' or its corresponding type declarations.
1+
tests/cases/compiler/amdDependencyCommentName3.ts(5,21): error TS2792: Cannot find module 'm2'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
22

33

44
==== tests/cases/compiler/amdDependencyCommentName3.ts (1 errors) ====
@@ -8,5 +8,5 @@ tests/cases/compiler/amdDependencyCommentName3.ts(5,21): error TS2307: Cannot fi
88

99
import m1 = require("m2")
1010
~~~~
11-
!!! error TS2307: Cannot find module 'm2' or its corresponding type declarations.
11+
!!! error TS2792: Cannot find module 'm2'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
1212
m1.f();

0 commit comments

Comments
 (0)