Skip to content

Commit

Permalink
Cherry-pick PR #37059 into release-3.8 (#37060)
Browse files Browse the repository at this point in the history
Component commits:
724c755 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 <[email protected]>
  • Loading branch information
TypeScript Bot and sheetalkamat authored Feb 26, 2020
1 parent 622c206 commit 9bf6058
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
9 changes: 2 additions & 7 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -2421,9 +2421,6 @@ namespace ts.server {
info.registerFileUpdate();
}
}
else {
Debug.assert(fileContent === undefined);
}
return info;
}

Expand Down Expand Up @@ -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,
Expand Down
51 changes: 42 additions & 9 deletions src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<protocol.OpenRequest>({
Expand Down Expand Up @@ -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<protocol.ApplyChangedToOpenFilesRequest>({
verify({
applyChangesToOpen: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
command: protocol.CommandTypes.ApplyChangedToOpenFiles,
arguments: {
openFiles: [
Expand Down Expand Up @@ -101,13 +116,22 @@ ${file.content}`;
file3.path
]
}
})
);
}),
openFile1Again: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
command: protocol.CommandTypes.ApplyChangedToOpenFiles,
arguments: {
openFiles: [{
fileName: commonFile1.path,
content: commonFile1.content
}]
}
}),
});
});

it("with updateOpen request", () => {
verify(session =>
session.executeCommandSeq<protocol.UpdateOpenRequest>({
verify({
applyChangesToOpen: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [
Expand Down Expand Up @@ -141,8 +165,17 @@ ${file.content}`;
file3.path
]
}
})
);
}),
openFile1Again: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [{
file: commonFile1.path,
fileContent: commonFile1.content
}]
}
}),
});
});
});
}
24 changes: 24 additions & 0 deletions src/testRunner/unittests/tsserver/openFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
}

0 comments on commit 9bf6058

Please sign in to comment.