Skip to content

Commit e77f9b2

Browse files
committed
allow to change settings for inferred projects
2 parents 361a852 + 6d497e3 commit e77f9b2

5 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,5 +1325,26 @@ namespace ts {
13251325
const actualText2 = snap2.getText(0, snap.getLength());
13261326
assert.equal(actualText2, "", `expected content to be empty string, got "${actualText2}"`);
13271327
});
1328+
1329+
it("project settings for inferred projects", () => {
1330+
const file1 = {
1331+
path: "/a/b/app.ts",
1332+
content: `import {x} from "mod"`
1333+
};
1334+
const modFile = {
1335+
path: "/a/mod.ts",
1336+
content: "export let x: number"
1337+
};
1338+
const host = createServerHost([file1, modFile]);
1339+
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useSingleInferredProject*/ false);
1340+
1341+
projectService.openClientFile(file1.path);
1342+
projectService.openClientFile(modFile.path);
1343+
1344+
checkNumberOfProjects(projectService, { inferredProjects: 2 });
1345+
1346+
projectService.setCompilerOptionsForInferredProjects({ moduleResolution: ModuleResolutionKind.Classic });
1347+
checkNumberOfProjects(projectService, { inferredProjects: 1 });
1348+
});
13281349
});
13291350
}

src/server/editorServices.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ namespace ts.server {
158158
*/
159159
readonly openFiles: ScriptInfo[] = [];
160160

161+
private compilerOptionsForInferredProjects: CompilerOptions;
161162
private readonly directoryWatchers: DirectoryWatchers;
162163
private readonly throttledOperations: ThrottledOperations;
163164

@@ -194,6 +195,14 @@ namespace ts.server {
194195
this.ensureInferredProjectsUpToDate();
195196
}
196197

198+
setCompilerOptionsForInferredProjects(compilerOptions: CompilerOptions): void {
199+
this.compilerOptionsForInferredProjects = compilerOptions;
200+
for (const proj of this.inferredProjects) {
201+
proj.setCompilerOptions(compilerOptions);
202+
}
203+
this.updateProjectGraphs(this.inferredProjects);
204+
}
205+
197206
stopWatchingDirectory(directory: string) {
198207
this.directoryWatchers.stopWatchingDirectory(directory);
199208
}
@@ -834,7 +843,7 @@ namespace ts.server {
834843
const useExistingProject = this.useSingleInferredProject && this.inferredProjects.length;
835844
const project = useExistingProject
836845
? this.inferredProjects[0]
837-
: new InferredProject(this, this.documentRegistry, /*languageServiceEnabled*/ true);
846+
: new InferredProject(this, this.documentRegistry, /*languageServiceEnabled*/ true, this.compilerOptionsForInferredProjects);
838847

839848
project.addRoot(root);
840849

src/server/project.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ namespace ts.server {
267267

268268
setCompilerOptions(compilerOptions: CompilerOptions) {
269269
if (compilerOptions) {
270+
if (this.projectKind === ProjectKind.Inferred) {
271+
compilerOptions.allowJs = true;
272+
}
270273
compilerOptions.allowNonTsExtensions = true;
271274
this.compilerOptions = compilerOptions;
272275
this.lsHost.setCompilationSettings(compilerOptions);
@@ -351,13 +354,13 @@ namespace ts.server {
351354
// Used to keep track of what directories are watched for this project
352355
directoriesWatchedForTsconfig: string[] = [];
353356

354-
constructor(projectService: ProjectService, documentRegistry: ts.DocumentRegistry, languageServiceEnabled: boolean) {
357+
constructor(projectService: ProjectService, documentRegistry: ts.DocumentRegistry, languageServiceEnabled: boolean, compilerOptions: CompilerOptions) {
355358
super(ProjectKind.Inferred,
356359
projectService,
357360
documentRegistry,
358361
/*files*/ undefined,
359362
languageServiceEnabled,
360-
/*compilerOptions*/ undefined);
363+
compilerOptions);
361364

362365
this.inferredProjectName = makeInferredProjectName(InferredProject.NextId);
363366
InferredProject.NextId++;

src/server/protocol.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,14 @@ declare namespace ts.server.protocol {
696696
closedFiles?: string[];
697697
}
698698

699+
export interface SetCompilerOptionsForInferredProjectsArgs {
700+
options: CompilerOptions;
701+
}
702+
703+
export interface SetCompilerOptionsForInferredProjectsRequest extends Request {
704+
arguments: SetCompilerOptionsForInferredProjectsArgs;
705+
}
706+
699707
/**
700708
* Exit request; value of command field is "exit". Ask the server process
701709
* to exit.

src/server/session.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ namespace ts.server {
143143
export const CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full";
144144
export const NameOrDottedNameSpan = "nameOrDottedNameSpan";
145145
export const BreakpointStatement = "breakpointStatement";
146+
export const CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects";
146147
}
147148

148149
namespace Errors {
@@ -502,6 +503,10 @@ namespace ts.server {
502503
}
503504
}
504505

506+
private setCompilerOptionsForInferredProjects(args: protocol.SetCompilerOptionsForInferredProjectsArgs): void {
507+
this.projectService.setCompilerOptionsForInferredProjects(args.options);
508+
}
509+
505510
private getProjectInfo(args: protocol.ProjectInfoRequestArgs): protocol.ProjectInfo {
506511
return this.getProjectInfoWorker(args.file, args.projectFileName, args.needFileNameList);
507512
}
@@ -1433,6 +1438,9 @@ namespace ts.server {
14331438
[CommandNames.DocumentHighlightsFull]: (request: protocol.DocumentHighlightsRequest) => {
14341439
return this.requiredResponse(this.getDocumentHighlights(request.arguments, /*simplifiedResult*/ false));
14351440
},
1441+
[CommandNames.CompilerOptionsForInferredProjects]: (request: protocol.SetCompilerOptionsForInferredProjectsRequest) => {
1442+
return this.requiredResponse(this.setCompilerOptionsForInferredProjects(request.arguments));
1443+
},
14361444
[CommandNames.ProjectInfo]: (request: protocol.ProjectInfoRequest) => {
14371445
return this.requiredResponse(this.getProjectInfo(request.arguments));
14381446
},

0 commit comments

Comments
 (0)