Skip to content

Commit 166f95c

Browse files
committed
Merge pull request microsoft#8311 from zhengbli/reportTsconfigError
[API] Add event for reporting tsconfig errors
2 parents c26e920 + 5da620e commit 166f95c

File tree

4 files changed

+133
-74
lines changed

4 files changed

+133
-74
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,10 @@
27482748
"category": "Message",
27492749
"code": 6128
27502750
},
2751+
"The config file '{0}' found doesn't contain any source files.": {
2752+
"category": "Error",
2753+
"code": 6129
2754+
},
27512755
"Variable '{0}' implicitly has an '{1}' type.": {
27522756
"category": "Error",
27532757
"code": 7005

src/server/editorServices.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,20 +1096,20 @@ namespace ts.server {
10961096
* @param filename is absolute pathname
10971097
* @param fileContent is a known version of the file content that is more up to date than the one on disk
10981098
*/
1099-
openClientFile(fileName: string, fileContent?: string, scriptKind?: ScriptKind) {
1100-
this.openOrUpdateConfiguredProjectForFile(fileName);
1099+
openClientFile(fileName: string, fileContent?: string, scriptKind?: ScriptKind): { configFileName?: string, configFileErrors?: Diagnostic[] } {
1100+
const { configFileName, configFileErrors } = this.openOrUpdateConfiguredProjectForFile(fileName);
11011101
const info = this.openFile(fileName, /*openedByClient*/ true, fileContent, scriptKind);
11021102
this.addOpenFile(info);
11031103
this.printProjects();
1104-
return info;
1104+
return { configFileName, configFileErrors };
11051105
}
11061106

11071107
/**
11081108
* This function tries to search for a tsconfig.json for the given file. If we found it,
11091109
* we first detect if there is already a configured project created for it: if so, we re-read
11101110
* the tsconfig file content and update the project; otherwise we create a new one.
11111111
*/
1112-
openOrUpdateConfiguredProjectForFile(fileName: string) {
1112+
openOrUpdateConfiguredProjectForFile(fileName: string): { configFileName?: string, configFileErrors?: Diagnostic[] } {
11131113
const searchPath = ts.normalizePath(getDirectoryPath(fileName));
11141114
this.log("Search path: " + searchPath, "Info");
11151115
const configFileName = this.findConfigFile(searchPath);
@@ -1119,7 +1119,7 @@ namespace ts.server {
11191119
if (!project) {
11201120
const configResult = this.openConfigFile(configFileName, fileName);
11211121
if (!configResult.success) {
1122-
this.log("Error opening config file " + configFileName + " " + configResult.errorMsg);
1122+
return { configFileName, configFileErrors: configResult.errors };
11231123
}
11241124
else {
11251125
this.log("Opened configuration file " + configFileName, "Info");
@@ -1133,6 +1133,7 @@ namespace ts.server {
11331133
else {
11341134
this.log("No config files found.");
11351135
}
1136+
return {};
11361137
}
11371138

11381139
/**
@@ -1222,24 +1223,25 @@ namespace ts.server {
12221223
return undefined;
12231224
}
12241225

1225-
configFileToProjectOptions(configFilename: string): { succeeded: boolean, projectOptions?: ProjectOptions, error?: ProjectOpenResult } {
1226+
configFileToProjectOptions(configFilename: string): { succeeded: boolean, projectOptions?: ProjectOptions, errors?: Diagnostic[] } {
12261227
configFilename = ts.normalizePath(configFilename);
12271228
// file references will be relative to dirPath (or absolute)
12281229
const dirPath = ts.getDirectoryPath(configFilename);
12291230
const contents = this.host.readFile(configFilename);
12301231
const rawConfig: { config?: ProjectOptions; error?: Diagnostic; } = ts.parseConfigFileTextToJson(configFilename, contents);
12311232
if (rawConfig.error) {
1232-
return { succeeded: false, error: rawConfig.error };
1233+
return { succeeded: false, errors: [rawConfig.error] };
12331234
}
12341235
else {
12351236
const parsedCommandLine = ts.parseJsonConfigFileContent(rawConfig.config, this.host, dirPath, /*existingOptions*/ {}, configFilename);
12361237
Debug.assert(!!parsedCommandLine.fileNames);
12371238

12381239
if (parsedCommandLine.errors && (parsedCommandLine.errors.length > 0)) {
1239-
return { succeeded: false, error: { errorMsg: "tsconfig option errors" } };
1240+
return { succeeded: false, errors: parsedCommandLine.errors };
12401241
}
12411242
else if (parsedCommandLine.fileNames.length === 0) {
1242-
return { succeeded: false, error: { errorMsg: "no files found" } };
1243+
const error = createCompilerDiagnostic(Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename);
1244+
return { succeeded: false, errors: [error] };
12431245
}
12441246
else {
12451247
const projectOptions: ProjectOptions = {
@@ -1252,10 +1254,10 @@ namespace ts.server {
12521254

12531255
}
12541256

1255-
openConfigFile(configFilename: string, clientFileName?: string): ProjectOpenResult {
1256-
const { succeeded, projectOptions, error } = this.configFileToProjectOptions(configFilename);
1257+
openConfigFile(configFilename: string, clientFileName?: string): { success: boolean, project?: Project, errors?: Diagnostic[] } {
1258+
const { succeeded, projectOptions, errors } = this.configFileToProjectOptions(configFilename);
12571259
if (!succeeded) {
1258-
return error;
1260+
return { success: false, errors };
12591261
}
12601262
else {
12611263
const project = this.createProject(configFilename, projectOptions);
@@ -1265,7 +1267,8 @@ namespace ts.server {
12651267
project.addRoot(info);
12661268
}
12671269
else {
1268-
return { errorMsg: "specified file " + rootFilename + " not found" };
1270+
const error = createCompilerDiagnostic(Diagnostics.File_0_not_found, rootFilename);
1271+
return { success: false, errors: [error] };
12691272
}
12701273
}
12711274
project.finishGraph();
@@ -1286,9 +1289,9 @@ namespace ts.server {
12861289
this.removeProject(project);
12871290
}
12881291
else {
1289-
const { succeeded, projectOptions, error } = this.configFileToProjectOptions(project.projectFilename);
1292+
const { succeeded, projectOptions, errors } = this.configFileToProjectOptions(project.projectFilename);
12901293
if (!succeeded) {
1291-
return error;
1294+
return errors;
12921295
}
12931296
else {
12941297
const oldFileNames = project.compilerService.host.roots.map(info => info.fileName);

0 commit comments

Comments
 (0)