Skip to content

Commit 57e397f

Browse files
committed
Use hash of source file text as version for the source file
1 parent 3d1c6e8 commit 57e397f

File tree

3 files changed

+17
-46
lines changed

3 files changed

+17
-46
lines changed

src/server/editorServices.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,6 @@ namespace ts.server {
655655
/*@internal*/
656656
readonly filenameToScriptInfo = new Map<string, ScriptInfo>();
657657
private readonly scriptInfoInNodeModulesWatchers = new Map<string, ScriptInfoInNodeModulesWatcher>();
658-
/**
659-
* Contains all the deleted script info's version information so that
660-
* it does not reset when creating script info again
661-
* (and could have potentially collided with version where contents mismatch)
662-
*/
663-
private readonly filenameToScriptInfoVersion = new Map<string, ScriptInfoVersion>();
664658
// Set of all '.js' files ever opened.
665659
private readonly allJsFilesForOpenFileTelemetry = new Map<string, true>();
666660

@@ -1598,7 +1592,6 @@ namespace ts.server {
15981592

15991593
private deleteScriptInfo(info: ScriptInfo) {
16001594
this.filenameToScriptInfo.delete(info.path);
1601-
this.filenameToScriptInfoVersion.set(info.path, info.getVersion());
16021595
const realpath = info.getRealpathIfDifferent();
16031596
if (realpath) {
16041597
this.realpathToScriptInfos!.remove(realpath, info); // TODO: GH#18217
@@ -2624,9 +2617,8 @@ namespace ts.server {
26242617
if (!openedByClient && !isDynamic && !(hostToQueryFileExistsOn || this.host).fileExists(fileName)) {
26252618
return;
26262619
}
2627-
info = new ScriptInfo(this.host, fileName, scriptKind!, !!hasMixedContent, path, this.filenameToScriptInfoVersion.get(path)); // TODO: GH#18217
2620+
info = new ScriptInfo(this.host, fileName, scriptKind!, !!hasMixedContent, path); // TODO: GH#18217
26282621
this.filenameToScriptInfo.set(info.path, info);
2629-
this.filenameToScriptInfoVersion.delete(info.path);
26302622
if (!openedByClient) {
26312623
this.watchClosedScriptInfo(info);
26322624
}

src/server/scriptInfo.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
namespace ts.server {
2-
export interface ScriptInfoVersion {
3-
svc: number;
4-
text: number;
5-
}
6-
72
/* @internal */
83
export class TextStorage {
9-
version: ScriptInfoVersion;
4+
version: string | undefined;
105

116
/**
127
* Generated only on demand (based on edits, or information requested)
@@ -46,14 +41,7 @@ namespace ts.server {
4641
*/
4742
private pendingReloadFromDisk = false;
4843

49-
constructor(private readonly host: ServerHost, private readonly info: ScriptInfo, initialVersion?: ScriptInfoVersion) {
50-
this.version = initialVersion || { svc: 0, text: 0 };
51-
}
52-
53-
public getVersion() {
54-
return this.svc
55-
? `SVC-${this.version.svc}-${this.svc.getSnapshotVersion()}`
56-
: `Text-${this.version.text}`;
44+
constructor(private readonly host: ServerHost, private readonly info: ScriptInfo) {
5745
}
5846

5947
public hasScriptVersionCache_TestOnly() {
@@ -77,16 +65,17 @@ namespace ts.server {
7765
public useText(newText?: string) {
7866
this.svc = undefined;
7967
this.text = newText;
68+
this.version = undefined;
8069
this.lineMap = undefined;
8170
this.fileSize = undefined;
8271
this.resetSourceMapInfo();
83-
this.version.text++;
8472
}
8573

8674
public edit(start: number, end: number, newText: string) {
8775
this.switchToScriptVersionCache().edit(start, end - start, newText);
8876
this.ownFileText = false;
8977
this.text = undefined;
78+
this.version = undefined;
9079
this.lineMap = undefined;
9180
this.fileSize = undefined;
9281
this.resetSourceMapInfo();
@@ -142,6 +131,7 @@ namespace ts.server {
142131

143132
public delayReloadFromFileIntoText() {
144133
this.pendingReloadFromDisk = true;
134+
this.version = undefined;
145135
}
146136

147137
/**
@@ -225,7 +215,6 @@ namespace ts.server {
225215
private switchToScriptVersionCache(): ScriptVersionCache {
226216
if (!this.svc || this.pendingReloadFromDisk) {
227217
this.svc = ScriptVersionCache.fromString(this.getOrLoadText());
228-
this.version.svc++;
229218
}
230219
return this.svc;
231220
}
@@ -334,10 +323,10 @@ namespace ts.server {
334323
readonly scriptKind: ScriptKind,
335324
public readonly hasMixedContent: boolean,
336325
readonly path: Path,
337-
initialVersion?: ScriptInfoVersion) {
326+
) {
338327
this.isDynamic = isDynamicFileName(fileName);
339328

340-
this.textStorage = new TextStorage(host, this, initialVersion);
329+
this.textStorage = new TextStorage(host, this);
341330
if (hasMixedContent || this.isDynamic) {
342331
this.textStorage.reload("");
343332
this.realpath = this.path;
@@ -347,11 +336,6 @@ namespace ts.server {
347336
: getScriptKindFromFileName(fileName);
348337
}
349338

350-
/*@internal*/
351-
getVersion() {
352-
return this.textStorage.version;
353-
}
354-
355339
/*@internal*/
356340
getTelemetryFileSize() {
357341
return this.textStorage.getTelemetryFileSize();
@@ -559,10 +543,15 @@ namespace ts.server {
559543
}
560544
}
561545

562-
getLatestVersion(): string {
546+
getLatestVersion() {
563547
// Ensure we have updated snapshot to give back latest version
564-
this.textStorage.getSnapshot();
565-
return this.textStorage.getVersion();
548+
const snapShot = this.textStorage.getSnapshot();
549+
if (this.textStorage.version === undefined) {
550+
// Ensure we have updated snapshot to give back latest version
551+
const text = getSnapshotText(snapShot);
552+
this.textStorage.version = this.host.createHash ? this.host.createHash(text) : generateDjb2Hash(text);
553+
}
554+
return this.textStorage.version;
566555
}
567556

568557
saveTo(fileName: string) {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9277,10 +9277,6 @@ declare namespace ts.server.protocol {
92779277
}
92789278
}
92799279
declare namespace ts.server {
9280-
interface ScriptInfoVersion {
9281-
svc: number;
9282-
text: number;
9283-
}
92849280
function isDynamicFileName(fileName: NormalizedPath): boolean;
92859281
class ScriptInfo {
92869282
private readonly host;
@@ -9295,7 +9291,7 @@ declare namespace ts.server {
92959291
private formatSettings;
92969292
private preferences;
92979293
private textStorage;
9298-
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path, initialVersion?: ScriptInfoVersion);
9294+
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path);
92999295
isScriptOpen(): boolean;
93009296
open(newText: string): void;
93019297
close(fileExists?: boolean): void;
@@ -9761,12 +9757,6 @@ declare namespace ts.server {
97619757
}
97629758
export class ProjectService {
97639759
private readonly scriptInfoInNodeModulesWatchers;
9764-
/**
9765-
* Contains all the deleted script info's version information so that
9766-
* it does not reset when creating script info again
9767-
* (and could have potentially collided with version where contents mismatch)
9768-
*/
9769-
private readonly filenameToScriptInfoVersion;
97709760
private readonly allJsFilesForOpenFileTelemetry;
97719761
/**
97729762
* maps external project file name to list of config files that were the part of this project

0 commit comments

Comments
 (0)