Skip to content

Commit 05c9ec3

Browse files
authored
Remove unnecessary assert (since we allow already open file to be opened again even through openFile command - partially) from updateOpen command (microsoft#37059)
Fixes microsoft#35034
1 parent c4e9685 commit 05c9ec3

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

src/server/editorServices.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ namespace ts.server {
24142414
this.openFilesWithNonRootedDiskPath.set(this.toCanonicalFileName(fileName), info);
24152415
}
24162416
}
2417-
if (openedByClient && !info.isScriptOpen()) {
2417+
if (openedByClient) {
24182418
// Opening closed script info
24192419
// either it was created just now, or was part of projects but was closed
24202420
this.stopWatchingScriptInfo(info);
@@ -2423,9 +2423,6 @@ namespace ts.server {
24232423
info.registerFileUpdate();
24242424
}
24252425
}
2426-
else {
2427-
Debug.assert(fileContent === undefined);
2428-
}
24292426
return info;
24302427
}
24312428

@@ -3170,11 +3167,9 @@ namespace ts.server {
31703167
const iterResult = openFiles.next();
31713168
if (iterResult.done) break;
31723169
const file = iterResult.value;
3173-
const scriptInfo = this.getScriptInfo(file.fileName);
3174-
Debug.assert(!scriptInfo || !scriptInfo.isScriptOpen(), "Script should not exist and not be open already");
31753170
// Create script infos so we have the new content for all the open files before we do any updates to projects
31763171
const info = this.getOrCreateOpenScriptInfo(
3177-
scriptInfo ? scriptInfo.fileName : toNormalizedPath(file.fileName),
3172+
toNormalizedPath(file.fileName),
31783173
file.content,
31793174
tryConvertScriptKindName(file.scriptKind!),
31803175
file.hasMixedContent,

src/testRunner/unittests/tsserver/applyChangesToOpenFiles.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ ${file.content}`;
2929
assert.equal(Number(project.getProjectVersion()), expected);
3030
}
3131

32-
function verify(applyChangesToOpen: (session: TestSession) => void) {
32+
interface Verify {
33+
applyChangesToOpen: (session: TestSession) => void;
34+
openFile1Again: (session: TestSession) => void;
35+
}
36+
function verify({ applyChangesToOpen, openFile1Again }: Verify) {
3337
const host = createServerHost([app, file3, commonFile1, commonFile2, libFile, configFile]);
3438
const session = createSession(host);
3539
session.executeCommandSeq<protocol.OpenRequest>({
@@ -65,11 +69,22 @@ ${file.content}`;
6569
verifyText(service, commonFile2.path, fileContentWithComment(commonFile2));
6670
verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;");
6771
verifyText(service, file3.path, file3.content);
72+
73+
// Open file1 again
74+
openFile1Again(session);
75+
assert.isTrue(service.getScriptInfo(commonFile1.path)!.isScriptOpen());
76+
77+
// Verify that file1 contents are changed
78+
verifyProjectVersion(project, 4);
79+
verifyText(service, commonFile1.path, commonFile1.content);
80+
verifyText(service, commonFile2.path, fileContentWithComment(commonFile2));
81+
verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;");
82+
verifyText(service, file3.path, file3.content);
6883
}
6984

7085
it("with applyChangedToOpenFiles request", () => {
71-
verify(session =>
72-
session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
86+
verify({
87+
applyChangesToOpen: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
7388
command: protocol.CommandTypes.ApplyChangedToOpenFiles,
7489
arguments: {
7590
openFiles: [
@@ -101,13 +116,22 @@ ${file.content}`;
101116
file3.path
102117
]
103118
}
104-
})
105-
);
119+
}),
120+
openFile1Again: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({
121+
command: protocol.CommandTypes.ApplyChangedToOpenFiles,
122+
arguments: {
123+
openFiles: [{
124+
fileName: commonFile1.path,
125+
content: commonFile1.content
126+
}]
127+
}
128+
}),
129+
});
106130
});
107131

108132
it("with updateOpen request", () => {
109-
verify(session =>
110-
session.executeCommandSeq<protocol.UpdateOpenRequest>({
133+
verify({
134+
applyChangesToOpen: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({
111135
command: protocol.CommandTypes.UpdateOpen,
112136
arguments: {
113137
openFiles: [
@@ -141,8 +165,17 @@ ${file.content}`;
141165
file3.path
142166
]
143167
}
144-
})
145-
);
168+
}),
169+
openFile1Again: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({
170+
command: protocol.CommandTypes.UpdateOpen,
171+
arguments: {
172+
openFiles: [{
173+
file: commonFile1.path,
174+
fileContent: commonFile1.content
175+
}]
176+
}
177+
}),
178+
});
146179
});
147180
});
148181
}

src/testRunner/unittests/tsserver/openFile.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,29 @@ namespace ts.projectSystem {
104104
checkProjectActualFiles(project, files.map(f => f.path));
105105
}
106106
});
107+
108+
it("can open same file again", () => {
109+
const projectFolder = "/user/someuser/projects/myproject";
110+
const aFile: File = {
111+
path: `${projectFolder}/src/a.ts`,
112+
content: "export const x = 0;"
113+
};
114+
const configFile: File = {
115+
path: `${projectFolder}/tsconfig.json`,
116+
content: "{}"
117+
};
118+
const files = [aFile, configFile, libFile];
119+
const host = createServerHost(files);
120+
const service = createProjectService(host);
121+
verifyProject(aFile.content);
122+
verifyProject(`${aFile.content}export const y = 10;`);
123+
124+
function verifyProject(aFileContent: string) {
125+
service.openClientFile(aFile.path, aFileContent, ScriptKind.TS, projectFolder);
126+
const project = service.configuredProjects.get(configFile.path)!;
127+
checkProjectActualFiles(project, files.map(f => f.path));
128+
assert.equal(project.getCurrentProgram()?.getSourceFile(aFile.path)!.text, aFileContent);
129+
}
130+
});
107131
});
108132
}

0 commit comments

Comments
 (0)