Skip to content

Commit

Permalink
log error to the console instead of crashing when failing to read or …
Browse files Browse the repository at this point in the history
…write the baseline file
  • Loading branch information
DetachHead committed Dec 13, 2024
1 parent be7a461 commit de1a5ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
9 changes: 5 additions & 4 deletions packages/pyright-internal/src/analyzer/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,10 +1247,11 @@ export class SourceFile {
// Now add in the "unnecessary type ignore" diagnostics.
diagList = diagList.concat(unnecessaryTypeIgnoreDiags);

diagList = new BaselineHandler(this.fileSystem, configOptions.projectRoot).sortDiagnosticsAndMatchBaseline(
this._uri,
diagList
);
diagList = new BaselineHandler(
this.fileSystem,
configOptions.projectRoot,
this._console
).sortDiagnosticsAndMatchBaseline(this._uri, diagList);

// If we're not returning any diagnostics, filter out all of
// the errors and warnings, leaving only the unreachable code
Expand Down
24 changes: 17 additions & 7 deletions packages/pyright-internal/src/baseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { diffArrays } from 'diff';
import { assert } from './common/debug';
import { Range } from './common/textRange';
import { add } from 'lodash';
import { ConsoleInterface } from './common/console';

export interface BaselinedDiagnostic {
code: DiagnosticRule | undefined;
Expand Down Expand Up @@ -84,7 +85,7 @@ export class BaselineHandler {
* none of this functionality should be observed by the user until they explicitly opt in to the baseline
* feature
*/
constructor(private _fs: FileSystem, private _rootDir: Uri) {
constructor(private _fs: FileSystem, private _rootDir: Uri, private _console: ConsoleInterface) {
this.fileUri = baselineFilePath(_rootDir);
}

Expand All @@ -96,7 +97,12 @@ export class BaselineHandler {
// assume the file didn't exist
return undefined;
}
return JSON.parse(baselineFileContents);
try {
return JSON.parse(baselineFileContents);
} catch (e) {
this._console.error(`failed to parse baseline file - ${e}`);
return undefined;
}
};

/**
Expand All @@ -113,13 +119,12 @@ export class BaselineHandler {
force: T,
removeDeletedFiles: boolean,
filesWithDiagnostics: readonly FileDiagnostics[]
): OptionalIfFalse<T, BaselineDiff<T>> => {
type Result = OptionalIfFalse<T, BaselineDiff<T>>;
): BaselineDiff<T> | undefined => {
const baselineData = this.getContents();
if (!force) {
if (!baselineData) {
// there currently is no baseline file and the user did not explicitly ask for one, so we do nothing
return undefined as Result;
return undefined;
}
/** diagnostics that haven't yet been baselined */
const newDiagnostics = filesWithDiagnostics.map((file) => ({
Expand All @@ -131,7 +136,7 @@ export class BaselineHandler {
if (newDiagnostics.map((fileWithDiagnostics) => fileWithDiagnostics.diagnostics.length).reduce(add, 0)) {
// there are unbaselined diagnostics and the user did not explicitly ask to update the baseline, so we do
// nothing
return undefined as Result;
return undefined;
}
}
const newBaselineFiles = this._filteredDiagnosticsToBaselineFormat(filesWithDiagnostics).files;
Expand Down Expand Up @@ -159,7 +164,12 @@ export class BaselineHandler {
}
}
this._fs.mkdirSync(this.fileUri.getDirectory(), { recursive: true });
this._fs.writeFileSync(this.fileUri, JSON.stringify(result, undefined, 4), null);
try {
this._fs.writeFileSync(this.fileUri, JSON.stringify(result, undefined, 4), null);
} catch (e) {
this._console.error(`failed to write baseline file - ${e}`);
return undefined;
}
return new BaselineDiff(this._rootDir, { files: previousBaselineFiles }, result, force);
};

Expand Down
6 changes: 4 additions & 2 deletions packages/pyright-internal/src/commands/writeBaseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class WriteBaselineCommand implements ServerCommand {
if (workspace) {
const workspaceRoot = workspace.rootUri;
if (workspaceRoot) {
const baselineHandler = new BaselineHandler(workspace.service.fs, workspaceRoot);
const baselineHandler = new BaselineHandler(workspace.service.fs, workspaceRoot, this._ls.console);
const configOptions = workspace.service.getConfigOptions();
// filter out excluded files. ideally they shouldn't be present at all. see
// https://github.com/DetachHead/basedpyright/issues/31
Expand All @@ -46,7 +46,9 @@ export class WriteBaselineCommand implements ServerCommand {
.map(([_, diagnostics]) => diagnostics);
const newBaseline = baselineHandler.write(true, true, filteredFiles);
workspace.service.baselineUpdated();
this._ls.window.showInformationMessage(newBaseline.getSummaryMessage());
if (newBaseline) {
this._ls.window.showInformationMessage(newBaseline.getSummaryMessage());
}
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/pyright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ const outputResults = (
? Uri.file(options.executionRoot ?? '', service.serviceProvider)
: options.executionRoot;

const baselineFile = new BaselineHandler(service.fs, rootDir);
const baselineFile = new BaselineHandler(service.fs, rootDir, console);
const baselineDiffMessage = baselineFile.write(args.writebaseline, true, results.diagnostics)?.getSummaryMessage();
if (baselineDiffMessage) {
console.info(baselineDiffMessage);
Expand Down

0 comments on commit de1a5ca

Please sign in to comment.