Skip to content

Commit

Permalink
fix write baseline command incorrectly deleting baselined errors if a…
Browse files Browse the repository at this point in the history
… file was opened and then closed
  • Loading branch information
DetachHead committed Sep 26, 2024
1 parent 4220a2d commit e44f35f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/pyright-internal/src/analyzer/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ export class Program {
fileUri: sourceFileInfo.sourceFile.getUri(),
version: sourceFileInfo.sourceFile.getClientVersion(),
diagnostics,
reason: 'analysis',
});

// Update the cached diagnosticsVersion so we can determine
Expand All @@ -923,6 +924,7 @@ export class Program {
fileUri: sourceFileInfo.sourceFile.getUri(),
version: sourceFileInfo.sourceFile.getClientVersion(),
diagnostics: [],
reason: 'analysis',
});
sourceFileInfo.diagnosticsVersion = undefined;
}
Expand Down Expand Up @@ -1087,6 +1089,7 @@ export class Program {
fileUri: fileInfo.sourceFile.getUri(),
version: fileInfo.sourceFile.getClientVersion(),
diagnostics: [],
reason: 'tracking',
});
}

Expand Down Expand Up @@ -1115,6 +1118,7 @@ export class Program {
fileUri: importedFile.sourceFile.getUri(),
version: importedFile.sourceFile.getClientVersion(),
diagnostics: [],
reason: 'tracking',
});
}

Expand All @@ -1138,6 +1142,7 @@ export class Program {
fileUri: fileInfo.sourceFile.getUri(),
version: fileInfo.sourceFile.getClientVersion(),
diagnostics: [],
reason: 'tracking',
});
fileInfo.diagnosticsVersion = undefined;
}
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/backgroundAnalysisBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ function convertAnalysisResults(result: AnalysisResults): AnalysisResults {
fileUri: Uri.fromJsonObj(f.fileUri),
version: f.version,
diagnostics: convertDiagnostics(f.diagnostics),
reason: f.reason,
};
});

Expand Down
8 changes: 7 additions & 1 deletion packages/pyright-internal/src/commands/writeBaseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export class WriteBaselineCommand implements ServerCommand {
// filter out excluded files. ideally they shouldn't be present at all. see
// https://github.com/DetachHead/basedpyright/issues/31
const filteredFiles = Object.entries(this._ls.documentsWithDiagnostics)
.filter(([filePath]) => matchFileSpecs(configOptions, Uri.file(filePath, this._ls.serviceProvider)))
.filter(
([filePath, fileDiagnostics]) =>
// filter out files whose diagnostics were cleared due to the file being closed
fileDiagnostics.reason === 'analysis' &&
// filter out files that aren't included in the project
matchFileSpecs(configOptions, Uri.file(filePath, this._ls.serviceProvider))
)
.map(([_, diagnostics]) => diagnostics);
const newBaseline = writeDiagnosticsToBaselineFile(
workspace.service.fs,
Expand Down
3 changes: 3 additions & 0 deletions packages/pyright-internal/src/common/diagnosticSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface FileDiagnostics {
fileUri: Uri;
version: number | undefined;
diagnostics: Diagnostic[];
reason: 'analysis' | 'tracking';
}

export namespace FileDiagnostics {
Expand All @@ -30,6 +31,7 @@ export namespace FileDiagnostics {
fileUri: fileDiag.fileUri.toJsonObj(),
version: fileDiag.version,
diagnostics: fileDiag.diagnostics.map((d) => d.toJsonObj()),
reason: fileDiag.reason,
};
}

Expand All @@ -38,6 +40,7 @@ export namespace FileDiagnostics {
fileUri: Uri.fromJsonObj(fileDiagObj.fileUri),
version: fileDiagObj.version,
diagnostics: fileDiagObj.diagnostics.map((d: any) => Diagnostic.fromJsonObj(d)),
reason: fileDiagObj.reason,
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
>();

// The URIs for which diagnostics are reported
readonly documentsWithDiagnostics: Record<string, FileDiagnostics & { reason: 'analysis' | 'tracking' }> = {};
readonly documentsWithDiagnostics: Record<string, FileDiagnostics> = {};

protected readonly dynamicFeatures = new DynamicFeatures();

Expand Down

0 comments on commit e44f35f

Please sign in to comment.