From 135214a761dad53cc56b7628e8934908ecf73036 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 25 Mar 2020 15:42:11 -0700 Subject: [PATCH] Report error when cannot read file This also consolidates helper for readFile failure --- src/compiler/commandLineParser.ts | 27 +++++++------------ src/compiler/diagnosticMessages.json | 4 +++ src/server/editorServices.ts | 6 ++--- .../unittests/tsserver/configuredProjects.ts | 17 +++++++++--- .../when-tsconfig-extends-the-missing-file.js | 4 +-- .../initial-build/test-exit-code.js | 2 +- ...tches-config-files-that-are-not-present.js | 2 +- .../programUpdates/config-file-is-deleted.js | 2 +- 8 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 6fd9803aaacf4..690529a07b89f 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1242,10 +1242,9 @@ namespace ts { } function parseResponseFile(fileName: string) { - const text = readFile ? readFile(fileName) : sys.readFile(fileName); - - if (!text) { - errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, fileName)); + const text = tryReadFile(fileName, readFile || (fileName => sys.readFile(fileName))); + if (!isString(text)) { + errors.push(text); return; } @@ -1466,18 +1465,9 @@ namespace ts { extendedConfigCache?: Map, watchOptionsToExtend?: WatchOptions ): ParsedCommandLine | undefined { - let configFileText: string | undefined; - try { - configFileText = host.readFile(configFileName); - } - catch (e) { - const error = createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, configFileName, e.message); - host.onUnRecoverableConfigFileDiagnostic(error); - return undefined; - } - if (!configFileText) { - const error = createCompilerDiagnostic(Diagnostics.File_0_not_found, configFileName); - host.onUnRecoverableConfigFileDiagnostic(error); + const configFileText = tryReadFile(configFileName, fileName => host.readFile(fileName)); + if (!isString(configFileText)) { + host.onUnRecoverableConfigFileDiagnostic(configFileText); return undefined; } @@ -1530,7 +1520,8 @@ namespace ts { return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : { parseDiagnostics: [textOrDiagnostic] }; } - function tryReadFile(fileName: string, readFile: (path: string) => string | undefined): string | Diagnostic { + /*@internal*/ + export function tryReadFile(fileName: string, readFile: (path: string) => string | undefined): string | Diagnostic { let text: string | undefined; try { text = readFile(fileName); @@ -1538,7 +1529,7 @@ namespace ts { catch (e) { return createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message); } - return text === undefined ? createCompilerDiagnostic(Diagnostics.The_specified_path_does_not_exist_Colon_0, fileName) : text; + return text === undefined ? createCompilerDiagnostic(Diagnostics.Cannot_read_file_0, fileName) : text; } function commandLineOptionsToMap(options: readonly CommandLineOption[]) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index d0d7b1f778555..195fc43dbb12d 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3461,6 +3461,10 @@ "category": "Error", "code": 5082 }, + "Cannot read file '{0}'.": { + "category": "Error", + "code": 5083 + }, "Generates a sourcemap for each corresponding '.d.ts' file.": { "category": "Message", diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 0b4f5c6f242d3..47d0dc71f2faa 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1974,13 +1974,13 @@ namespace ts.server { // Read updated contents from disk const configFilename = normalizePath(project.getConfigFilePath()); - const configFileContent = this.host.readFile(configFilename) || ""; - - const result = parseJsonText(configFilename, configFileContent); + const configFileContent = tryReadFile(configFilename, fileName => this.host.readFile(fileName)); + const result = parseJsonText(configFilename, isString(configFileContent) ? configFileContent : ""); if (!result.endOfFileToken) { result.endOfFileToken = { kind: SyntaxKind.EndOfFileToken }; } const configFileErrors = result.parseDiagnostics as Diagnostic[]; + if (!isString(configFileContent)) configFileErrors.push(configFileContent); const parsedCommandLine = parseJsonSourceFileConfigFileContent( result, project.getCachedDirectoryStructureHost(), diff --git a/src/testRunner/unittests/tsserver/configuredProjects.ts b/src/testRunner/unittests/tsserver/configuredProjects.ts index 438b09eb68b11..3838702cd328b 100644 --- a/src/testRunner/unittests/tsserver/configuredProjects.ts +++ b/src/testRunner/unittests/tsserver/configuredProjects.ts @@ -1245,15 +1245,26 @@ declare var console: { content: "let t = 10;" }; - const host = createServerHost([file1, configFile]); - const projectService = createProjectService(host); + const host = createServerHost([file1, libFile, configFile]); + const { session, events } = createSessionWithEventTracking(host, server.ConfigFileDiagEvent); const originalReadFile = host.readFile; host.readFile = f => { return f === configFile.path ? undefined : originalReadFile.call(host, f); }; - projectService.openClientFile(file1.path); + openFilesForSession([file1], session); + + assert.deepEqual(events, [{ + eventName: server.ConfigFileDiagEvent, + data: { + triggerFile: file1.path, + configFileName: configFile.path, + diagnostics: [ + createCompilerDiagnostic(Diagnostics.Cannot_read_file_0, configFile.path) + ] + } + }]); }); }); } diff --git a/tests/baselines/reference/tsbuild/configFileErrors/initial-build/when-tsconfig-extends-the-missing-file.js b/tests/baselines/reference/tsbuild/configFileErrors/initial-build/when-tsconfig-extends-the-missing-file.js index 4b8a6e326ccbb..0403cf03bb88e 100644 --- a/tests/baselines/reference/tsbuild/configFileErrors/initial-build/when-tsconfig-extends-the-missing-file.js +++ b/tests/baselines/reference/tsbuild/configFileErrors/initial-build/when-tsconfig-extends-the-missing-file.js @@ -1,10 +1,10 @@ //// [/lib/initial-buildOutput.txt] /lib/tsc --b /src/tsconfig.json -error TS5058: The specified path does not exist: '/src/foobar.json'. +error TS5083: Cannot read file '/src/foobar.json'. error TS18003: No inputs were found in config file '/src/tsconfig.first.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'. -error TS5058: The specified path does not exist: '/src/foobar.json'. +error TS5083: Cannot read file '/src/foobar.json'. error TS18003: No inputs were found in config file '/src/tsconfig.second.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'. diff --git a/tests/baselines/reference/tsbuild/exitCodeOnBogusFile/initial-build/test-exit-code.js b/tests/baselines/reference/tsbuild/exitCodeOnBogusFile/initial-build/test-exit-code.js index fcd2a5c5b745d..96d503b040789 100644 --- a/tests/baselines/reference/tsbuild/exitCodeOnBogusFile/initial-build/test-exit-code.js +++ b/tests/baselines/reference/tsbuild/exitCodeOnBogusFile/initial-build/test-exit-code.js @@ -1,6 +1,6 @@ //// [/lib/initial-buildOutput.txt] /lib/tsc -b bogus.json -error TS6053: File '/bogus.json' not found. +error TS5083: Cannot read file '/bogus.json'. Found 1 error. diff --git a/tests/baselines/reference/tsbuild/watchMode/programUpdates/watches-config-files-that-are-not-present.js b/tests/baselines/reference/tsbuild/watchMode/programUpdates/watches-config-files-that-are-not-present.js index 7a1bcb4f7fe96..689551fa7519c 100644 --- a/tests/baselines/reference/tsbuild/watchMode/programUpdates/watches-config-files-that-are-not-present.js +++ b/tests/baselines/reference/tsbuild/watchMode/programUpdates/watches-config-files-that-are-not-present.js @@ -143,7 +143,7 @@ Output:: [12:00:37 AM] Starting compilation in watch mode... -error TS6053: File '/user/username/projects/sample1/logic/tsconfig.json' not found. +error TS5083: Cannot read file '/user/username/projects/sample1/logic/tsconfig.json'. [12:00:52 AM] Found 1 error. Watching for file changes. diff --git a/tests/baselines/reference/tscWatch/programUpdates/config-file-is-deleted.js b/tests/baselines/reference/tscWatch/programUpdates/config-file-is-deleted.js index 86020466715b3..73d445952b7c5 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/config-file-is-deleted.js +++ b/tests/baselines/reference/tscWatch/programUpdates/config-file-is-deleted.js @@ -80,7 +80,7 @@ Output:: [12:00:24 AM] File change detected. Starting incremental compilation... -error TS6053: File '/a/b/tsconfig.json' not found. +error TS5083: Cannot read file '/a/b/tsconfig.json'.