Skip to content

Commit c605a26

Browse files
committed
Use hash of source file text as version for the source file
1 parent 78ded65 commit c605a26

File tree

3 files changed

+15
-46
lines changed

3 files changed

+15
-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

@@ -1599,7 +1593,6 @@ namespace ts.server {
15991593

16001594
private deleteScriptInfo(info: ScriptInfo) {
16011595
this.filenameToScriptInfo.delete(info.path);
1602-
this.filenameToScriptInfoVersion.set(info.path, info.getVersion());
16031596
const realpath = info.getRealpathIfDifferent();
16041597
if (realpath) {
16051598
this.realpathToScriptInfos!.remove(realpath, info); // TODO: GH#18217
@@ -2636,9 +2629,8 @@ namespace ts.server {
26362629
if (!openedByClient && !isDynamic && !(hostToQueryFileExistsOn || this.host).fileExists(fileName)) {
26372630
return;
26382631
}
2639-
info = new ScriptInfo(this.host, fileName, scriptKind!, !!hasMixedContent, path, this.filenameToScriptInfoVersion.get(path)); // TODO: GH#18217
2632+
info = new ScriptInfo(this.host, fileName, scriptKind!, !!hasMixedContent, path); // TODO: GH#18217
26402633
this.filenameToScriptInfo.set(info.path, info);
2641-
this.filenameToScriptInfoVersion.delete(info.path);
26422634
if (!openedByClient) {
26432635
this.watchClosedScriptInfo(info);
26442636
}

src/server/scriptInfo.ts

Lines changed: 13 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();
@@ -560,9 +544,12 @@ namespace ts.server {
560544
}
561545

562546
getLatestVersion() {
563-
// Ensure we have updated snapshot to give back latest version
564-
this.textStorage.getSnapshot();
565-
return this.textStorage.getVersion();
547+
if (this.textStorage.version === undefined) {
548+
// Ensure we have updated snapshot to give back latest version
549+
const text = getSnapshotText(this.textStorage.getSnapshot());
550+
this.textStorage.version = this.host.createHash ? this.host.createHash(text) : generateDjb2Hash(text);
551+
}
552+
return this.textStorage.version;
566553
}
567554

568555
saveTo(fileName: string) {

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9139,10 +9139,6 @@ declare namespace ts.server.protocol {
91399139
}
91409140
}
91419141
declare namespace ts.server {
9142-
interface ScriptInfoVersion {
9143-
svc: number;
9144-
text: number;
9145-
}
91469142
function isDynamicFileName(fileName: NormalizedPath): boolean;
91479143
class ScriptInfo {
91489144
private readonly host;
@@ -9157,7 +9153,7 @@ declare namespace ts.server {
91579153
private formatSettings;
91589154
private preferences;
91599155
private textStorage;
9160-
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path, initialVersion?: ScriptInfoVersion);
9156+
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path);
91619157
isScriptOpen(): boolean;
91629158
open(newText: string): void;
91639159
close(fileExists?: boolean): void;
@@ -9624,12 +9620,6 @@ declare namespace ts.server {
96249620
}
96259621
export class ProjectService {
96269622
private readonly scriptInfoInNodeModulesWatchers;
9627-
/**
9628-
* Contains all the deleted script info's version information so that
9629-
* it does not reset when creating script info again
9630-
* (and could have potentially collided with version where contents mismatch)
9631-
*/
9632-
private readonly filenameToScriptInfoVersion;
96339623
private readonly allJsFilesForOpenFileTelemetry;
96349624
/**
96359625
* maps external project file name to list of config files that were the part of this project

0 commit comments

Comments
 (0)