Skip to content

Commit 3820dfa

Browse files
committed
Merge pull request #7068 from Microsoft/relativeNamesInClassicResolution
classic resolution: don't perform folder walk if module name is relative
2 parents 4353865 + 3ecc42b commit 3820dfa

File tree

5 files changed

+62
-24
lines changed

5 files changed

+62
-24
lines changed

src/compiler/program.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -533,18 +533,25 @@ namespace ts {
533533
}
534534

535535
let referencedSourceFile: string;
536-
while (true) {
537-
const searchName = normalizePath(combinePaths(containingDirectory, moduleName));
538-
referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
539-
if (referencedSourceFile) {
540-
break;
541-
}
542-
const parentPath = getDirectoryPath(containingDirectory);
543-
if (parentPath === containingDirectory) {
544-
break;
536+
if (moduleHasNonRelativeName(moduleName)) {
537+
while (true) {
538+
const searchName = normalizePath(combinePaths(containingDirectory, moduleName));
539+
referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
540+
if (referencedSourceFile) {
541+
break;
542+
}
543+
const parentPath = getDirectoryPath(containingDirectory);
544+
if (parentPath === containingDirectory) {
545+
break;
546+
}
547+
containingDirectory = parentPath;
545548
}
546-
containingDirectory = parentPath;
547549
}
550+
else {
551+
const candidate = normalizePath(combinePaths(containingDirectory, moduleName));
552+
referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state);
553+
}
554+
548555

549556
return referencedSourceFile
550557
? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/somefolder/a.ts(2,17): error TS2307: Cannot find module './b'.
2+
3+
4+
==== tests/cases/compiler/somefolder/a.ts (1 errors) ====
5+
6+
import {x} from "./b"
7+
~~~~~
8+
!!! error TS2307: Cannot find module './b'.
9+
10+
==== tests/cases/compiler/b.ts (0 errors) ====
11+
export let x = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/relativeNamesInClassicResolution.ts] ////
2+
3+
//// [a.ts]
4+
5+
import {x} from "./b"
6+
7+
//// [b.ts]
8+
export let x = 1;
9+
10+
//// [a.js]
11+
define(["require", "exports"], function (require, exports) {
12+
"use strict";
13+
});
14+
//// [b.js]
15+
define(["require", "exports"], function (require, exports) {
16+
"use strict";
17+
exports.x = 1;
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @module:amd
2+
3+
// @filename: somefolder/a.ts
4+
import {x} from "./b"
5+
6+
// @filename: b.ts
7+
export let x = 1;

tests/cases/unittests/moduleResolution.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module ts {
4848
return hasProperty(directories, path);
4949
},
5050
fileExists: path => {
51-
assert.isTrue(hasProperty(directories, getDirectoryPath(path)), "'fileExists' request in non-existing directory");
51+
assert.isTrue(hasProperty(directories, getDirectoryPath(path)), `'fileExists' '${path}' request in non-existing directory`);
5252
return hasProperty(map, path);
5353
}
5454
}
@@ -814,7 +814,6 @@ import b = require("./moduleB.ts");
814814

815815
it ("classic + rootDirs", () => {
816816
test(/*hasDirectoryExists*/ false);
817-
test(/*hasDirectoryExists*/ true);
818817

819818
function test(hasDirectoryExists: boolean) {
820819
let file1: File = { name: "/root/folder1/file1.ts" };
@@ -844,24 +843,20 @@ import b = require("./moduleB.ts");
844843
"/root/generated/folder1/file1.d.ts",
845844
// then try alternative rootDir entry
846845
]);
847-
check("../folder1/file1_1", file3, file4, [
848-
// load from initial location
849-
"/root/generated/folder1/file1_1.ts",
850-
"/root/generated/folder1/file1_1.tsx",
851-
"/root/generated/folder1/file1_1.d.ts",
852-
// load from alternative rootDir entry
853-
"/root/folder1/file1_1.ts",
854-
"/root/folder1/file1_1.tsx",
855-
"/root/folder1/file1_1.d.ts",
856-
// fallback to classic
857-
// step1: initial location
846+
check("folder1/file1_1", file3, file4, [
847+
// current location
848+
"/root/generated/folder2/folder1/file1_1.ts",
849+
"/root/generated/folder2/folder1/file1_1.tsx",
850+
"/root/generated/folder2/folder1/file1_1.d.ts",
851+
// other entry in rootDirs
858852
"/root/generated/folder1/file1_1.ts",
859853
"/root/generated/folder1/file1_1.tsx",
860854
"/root/generated/folder1/file1_1.d.ts",
861-
// step2: walk 1 level up
855+
// fallback
862856
"/root/folder1/file1_1.ts",
863857
"/root/folder1/file1_1.tsx",
864858
"/root/folder1/file1_1.d.ts",
859+
// found one
865860
]);
866861

867862
function check(name: string, container: File, expected: File, expectedFailedLookups: string[]) {

0 commit comments

Comments
 (0)