Skip to content

Commit 2668f50

Browse files
committed
Reload contents of file from disk irrespective of project presence and file already containing its own text
Fixes microsoft#19336
1 parent fd89808 commit 2668f50

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

src/server/project.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -946,16 +946,6 @@ namespace ts.server {
946946
}
947947
}
948948

949-
reloadScript(filename: NormalizedPath, tempFileName?: NormalizedPath): boolean {
950-
const script = this.projectService.getScriptInfoForNormalizedPath(filename);
951-
if (script) {
952-
Debug.assert(script.isAttached(this));
953-
script.reloadFromFile(tempFileName);
954-
return true;
955-
}
956-
return false;
957-
}
958-
959949
/* @internal */
960950
getChangesSinceVersion(lastKnownVersion?: number): ProjectFilesWithTSDiagnostics {
961951
this.updateGraph();

src/server/scriptInfo.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ namespace ts.server {
6767
this.lineMap = undefined;
6868
}
6969

70-
/** returns true if text changed */
70+
/**
71+
* Set the contents as newText
72+
* returns true if text changed
73+
*/
7174
public reload(newText: string) {
7275
Debug.assert(newText !== undefined);
7376

@@ -87,31 +90,31 @@ namespace ts.server {
8790
}
8891
}
8992

90-
/** returns true if text changed */
93+
/**
94+
* Reads the contents from tempFile(if supplied) or own file and sets it as contents
95+
* returns true if text changed
96+
*/
97+
public reloadWithFileText(tempFileName?: string) {
98+
const reloaded = this.reload(this.getFileText(tempFileName));
99+
this.ownFileText = !tempFileName || tempFileName === this.fileName;
100+
return reloaded;
101+
}
102+
103+
/**
104+
* Reloads the contents from the file if there is no pending reload from disk or the contents of file are same as file text
105+
* returns true if text changed
106+
*/
91107
public reloadFromDisk() {
92-
let reloaded = false;
93108
if (!this.pendingReloadFromDisk && !this.ownFileText) {
94-
reloaded = this.reload(this.getFileText());
95-
this.ownFileText = true;
109+
return this.reloadWithFileText();
96110
}
97-
return reloaded;
111+
return false;
98112
}
99113

100114
public delayReloadFromFileIntoText() {
101115
this.pendingReloadFromDisk = true;
102116
}
103117

104-
/** returns true if text changed */
105-
public reloadFromFile(tempFileName: string) {
106-
let reloaded = false;
107-
// Reload if different file or we dont know if we are working with own file text
108-
if (tempFileName !== this.fileName || !this.ownFileText) {
109-
reloaded = this.reload(this.getFileText(tempFileName));
110-
this.ownFileText = !tempFileName || tempFileName === this.fileName;
111-
}
112-
return reloaded;
113-
}
114-
115118
public getSnapshot(): IScriptSnapshot {
116119
return this.useScriptVersionCacheIfValidOrOpen()
117120
? this.svc.getSnapshot()
@@ -180,8 +183,7 @@ namespace ts.server {
180183
private getOrLoadText() {
181184
if (this.text === undefined || this.pendingReloadFromDisk) {
182185
Debug.assert(!this.svc || this.pendingReloadFromDisk, "ScriptVersionCache should not be set when reloading from disk");
183-
this.reload(this.getFileText());
184-
this.ownFileText = true;
186+
this.reloadWithFileText();
185187
}
186188
return this.text;
187189
}
@@ -385,7 +387,7 @@ namespace ts.server {
385387
this.markContainingProjectsAsDirty();
386388
}
387389
else {
388-
if (this.textStorage.reloadFromFile(tempFileName)) {
390+
if (this.textStorage.reloadWithFileText(tempFileName)) {
389391
this.markContainingProjectsAsDirty();
390392
}
391393
}

src/server/session.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,11 +1311,13 @@ namespace ts.server {
13111311
private reload(args: protocol.ReloadRequestArgs, reqSeq: number) {
13121312
const file = toNormalizedPath(args.file);
13131313
const tempFileName = args.tmpfile && toNormalizedPath(args.tmpfile);
1314-
const project = this.projectService.getDefaultProjectForFile(file, /*ensureProject*/ true);
1315-
this.changeSeq++;
1316-
// make sure no changes happen before this one is finished
1317-
if (project.reloadScript(file, tempFileName)) {
1318-
this.doOutput(/*info*/ undefined, CommandNames.Reload, reqSeq, /*success*/ true);
1314+
const info = this.projectService.getScriptInfoForNormalizedPath(file);
1315+
if (info) {
1316+
this.changeSeq++;
1317+
// make sure no changes happen before this one is finished
1318+
if (info.reloadFromFile(tempFileName)) {
1319+
this.doOutput(/*info*/ undefined, CommandNames.Reload, reqSeq, /*success*/ true);
1320+
}
13191321
}
13201322
}
13211323

0 commit comments

Comments
 (0)