Skip to content

Commit 6b6fcd7

Browse files
kyliaumhevery
authored andcommitted
fix(language-service): do not return external template that does not exist (#39898)
There is a bug in tsserver that causes it to crash when it tries to create script info for an external template that does not exist. I've submitted an upstream PR microsoft/TypeScript#41737 to fix this, but before the commit lands in the stable release, we'll have to workaround the issue in language service. Close angular/vscode-ng-language-service#1001 PR Close #39898
1 parent 3680ad1 commit 6b6fcd7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/language-service/src/ts_plugin.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ export function getExternalFiles(project: tss.server.Project): string[] {
2929
return [];
3030
}
3131
const ngLsHost = PROJECT_MAP.get(project);
32-
ngLsHost?.getAnalyzedModules();
33-
return ngLsHost?.getExternalTemplates() || [];
32+
if (ngLsHost === undefined) {
33+
return [];
34+
}
35+
ngLsHost.getAnalyzedModules();
36+
return ngLsHost.getExternalTemplates().filter(fileName => {
37+
// TODO(kyliau): Remove this when the following PR lands on the version of
38+
// TypeScript used in this repo.
39+
// https://github.com/microsoft/TypeScript/pull/41737
40+
return project.fileExists(fileName);
41+
});
3442
}
3543

3644
export function create(info: tss.server.PluginCreateInfo): tss.LanguageService {

packages/language-service/test/ts_plugin_spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const mockProject = {
2121
},
2222
},
2323
hasRoots: () => true,
24+
fileExists: () => true,
2425
} as any;
2526

2627
describe('plugin', () => {
@@ -136,6 +137,12 @@ describe('plugin', () => {
136137
'/app/test.ng',
137138
]);
138139
});
140+
141+
it('should not return external template that does not exist', () => {
142+
spyOn(mockProject, 'fileExists').and.returnValue(false);
143+
const externalTemplates = getExternalFiles(mockProject);
144+
expect(externalTemplates.length).toBe(0);
145+
});
139146
});
140147

141148
describe(`with config 'angularOnly = true`, () => {

0 commit comments

Comments
 (0)