-
Notifications
You must be signed in to change notification settings - Fork 26.5k
fix(language-service): [Ivy] create compiler only when program changes #39231
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core'; | ||
import {NgCompilerOptions} from '@angular/compiler-cli/src/ngtsc/core/api'; | ||
import {TrackedIncrementalBuildStrategy} from '@angular/compiler-cli/src/ngtsc/incremental'; | ||
import {TypeCheckingProgramStrategy} from '@angular/compiler-cli/src/ngtsc/typecheck/api'; | ||
import * as ts from 'typescript/lib/tsserverlibrary'; | ||
|
||
import {isExternalTemplate, LanguageServiceAdapter} from './language_service_adapter'; | ||
|
||
export class CompilerFactory { | ||
private readonly incrementalStrategy = new TrackedIncrementalBuildStrategy(); | ||
private compiler: NgCompiler|null = null; | ||
private lastKnownProgram: ts.Program|null = null; | ||
|
||
constructor( | ||
private readonly adapter: LanguageServiceAdapter, | ||
private readonly programStrategy: TypeCheckingProgramStrategy, | ||
) {} | ||
|
||
/** | ||
* Create a new instance of the Ivy compiler if the program has changed since | ||
* the last time the compiler was instantiated. If the program has not changed, | ||
* return the existing instance. | ||
* @param fileName override the template if this is an external template file | ||
* @param options angular compiler options | ||
*/ | ||
getOrCreateWithChangedFile(fileName: string, options: NgCompilerOptions): NgCompiler { | ||
const program = this.programStrategy.getProgram(); | ||
if (!this.compiler || program !== this.lastKnownProgram) { | ||
this.compiler = new NgCompiler( | ||
this.adapter, // like compiler host | ||
options, // angular compiler options | ||
program, | ||
this.programStrategy, | ||
this.incrementalStrategy, | ||
true, // enableTemplateTypeChecker | ||
this.lastKnownProgram, | ||
undefined, // perfRecorder (use default) | ||
); | ||
this.lastKnownProgram = program; | ||
} | ||
if (isExternalTemplate(fileName)) { | ||
this.overrideTemplate(fileName, this.compiler); | ||
} | ||
return this.compiler; | ||
} | ||
|
||
private overrideTemplate(fileName: string, compiler: NgCompiler) { | ||
if (!this.adapter.isTemplateDirty(fileName)) { | ||
return; | ||
} | ||
// 1. Get the latest snapshot | ||
const latestTemplate = this.adapter.readResource(fileName); | ||
// 2. Find all components that use the template | ||
const ttc = compiler.getTemplateTypeChecker(); | ||
const components = compiler.getComponentsWithTemplateFile(fileName); | ||
// 3. Update component template | ||
for (const component of components) { | ||
if (ts.isClassDeclaration(component)) { | ||
ttc.overrideComponentTemplate(component, latestTemplate); | ||
} | ||
} | ||
} | ||
|
||
registerLastKnownProgram() { | ||
this.lastKnownProgram = this.programStrategy.getProgram(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/language-service/ivy/test/compiler_factory_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {APP_COMPONENT, setup, TEST_TEMPLATE} from './mock_host'; | ||
|
||
const {project, service} = setup(); | ||
|
||
/** | ||
* The following specs do not directly test the CompilerFactory class, rather | ||
* it asserts our understanding of the project/program/template interaction | ||
* which forms the underlying assumption used in the CompilerFactory. | ||
*/ | ||
|
||
describe('tsserver', () => { | ||
beforeEach(() => { | ||
service.reset(); | ||
}); | ||
|
||
describe('project version', () => { | ||
it('is updated after TS changes', () => { | ||
const v0 = project.getProjectVersion(); | ||
service.overwriteInlineTemplate(APP_COMPONENT, `{{ foo }}`); | ||
const v1 = project.getProjectVersion(); | ||
expect(v1).not.toBe(v0); | ||
}); | ||
|
||
it('is updated after template changes', () => { | ||
const v0 = project.getProjectVersion(); | ||
service.overwrite(TEST_TEMPLATE, `{{ bar }}`); | ||
const v1 = project.getProjectVersion(); | ||
expect(v1).not.toBe(v0); | ||
}); | ||
}); | ||
|
||
describe('program', () => { | ||
it('is updated after TS changes', () => { | ||
const p0 = project.getLanguageService().getProgram(); | ||
expect(p0).toBeDefined(); | ||
service.overwriteInlineTemplate(APP_COMPONENT, `{{ foo }}`); | ||
const p1 = project.getLanguageService().getProgram(); | ||
expect(p1).not.toBe(p0); | ||
}); | ||
|
||
it('is not updated after template changes', () => { | ||
const p0 = project.getLanguageService().getProgram(); | ||
expect(p0).toBeDefined(); | ||
service.overwrite(TEST_TEMPLATE, `{{ bar }}`); | ||
const p1 = project.getLanguageService().getProgram(); | ||
expect(p1).toBe(p0); | ||
}); | ||
}); | ||
|
||
describe('external template', () => { | ||
it('should not be part of the project root', () => { | ||
// Templates should _never_ be added to the project root, otherwise they | ||
// will be parsed as TS files. | ||
const scriptInfo = service.getScriptInfo(TEST_TEMPLATE); | ||
expect(project.isRoot(scriptInfo)).toBeFalse(); | ||
}); | ||
|
||
it('should be attached to project', () => { | ||
const scriptInfo = service.getScriptInfo(TEST_TEMPLATE); | ||
// Script info for external template must be attached to a project because | ||
// that's the primary mechanism used in the extension to locate the right | ||
// language service instance. See | ||
// https://github.com/angular/vscode-ng-language-service/blob/b048f5f6b9996c5cf113b764c7ffe13d9eeb4285/server/src/session.ts#L192 | ||
expect(scriptInfo.isAttached(project)).toBeTrue(); | ||
// Attaching a script info to a project does not mean that the project | ||
// will contain the script info. Kinda like friendship on social media. | ||
expect(project.containsScriptInfo(scriptInfo)).toBeFalse(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.