diff --git a/src/services/pathCompletions.ts b/src/services/pathCompletions.ts index 780b14db719..190e980c936 100644 --- a/src/services/pathCompletions.ts +++ b/src/services/pathCompletions.ts @@ -144,8 +144,8 @@ namespace ts.Completions.PathCompletions { let result: CompletionEntry[]; + const fileExtensions = getSupportedExtensions(compilerOptions); if (baseUrl) { - const fileExtensions = getSupportedExtensions(compilerOptions); const projectDir = compilerOptions.project || host.getCurrentDirectory(); const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl); result = getCompletionEntriesForDirectoryFragment(fragment, normalizePath(absolute), fileExtensions, /*includeExtensions*/ false, span, host); @@ -176,6 +176,15 @@ namespace ts.Completions.PathCompletions { result = []; } + if (compilerOptions.moduleResolution === ts.ModuleResolutionKind.NodeJs) { + forEachAncestorDirectory(scriptPath, ancestor => { + const nodeModules = combinePaths(ancestor, "node_modules"); + if (host.directoryExists(nodeModules)) { + getCompletionEntriesForDirectoryFragment(fragment, nodeModules, fileExtensions, /*includeExtensions*/ false, span, host, /*exclude*/ undefined, result); + } + }); + } + getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span, result); for (const moduleName of enumeratePotentialNonRelativeModules(fragment, scriptPath, compilerOptions, typeChecker, host)) { diff --git a/tests/cases/fourslash/completionsPaths.ts b/tests/cases/fourslash/completionsPaths.ts new file mode 100644 index 00000000000..9473ae54b25 --- /dev/null +++ b/tests/cases/fourslash/completionsPaths.ts @@ -0,0 +1,21 @@ +/// + +// @moduleResolution: node + +// @Filename: /node_modules/x/foo.d.ts +////not read + +// @Filename: /node_modules/x/bar.d.ts +////not read + +// @Filename: /src/node_modules/y/index.d.ts +////not read + +// @Filename: /src/a.ts +////import {} from "/*1*/"; + +// @Filename: /src/folder/b.ts +////import {} from "x//*2*/"; + +verify.completionsAt("1", ["y", "x"]); +verify.completionsAt("2", ["bar", "foo"]);