diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 3412c1c1598f7..47ab9e6a12f62 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2414,7 +2414,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); @@ -2423,9 +2423,6 @@ namespace ts.server { info.registerFileUpdate(); } } - else { - Debug.assert(fileContent === undefined); - } return info; } @@ -3170,11 +3167,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); + } + }); }); }