test(language-service): Add test to expose bug caused by source file change (#41500)

This commit adds a test to expose the bug caused by source file change in
between typecheck programs.

PR Close #41500
This commit is contained in:
Keen Yee Liau 2021-04-07 17:01:04 -07:00 committed by Zach Arend
parent 5e0d5a9ec2
commit 61bfa3d9df
2 changed files with 41 additions and 0 deletions

View File

@ -173,4 +173,44 @@ describe('language-service/compiler integration', () => {
const ngDiagsAfter = project.getDiagnosticsForFile('mod.ts').filter(isNgSpecificDiagnostic);
expect(ngDiagsAfter.length).toBe(0);
});
it('detects source file change in between typecheck programs', () => {
const env = LanguageServiceTestEnv.setup();
const project = env.addProject('test', {
'module.ts': `
import {NgModule} from '@angular/core';
import {BarCmp} from './bar';
@NgModule({
declarations: [BarCmp],
})
export class AppModule {}
`,
'bar.ts': `
import {Component} from '@angular/core';
@Component({
template: '{{ bar }}',
})
export class BarCmp {
readonly bar = 'bar';
}
`,
});
// The opening of 'bar.ts' causes its version to change, because the internal
// representation switches from TextStorage to ScriptVersionCache.
const bar = project.openFile('bar.ts');
// When getDiagnostics is called, NgCompiler calls ensureAllShimsForOneFile
// and caches the source file for 'bar.ts' in the input program.
// The input program has not picked up the version change because the project
// is clean (rightly so since there's no user-initiated change).
expect(project.getDiagnosticsForFile('bar.ts').length).toBe(0);
// A new program is generated due to addition of typecheck file. During
// program creation, TS language service does a sweep of all source files,
// and detects the version change. Consequently, it creates a new source
// file for 'bar.ts'. This is a violation of our assumption that a SourceFile
// will never change in between typecheck programs.
bar.moveCursorToText(`template: '{{ b¦ar }}'`);
expect(bar.getQuickInfoAtPosition()).toBeDefined();
});
});

View File

@ -103,6 +103,7 @@ export class Project {
// Mark the project as dirty because the act of opening a file may result in the version
// changing since TypeScript will `switchToScriptVersionCache` when a file is opened.
// Note that this emulates what we have to do in the server/extension as well.
// TODO: remove this once PR #41475 lands
this.tsProject.markAsDirty();
scriptInfo = this.tsProject.getScriptInfo(fileName);