From 9bf60580afcc86e837a92e8408d3053ad30b2492 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Wed, 26 Feb 2020 18:26:02 -0500 Subject: [PATCH] Cherry-pick PR #37059 into release-3.8 (#37060) Component commits: 724c7555d0 Remove unnecessary assert (since we allow already open file to be opened again even through openFile command - partially) from updateOpen command Fixes #35034 Co-authored-by: Sheetal Nandi --- src/server/editorServices.ts | 9 +--- .../tsserver/applyChangesToOpenFiles.ts | 51 +++++++++++++++---- src/testRunner/unittests/tsserver/openFile.ts | 24 +++++++++ 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index e34aef3532946..4b5a71d2eeb9b 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2412,7 +2412,7 @@ namespace ts.server { this.openFilesWithNonRootedDiskPath.set(this.toCanonicalFileName(fileName), info); } } - if (openedByClient && !info.isScriptOpen()) { + if (openedByClient) { // Opening closed script info // either it was created just now, or was part of projects but was closed this.stopWatchingScriptInfo(info); @@ -2421,9 +2421,6 @@ namespace ts.server { info.registerFileUpdate(); } } - else { - Debug.assert(fileContent === undefined); - } return info; } @@ -3168,11 +3165,9 @@ namespace ts.server { const iterResult = openFiles.next(); if (iterResult.done) break; const file = iterResult.value; - const scriptInfo = this.getScriptInfo(file.fileName); - Debug.assert(!scriptInfo || !scriptInfo.isScriptOpen(), "Script should not exist and not be open already"); // Create script infos so we have the new content for all the open files before we do any updates to projects const info = this.getOrCreateOpenScriptInfo( - scriptInfo ? scriptInfo.fileName : toNormalizedPath(file.fileName), + toNormalizedPath(file.fileName), file.content, tryConvertScriptKindName(file.scriptKind!), file.hasMixedContent, diff --git a/src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts b/src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts index 316ecc8b65234..6d5ee91d68d67 100644 --- a/src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts +++ b/src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts @@ -29,7 +29,11 @@ ${file.content}`; assert.equal(Number(project.getProjectVersion()), expected); } - function verify(applyChangesToOpen: (session: TestSession) => void) { + interface Verify { + applyChangesToOpen: (session: TestSession) => void; + openFile1Again: (session: TestSession) => void; + } + function verify({ applyChangesToOpen, openFile1Again }: Verify) { const host = createServerHost([app, file3, commonFile1, commonFile2, libFile, configFile]); const session = createSession(host); session.executeCommandSeq({ @@ -65,11 +69,22 @@ ${file.content}`; verifyText(service, commonFile2.path, fileContentWithComment(commonFile2)); verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;"); verifyText(service, file3.path, file3.content); + + // Open file1 again + openFile1Again(session); + assert.isTrue(service.getScriptInfo(commonFile1.path)!.isScriptOpen()); + + // Verify that file1 contents are changed + verifyProjectVersion(project, 4); + verifyText(service, commonFile1.path, commonFile1.content); + verifyText(service, commonFile2.path, fileContentWithComment(commonFile2)); + verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;"); + verifyText(service, file3.path, file3.content); } it("with applyChangedToOpenFiles request", () => { - verify(session => - session.executeCommandSeq({ + verify({ + applyChangesToOpen: session => session.executeCommandSeq({ command: protocol.CommandTypes.ApplyChangedToOpenFiles, arguments: { openFiles: [ @@ -101,13 +116,22 @@ ${file.content}`; file3.path ] } - }) - ); + }), + openFile1Again: session => session.executeCommandSeq({ + command: protocol.CommandTypes.ApplyChangedToOpenFiles, + arguments: { + openFiles: [{ + fileName: commonFile1.path, + content: commonFile1.content + }] + } + }), + }); }); it("with updateOpen request", () => { - verify(session => - session.executeCommandSeq({ + verify({ + applyChangesToOpen: session => session.executeCommandSeq({ command: protocol.CommandTypes.UpdateOpen, arguments: { openFiles: [ @@ -141,8 +165,17 @@ ${file.content}`; file3.path ] } - }) - ); + }), + openFile1Again: session => session.executeCommandSeq({ + command: protocol.CommandTypes.UpdateOpen, + arguments: { + openFiles: [{ + file: commonFile1.path, + fileContent: commonFile1.content + }] + } + }), + }); }); }); } diff --git a/src/testRunner/unittests/tsserver/openFile.ts b/src/testRunner/unittests/tsserver/openFile.ts index d2a995c68f88e..62a7fcc729ac8 100644 --- a/src/testRunner/unittests/tsserver/openFile.ts +++ b/src/testRunner/unittests/tsserver/openFile.ts @@ -104,5 +104,29 @@ namespace ts.projectSystem { checkProjectActualFiles(project, files.map(f => f.path)); } }); + + it("can open same file again", () => { + const projectFolder = "/user/someuser/projects/myproject"; + const aFile: File = { + path: `${projectFolder}/src/a.ts`, + content: "export const x = 0;" + }; + const configFile: File = { + path: `${projectFolder}/tsconfig.json`, + content: "{}" + }; + const files = [aFile, configFile, libFile]; + const host = createServerHost(files); + const service = createProjectService(host); + verifyProject(aFile.content); + verifyProject(`${aFile.content}export const y = 10;`); + + function verifyProject(aFileContent: string) { + service.openClientFile(aFile.path, aFileContent, ScriptKind.TS, projectFolder); + const project = service.configuredProjects.get(configFile.path)!; + checkProjectActualFiles(project, files.map(f => f.path)); + assert.equal(project.getCurrentProgram()?.getSourceFile(aFile.path)!.text, aFileContent); + } + }); }); }