Skip to content

Commit 6515d37

Browse files
committed
Optionally stop looking for the default configured project at
node_modules
1 parent f7d2beb commit 6515d37

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

src/compiler/path.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,8 @@ namespace ts {
852852
directory = parentPath;
853853
}
854854
}
855+
856+
export function isNodeModulesDirectory(dirPath: Path) {
857+
return endsWith(dirPath, "/node_modules");
858+
}
855859
}

src/compiler/resolutionCache.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,6 @@ namespace ts {
428428
return cache && cache.get(moduleName);
429429
}
430430

431-
function isNodeModulesDirectory(dirPath: Path) {
432-
return endsWith(dirPath, "/node_modules");
433-
}
434-
435431
function isNodeModulesAtTypesDirectory(dirPath: Path) {
436432
return endsWith(dirPath, "/node_modules/@types");
437433
}

src/server/editorServices.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ namespace ts.server {
362362
RootOfInferredProjectFalse = "Open file was set as not inferred root",
363363
}
364364

365+
const enum ConfigFileActionResult {
366+
Continue,
367+
Return,
368+
Break,
369+
}
370+
365371
/*@internal*/
366372
interface ConfigFileExistenceInfo {
367373
/**
@@ -1637,7 +1643,7 @@ namespace ts.server {
16371643
* The server must start searching from the directory containing
16381644
* the newly opened file.
16391645
*/
1640-
private forEachConfigFileLocation(info: OpenScriptInfoOrClosedOrConfigFileInfo, action: (configFileName: NormalizedPath, canonicalConfigFilePath: string) => boolean | void) {
1646+
private forEachConfigFileLocation(info: OpenScriptInfoOrClosedOrConfigFileInfo, action: (configFileName: NormalizedPath, canonicalConfigFilePath: string) => ConfigFileActionResult | void) {
16411647
if (this.syntaxOnly) {
16421648
return undefined;
16431649
}
@@ -1658,12 +1664,23 @@ namespace ts.server {
16581664
if (searchInDirectory) {
16591665
const canonicalSearchPath = normalizedPathToPath(searchPath, this.currentDirectory, this.toCanonicalFileName);
16601666
const tsconfigFileName = asNormalizedPath(combinePaths(searchPath, "tsconfig.json"));
1661-
let result = action(tsconfigFileName, combinePaths(canonicalSearchPath, "tsconfig.json"));
1662-
if (result) return tsconfigFileName;
1667+
const tsResult = action(tsconfigFileName, combinePaths(canonicalSearchPath, "tsconfig.json"));
1668+
if (tsResult === ConfigFileActionResult.Return) {
1669+
return tsconfigFileName;
1670+
}
16631671

16641672
const jsconfigFileName = asNormalizedPath(combinePaths(searchPath, "jsconfig.json"));
1665-
result = action(jsconfigFileName, combinePaths(canonicalSearchPath, "jsconfig.json"));
1666-
if (result) return jsconfigFileName;
1673+
const jsResult = action(jsconfigFileName, combinePaths(canonicalSearchPath, "jsconfig.json"));
1674+
if (jsResult === ConfigFileActionResult.Return) {
1675+
return jsconfigFileName;
1676+
}
1677+
1678+
if (tsResult === ConfigFileActionResult.Break || jsResult === ConfigFileActionResult.Break) {
1679+
break;
1680+
}
1681+
1682+
Debug.assert(!tsResult);
1683+
Debug.assert(!jsResult);
16671684
}
16681685

16691686
const parentPath = asNormalizedPath(getDirectoryPath(searchPath));
@@ -1678,7 +1695,7 @@ namespace ts.server {
16781695
/*@internal*/
16791696
findDefaultConfiguredProject(info: ScriptInfo) {
16801697
if (!info.isScriptOpen()) return undefined;
1681-
const configFileName = this.getConfigFileNameForFile(info);
1698+
const configFileName = this.getConfigFileNameForFile(info, /*stopAtNodeModules*/ false);
16821699
return configFileName &&
16831700
this.findConfiguredProjectByProjectName(configFileName);
16841701
}
@@ -1693,11 +1710,15 @@ namespace ts.server {
16931710
* If script info is passed in, it is asserted to be open script info
16941711
* otherwise just file name
16951712
*/
1696-
private getConfigFileNameForFile(info: OpenScriptInfoOrClosedOrConfigFileInfo) {
1713+
private getConfigFileNameForFile(info: OpenScriptInfoOrClosedOrConfigFileInfo, stopAtNodeModules: boolean) {
16971714
if (isOpenScriptInfo(info)) Debug.assert(info.isScriptOpen());
16981715
this.logger.info(`Search path: ${getDirectoryPath(info.fileName)}`);
16991716
const configFileName = this.forEachConfigFileLocation(info, (configFileName, canonicalConfigFilePath) =>
1700-
this.configFileExists(configFileName, canonicalConfigFilePath, info));
1717+
this.configFileExists(configFileName, canonicalConfigFilePath, info)
1718+
? ConfigFileActionResult.Return
1719+
: stopAtNodeModules && isNodeModulesDirectory(getDirectoryPath(canonicalConfigFilePath) as Path)
1720+
? ConfigFileActionResult.Break
1721+
: ConfigFileActionResult.Continue);
17011722
if (configFileName) {
17021723
this.logger.info(`For info: ${info.fileName} :: Config file name: ${configFileName}`);
17031724
}
@@ -2706,7 +2727,7 @@ namespace ts.server {
27062727
// we first detect if there is already a configured project created for it: if so,
27072728
// we re- read the tsconfig file content and update the project only if we havent already done so
27082729
// otherwise we create a new one.
2709-
const configFileName = this.getConfigFileNameForFile(info);
2730+
const configFileName = this.getConfigFileNameForFile(info, /*stopAtNodeModules*/ true);
27102731
if (configFileName) {
27112732
const project = this.findConfiguredProjectByProjectName(configFileName) || this.createConfiguredProject(configFileName);
27122733
if (!updatedProjects.has(configFileName)) {
@@ -2803,7 +2824,7 @@ namespace ts.server {
28032824
if (!this.getScriptInfo(fileName) && !this.host.fileExists(fileName)) return undefined;
28042825

28052826
const originalFileInfo: OriginalFileInfo = { fileName: toNormalizedPath(fileName), path: this.toPath(fileName) };
2806-
const configFileName = this.getConfigFileNameForFile(originalFileInfo);
2827+
const configFileName = this.getConfigFileNameForFile(originalFileInfo, /*stopAtNodeModules*/ false);
28072828
if (!configFileName) return undefined;
28082829

28092830
const configuredProject = this.findConfiguredProjectByProjectName(configFileName) ||
@@ -2857,7 +2878,7 @@ namespace ts.server {
28572878
let project: ConfiguredProject | ExternalProject | undefined = this.findExternalProjectContainingOpenScriptInfo(info);
28582879
let defaultConfigProject: ConfiguredProject | undefined;
28592880
if (!project && !this.syntaxOnly) { // Checking syntaxOnly is an optimization
2860-
configFileName = this.getConfigFileNameForFile(info);
2881+
configFileName = this.getConfigFileNameForFile(info, /*stopAtNodeModules*/ true);
28612882
if (configFileName) {
28622883
project = this.findConfiguredProjectByProjectName(configFileName);
28632884
if (!project) {
@@ -2920,7 +2941,7 @@ namespace ts.server {
29202941
fileName: project.getConfigFilePath(),
29212942
path: info.path,
29222943
configFileInfo: true
2923-
});
2944+
}, /*stopAtNodeModules*/ true);
29242945
if (!configFileName) return;
29252946

29262947
// find or delay load the project

0 commit comments

Comments
 (0)