Skip to content

Commit 8ee2dbb

Browse files
[release/10.0] Fix Validation source generator deployment for non-Web SDKs (#63715)
* Proof-of-concept fix for getting validation SG to work in Blazor Wasm SDK projects * Use special type symbol for missing type comparisons --------- Co-authored-by: Ondřej Roztočil <[email protected]>
1 parent 4099eb3 commit 8ee2dbb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Shared/RoslynUtils/WellKnownTypes.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static WellKnownTypes GetOrCreate(Compilation compilation) =>
1919

2020
private readonly INamedTypeSymbol?[] _lazyWellKnownTypes;
2121
private readonly Compilation _compilation;
22+
private readonly INamedTypeSymbol _missingTypeSymbol;
2223

2324
static WellKnownTypes()
2425
{
@@ -51,6 +52,7 @@ private WellKnownTypes(Compilation compilation)
5152
{
5253
_lazyWellKnownTypes = new INamedTypeSymbol?[WellKnownTypeData.WellKnownTypeNames.Length];
5354
_compilation = compilation;
55+
_missingTypeSymbol = compilation.GetTypeByMetadataName(typeof(MissingType).FullName!)!;
5456
}
5557

5658
public INamedTypeSymbol Get(SpecialType type)
@@ -74,11 +76,7 @@ public INamedTypeSymbol Get(WellKnownTypeData.WellKnownType type)
7476

7577
private INamedTypeSymbol GetAndCache(int index)
7678
{
77-
var result = GetTypeByMetadataNameInTargetAssembly(WellKnownTypeData.WellKnownTypeNames[index]);
78-
if (result == null)
79-
{
80-
throw new InvalidOperationException($"Failed to resolve well-known type '{WellKnownTypeData.WellKnownTypeNames[index]}'.");
81-
}
79+
var result = GetTypeByMetadataNameInTargetAssembly(WellKnownTypeData.WellKnownTypeNames[index]) ?? _missingTypeSymbol;
8280
Interlocked.CompareExchange(ref _lazyWellKnownTypes[index], result, null);
8381

8482
// GetTypeByMetadataName should always return the same instance for a name.
@@ -159,4 +157,6 @@ public static bool Implements(ITypeSymbol? type, ITypeSymbol interfaceType)
159157
}
160158
return false;
161159
}
160+
161+
internal class MissingType { }
162162
}

src/Validation/src/Microsoft.Extensions.Validation.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
<EnableDefaultItems>true</EnableDefaultItems>
1212
</PropertyGroup>
1313

14+
<ItemGroup>
15+
<ProjectReference Include="$(RepoRoot)src\Validation\gen\Microsoft.Extensions.Validation.ValidationsGenerator.csproj" Pack="false">
16+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
17+
<OutputItemType>Content</OutputItemType>
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</ProjectReference>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<!-- Package the generator in the analyzer directory of the nuget package -->
24+
<None Include="$(OutputPath)\$(AssemblyName).ValidationsGenerator.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
25+
</ItemGroup>
26+
1427
<ItemGroup>
1528
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
1629
<Reference Include="Microsoft.Extensions.Options" />

0 commit comments

Comments
 (0)