Skip to content

Commit 7cf93f9

Browse files
committed
Report config file errors as part of syntactic diagnostic on the file
1 parent ea60e99 commit 7cf93f9

File tree

6 files changed

+127
-53
lines changed

6 files changed

+127
-53
lines changed

src/harness/unittests/projectErrors.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace ts.projectSystem {
66
describe("Project errors", () => {
77
function checkProjectErrors(projectFiles: server.ProjectFilesWithTSDiagnostics, expectedErrors: string[]) {
88
assert.isTrue(projectFiles !== undefined, "missing project files");
9-
const errors = projectFiles.projectErrors;
9+
checkProjectErrorsWorker(projectFiles.projectErrors, expectedErrors);
10+
}
11+
12+
function checkProjectErrorsWorker(errors: Diagnostic[], expectedErrors: string[]) {
1013
assert.equal(errors ? errors.length : 0, expectedErrors.length, `expected ${expectedErrors.length} error in the list`);
1114
if (expectedErrors.length) {
1215
for (let i = 0; i < errors.length; i++) {
@@ -122,9 +125,13 @@ namespace ts.projectSystem {
122125
projectService.checkNumberOfProjects({ configuredProjects: 1 });
123126
const configuredProject = forEach(projectService.synchronizeProjectList([]), f => f.info.projectName === corruptedConfig.path && f);
124127
assert.isTrue(configuredProject !== undefined, "should find configured project");
125-
checkProjectErrors(configuredProject, [
128+
checkProjectErrors(configuredProject, []);
129+
const projectErrors = projectService.configuredProjects[0].getAllProjectErrors();
130+
checkProjectErrorsWorker(projectErrors, [
126131
"'{' expected."
127132
]);
133+
assert.isNotNull(projectErrors[0].file);
134+
assert.equal(projectErrors[0].file.fileName, corruptedConfig.path);
128135
}
129136
// fix config and trigger watcher
130137
host.reloadFS([file1, file2, correctConfig]);
@@ -134,6 +141,8 @@ namespace ts.projectSystem {
134141
const configuredProject = forEach(projectService.synchronizeProjectList([]), f => f.info.projectName === corruptedConfig.path && f);
135142
assert.isTrue(configuredProject !== undefined, "should find configured project");
136143
checkProjectErrors(configuredProject, []);
144+
const projectErrors = projectService.configuredProjects[0].getAllProjectErrors();
145+
checkProjectErrorsWorker(projectErrors, []);
137146
}
138147
});
139148

@@ -163,6 +172,8 @@ namespace ts.projectSystem {
163172
const configuredProject = forEach(projectService.synchronizeProjectList([]), f => f.info.projectName === corruptedConfig.path && f);
164173
assert.isTrue(configuredProject !== undefined, "should find configured project");
165174
checkProjectErrors(configuredProject, []);
175+
const projectErrors = projectService.configuredProjects[0].getAllProjectErrors();
176+
checkProjectErrorsWorker(projectErrors, []);
166177
}
167178
// break config and trigger watcher
168179
host.reloadFS([file1, file2, corruptedConfig]);
@@ -171,10 +182,14 @@ namespace ts.projectSystem {
171182
projectService.checkNumberOfProjects({ configuredProjects: 1 });
172183
const configuredProject = forEach(projectService.synchronizeProjectList([]), f => f.info.projectName === corruptedConfig.path && f);
173184
assert.isTrue(configuredProject !== undefined, "should find configured project");
174-
checkProjectErrors(configuredProject, [
185+
checkProjectErrors(configuredProject, []);
186+
const projectErrors = projectService.configuredProjects[0].getAllProjectErrors();
187+
checkProjectErrorsWorker(projectErrors, [
175188
"'{' expected."
176189
]);
190+
assert.isNotNull(projectErrors[0].file);
191+
assert.equal(projectErrors[0].file.fileName, corruptedConfig.path);
177192
}
178193
});
179194
});
180-
}
195+
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4005,38 +4005,37 @@ namespace ts.projectSystem {
40054005
checkNumberOfProjects(projectService, { configuredProjects: 1 });
40064006
const projectName = projectService.configuredProjects[0].getProjectName();
40074007

4008-
const diags = session.executeCommand(<server.protocol.CompilerOptionsDiagnosticsRequest>{
4008+
const diags = session.executeCommand(<server.protocol.SemanticDiagnosticsSyncRequest>{
40094009
type: "request",
4010-
command: server.CommandNames.CompilerOptionsDiagnosticsFull,
4010+
command: server.CommandNames.SemanticDiagnosticsSync,
40114011
seq: 2,
4012-
arguments: { projectFileName: projectName }
4012+
arguments: { file: configFile.path, projectFileName: projectName, includeLinePosition: true }
40134013
}).response;
40144014
assert.isTrue(diags.length === 2);
40154015

40164016
configFile.content = configFileContentWithoutCommentLine;
40174017
host.reloadFS([file, configFile]);
40184018
host.triggerFileWatcherCallback(configFile.path);
40194019

4020-
const diagsAfterEdit = session.executeCommand(<server.protocol.CompilerOptionsDiagnosticsRequest>{
4020+
const diagsAfterEdit = session.executeCommand(<server.protocol.SemanticDiagnosticsSyncRequest>{
40214021
type: "request",
4022-
command: server.CommandNames.CompilerOptionsDiagnosticsFull,
4022+
command: server.CommandNames.SemanticDiagnosticsSync,
40234023
seq: 2,
4024-
arguments: { projectFileName: projectName }
4024+
arguments: { file: configFile.path, projectFileName: projectName, includeLinePosition: true }
40254025
}).response;
40264026
assert.isTrue(diagsAfterEdit.length === 2);
40274027

40284028
verifyDiagnostic(diags[0], diagsAfterEdit[0]);
40294029
verifyDiagnostic(diags[1], diagsAfterEdit[1]);
40304030

4031-
function verifyDiagnostic(beforeEditDiag: server.protocol.DiagnosticWithLinePositionAndFileName, afterEditDiag: server.protocol.DiagnosticWithLinePositionAndFileName) {
4031+
function verifyDiagnostic(beforeEditDiag: server.protocol.DiagnosticWithLinePosition, afterEditDiag: server.protocol.DiagnosticWithLinePosition) {
40324032
assert.equal(beforeEditDiag.message, afterEditDiag.message);
40334033
assert.equal(beforeEditDiag.code, afterEditDiag.code);
40344034
assert.equal(beforeEditDiag.category, afterEditDiag.category);
40354035
assert.equal(beforeEditDiag.startLocation.line, afterEditDiag.startLocation.line + 1);
40364036
assert.equal(beforeEditDiag.startLocation.offset, afterEditDiag.startLocation.offset);
40374037
assert.equal(beforeEditDiag.endLocation.line, afterEditDiag.endLocation.line + 1);
40384038
assert.equal(beforeEditDiag.endLocation.offset, afterEditDiag.endLocation.offset);
4039-
assert.equal(beforeEditDiag.fileName, afterEditDiag.fileName);
40404039
}
40414040
});
40424041
});

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ namespace ts.server {
10481048
return {
10491049
success: conversionResult.success,
10501050
project,
1051-
errors: project.getProjectErrors()
1051+
errors: project.getGlobalProjectErrors()
10521052
};
10531053
}
10541054

src/server/project.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,14 @@ namespace ts.server {
221221
}
222222
}
223223

224-
getProjectErrors() {
224+
/**
225+
* Get the errors that dont have any file name associated
226+
*/
227+
getGlobalProjectErrors() {
228+
return filter(this.projectErrors, diagnostic => !diagnostic.file);
229+
}
230+
231+
getAllProjectErrors() {
225232
return this.projectErrors;
226233
}
227234

@@ -399,6 +406,25 @@ namespace ts.server {
399406
return result;
400407
}
401408

409+
hasConfigFile(configFilePath: NormalizedPath) {
410+
if (this.program && this.languageServiceEnabled) {
411+
const configFile = this.program.getCompilerOptions().configFile;
412+
if (configFile) {
413+
if (configFilePath === asNormalizedPath(configFile.fileName)) {
414+
return true;
415+
}
416+
if (configFile.extendedSourceFiles) {
417+
for (const f of configFile.extendedSourceFiles) {
418+
if (configFilePath === asNormalizedPath(f)) {
419+
return true;
420+
}
421+
}
422+
}
423+
}
424+
}
425+
return false;
426+
}
427+
402428
getAllEmittableFiles() {
403429
if (!this.languageServiceEnabled) {
404430
return [];
@@ -655,7 +681,7 @@ namespace ts.server {
655681
if (this.lastReportedFileNames && lastKnownVersion === this.lastReportedVersion) {
656682
// if current structure version is the same - return info without any changes
657683
if (this.projectStructureVersion === this.lastReportedVersion && !updatedFileNames) {
658-
return { info, projectErrors: this.projectErrors };
684+
return { info, projectErrors: this.getGlobalProjectErrors() };
659685
}
660686
// compute and return the difference
661687
const lastReportedFileNames = this.lastReportedFileNames;
@@ -677,14 +703,14 @@ namespace ts.server {
677703
});
678704
this.lastReportedFileNames = currentFiles;
679705
this.lastReportedVersion = this.projectStructureVersion;
680-
return { info, changes: { added, removed, updated }, projectErrors: this.projectErrors };
706+
return { info, changes: { added, removed, updated }, projectErrors: this.getGlobalProjectErrors() };
681707
}
682708
else {
683709
// unknown version - return everything
684710
const projectFileNames = this.getFileNames();
685711
this.lastReportedFileNames = arrayToMap(projectFileNames, x => x);
686712
this.lastReportedVersion = this.projectStructureVersion;
687-
return { info, files: projectFileNames, projectErrors: this.projectErrors };
713+
return { info, files: projectFileNames, projectErrors: this.getGlobalProjectErrors() };
688714
}
689715
}
690716

@@ -1114,4 +1140,4 @@ namespace ts.server {
11141140
this.typeAcquisition = newTypeAcquisition;
11151141
}
11161142
}
1117-
}
1143+
}

src/server/protocol.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ namespace ts.server.protocol {
358358
code: number;
359359
}
360360

361-
export interface DiagnosticWithLinePositionAndFileName extends DiagnosticWithLinePosition {
362-
fileName: string;
363-
}
364-
365361
/**
366362
* Response message for "projectInfo" request
367363
*/
@@ -969,7 +965,7 @@ namespace ts.server.protocol {
969965
/**
970966
* List of errors in project
971967
*/
972-
projectErrors: DiagnosticWithLinePositionAndFileName[];
968+
projectErrors: DiagnosticWithLinePosition[];
973969
}
974970

975971
/**

0 commit comments

Comments
 (0)