Added code to command-line version to report a non-zero exit code if the config file couldn't be read or parsed.

This commit is contained in:
Eric Traut 2019-10-23 23:34:27 -07:00
parent bc22f60eed
commit 358abe62e4
3 changed files with 27 additions and 2 deletions

View File

@ -30,4 +30,5 @@ Pyright can be run as either a VS Code extension or as a node-based command-line
| 0 | No errors reported | | 0 | No errors reported |
| 1 | One or more errors reported | | 1 | One or more errors reported |
| 2 | Fatal error occurred with no errors or warnings reported | | 2 | Fatal error occurred with no errors or warnings reported |
| 3 | Config file could not be read or parsed |

View File

@ -39,6 +39,7 @@ export interface AnalysisResults {
filesInProgram: number; filesInProgram: number;
filesRequiringAnalysis: number; filesRequiringAnalysis: number;
fatalErrorOccurred: boolean; fatalErrorOccurred: boolean;
configParseErrorOccurred: boolean;
elapsedTime: number; elapsedTime: number;
} }
@ -510,6 +511,7 @@ export class AnalyzerService {
configContents = fs.readFileSync(configPath, { encoding: 'utf8' }); configContents = fs.readFileSync(configPath, { encoding: 'utf8' });
} catch { } catch {
this._console.log(`Config file "${ configPath }" could not be read.`); this._console.log(`Config file "${ configPath }" could not be read.`);
this._reportConfigParseError();
return undefined; return undefined;
} }
@ -532,7 +534,8 @@ export class AnalyzerService {
// resulting in parse errors. We'll give it a little more time and // resulting in parse errors. We'll give it a little more time and
// try again. // try again.
if (parseAttemptCount++ >= 5) { if (parseAttemptCount++ >= 5) {
this._console.log(`Config file "${ configPath }" could not be parsed.`); this._console.log(`Config file "${ configPath }" could not be parsed. Verify that JSON is correct.`);
this._reportConfigParseError();
return undefined; return undefined;
} }
} }
@ -876,6 +879,7 @@ export class AnalyzerService {
filesInProgram: this._program.getFileCount(), filesInProgram: this._program.getFileCount(),
filesRequiringAnalysis: filesLeftToAnalyze, filesRequiringAnalysis: filesLeftToAnalyze,
fatalErrorOccurred: false, fatalErrorOccurred: false,
configParseErrorOccurred: false,
elapsedTime: duration.getDurationInSeconds() elapsedTime: duration.getDurationInSeconds()
}; };
@ -896,6 +900,7 @@ export class AnalyzerService {
filesInProgram: 0, filesInProgram: 0,
filesRequiringAnalysis: 0, filesRequiringAnalysis: 0,
fatalErrorOccurred: true, fatalErrorOccurred: true,
configParseErrorOccurred: false,
elapsedTime: 0 elapsedTime: 0
}); });
} }
@ -912,9 +917,23 @@ export class AnalyzerService {
filesInProgram: this._program.getFileCount(), filesInProgram: this._program.getFileCount(),
filesRequiringAnalysis: this._program.getFilesToAnalyzeCount(), filesRequiringAnalysis: this._program.getFilesToAnalyzeCount(),
fatalErrorOccurred: false, fatalErrorOccurred: false,
configParseErrorOccurred: false,
elapsedTime: 0 elapsedTime: 0
}); });
} }
} }
} }
private _reportConfigParseError() {
if (this._onCompletionCallback) {
this._onCompletionCallback({
diagnostics: [],
filesInProgram: 0,
filesRequiringAnalysis: 0,
fatalErrorOccurred: false,
configParseErrorOccurred: true,
elapsedTime: 0
});
}
}
} }

View File

@ -27,7 +27,8 @@ const toolName = 'pyright';
enum ExitStatus { enum ExitStatus {
NoErrors = 0, NoErrors = 0,
ErrorsReported = 1, ErrorsReported = 1,
FatalError = 2 FatalError = 2,
ConfigFileParseError = 3
} }
interface DiagnosticResult { interface DiagnosticResult {
@ -119,6 +120,10 @@ function processArgs() {
process.exit(ExitStatus.FatalError); process.exit(ExitStatus.FatalError);
} }
if (results.configParseErrorOccurred) {
process.exit(ExitStatus.ConfigFileParseError);
}
let errorCount = 0; let errorCount = 0;
if (results.diagnostics.length > 0 && !args.createstub) { if (results.diagnostics.length > 0 && !args.createstub) {
const report = reportDiagnostics(results.diagnostics); const report = reportDiagnostics(results.diagnostics);