Skip to content

Commit 0ae938b

Browse files
authored
Report error when cannot read file (microsoft#37611)
This also consolidates helper for readFile failure
1 parent 9119fe3 commit 0ae938b

File tree

8 files changed

+35
-29
lines changed

8 files changed

+35
-29
lines changed

src/compiler/commandLineParser.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -1242,10 +1242,9 @@ namespace ts {
12421242
}
12431243

12441244
function parseResponseFile(fileName: string) {
1245-
const text = readFile ? readFile(fileName) : sys.readFile(fileName);
1246-
1247-
if (!text) {
1248-
errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, fileName));
1245+
const text = tryReadFile(fileName, readFile || (fileName => sys.readFile(fileName)));
1246+
if (!isString(text)) {
1247+
errors.push(text);
12491248
return;
12501249
}
12511250

@@ -1466,18 +1465,9 @@ namespace ts {
14661465
extendedConfigCache?: Map<ExtendedConfigCacheEntry>,
14671466
watchOptionsToExtend?: WatchOptions
14681467
): ParsedCommandLine | undefined {
1469-
let configFileText: string | undefined;
1470-
try {
1471-
configFileText = host.readFile(configFileName);
1472-
}
1473-
catch (e) {
1474-
const error = createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, configFileName, e.message);
1475-
host.onUnRecoverableConfigFileDiagnostic(error);
1476-
return undefined;
1477-
}
1478-
if (!configFileText) {
1479-
const error = createCompilerDiagnostic(Diagnostics.File_0_not_found, configFileName);
1480-
host.onUnRecoverableConfigFileDiagnostic(error);
1468+
const configFileText = tryReadFile(configFileName, fileName => host.readFile(fileName));
1469+
if (!isString(configFileText)) {
1470+
host.onUnRecoverableConfigFileDiagnostic(configFileText);
14811471
return undefined;
14821472
}
14831473

@@ -1530,15 +1520,16 @@ namespace ts {
15301520
return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : <TsConfigSourceFile>{ parseDiagnostics: [textOrDiagnostic] };
15311521
}
15321522

1533-
function tryReadFile(fileName: string, readFile: (path: string) => string | undefined): string | Diagnostic {
1523+
/*@internal*/
1524+
export function tryReadFile(fileName: string, readFile: (path: string) => string | undefined): string | Diagnostic {
15341525
let text: string | undefined;
15351526
try {
15361527
text = readFile(fileName);
15371528
}
15381529
catch (e) {
15391530
return createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message);
15401531
}
1541-
return text === undefined ? createCompilerDiagnostic(Diagnostics.The_specified_path_does_not_exist_Colon_0, fileName) : text;
1532+
return text === undefined ? createCompilerDiagnostic(Diagnostics.Cannot_read_file_0, fileName) : text;
15421533
}
15431534

15441535
function commandLineOptionsToMap(options: readonly CommandLineOption[]) {

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,10 @@
34613461
"category": "Error",
34623462
"code": 5082
34633463
},
3464+
"Cannot read file '{0}'.": {
3465+
"category": "Error",
3466+
"code": 5083
3467+
},
34643468

34653469
"Generates a sourcemap for each corresponding '.d.ts' file.": {
34663470
"category": "Message",

src/server/editorServices.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1974,13 +1974,13 @@ namespace ts.server {
19741974
// Read updated contents from disk
19751975
const configFilename = normalizePath(project.getConfigFilePath());
19761976

1977-
const configFileContent = this.host.readFile(configFilename) || "";
1978-
1979-
const result = parseJsonText(configFilename, configFileContent);
1977+
const configFileContent = tryReadFile(configFilename, fileName => this.host.readFile(fileName));
1978+
const result = parseJsonText(configFilename, isString(configFileContent) ? configFileContent : "");
19801979
if (!result.endOfFileToken) {
19811980
result.endOfFileToken = <EndOfFileToken>{ kind: SyntaxKind.EndOfFileToken };
19821981
}
19831982
const configFileErrors = result.parseDiagnostics as Diagnostic[];
1983+
if (!isString(configFileContent)) configFileErrors.push(configFileContent);
19841984
const parsedCommandLine = parseJsonSourceFileConfigFileContent(
19851985
result,
19861986
project.getCachedDirectoryStructureHost(),

src/testRunner/unittests/tsserver/configuredProjects.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1245,15 +1245,26 @@ declare var console: {
12451245
content: "let t = 10;"
12461246
};
12471247

1248-
const host = createServerHost([file1, configFile]);
1249-
const projectService = createProjectService(host);
1248+
const host = createServerHost([file1, libFile, configFile]);
1249+
const { session, events } = createSessionWithEventTracking<server.ConfigFileDiagEvent>(host, server.ConfigFileDiagEvent);
12501250
const originalReadFile = host.readFile;
12511251
host.readFile = f => {
12521252
return f === configFile.path ?
12531253
undefined :
12541254
originalReadFile.call(host, f);
12551255
};
1256-
projectService.openClientFile(file1.path);
1256+
openFilesForSession([file1], session);
1257+
1258+
assert.deepEqual(events, [{
1259+
eventName: server.ConfigFileDiagEvent,
1260+
data: {
1261+
triggerFile: file1.path,
1262+
configFileName: configFile.path,
1263+
diagnostics: [
1264+
createCompilerDiagnostic(Diagnostics.Cannot_read_file_0, configFile.path)
1265+
]
1266+
}
1267+
}]);
12571268
});
12581269
});
12591270
}

tests/baselines/reference/tsbuild/configFileErrors/initial-build/when-tsconfig-extends-the-missing-file.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//// [/lib/initial-buildOutput.txt]
22
/lib/tsc --b /src/tsconfig.json
3-
[91merror[0m[90m TS5058: [0mThe specified path does not exist: '/src/foobar.json'.
3+
[91merror[0m[90m TS5083: [0mCannot read file '/src/foobar.json'.
44

55
error TS18003: No inputs were found in config file '/src/tsconfig.first.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.
66

7-
[91merror[0m[90m TS5058: [0mThe specified path does not exist: '/src/foobar.json'.
7+
[91merror[0m[90m TS5083: [0mCannot read file '/src/foobar.json'.
88

99
error TS18003: No inputs were found in config file '/src/tsconfig.second.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.
1010

tests/baselines/reference/tsbuild/exitCodeOnBogusFile/initial-build/test-exit-code.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [/lib/initial-buildOutput.txt]
22
/lib/tsc -b bogus.json
3-
[91merror[0m[90m TS6053: [0mFile '/bogus.json' not found.
3+
[91merror[0m[90m TS5083: [0mCannot read file '/bogus.json'.
44

55

66
Found 1 error.

tests/baselines/reference/tsbuild/watchMode/programUpdates/watches-config-files-that-are-not-present.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Output::
146146
[12:00:37 AM] Starting compilation in watch mode...
147147

148148

149-
[91merror[0m[90m TS6053: [0mFile '/user/username/projects/sample1/logic/tsconfig.json' not found.
149+
[91merror[0m[90m TS5083: [0mCannot read file '/user/username/projects/sample1/logic/tsconfig.json'.
150150

151151

152152
[12:00:52 AM] Found 1 error. Watching for file changes.

tests/baselines/reference/tscWatch/programUpdates/config-file-is-deleted.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Output::
8080
[12:00:24 AM] File change detected. Starting incremental compilation...
8181

8282

83-
[91merror[0m[90m TS6053: [0mFile '/a/b/tsconfig.json' not found.
83+
[91merror[0m[90m TS5083: [0mCannot read file '/a/b/tsconfig.json'.
8484

8585

8686

0 commit comments

Comments
 (0)