Skip to content

Commit 95082e4

Browse files
committed
make it work for root directory
1 parent 27d2a2d commit 95082e4

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ namespace ts {
407407
// directory: /a/b/c/d/e
408408
// resolvedFileName: /a/b/foo.d.ts
409409
const commonPrefix = getCommonPrefix(path, resolvedFileName);
410-
if (commonPrefix === undefined) {
410+
if (!commonPrefix) {
411411
return;
412412
}
413413
let current = path;
@@ -427,23 +427,21 @@ namespace ts {
427427
}
428428
const resolutionDirectory = toPath(getDirectoryPath(resolution), currentDirectory, getCanonicalFileName);
429429

430-
// find first position where directory and resolution differs
431-
let i = 0;
432-
while (i < Math.min(directory.length, resolutionDirectory.length) && directory.charCodeAt(i) === resolutionDirectory.charCodeAt(i)) {
433-
i++;
434-
}
435-
436-
if (i === directory.length && resolutionDirectory.length > i && resolutionDirectory[i] === directorySeparator) {
437-
return directory;
438-
}
439-
440-
// find last directory separator before position i
441-
const sep = directory.lastIndexOf(directorySeparator, i);
442-
if (sep < 0) {
443-
return undefined;
430+
let current = directory;
431+
let parent = getDirectoryPath(current);
432+
while (
433+
// keep going until we find a matching prefix
434+
!startsWith(resolutionDirectory, current) ||
435+
// keep going if the prefix is not a complete directory segment, e.g. '/dir' as prefix of '/directory'
436+
resolutionDirectory.length > current.length && resolutionDirectory[current.length] !== directorySeparator && current !== parent
437+
) {
438+
if (current === parent) {
439+
return undefined;
440+
}
441+
current = parent;
442+
parent = getDirectoryPath(current);
444443
}
445-
446-
return directory.substr(0, sep);
444+
return current;
447445
}
448446
}
449447
}

src/testRunner/unittests/moduleResolution.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ namespace ts {
195195

196196
describe("Node module resolution - non-relative paths", () => {
197197
it("computes correct commonPrefix for moduleName cache", () => {
198-
const cache = createModuleResolutionCache("/", (f) => f).getOrCreateCacheForModuleName("a");
198+
const resolutionCache = createModuleResolutionCache("/", (f) => f);
199+
let cache = resolutionCache.getOrCreateCacheForModuleName("a");
199200
cache.set("/sub", {
200201
resolvedModule: {
201202
originalPath: undefined,
@@ -207,6 +208,62 @@ namespace ts {
207208
});
208209
assert.isDefined(cache.get("/sub"));
209210
assert.isUndefined(cache.get("/"));
211+
212+
cache = resolutionCache.getOrCreateCacheForModuleName("b");
213+
cache.set("/sub/dir/foo", {
214+
resolvedModule: {
215+
originalPath: undefined,
216+
resolvedFileName: "/sub/directory/node_modules/b/index.ts",
217+
isExternalLibraryImport: true,
218+
extension: Extension.Ts,
219+
},
220+
failedLookupLocations: [],
221+
});
222+
assert.isDefined(cache.get("/sub/dir/foo"));
223+
assert.isDefined(cache.get("/sub/dir"));
224+
assert.isDefined(cache.get("/sub"));
225+
assert.isUndefined(cache.get("/"));
226+
227+
cache = resolutionCache.getOrCreateCacheForModuleName("c");
228+
cache.set("/foo/bar", {
229+
resolvedModule: {
230+
originalPath: undefined,
231+
resolvedFileName: "/bar/node_modules/c/index.ts",
232+
isExternalLibraryImport: true,
233+
extension: Extension.Ts,
234+
},
235+
failedLookupLocations: [],
236+
});
237+
assert.isDefined(cache.get("/foo/bar"));
238+
assert.isDefined(cache.get("/foo"));
239+
assert.isDefined(cache.get("/"));
240+
241+
cache = resolutionCache.getOrCreateCacheForModuleName("d");
242+
cache.set("/foo", {
243+
resolvedModule: {
244+
originalPath: undefined,
245+
resolvedFileName: "/foo/index.ts",
246+
isExternalLibraryImport: true,
247+
extension: Extension.Ts,
248+
},
249+
failedLookupLocations: [],
250+
});
251+
assert.isDefined(cache.get("/foo"));
252+
assert.isUndefined(cache.get("/"));
253+
254+
cache = resolutionCache.getOrCreateCacheForModuleName("e");
255+
cache.set("c:/foo", {
256+
resolvedModule: {
257+
originalPath: undefined,
258+
resolvedFileName: "d:/bar/node_modules/e/index.ts",
259+
isExternalLibraryImport: true,
260+
extension: Extension.Ts,
261+
},
262+
failedLookupLocations: [],
263+
});
264+
assert.isDefined(cache.get("c:/foo"));
265+
assert.isUndefined(cache.get("c:/"));
266+
assert.isUndefined(cache.get("d:/"));
210267
});
211268

212269
it("load module as file - ts files not loaded", () => {

0 commit comments

Comments
 (0)