Skip to content

Report error when cannot read file #37611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -1466,18 +1465,9 @@ namespace ts {
extendedConfigCache?: Map<ExtendedConfigCacheEntry>,
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;
}

Expand Down Expand Up @@ -1530,15 +1520,16 @@ namespace ts {
return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : <TsConfigSourceFile>{ 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);
}
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[]) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <EndOfFileToken>{ kind: SyntaxKind.EndOfFileToken };
}
const configFileErrors = result.parseDiagnostics as Diagnostic[];
if (!isString(configFileContent)) configFileErrors.push(configFileContent);
const parsedCommandLine = parseJsonSourceFileConfigFileContent(
result,
project.getCachedDirectoryStructureHost(),
Expand Down
17 changes: 14 additions & 3 deletions src/testRunner/unittests/tsserver/configuredProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<server.ConfigFileDiagEvent>(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)
]
}
}]);
});
});
}
Original file line number Diff line number Diff line change
@@ -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 '[]'.

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'.



Expand Down