in server/src/session.ts [647:700]
private onDidOpenTextDocument(params: lsp.DidOpenTextDocumentParams) {
const {uri, languageId, text} = params.textDocument;
const filePath = uriToFilePath(uri);
if (!filePath) {
return;
}
this.openFiles.update(filePath);
// External templates (HTML files) should be tagged as ScriptKind.Unknown
// so that they don't get parsed as TS files. See
// https://github.com/microsoft/TypeScript/blob/b217f22e798c781f55d17da72ed099a9dee5c650/src/compiler/program.ts#L1897-L1899
const scriptKind = languageId === LanguageId.TS ? ts.ScriptKind.TS : ts.ScriptKind.Unknown;
try {
// The content could be newer than that on disk. This could be due to
// buffer in the user's editor which has not been saved to disk.
// See https://github.com/angular/vscode-ng-language-service/issues/632
const result = this.projectService.openClientFile(filePath, text, scriptKind);
const {configFileName, configFileErrors} = result;
if (configFileErrors && configFileErrors.length) {
// configFileErrors is an empty array even if there's no error, so check length.
this.error(configFileErrors.map(e => e.messageText).join('\n'));
}
const project = configFileName ?
this.projectService.findProject(configFileName) :
this.projectService.getScriptInfo(filePath)?.containingProjects.find(isConfiguredProject);
if (!project) {
return;
}
if (project.languageServiceEnabled) {
// The act of opening a file can cause the text storage to switchToScriptVersionCache for
// version tracking, which results in an identity change for the source file. This isn't
// typically an issue but the identity can change during an update operation for template
// type-checking, when we _only_ expect the typecheck files to change. This _is_ an issue
// because the because template type-checking should not modify the identity of any other
// source files (other than the generated typecheck files). We need to ensure that the
// compiler is aware of this change that shouldn't have happened and recompiles the file
// because we store references to some string expressions (inline templates, style/template
// urls).
project.markAsDirty();
// Show initial diagnostics
this.requestDiagnosticsOnOpenOrChangeFile(filePath, `Opening ${filePath}`);
}
} catch (error) {
if (this.isProjectLoading) {
this.isProjectLoading = false;
this.connection.sendNotification(ProjectLoadingFinish);
}
if (error.stack) {
this.error(error.stack);
}
throw error;
}
this.closeOrphanedExternalProjects();
}