Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an analyzer for Debug.Assert #7416

Merged
merged 10 commits into from
Sep 26, 2024
Merged

Conversation

333fred
Copy link
Member

@333fred 333fred commented Sep 19, 2024

As @jaredpar found in dotnet/roslyn#75163, interpolated strings in Debug.Assert can consume a surprising amount of memory. On modern .NET, this is fine; Debug.Assert has an interpolated string handler that will avoid the allocations if the assert isn't triggered. However, on our framework tests, this can be very bad and OOM our tests. So, this analyzer looks for cases where interpolated strings are passed to Debug.Assert, and recommends moving over to RoslynDebug.Assert instead, which is an interpolated string handler on all platforms. Note that I only did C# support, as there's no equivalent handler API for VB.

As @jaredpar found in dotnet/roslyn#75163, interpolated strings in `Debug.Assert` can consume a surprising amount of memory. On modern .NET, this is fine; `Debug.Assert` has an interpolated string handler that will avoid the allocations if the assert isn't triggered. However, on our framework tests, this can be very bad and OOM our tests. So, this analyzer looks for cases where interpolated strings are passed to `Debug.Assert`, and recommends moving over to `RoslynDebug.Assert` instead, which is an interpolated string handler on all platforms. Note that I only did C# support, as there's no equivalent handler API for VB.
@333fred 333fred requested a review from a team as a code owner September 19, 2024 17:49
@333fred
Copy link
Member Author

333fred commented Sep 19, 2024

@sharwell, any what's going on with this failure? Running dotnet msbuild /t:Pack locally gives no errors, and changes no files.

