diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 783a6016f7a..13d8a6bf824 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -1799,7 +1799,6 @@ namespace ts.projectSystem { }); it("language service disabled state is updated in external projects", () => { - debugger const f1 = { path: "/a/app.js", content: "var x = 1" @@ -1940,6 +1939,31 @@ namespace ts.projectSystem { const edits = project.getLanguageService().getFormattingEditsForDocument(f1.path, options); assert.deepEqual(edits, [{ span: createTextSpan(/*start*/ 7, /*length*/ 3), newText: " " }]); }); + + it("snapshot from different caches are incompatible", () => { + const f1 = { + path: "/a/b/app.ts", + content: "let x = 1;" + }; + const host = createServerHost([f1]); + const projectFileName = "/a/b/proj.csproj"; + const projectService = createProjectService(host); + projectService.openExternalProject({ + projectFileName, + rootFiles: [toExternalFile(f1.path)], + options: {} + }) + projectService.openClientFile(f1.path, "let x = 1;\nlet y = 2;"); + + projectService.checkNumberOfProjects({ externalProjects: 1 }); + projectService.externalProjects[0].getLanguageService(/*ensureSynchronized*/false).getNavigationBarItems(f1.path); + projectService.closeClientFile(f1.path); + + projectService.openClientFile(f1.path); + projectService.checkNumberOfProjects({ externalProjects: 1 }); + const navbar = projectService.externalProjects[0].getLanguageService(/*ensureSynchronized*/false).getNavigationBarItems(f1.path); + assert.equal(navbar[0].spans[0].length, f1.content.length); + }); }); describe("Proper errors", () => { diff --git a/src/server/scriptVersionCache.ts b/src/server/scriptVersionCache.ts index aaf04d39a1f..e087c3b6dde 100644 --- a/src/server/scriptVersionCache.ts +++ b/src/server/scriptVersionCache.ts @@ -398,7 +398,7 @@ namespace ts.server { index: LineIndex; changesSincePreviousVersion: TextChange[] = []; - constructor(public version: number, public cache: ScriptVersionCache) { + constructor(readonly version: number, readonly cache: ScriptVersionCache) { } getText(rangeStart: number, rangeEnd: number) { @@ -438,7 +438,7 @@ namespace ts.server { } } getChangeRange(oldSnapshot: ts.IScriptSnapshot): ts.TextChangeRange { - if (oldSnapshot instanceof LineIndexSnapshot) { + if (oldSnapshot instanceof LineIndexSnapshot && this.cache === oldSnapshot.cache) { return this.getTextChangeRangeSinceVersion(oldSnapshot.version); } }