diff --git a/packages/pyright-internal/src/analyzer/sourceFile.ts b/packages/pyright-internal/src/analyzer/sourceFile.ts index 772de617ea..f8bc1632ce 100644 --- a/packages/pyright-internal/src/analyzer/sourceFile.ts +++ b/packages/pyright-internal/src/analyzer/sourceFile.ts @@ -48,7 +48,7 @@ import { SourceMapper } from './sourceMapper'; import { SymbolTable } from './symbol'; import { TestWalker } from './testWalker'; import { TypeEvaluator } from './typeEvaluatorTypes'; -import { sortDiagnosticsAndMatchBaseline } from '../baseline'; +import { matchBaselinedDiagnostics } from '../baseline'; // Limit the number of import cycles tracked per source file. const _maxImportCyclesPerFile = 4; @@ -1247,7 +1247,7 @@ export class SourceFile { // Now add in the "unnecessary type ignore" diagnostics. diagList = diagList.concat(unnecessaryTypeIgnoreDiags); - diagList = sortDiagnosticsAndMatchBaseline(this.fileSystem, configOptions.projectRoot, this._uri, diagList); + diagList = matchBaselinedDiagnostics(this.fileSystem, configOptions.projectRoot, this._uri, diagList); // If we're not returning any diagnostics, filter out all of // the errors and warnings, leaving only the unreachable code diff --git a/packages/pyright-internal/src/baseline.ts b/packages/pyright-internal/src/baseline.ts index 753b6b04d8..c29503fef7 100644 --- a/packages/pyright-internal/src/baseline.ts +++ b/packages/pyright-internal/src/baseline.ts @@ -148,14 +148,21 @@ export const getBaselinedErrorsForFile = (fs: ReadOnlyFileSystem, rootDir: Uri, return result ?? []; }; -export const sortDiagnosticsAndMatchBaseline = ( +export const matchBaselinedDiagnostics = ( fs: ReadOnlyFileSystem, rootDir: Uri, file: Uri, diagnostics: Diagnostic[] ): Diagnostic[] => { - diagnostics.sort(compareDiagnostics); - const diff = diffArrays(getBaselinedErrorsForFile(fs, rootDir, file), diagnostics, { + const baselinedDiagnostics = getBaselinedErrorsForFile(fs, rootDir, file); + if (baselinedDiagnostics.length) { + // don't bother sorting if there's no baselined diagnostics to compare against, as they get sorted + // later anyway + diagnostics = [...diagnostics].sort(compareDiagnostics); + } else { + return diagnostics; + } + const diff = diffArrays(baselinedDiagnostics, diagnostics, { comparator: (baselinedDiagnostic, diagnostic) => baselinedDiagnostic.code === diagnostic.getRule() && baselinedDiagnostic.range.startColumn === diagnostic.range.start.character && diff --git a/packages/pyright-internal/src/pyright.ts b/packages/pyright-internal/src/pyright.ts index d3fb701f42..894303608e 100644 --- a/packages/pyright-internal/src/pyright.ts +++ b/packages/pyright-internal/src/pyright.ts @@ -30,7 +30,7 @@ import { CommandLineOptions as PyrightCommandLineOptions } from './common/comman import { ConsoleInterface, LogLevel, StandardConsole, StderrConsole } from './common/console'; import { fail } from './common/debug'; import { createDeferred } from './common/deferred'; -import { Diagnostic, DiagnosticCategory } from './common/diagnostic'; +import { compareDiagnostics, Diagnostic, DiagnosticCategory } from './common/diagnostic'; import { FileDiagnostics } from './common/diagnosticSink'; import { FullAccessHost } from './common/fullAccessHost'; import { combinePaths, normalizePath } from './common/pathUtils'; @@ -1199,7 +1199,7 @@ function reportDiagnosticsAsJsonWithoutLogging( }; fileDiagnostics.forEach((fileDiag) => { - fileDiag.diagnostics.forEach((diag) => { + fileDiag.diagnostics.sort(compareDiagnostics).forEach((diag) => { if ( diag.category === DiagnosticCategory.Error || diag.category === DiagnosticCategory.Warning || @@ -1308,11 +1308,13 @@ function reportDiagnosticsAsText( fileDiagnostics.forEach((fileDiagnostics) => { // Don't report unused code or deprecated diagnostics. - const fileErrorsAndWarnings = fileDiagnostics.diagnostics.filter( - (diag) => - !isHintDiagnostic(diag) && - isDiagnosticIncluded(convertDiagnosticCategoryToSeverity(diag.category), minSeverityLevel) - ); + const fileErrorsAndWarnings = fileDiagnostics.diagnostics + .filter( + (diag) => + !isHintDiagnostic(diag) && + isDiagnosticIncluded(convertDiagnosticCategoryToSeverity(diag.category), minSeverityLevel) + ) + .sort(compareDiagnostics); if (fileErrorsAndWarnings.length > 0) { console.info(`${fileDiagnostics.fileUri.toUserVisibleString()}`);