Skip to content

Commit

Permalink
fix(language-service): do not return external template that does not …
Browse files Browse the repository at this point in the history
…exist

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
  • Loading branch information
kyliau committed Dec 2, 2020
1 parent a55e01b commit 2e45f9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/language-service/src/ts_plugin.ts
Expand Up @@ -29,8 +29,16 @@ export function getExternalFiles(project: tss.server.Project): string[] {
return [];
}
const ngLsHost = PROJECT_MAP.get(project);
ngLsHost?.getAnalyzedModules();
return ngLsHost?.getExternalTemplates() || [];
if (ngLsHost === undefined) {
return [];
}
ngLsHost.getAnalyzedModules();
return ngLsHost.getExternalTemplates().filter(fileName => {
// TODO(kyliau): Remove this when the following PR lands on the version of
// TypeScript used in this repo.
// https://github.com/microsoft/TypeScript/pull/41737
return project.fileExists(fileName);
});
}

export function create(info: tss.server.PluginCreateInfo): tss.LanguageService {
Expand Down
7 changes: 7 additions & 0 deletions packages/language-service/test/ts_plugin_spec.ts
Expand Up @@ -21,6 +21,7 @@ const mockProject = {
},
},
hasRoots: () => true,
fileExists: () => true,
} as any;

describe('plugin', () => {
Expand Down Expand Up @@ -136,6 +137,12 @@ describe('plugin', () => {
'/app/test.ng',
]);
});

it('should not return external template that does not exist', () => {
spyOn(mockProject, 'fileExists').and.returnValue(false);
const externalTemplates = getExternalFiles(mockProject);
expect(externalTemplates.length).toBe(0);
});
});

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

0 comments on commit 2e45f9f

Please sign in to comment.