Skip to content

Commit d45ba15

Browse files
author
Keen Yee Liau
committed
fix: Check language service is not already disabled
When making the check to determine if a project is Angular, it could be the case that the language service is already disabled. This happens when there are files in the project that exceeded limit. This PR fixes the issue by first checking if the language service is already disabled. Also added much more information to the logs to help users diagnose the problem. Messages are logged to both remote console (vscode) and file so that the former could inform users in their editor, and the latter makes it much easier to correlate the sequence of events in both TypeScript and Angular. Related: #416
1 parent cf0a0b9 commit d45ba15

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

server/src/session.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,9 @@ export class Session {
8383
break;
8484
case ts.server.ProjectLoadingFinishEvent: {
8585
const {project} = event.data;
86-
if (!isAngularProject(project)) {
87-
project.disableLanguageService();
88-
this.connection.console.info(`Disabling language service for ${
89-
project.projectName} because it is not an Angular project.`);
90-
}
91-
this.connection.sendNotification(
92-
projectLoadingNotification.finish, event.data.project.projectName);
86+
// Disable language service if project is not Angular
87+
this.checkIsAngularProject(project);
88+
this.connection.sendNotification(projectLoadingNotification.finish, project.projectName);
9389
break;
9490
}
9591
case ts.server.ProjectsUpdatedInBackgroundEvent:
@@ -378,19 +374,54 @@ export class Session {
378374
listen() {
379375
this.connection.listen();
380376
}
377+
378+
/**
379+
* Determine if the specified `project` is Angular, and disable the language
380+
* service if not.
381+
* @param project
382+
*/
383+
private checkIsAngularProject(project: ts.server.Project) {
384+
const NG_CORE = '@angular/core/core.d.ts';
385+
const {projectName} = project;
386+
if (!project.languageServiceEnabled) {
387+
const msg = `Language service is already disabled for ${projectName}. ` +
388+
`This could be due to non-TS files that exceeded the size limit (${
389+
ts.server.maxProgramSizeForNonTsFiles} bytes).` +
390+
`Please check log file for details.`;
391+
this.connection.console.info(msg); // log to remote console to inform users
392+
project.log(msg); // log to file, so that it's easier to correlate with ts entries
393+
return;
394+
}
395+
if (!isAngularProject(project, NG_CORE)) {
396+
project.disableLanguageService();
397+
const totalFiles = project.getFileNames().length;
398+
const msg =
399+
`Disabling language service for ${projectName} because it is not an Angular project. ` +
400+
`There are ${totalFiles} files in the project but '${NG_CORE}' is not detected.`;
401+
this.connection.console.info(msg);
402+
project.log(msg);
403+
if (project.getExcludedFiles().some(f => f.endsWith(NG_CORE))) {
404+
const msg =
405+
`Please check your tsconfig.json to make sure 'node_modules' directory is not excluded.`;
406+
this.connection.console.info(msg);
407+
project.log(msg);
408+
}
409+
}
410+
}
381411
}
382412

383413
/**
384414
* Return true if the specified `project` contains the Angular core declaration.
385415
* @param project
416+
* @param ngCore path that uniquely identifies `@angular/core`.
386417
*/
387-
function isAngularProject(project: ts.server.Project): boolean {
418+
function isAngularProject(project: ts.server.Project, ngCore: string): boolean {
388419
project.markAsDirty(); // Must mark project as dirty to rebuild the program.
389420
if (project.isNonTsProject()) {
390421
return false;
391422
}
392423
for (const fileName of project.getFileNames()) {
393-
if (fileName.endsWith('@angular/core/core.d.ts')) {
424+
if (fileName.endsWith(ngCore)) {
394425
return true;
395426
}
396427
}

0 commit comments

Comments
 (0)