Skip to content

Commit 552d5d6

Browse files
committed
Fixed Source Generator Visual Studio 2022 incompatibility (#8112)
1 parent 46f5a78 commit 552d5d6

File tree

8 files changed

+49
-12
lines changed

8 files changed

+49
-12
lines changed

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/ConnectionTypeFileBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override void WriteInitializeMethod(IOutputTypeInfo type)
6969
}
7070
}
7171
else if (!string.IsNullOrEmpty(connectionType.NameFormat)
72-
&& !connectionType.NameFormat.Contains("{0}"))
72+
&& !connectionType.NameFormat!.Contains("{0}"))
7373
{
7474
Writer.WriteLine();
7575
Writer.WriteIndentedLine(

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/EdgeTypeFileBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override void WriteInitializeMethod(IOutputTypeInfo type)
6868
}
6969
}
7070
else if (!string.IsNullOrEmpty(edgeType.NameFormat)
71-
&& !edgeType.NameFormat.Contains("{0}"))
71+
&& !edgeType.NameFormat!.Contains("{0}"))
7272
{
7373
Writer.WriteLine();
7474
Writer.WriteIndentedLine(

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/TypeFileBuilderBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Runtime.InteropServices.Marshalling;
21
using System.Text;
32
using HotChocolate.Types.Analyzers.Generators;
43
using HotChocolate.Types.Analyzers.Helpers;

src/HotChocolate/Core/src/Types.Analyzers/Generators/TypesSyntaxGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private static void WriteTypes(
8686

8787
static string CreateFileName(IOutputTypeInfo type)
8888
{
89+
#if NET8_0_OR_GREATER
8990
Span<byte> hash = stackalloc byte[64];
9091
var bytes = Encoding.UTF8.GetBytes(type.Namespace);
9192
MD5.HashData(bytes, hash);
@@ -110,6 +111,14 @@ static string CreateFileName(IOutputTypeInfo type)
110111
}
111112

112113
return $"{type.Name}.{Encoding.UTF8.GetString(hash)}.hc.g.cs";
114+
#else
115+
var bytes = Encoding.UTF8.GetBytes(type.Namespace);
116+
var md5 = MD5.Create();
117+
var hash = md5.ComputeHash(bytes);
118+
var hashString = Convert.ToBase64String(hash, Base64FormattingOptions.None);
119+
hashString = hashString.Replace("+", "-").Replace("/", "_").TrimEnd('=');
120+
return $"{type.Name}.{hashString}.hc.g.cs";
121+
#endif
113122
}
114123
}
115124

src/HotChocolate/Core/src/Types.Analyzers/HotChocolate.Types.Analyzers.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0</TargetFrameworks>
5-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0; net8.0</TargetFrameworks>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
<IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency -->
77
<IncludeSymbols>false</IncludeSymbols>
88
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
@@ -21,6 +21,10 @@
2121
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" VersionOverride="3.11.0" PrivateAssets="all" />
2222
</ItemGroup>
2323

24+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
25+
<PackageReference Include="Microsoft.Bcl.HashCode" VersionOverride="1.1.0" PrivateAssets="all" />
26+
</ItemGroup>
27+
2428
<ItemGroup>
2529
<!-- Package the generator in the analyzer directory of the nuget package -->
2630
<None Include="$(OutputPath)\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

src/HotChocolate/Core/src/Types.Analyzers/Inspectors/ConnectionTypeTransformer.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Immutable;
22
using System.Diagnostics.CodeAnalysis;
3-
using HotChocolate.Types.Analyzers.Models;
43
using HotChocolate.Types.Analyzers.Helpers;
4+
using HotChocolate.Types.Analyzers.Models;
55
using Microsoft.CodeAnalysis;
66

77
namespace HotChocolate.Types.Analyzers.Inspectors;
@@ -49,12 +49,23 @@ public ImmutableArray<SyntaxInfo> Transform(
4949
{
5050
if (syntaxInfo is IOutputTypeInfo { HasRuntimeType: true } typeInfo)
5151
{
52+
#if NET8_0_OR_GREATER
5253
connectionTypeLookup[typeInfo.RuntimeTypeFullName] = typeInfo;
54+
#else
55+
connectionTypeLookup[typeInfo.RuntimeTypeFullName!] = typeInfo;
56+
#endif
5357
}
5458
}
5559

60+
#if NET8_0_OR_GREATER
5661
foreach (var (connectionResolver, owner) in connectionResolvers)
5762
{
63+
#else
64+
foreach (var item in connectionResolvers.ToImmutableArray())
65+
{
66+
var connectionResolver = item.Key;
67+
var owner = item.Value;
68+
#endif
5869
var connectionType = GetConnectionType(compilation, connectionResolver.Member.GetReturnType());
5970
ConnectionTypeInfo connectionTypeInfo;
6071
ConnectionClassInfo? connectionClass;
@@ -330,11 +341,17 @@ private static INamedTypeSymbol GetConnectionType(Compilation compilation, IType
330341
return new GenericTypeInfo(typeDefinitionName, genericType, name, nameFormat);
331342
}
332343

333-
private record GenericTypeInfo(
334-
string TypeDefinitionName,
335-
INamedTypeSymbol Type,
336-
string Name,
337-
string? NameFormat);
344+
private sealed class GenericTypeInfo(
345+
string typeDefinitionName,
346+
INamedTypeSymbol type,
347+
string name,
348+
string? nameFormat)
349+
{
350+
public string TypeDefinitionName { get; } = typeDefinitionName;
351+
public INamedTypeSymbol Type { get; } = type;
352+
public string Name { get; } = name;
353+
public string? NameFormat { get; } = nameFormat;
354+
}
338355

339356
private static INamedTypeSymbol? GetEdgeType(INamedTypeSymbol connectionType)
340357
{

src/HotChocolate/Core/src/Types.Analyzers/KnownSymbols.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public static bool TryGetConnectionNameFromResolver(
3434
{
3535
if (namedValue.EndsWith(connection))
3636
{
37+
#if NET8_0_OR_GREATER
3738
namedValue = namedValue[..^connection.Length];
39+
#else
40+
namedValue = namedValue.Substring(0, namedValue.Length - connection.Length);
41+
#endif
3842
}
3943

4044
name = namedValue;

src/HotChocolate/Core/src/Types.Analyzers/Models/IOutputTypeInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public interface IOutputTypeInfo
3535
/// <summary>
3636
/// Specifies if this type info has a schema type.
3737
/// </summary>
38-
[MemberNotNull(nameof(SchemaTypeFullName), nameof(SchemaSchemaType))]
38+
#if NET8_0_OR_GREATER
39+
[MemberNotNull(nameof(RuntimeTypeFullName), nameof(RuntimeType))]
40+
#endif
3941
bool HasSchemaType { get; }
4042

4143
/// <summary>
@@ -51,7 +53,9 @@ public interface IOutputTypeInfo
5153
/// <summary>
5254
/// Specifies if this type info has a runtime type.
5355
/// </summary>
56+
#if NET8_0_OR_GREATER
5457
[MemberNotNull(nameof(RuntimeTypeFullName), nameof(RuntimeType))]
58+
#endif
5559
bool HasRuntimeType { get; }
5660

5761
/// <summary>

0 commit comments

Comments
 (0)