Skip to content

Commit 1583781

Browse files
kyliauKeen Yee Liau
authored and
Keen Yee Liau
committed
fix: LS not showing existing diagnotics on file open
This commit fixes a bug whereby existing diagnostics in an external template are not shown when the file is opened the first time. Diagnostics only show up when subsequent edits are made to the file. This is a regression after upgrading to TS 4.0 Fix #922
1 parent a0c3b0b commit 1583781

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

integration/lsp/viewengine_spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ describe('Angular language server', () => {
8686
},
8787
});
8888
});
89+
90+
it('should show existing diagnostics on external template', async () => {
91+
client.sendNotification(lsp.DidOpenTextDocumentNotification.type, {
92+
textDocument: {
93+
uri: `file://${FOO_TEMPLATE}`,
94+
languageId: 'typescript',
95+
version: 1,
96+
text: `{{ doesnotexist }}`,
97+
},
98+
});
99+
const diagnostics: lsp.Diagnostic[] = await new Promise(resolve => {
100+
client.onNotification(
101+
lsp.PublishDiagnosticsNotification.type, (params: lsp.PublishDiagnosticsParams) => {
102+
resolve(params.diagnostics);
103+
});
104+
});
105+
expect(diagnostics.length).toBe(1);
106+
expect(diagnostics[0].message).toContain(`Identifier 'doesnotexist' is not defined.`);
107+
});
89108
});
90109

91110
describe('initialization', () => {

server/src/session.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,9 @@ export class Session {
272272
// configFileErrors is an empty array even if there's no error, so check length.
273273
this.error(configFileErrors.map(e => e.messageText).join('\n'));
274274
}
275-
if (!configFileName) {
276-
// It is not really an error if there is no config file, because the
277-
// first call to openClientFile() will create a project for the file if
278-
// it does not exist, but the method will not return the config filename.
279-
// In subsequent operations, we'll call this.getDefaultProjectForScriptInfo(),
280-
// and there we make a second call to openClientFile(). By then, since
281-
// the project has already been created, we will receive the config
282-
// filename, and we can attach the file to the project it belongs to.
283-
return;
284-
}
285-
const project = this.projectService.findProject(configFileName);
275+
const project = configFileName ?
276+
this.projectService.findProject(configFileName) :
277+
this.projectService.getScriptInfo(filePath)?.containingProjects.find(isConfiguredProject);
286278
if (!project) {
287279
this.error(`Failed to find project for ${filePath}`);
288280
return;
@@ -573,3 +565,7 @@ function isAngularProject(project: ts.server.Project, ngCore: string): boolean {
573565
}
574566
return false;
575567
}
568+
569+
function isConfiguredProject(project: ts.server.Project): project is ts.server.ConfiguredProject {
570+
return project.projectKind === ts.server.ProjectKind.Configured;
571+
}

0 commit comments

Comments
 (0)