Skip to content

Commit

Permalink
fix(language-service): return all typecheck files via getExternalFiles (
Browse files Browse the repository at this point in the history
#40162)

We need a means to preserve typecheck files when a project is reloaded,
otherwise the Ivy compiler will throw an error when it's unable to find
them. This commit implements `getExternalFiles()` called by the langauge
server to achieve this goal.

For more info see angular/vscode-ng-language-service#1030

PR Close #40162
  • Loading branch information
kyliau authored and josephperrott committed Jan 6, 2021
1 parent 0264f76 commit 183fb7e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/language-service/ivy/language_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,11 @@ function getOrCreateTypeCheckScriptInfo(
// attempt to fetch the content from disk and fail.
scriptInfo = projectService.getOrCreateScriptInfoForNormalizedPath(
ts.server.toNormalizedPath(tcf),
true, // openedByClient
'', // fileContent
ts.ScriptKind.TS, // scriptKind
true, // openedByClient
'', // fileContent
// script info added by plugins should be marked as external, see
// https://github.com/microsoft/TypeScript/blob/b217f22e798c781f55d17da72ed099a9dee5c650/src/compiler/program.ts#L1897-L1899
ts.ScriptKind.External, // scriptKind
);
if (!scriptInfo) {
throw new Error(`Failed to create script info for ${tcf}`);
Expand Down
6 changes: 3 additions & 3 deletions packages/language-service/ivy/test/legacy/mock_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ export class MockService {
}
const newScriptInfo = this.ps.getOrCreateScriptInfoForNormalizedPath(
ts.server.toNormalizedPath(fileName),
true, // openedByClient
'', // fileContent
ts.ScriptKind.External, // scriptKind
true, // openedByClient
'', // fileContent
ts.ScriptKind.Unknown, // scriptKind
);
if (!newScriptInfo) {
throw new Error(`Failed to create new script info for ${fileName}`);
Expand Down
29 changes: 29 additions & 0 deletions packages/language-service/ivy/test/legacy/ts_plugin_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @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 {LanguageService} from '../../language_service';
import {getExternalFiles} from '../../ts_plugin';

import {APP_COMPONENT, setup} from './mock_host';

describe('getExternalFiles()', () => {
it('should return all typecheck files', () => {
const {project, tsLS} = setup();
let externalFiles = getExternalFiles(project);
// Initially there are no external files because Ivy compiler hasn't done
// a global analysis
expect(externalFiles).toEqual([]);
// Trigger global analysis
const ngLS = new LanguageService(project, tsLS);
ngLS.getSemanticDiagnostics(APP_COMPONENT);
// Now that global analysis is run, we should have all the typecheck files
externalFiles = getExternalFiles(project);
expect(externalFiles.length).toBe(1);
expect(externalFiles[0].endsWith('app.component.ngtypecheck.ts')).toBeTrue();
});
});
16 changes: 16 additions & 0 deletions packages/language-service/ivy/ts_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,19 @@ export function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
getCompletionEntrySymbol,
};
}

export function getExternalFiles(project: ts.server.Project): string[] {
if (!project.hasRoots()) {
return []; // project has not been initialized
}
const typecheckFiles: string[] = [];
for (const scriptInfo of project.getScriptInfos()) {
if (scriptInfo.scriptKind === ts.ScriptKind.External) {
// script info for typecheck file is marked as external, see
// getOrCreateTypeCheckScriptInfo() in
// packages/language-service/ivy/language_service.ts
typecheckFiles.push(scriptInfo.fileName);
}
}
return typecheckFiles;
}

0 comments on commit 183fb7e

Please sign in to comment.