Skip to content

Commit

Permalink
fix(language-service): Prevent TSServer from removing templates from …
Browse files Browse the repository at this point in the history
…project (#45965)

As part of the `updateProjectIfDirty` process and inside `updateNonInferredProjectFiles`
TS Server will remove the template files that we added as roots in
`readResource`.
https://sourcegraph.com/github.com/microsoft/TypeScript@c300fea3250abd7f75920d95a58d9e742ac730ee/-/blob/src/server/editorServices.ts?L2363

The external files are added to the list here so ensuring that the
templates are included in the `getExternalFiles` will prevent this from
happening
https://sourcegraph.com/github.com/microsoft/TypeScript@c300fea3250abd7f75920d95a58d9e742ac730ee/-/blob/src/server/editorServices.ts?L2395:18

PR Close #45965
  • Loading branch information
atscott authored and thePunderWoman committed May 12, 2022
1 parent 5e404d3 commit fa754cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/language-service/src/ts_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,22 @@ export function getExternalFiles(project: ts.server.Project): string[] {
return []; // project has not been initialized
}
const typecheckFiles: string[] = [];
const resourceFiles: 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/src/language_service.ts
typecheckFiles.push(scriptInfo.fileName);
}
if (scriptInfo.scriptKind === ts.ScriptKind.Unknown) {
// script info for resource file is marked as unknown.
// Including these as external files is necessary because otherwise they will get removed from
// the project when `updateNonInferredProjectFiles` is called as part of the
// `updateProjectIfDirty` cycle.
// https://sourcegraph.com/github.com/microsoft/TypeScript@c300fea3250abd7f75920d95a58d9e742ac730ee/-/blob/src/server/editorServices.ts?L2363
resourceFiles.push(scriptInfo.fileName);
}
}
return typecheckFiles;
return [...typecheckFiles, ...resourceFiles];
}
3 changes: 2 additions & 1 deletion packages/language-service/test/legacy/ts_plugin_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('getExternalFiles()', () => {
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);
// Includes 1 typecheck file, 1 template, and 1 css files
expect(externalFiles.length).toBe(3);
expect(externalFiles[0].endsWith('app.component.ngtypecheck.ts')).toBeTrue();
});
});

0 comments on commit fa754cd

Please sign in to comment.