-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Syntax only server creates inferred project with all the open files w… #38561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
5726838
ffe50a3
90d8a96
cb638d9
807c008
89127a5
3e1a3c2
901cfc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -575,6 +575,42 @@ namespace ts.server { | |
| undefined; | ||
| } | ||
|
|
||
| const invalidSyntaxOnlyCommands: readonly CommandNames[] = [ | ||
| CommandNames.OpenExternalProject, | ||
| CommandNames.OpenExternalProjects, | ||
| CommandNames.CloseExternalProject, | ||
| CommandNames.SynchronizeProjectList, | ||
| CommandNames.EmitOutput, | ||
| CommandNames.CompileOnSaveAffectedFileList, | ||
| CommandNames.CompileOnSaveEmitFile, | ||
| CommandNames.CompilerOptionsDiagnosticsFull, | ||
| CommandNames.EncodedSemanticClassificationsFull, | ||
| CommandNames.SemanticDiagnosticsSync, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SyntacticDiagnosticsSync: This is somewhat project setting dependent ? Atleast at some point parsing errors use to be different based on target .. eg target determines what unicode is considered identifier start... So i am not sure if this should be enabled.. But if we do enable we i was wondering if GetErr should only do syntax checks and skip semantic and suggetion diagnostics on syntax server Some questionable which i have disabled for now |
||
| CommandNames.SyntacticDiagnosticsSync, | ||
| CommandNames.SuggestionDiagnosticsSync, | ||
| CommandNames.Geterr, | ||
| CommandNames.GeterrForProject, | ||
| CommandNames.Reload, | ||
| CommandNames.ReloadProjects, | ||
| CommandNames.GetCodeFixes, | ||
| CommandNames.GetCodeFixesFull, | ||
| CommandNames.GetCombinedCodeFix, | ||
| CommandNames.GetCombinedCodeFixFull, | ||
| CommandNames.ApplyCodeActionCommand, | ||
| CommandNames.GetSupportedCodeFixes, | ||
| CommandNames.GetApplicableRefactors, | ||
| CommandNames.GetEditsForRefactor, | ||
| CommandNames.GetEditsForRefactorFull, | ||
| CommandNames.OrganizeImports, | ||
| CommandNames.OrganizeImportsFull, | ||
| CommandNames.GetEditsForFileRename, | ||
| CommandNames.GetEditsForFileRenameFull, | ||
| CommandNames.ConfigurePlugin, | ||
| CommandNames.PrepareCallHierarchy, | ||
| CommandNames.ProvideCallHierarchyIncomingCalls, | ||
| CommandNames.ProvideCallHierarchyOutgoingCalls, | ||
| ]; | ||
|
|
||
| export interface SessionOptions { | ||
| host: ServerHost; | ||
| cancellationToken: ServerCancellationToken; | ||
|
|
@@ -667,6 +703,15 @@ namespace ts.server { | |
| this.projectService = new ProjectService(settings); | ||
| this.projectService.setPerformanceEventHandler(this.performanceEventHandler.bind(this)); | ||
| this.gcTimer = new GcTimer(this.host, /*delay*/ 7000, this.logger); | ||
|
|
||
| // Make sure to setup handlers to throw error for not allowed commands on syntax server; | ||
| if (this.projectService.syntaxOnly) { | ||
| invalidSyntaxOnlyCommands.forEach(commandName => | ||
| this.handlers.set(commandName, request => { | ||
| throw new Error(`Request: ${request.command} not allowed on syntaxServer:: Request::${JSON.stringify(request)}`); | ||
sheetalkamat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private sendRequestCompletedEvent(requestId: number): void { | ||
|
|
@@ -1253,9 +1298,9 @@ namespace ts.server { | |
| } | ||
|
|
||
| private getJsxClosingTag(args: protocol.JsxClosingTagRequestArgs): TextInsertion | undefined { | ||
| const { file, project } = this.getFileAndProject(args); | ||
| const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args); | ||
| const position = this.getPositionInFile(args, file); | ||
| const tag = project.getLanguageService().getJsxClosingTagAtPosition(file, position); | ||
| const tag = languageService.getJsxClosingTagAtPosition(file, position); | ||
| return tag === undefined ? undefined : { newText: tag.newText, caretOffset: 0 }; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1171,6 +1171,26 @@ namespace ts { | |
| } | ||
| } | ||
|
|
||
| const invalidOperationsOnSyntaxOnly: readonly (keyof LanguageService)[] = [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this relate to the list in session.ts? Do they need to stay in sync? Is one redundant? |
||
| "getSyntacticDiagnostics", | ||
| "getSemanticDiagnostics", | ||
| "getSuggestionDiagnostics", | ||
| "getCompilerOptionsDiagnostics", | ||
| "getSemanticClassifications", | ||
| "getEncodedSemanticClassifications", | ||
| "getCodeFixesAtPosition", | ||
| "getCombinedCodeFix", | ||
| "applyCodeActionCommand", | ||
| "organizeImports", | ||
| "getEditsForFileRename", | ||
| "getEmitOutput", | ||
| "getApplicableRefactors", | ||
| "getEditsForRefactor", | ||
| "prepareCallHierarchy", | ||
| "provideCallHierarchyIncomingCalls", | ||
| "provideCallHierarchyOutgoingCalls", | ||
| ]; | ||
|
|
||
| export function createLanguageService( | ||
| host: LanguageServiceHost, | ||
| documentRegistry: DocumentRegistry = createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory()), | ||
|
|
@@ -1224,8 +1244,6 @@ namespace ts { | |
| } | ||
|
|
||
| function synchronizeHostData(): void { | ||
| Debug.assert(!syntaxOnly); | ||
|
|
||
| // perform fast check if host supports it | ||
| if (host.getProjectVersion) { | ||
| const hostProjectVersion = host.getProjectVersion(); | ||
|
|
@@ -1419,11 +1437,6 @@ namespace ts { | |
|
|
||
| // TODO: GH#18217 frequently asserted as defined | ||
| function getProgram(): Program | undefined { | ||
| if (syntaxOnly) { | ||
| Debug.assert(program === undefined); | ||
| return undefined; | ||
| } | ||
|
|
||
| synchronizeHostData(); | ||
|
|
||
| return program; | ||
|
|
@@ -2199,7 +2212,7 @@ namespace ts { | |
| return declaration ? CallHierarchy.getOutgoingCalls(program, declaration) : []; | ||
| } | ||
|
|
||
| return { | ||
| const ls: LanguageService = { | ||
| dispose, | ||
| cleanupSemanticCache, | ||
| getSyntacticDiagnostics, | ||
|
|
@@ -2259,6 +2272,16 @@ namespace ts { | |
| provideCallHierarchyIncomingCalls, | ||
| provideCallHierarchyOutgoingCalls | ||
| }; | ||
|
|
||
| if (syntaxOnly) { | ||
| invalidOperationsOnSyntaxOnly.forEach(key => | ||
| ls[key] = (...args: any[]) => { | ||
| throw new Error(`LanguageService Operation: ${key} not allowed on syntaxServer:: arguments::${JSON.stringify(args)}`); | ||
|
||
| } | ||
| ); | ||
| } | ||
|
|
||
| return ls; | ||
| } | ||
|
|
||
| /* @internal */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| namespace ts.projectSystem { | ||
| describe("unittests:: tsserver:: Semantic operations on Syntax server", () => { | ||
| function setup() { | ||
| const file1: File = { | ||
| path: `${tscWatch.projectRoot}/a.ts`, | ||
| content: `import { y } from "./b"; | ||
| class c { prop = "hello"; foo() { return this.prop; } }` | ||
| }; | ||
| const file2: File = { | ||
| path: `${tscWatch.projectRoot}/b.ts`, | ||
| content: "export const y = 10;" | ||
| }; | ||
| const configFile: File = { | ||
| path: `${tscWatch.projectRoot}/tsconfig.json`, | ||
| content: "{}" | ||
| }; | ||
| const host = createServerHost([file1, file2, libFile, configFile]); | ||
| const session = createSession(host, { syntaxOnly: true, useSingleInferredProject: true }); | ||
| return { host, session, file1, file2, configFile }; | ||
| } | ||
|
|
||
| it("open files are added to inferred project even if config file is present and semantic operations succeed", () => { | ||
| const { host, session, file1, file2 } = setup(); | ||
| const service = session.getProjectService(); | ||
| openFilesForSession([file1], session); | ||
| checkNumberOfProjects(service, { inferredProjects: 1 }); | ||
| const project = service.inferredProjects[0]; | ||
| checkProjectActualFiles(project, [libFile.path, file1.path]); // Import is not resolved | ||
| verifyCompletions(); | ||
|
|
||
| openFilesForSession([file2], session); | ||
| checkNumberOfProjects(service, { inferredProjects: 1 }); | ||
| checkProjectActualFiles(project, [libFile.path, file1.path, file2.path]); | ||
| verifyCompletions(); | ||
|
|
||
| function verifyCompletions() { | ||
| assert.isTrue(project.languageServiceEnabled); | ||
| checkWatchedFiles(host, emptyArray); | ||
| checkWatchedDirectories(host, emptyArray, /*recursive*/ true); | ||
| checkWatchedDirectories(host, emptyArray, /*recursive*/ false); | ||
| const response = session.executeCommandSeq<protocol.CompletionsRequest>({ | ||
| command: protocol.CommandTypes.Completions, | ||
| arguments: protocolFileLocationFromSubstring(file1, "prop", { index: 1 }) | ||
| }).response as protocol.CompletionEntry[]; | ||
| assert.deepEqual(response, [ | ||
| completionEntry("foo", ScriptElementKind.memberFunctionElement), | ||
| completionEntry("prop", ScriptElementKind.memberVariableElement), | ||
| ]); | ||
| } | ||
|
|
||
| function completionEntry(name: string, kind: ScriptElementKind): protocol.CompletionEntry { | ||
| return { | ||
| name, | ||
| kind, | ||
| kindModifiers: "", | ||
| sortText: Completions.SortText.LocationPriority, | ||
| hasAction: undefined, | ||
| insertText: undefined, | ||
| isRecommended: undefined, | ||
| replacementSpan: undefined, | ||
| source: undefined | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| it("throws on unsupported commands", () => { | ||
| const { session, file1 } = setup(); | ||
| const service = session.getProjectService(); | ||
| openFilesForSession([file1], session); | ||
| let hasException = false; | ||
| const request: protocol.SemanticDiagnosticsSyncRequest = { | ||
| type: "request", | ||
| seq: 1, | ||
| command: protocol.CommandTypes.SemanticDiagnosticsSync, | ||
| arguments: { file: file1.path } | ||
| }; | ||
| try { | ||
| session.executeCommand(request); | ||
| } | ||
| catch (e) { | ||
| assert.equal(e.message, `Request: semanticDiagnosticsSync not allowed on syntaxServer:: Request::${JSON.stringify(request)}`); | ||
| hasException = true; | ||
| } | ||
| assert.isTrue(hasException); | ||
|
|
||
| hasException = false; | ||
| const project = service.inferredProjects[0]; | ||
| try { | ||
| project.getLanguageService().getSemanticDiagnostics(file1.path); | ||
| } | ||
| catch (e) { | ||
| assert.equal(e.message, `LanguageService Operation: getSemanticDiagnostics not allowed on syntaxServer:: arguments::${JSON.stringify([file1.path])}`); | ||
| hasException = true; | ||
| } | ||
| assert.isTrue(hasException); | ||
| }); | ||
| }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.