Fix `ReflectionCacheGenerator` choking on dummy projects

This commit is contained in:
YoshiRulz 2025-05-21 04:35:55 +10:00
parent fda159a8ca
commit e5d641fc09
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 3 additions and 1 deletions

View File

@ -6,7 +6,7 @@
<AnalysisModeReliability>Recommended</AnalysisModeReliability>
<AnalysisModePerformance>Recommended</AnalysisModePerformance>
<AnalysisModeUsage>Recommended</AnalysisModeUsage>
<EmitCompilerGeneratedFiles>false</EmitCompilerGeneratedFiles> <!-- enable to copy Roslyn Source Gemerators' outputs to $(ProjectDir)/obj/.../generated -->
<EmitCompilerGeneratedFiles>false</EmitCompilerGeneratedFiles> <!-- enable to copy Roslyn Source Generators' outputs to $(ProjectDir)/obj/.../generated -->
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
<ContinuousIntegrationBuild Condition=" '$(GITLAB_CI)' != '' Or '$(APPVEYOR)' != '' ">true</ContinuousIntegrationBuild>

View File

@ -27,6 +27,7 @@ public sealed class ReflectionCacheGenerator : IIncrementalGenerator
private string CalcNamespace()
{
if (_namespaces.Count is 0) return string.Empty; // this can happen if the Analyzer is applied to a project with no source files
// black magic wizardry to find common prefix https://stackoverflow.com/a/35081977
var ns = new string(_namespaces[0]
.Substring(0, _namespaces.Min(s => s.Length))
@ -63,6 +64,7 @@ public sealed class ReflectionCacheGenerator : IIncrementalGenerator
public void Execute(SourceProductionContext context, string nSpace)
{
if (string.IsNullOrWhiteSpace(nSpace)) return; // see note in `CalcNamespace`
var src = $@"#nullable enable
using System;