Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/language-service/ivy/test/compiler_spec.ts
Expand Up @@ -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();
});
});
1 change: 1 addition & 0 deletions packages/language-service/ivy/testing/src/project.ts
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test will fail if this line is removed.


scriptInfo = this.tsProject.getScriptInfo(fileName);
Expand Down