diff --git a/src/Analyzers/AnalyzerFormatter.cs b/src/Analyzers/AnalyzerFormatter.cs index 39c29ce44d..cbefa24988 100644 --- a/src/Analyzers/AnalyzerFormatter.cs +++ b/src/Analyzers/AnalyzerFormatter.cs @@ -73,13 +73,17 @@ internal class AnalyzerFormatter : ICodeFormatter var projectDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds; logger.LogTrace(Resources.Complete_in_0_ms, projectDiagnosticsMS); - logger.LogTrace(Resources.Fixing_diagnostics); + // Only run code fixes when we are saving changes. + if (formatOptions.SaveFormattedFiles) + { + logger.LogTrace(Resources.Fixing_diagnostics); - // Run each analyzer individually and apply fixes if possible. - solution = await FixDiagnosticsAsync(solution, analyzers, fixers, projectDiagnostics, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false); + // Run each analyzer individually and apply fixes if possible. + solution = await FixDiagnosticsAsync(solution, analyzers, fixers, projectDiagnostics, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false); - var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS; - logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS); + var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS; + logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS); + } logger.LogTrace(Resources.Analysis_complete_in_0ms_, analysisStopwatch.ElapsedMilliseconds); diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index ec2ddd9549..e6a6240bd4 100644 --- a/src/CodeFormatter.cs +++ b/src/CodeFormatter.cs @@ -73,22 +73,12 @@ internal static class CodeFormatter var formatterRanMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS - determineFilesMS; logger.LogTrace(Resources.Complete_in_0_ms, formatterRanMS); - var solutionChanges = formattedSolution.GetChanges(solution); - - var filesFormatted = 0; - foreach (var projectChanges in solutionChanges.GetProjectChanges()) + var documentIdsWithErrors = formattedFiles.Select(file => file.DocumentId).Distinct().ToImmutableArray(); + foreach (var documentId in documentIdsWithErrors) { - foreach (var changedDocumentId in projectChanges.GetChangedDocuments()) - { - var changedDocument = solution.GetDocument(changedDocumentId); - if (changedDocument?.FilePath is null) - { - continue; - } + var documentWithError = solution.GetDocument(documentId); - logger.LogInformation(Resources.Formatted_code_file_0, changedDocument.FilePath); - filesFormatted++; - } + logger.LogInformation(Resources.Formatted_code_file_0, documentWithError!.FilePath); } var exitCode = 0; @@ -104,11 +94,11 @@ internal static class CodeFormatter ReportWriter.Write(formatOptions.ReportPath!, formattedFiles, logger); } - logger.LogDebug(Resources.Formatted_0_of_1_files, filesFormatted, fileCount); + logger.LogDebug(Resources.Formatted_0_of_1_files, documentIdsWithErrors.Length, fileCount); logger.LogInformation(Resources.Format_complete_in_0_ms, workspaceStopwatch.ElapsedMilliseconds); - return new WorkspaceFormatResult(filesFormatted, fileCount, exitCode); + return new WorkspaceFormatResult(documentIdsWithErrors.Length, fileCount, exitCode); } private static Workspace OpenFolderWorkspace(string workspacePath, SourceFileMatcher fileMatcher) diff --git a/src/Formatters/UnnecessaryImportsFormatter.cs b/src/Formatters/UnnecessaryImportsFormatter.cs index 294da7498c..c386b79777 100644 --- a/src/Formatters/UnnecessaryImportsFormatter.cs +++ b/src/Formatters/UnnecessaryImportsFormatter.cs @@ -36,6 +36,13 @@ internal sealed class UnnecessaryImportsFormatter : DocumentFormatter return sourceText; } + // If we are not saving files, do not make changes in this formatter. They will be + // reported by their diagnostic id when the analyzer formatter runs. + if (!formatOptions.SaveFormattedFiles) + { + return sourceText; + } + var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (tree is null) { diff --git a/tests/Formatters/AbstractFormatterTests.cs b/tests/Formatters/AbstractFormatterTests.cs index d9a60ad772..9f7dcd26a4 100644 --- a/tests/Formatters/AbstractFormatterTests.cs +++ b/tests/Formatters/AbstractFormatterTests.cs @@ -138,7 +138,7 @@ protected AbstractFormatterTest() codeStyleSeverity, fixAnalyzers, analyzerSeverity, - saveFormattedFiles: false, + saveFormattedFiles: true, changesAreErrors: false, fileMatcher, reportPath: string.Empty,