diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index e1ac647c2ac..dbf9bf670e6 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -3068,9 +3068,11 @@ namespace ts.projectSystem { projectService.openClientFile(file1.path); const project = projectService.inferredProjects[0]; - const sourceFile = project.getSourceFile(file1.path); - assert.isTrue("test" in sourceFile.resolvedModules); - assert.equal((sourceFile.resolvedModules["test"]).resolvedFileName, moduleFile.path); + const sourceFileForFile1 = project.getSourceFile(file1.path); + const sourceFileForModuleFile = project.getSourceFile(moduleFile.path); + + assert.isNotNull(sourceFileForFile1); + assert.isNotNull(sourceFileForModuleFile); }); }); diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 23b172d1cfe..eba0dbb119c 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1072,6 +1072,10 @@ namespace ts.server { ? this.inferredProjects[0] : new InferredProject(this, this.documentRegistry, this.compilerOptionsForInferredProjects); + if (root.scriptKind === ScriptKind.JS || root.scriptKind === ScriptKind.JSX) { + project.isJsInferredProject = true; + } + project.addRoot(root); this.directoryWatchers.startWatchingContainingDirectoriesForFile( @@ -1079,12 +1083,6 @@ namespace ts.server { project, fileName => this.onConfigFileAddedForInferredProject(fileName)); - if (root.scriptKind === ScriptKind.JS || root.scriptKind === ScriptKind.JSX) { - const options = project.getCompilerOptions(); - options.maxNodeModuleJsDepth = 2; - project.setCompilerOptions(options); - } - project.updateGraph(); if (!useExistingProject) { diff --git a/src/server/project.ts b/src/server/project.ts index 6085ee05159..ca3950a48a8 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -566,9 +566,6 @@ namespace ts.server { setCompilerOptions(compilerOptions: CompilerOptions) { if (compilerOptions) { - if (this.projectKind === ProjectKind.Inferred) { - compilerOptions.allowJs = true; - } compilerOptions.allowNonTsExtensions = true; if (changesAffectModuleResolution(this.compilerOptions, compilerOptions)) { // reset cached unresolved imports if changes in compiler options affected module resolution @@ -715,6 +712,26 @@ namespace ts.server { } })(); + private _isJsInferredProject = false; + set isJsInferredProject(newValue: boolean) { + if (newValue && !this._isJsInferredProject) { + this.setCompilerOptions(this.getCompilerOptions()); + } + this._isJsInferredProject = newValue; + } + + setCompilerOptions(newOptions: CompilerOptions) { + if (!newOptions) { + return; + } + + if (this._isJsInferredProject && typeof newOptions.maxNodeModuleJsDepth !== "number") { + newOptions.maxNodeModuleJsDepth = 2; + } + newOptions.allowJs = true; + super.setCompilerOptions(newOptions); + } + // Used to keep track of what directories are watched for this project directoriesWatchedForTsconfig: string[] = [];