Remove unnecessary assert (since we allow already open file to be opened again even through openFile command - partially) from updateOpen command (#37059)

Fixes #35034
This commit is contained in:
Sheetal Nandi 2020-02-26 15:25:51 -08:00 committed by GitHub
parent c4e96856ac
commit 05c9ec3f12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 16 deletions

View file

@ -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,

View file

@ -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>({
@ -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: [
@ -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: [
@ -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
}]
}
}),
});
});
});
}

View file

@ -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);
}
});
});
}