➜ dotnet msbuild /t:Pack
  Roslyn.Diagnostics.Analyzers                                                                AssignLinkMetadata (0.0s)
  Microsoft.CodeAnalysis.AnalyzerUtilities                                                   UpdateXlfSourceList (0.0s)
  Roslyn.Diagnostics.Analyzers                                                              ResolvePackageAssets (0.1s)
  Microsoft.CodeAnalysis.AnalyzerUtilities                                                    AssignLinkMetadata (0.0s)
  Roslyn.Diagnostics.Analyzers                                                          ResolveLockFileAnalyzers (0.1s)
  Microsoft.CodeAnalysis.AnalyzerUtilities                                                    AssignLinkMetadata (0.0s)
  Roslyn.Diagnostics.VisualBasic.Analyzers                    _ComputeToolPackInputsToProcessFrameworkReferences (0.0s)
  Microsoft.CodeAnalysis.AnalyzerUtilities                                                    AssignLinkMetadata (0.1s)
  Roslyn.Diagnostics.VisualBasic.Analyzers                    _ComputeToolPackInputsToProcessFrameworkReferences (0.1s)
  RulesetToEditorconfigConverter net472 succeeded (2.9s) → artifacts/bin/RulesetToEditorconfigConverter/Debug/net472/RulesetToEditorconfigConverter.exe
  Microsoft.CodeAnalysis.AnalyzerUtilities succeeded (6.5s) → artifacts/bin/Microsoft.CodeAnalysis.AnalyzerUtilities/Debug/netstandard2.0/Microsoft.CodeAnalysis.AnalyzerUtilities.dll
  Microsoft.CodeAnalysis.ResxSourceGenerator succeeded (6.7s) → artifacts/bin/Microsoft.CodeAnalysis.ResxSourceGenerator/Debug/netstandard2.0/Microsoft.CodeAnalysis.ResxSourceGenerator.dll
  Microsoft.CodeAnalysis.PublicApiAnalyzers succeeded (6.8s) → artifacts/bin/Microsoft.CodeAnalysis.PublicApiAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.PublicApiAnalyzers.dll
  Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers succeeded (6.8s) → artifacts/bin/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers.dll
  Microsoft.CodeAnalysis.BannedApiAnalyzers succeeded (6.8s) → artifacts/bin/Microsoft.CodeAnalysis.BannedApiAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.BannedApiAnalyzers.dll
  Text.Analyzers succeeded (6.8s) → artifacts/bin/Text.Analyzers/Debug/netstandard2.0/Text.Analyzers.dll
  Roslyn.Diagnostics.Analyzers succeeded (6.9s) → artifacts/bin/Roslyn.Diagnostics.Analyzers/Debug/netstandard2.0/Roslyn.Diagnostics.Analyzers.dll
  Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp succeeded (1.9s) → artifacts/bin/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Debug/netstandard2.0/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.dll
  Microsoft.CodeAnalysis.ResxSourceGenerator.VisualBasic succeeded (3.0s) → artifacts/bin/Microsoft.CodeAnalysis.ResxSourceGenerator.VisualBasic/Debug/netstandard2.0/Microsoft.CodeAnalysis.ResxSourceGenerator.VisualBasic.dll
  Text.CSharp.Analyzers succeeded (2.8s) → artifacts/bin/Text.CSharp.Analyzers/Debug/netstandard2.0/Text.CSharp.Analyzers.dll
  Microsoft.CodeAnalysis.PublicApiAnalyzers.CodeFixes succeeded (3.1s) → artifacts/bin/Microsoft.CodeAnalysis.PublicApiAnalyzers.CodeFixes/Debug/netstandard2.0/Microsoft.CodeAnalysis.PublicApiAnalyzers.CodeFixes.dll
  Microsoft.CodeAnalysis.Analyzers succeeded (10.0s) → artifacts/bin/Microsoft.CodeAnalysis.Analyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.Analyzers.dll
  Microsoft.CodeAnalysis.CSharp.Analyzers succeeded (1.3s) → artifacts/bin/Microsoft.CodeAnalysis.CSharp.Analyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.CSharp.Analyzers.dll
  Metrics.Legacy net472 succeeded (4.6s) → artifacts/bin/Metrics.Legacy/Debug/net472/Metrics.Legacy.exe
  Metrics net472 succeeded (4.7s) → artifacts/bin/Metrics/Debug/net472/Metrics.exe
  Microsoft.CodeAnalysis.CSharp.PerformanceSensitiveAnalyzers succeeded (4.6s) → artifacts/bin/Microsoft.CodeAnalysis.CSharp.PerformanceSensitiveAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.CSharp.PerformanceSensitiveAnalyzers.dll
  Microsoft.CodeAnalysis.CSharp.BannedApiAnalyzers succeeded (2.6s) → artifacts/bin/Microsoft.CodeAnalysis.CSharp.BannedApiAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.CSharp.BannedApiAnalyzers.dll
  Microsoft.CodeAnalysis.VisualBasic.Analyzers succeeded (1.9s) → artifacts/bin/Microsoft.CodeAnalysis.VisualBasic.Analyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.VisualBasic.Analyzers.dll
  Microsoft.CodeAnalysis.CSharp.PerformanceSensitiveAnalyzers.CodeFixes succeeded (0.6s) → artifacts/bin/Microsoft.CodeAnalysis.CSharp.PerformanceSensitiveAnalyzers.CodeFixes/Debug/netstandard2.0/Microsoft.CodeAnalysis.CSharp.PerformanceSensitiveAnalyzers.CodeFixes.dll
  Microsoft.CodeAnalysis.VisualBasic.BannedApiAnalyzers succeeded (2.5s) → artifacts/bin/Microsoft.CodeAnalysis.VisualBasic.BannedApiAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.VisualBasic.BannedApiAnalyzers.dll
  Roslyn.Diagnostics.CSharp.Analyzers succeeded (3.0s) → artifacts/bin/Roslyn.Diagnostics.CSharp.Analyzers/Debug/netstandard2.0/Roslyn.Diagnostics.CSharp.Analyzers.dll
  Roslyn.Diagnostics.VisualBasic.Analyzers succeeded (0.4s) → artifacts/bin/Roslyn.Diagnostics.VisualBasic.Analyzers/Debug/netstandard2.0/Roslyn.Diagnostics.VisualBasic.Analyzers.dll
  Microsoft.CodeAnalysis.NetAnalyzers succeeded (13.8s) → artifacts/bin/Microsoft.CodeAnalysis.NetAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.NetAnalyzers.dll
  Text.VisualBasic.Analyzers succeeded (0.2s) → artifacts/bin/Text.VisualBasic.Analyzers/Debug/netstandard2.0/Text.VisualBasic.Analyzers.dll
  GenerateDocumentationAndConfigFiles succeeded (3.1s) → artifacts/bin/GenerateDocumentationAndConfigFiles/Debug/net9.0/GenerateDocumentationAndConfigFiles.dll
  Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers succeeded (0.8s) → artifacts/bin/Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers.dll
  Microsoft.CodeAnalysis.CSharp.NetAnalyzers succeeded (0.8s) → artifacts/bin/Microsoft.CodeAnalysis.CSharp.NetAnalyzers/Debug/netstandard2.0/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll

Build succeeded in 72.7s

@333fred
Copy link
Member Author

333fred commented Sep 20, 2024

@dotnet/roslyn-compiler @sharwell for review

Copy link

codecov bot commented Sep 20, 2024

Codecov Report

Attention: Patch coverage is 90.47619% with 16 lines in your changes missing coverage. Please review.

Project coverage is 96.50%. Comparing base (f4c8475) to head (4170647).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7416      +/-   ##
==========================================
- Coverage   96.50%   96.50%   -0.01%     
==========================================
  Files        1443     1446       +3     
  Lines      346376   346544     +168     
  Branches    11387    11396       +9     
==========================================
+ Hits       334263   334417     +154     
- Misses       9232     9240       +8     
- Partials     2881     2887       +6     

@333fred 333fred enabled auto-merge (squash) September 26, 2024 18:53
@333fred 333fred merged commit 1d3c176 into dotnet:main Sep 26, 2024
11 checks passed
@333fred 333fred deleted the interpolated-debug-assert branch September 26, 2024 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants