Skip to content

Commit b9592d4

Browse files
committed
Use the parent most node_modules directory for module resolution failed lookup locations
1 parent 1db7623 commit b9592d4

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/compiler/resolutionCache.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,17 @@ namespace ts {
323323
let dir = getDirectoryPath(getNormalizedAbsolutePath(failedLookupLocation, getCurrentDirectory()));
324324
let dirPath = getDirectoryPath(failedLookupLocationPath);
325325

326+
// If directory path contains node module, get the most parent node_modules directory for watching
327+
while (dirPath.indexOf("/node_modules/") !== -1) {
328+
dir = getDirectoryPath(dir);
329+
dirPath = getDirectoryPath(dirPath);
330+
}
331+
326332
// If the directory is node_modules use it to watch
327333
if (isNodeModulesDirectory(dirPath)) {
328334
return { dir, dirPath };
329335
}
330336

331-
// If directory path contains node module, get the node_modules directory for watching
332-
if (dirPath.indexOf("/node_modules/") !== -1) {
333-
while (!isNodeModulesDirectory(dirPath)) {
334-
dir = getDirectoryPath(dir);
335-
dirPath = getDirectoryPath(dirPath);
336-
}
337-
return { dir, dirPath };
338-
}
339337

340338
// Use some ancestor of the root directory
341339
if (rootPath !== undefined) {

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,43 @@ namespace ts.projectSystem {
23992399
checkWatchedDirectories(host, watchedRecursiveDirectories, /*recursive*/ true);
24002400

24012401
});
2402+
2403+
it("Failed lookup locations are uses parent most node_modules directory", () => {
2404+
const file1: FileOrFolder = {
2405+
path: "/a/b/src/file1.ts",
2406+
content: 'import { classc } from "module1"'
2407+
};
2408+
const module1: FileOrFolder = {
2409+
path: "/a/b/node_modules/module1/index.d.ts",
2410+
content: `import { class2 } from "module2";
2411+
export classc { method2a(): class2; }`
2412+
};
2413+
const module2: FileOrFolder = {
2414+
path: "/a/b/node_modules/module2/index.d.ts",
2415+
content: "export class2 { method2() { return 10; } }"
2416+
};
2417+
const module3: FileOrFolder = {
2418+
path: "/a/b/node_modules/module/node_modules/module3/index.d.ts",
2419+
content: "export class3 { method2() { return 10; } }"
2420+
};
2421+
const configFile: FileOrFolder = {
2422+
path: "/a/b/src/tsconfig.json",
2423+
content: JSON.stringify({ files: [file1.path] })
2424+
};
2425+
const files = [file1, module1, module2, module3, configFile, libFile];
2426+
const host = createServerHost(files);
2427+
const projectService = createProjectService(host);
2428+
projectService.openClientFile(file1.path);
2429+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
2430+
const project = projectService.configuredProjects.get(configFile.path);
2431+
assert.isDefined(project);
2432+
checkProjectActualFiles(project, [file1.path, libFile.path, module1.path, module2.path, configFile.path]);
2433+
checkWatchedFiles(host, [libFile.path, module1.path, module2.path, configFile.path]);
2434+
checkWatchedDirectories(host, [], /*recursive*/ false);
2435+
const watchedRecursiveDirectories = getTypeRootsFromLocation("/a/b/src");
2436+
watchedRecursiveDirectories.push("/a/b/src", "/a/b/node_modules");
2437+
checkWatchedDirectories(host, watchedRecursiveDirectories, /*recursive*/ true);
2438+
});
24022439
});
24032440

24042441
describe("Proper errors", () => {

0 commit comments

Comments
 (0)