Skip to content

Commit 20442fe

Browse files
committed
handle failed lookups
1 parent 95082e4 commit 20442fe

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,12 @@ namespace ts {
403403

404404
const resolvedFileName = result.resolvedModule && result.resolvedModule.resolvedFileName;
405405
// find common prefix between directory and resolved file name
406-
// this common prefix should be the shorted path that has the same resolution
406+
// this common prefix should be the shortest path that has the same resolution
407407
// directory: /a/b/c/d/e
408408
// resolvedFileName: /a/b/foo.d.ts
409-
const commonPrefix = getCommonPrefix(path, resolvedFileName);
409+
// commonPrefix: /a/b
410+
// for failed lookups use the root directory as commonPrefix
411+
const commonPrefix = resolvedFileName ? getCommonPrefix(path, resolvedFileName) : path.substr(0, getRootLength(path));
410412
if (!commonPrefix) {
411413
return;
412414
}
@@ -421,10 +423,7 @@ namespace ts {
421423
}
422424
}
423425

424-
function getCommonPrefix(directory: Path, resolution: string | undefined) {
425-
if (resolution === undefined) {
426-
return undefined;
427-
}
426+
function getCommonPrefix(directory: Path, resolution: string) {
428427
const resolutionDirectory = toPath(getDirectoryPath(resolution), currentDirectory, getCanonicalFileName);
429428

430429
let current = directory;

src/testRunner/unittests/moduleResolution.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ namespace ts {
264264
assert.isDefined(cache.get("c:/foo"));
265265
assert.isUndefined(cache.get("c:/"));
266266
assert.isUndefined(cache.get("d:/"));
267+
268+
cache = resolutionCache.getOrCreateCacheForModuleName("f");
269+
cache.set("/foo/bar/baz", {
270+
resolvedModule: undefined,
271+
failedLookupLocations: [],
272+
});
273+
assert.isDefined(cache.get("/foo/bar/baz"));
274+
assert.isDefined(cache.get("/foo/bar"));
275+
assert.isDefined(cache.get("/foo"));
276+
assert.isDefined(cache.get("/"));
267277
});
268278

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

0 commit comments

Comments
 (0)