Skip to content

Commit fba76e1

Browse files
authored
Merge pull request #204 from EFNext/fix-net10-sdk-requirement
Fix net8.0 compatibility
2 parents 6eae48b + 167db05 commit fba76e1

13 files changed

Lines changed: 102 additions & 13 deletions

Directory.Build.props

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
<Project>
2+
<PropertyGroup>
3+
<!--
4+
RoslynAnalyzerVersion controls which Roslyn version the source generator is compiled against.
5+
Auto-detected from the .NET SDK major version encoded in $(MSBuildSDKsPath):
6+
- SDK 10+ → 5.0.0 (full C# 14 extension-member support; ROSLYN_5_0_OR_LATER is defined)
7+
- SDK 9 → 4.14.0 (no C# 14; the generator DLL loads on SDK 9 and SDK 10)
8+
Override at the CLI: dotnet build -p:RoslynAnalyzerVersion=4.14.0
9+
-->
10+
<!-- Extract the SDK major version from the path (e.g. "…\sdk\10.0.203\Sdks" → "10") -->
11+
<_DotNetSdkMajorVersion>$([System.Text.RegularExpressions.Regex]::Match('$(MSBuildSDKsPath)', '[\\/]sdk[\\/](\d+)\.').Groups[1].Value)</_DotNetSdkMajorVersion>
12+
<!-- SDK 10+ ships Roslyn 5.0 — auto-enable full C# 14 support -->
13+
<RoslynAnalyzerVersion Condition="'$(RoslynAnalyzerVersion)' == '' AND '$(_DotNetSdkMajorVersion)' != '' AND $([MSBuild]::VersionGreaterThanOrEquals('$(_DotNetSdkMajorVersion).0.0', '10.0.0'))">5.0.0</RoslynAnalyzerVersion>
14+
<!-- SDK 9 or unknown: Roslyn 4.14.0 is safe on both SDK 9 and SDK 10 hosts -->
15+
<RoslynAnalyzerVersion Condition="'$(RoslynAnalyzerVersion)' == ''">4.14.0</RoslynAnalyzerVersion>
16+
</PropertyGroup>
17+
218
<PropertyGroup>
319
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
420
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

Directory.Packages.props

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<Project>
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
<!-- RoslynAnalyzerVersion is auto-detected in Directory.Build.props (SDK 10+ → 5.0.0, SDK 9 → 4.14.0).
5+
The fallback here covers any edge case where Directory.Build.props runs after this file. -->
6+
<RoslynAnalyzerVersion Condition="'$(RoslynAnalyzerVersion)' == ''">4.14.0</RoslynAnalyzerVersion>
47
</PropertyGroup>
58
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
69
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
@@ -26,8 +29,8 @@
2629
<PackageVersion Include="EFCore.BulkExtensions" Version="8.0.4" />
2730
<PackageVersion Include="coverlet.collector" Version="8.0.1" />
2831
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
29-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
30-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.0.0" />
32+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="$(RoslynAnalyzerVersion)" />
33+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(RoslynAnalyzerVersion)" />
3134
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0" />
3235
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
3336
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />

src/EntityFrameworkCore.Projectables.CodeFixes/BlockBodyExperimentalCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace EntityFrameworkCore.Projectables.CodeFixes;
1616
[Shared]
1717
public sealed class BlockBodyExperimentalCodeFixProvider : CodeFixProvider
1818
{
19-
public override ImmutableArray<string> FixableDiagnosticIds => ["EFP0001"];
19+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create("EFP0001");
2020

2121
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
2222

src/EntityFrameworkCore.Projectables.CodeFixes/FactoryMethodToCtorCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace EntityFrameworkCore.Projectables.CodeFixes;
2020
public sealed class FactoryMethodToCtorCodeFixProvider : CodeFixProvider
2121
{
2222
/// <inheritdoc />
23-
public override ImmutableArray<string> FixableDiagnosticIds => ["EFP0012"];
23+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create("EFP0012");
2424

2525
/// <inheritdoc />
2626
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

src/EntityFrameworkCore.Projectables.CodeFixes/MissingParameterlessConstructorCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace EntityFrameworkCore.Projectables.CodeFixes;
1919
[Shared]
2020
public sealed class MissingParameterlessConstructorCodeFixProvider : CodeFixProvider
2121
{
22-
public override ImmutableArray<string> FixableDiagnosticIds => ["EFP0008"];
22+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create("EFP0008");
2323

2424
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
2525

src/EntityFrameworkCore.Projectables.CodeFixes/NullConditionalRewriteUnsupportedCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace EntityFrameworkCore.Projectables.CodeFixes;
2020
[Shared]
2121
public sealed class NullConditionalRewriteUnsupportedCodeFixProvider : CodeFixProvider
2222
{
23-
public override ImmutableArray<string> FixableDiagnosticIds => ["EFP0002"];
23+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create("EFP0002");
2424

2525
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
2626

src/EntityFrameworkCore.Projectables.Generator/EntityFrameworkCore.Projectables.Generator.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
99
</PropertyGroup>
1010

11+
<!-- Define ROSLYN_5_0_OR_LATER when building with .NET SDK 10+ (NETCoreSdkVersion is set by
12+
Microsoft.NET.Sdk.props, which runs before this project file body is evaluated).
13+
This enables C# 14 extension-type support in the generator code.
14+
On SDK 9 the constant is absent and extension block members are skipped safely. -->
15+
<PropertyGroup Condition="'$(NETCoreSdkVersion)' != '' AND $([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '10.0.0'))">
16+
<DefineConstants>$(DefineConstants);ROSLYN_5_0_OR_LATER</DefineConstants>
17+
</PropertyGroup>
18+
1119
<ItemGroup>
1220
<Compile Include="..\EntityFrameworkCore.Projectables.Abstractions\NullConditionalRewriteSupport.cs" Link="Infrastructure\NullConditionalRewriteSupport.cs" />
1321
<Compile Include="..\EntityFrameworkCore.Projectables\Services\ProjectionExpressionClassNameGenerator.cs" Link="Infrastructure\ProjectionExpressionClassNameGenerator.cs" />

src/EntityFrameworkCore.Projectables.Generator/Interpretation/ProjectableInterpreter.BodyProcessors.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ private static bool TryApplyExpressionPropertyBody(
120120
// implicit @this receiver parameter. If the expression property lambda uses a
121121
// different parameter name (e.g. c => c.Value > 0), rename it so the generated
122122
// code references @this instead of an undefined identifier.
123+
#if ROSLYN_5_0_OR_LATER
123124
var isExtensionMember = memberSymbol.ContainingType is { IsExtension: true };
125+
#else
126+
var isExtensionMember = false;
127+
#endif
124128
var hasImplicitReceiver = isExtensionMember
125129
|| !originalMethodDecl.Modifiers.Any(SyntaxKind.StaticKeyword);
126130

src/EntityFrameworkCore.Projectables.Generator/Interpretation/ProjectableInterpreter.Helpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private static void ApplyExtensionBlockTypeParameters(
6666
ISymbol memberSymbol,
6767
ProjectableDescriptor descriptor)
6868
{
69+
#if ROSLYN_5_0_OR_LATER
6970
if (memberSymbol.ContainingType is not { IsExtension: true } extensionType
7071
|| extensionType.TypeParameters.IsDefaultOrEmpty)
7172
{
@@ -95,6 +96,7 @@ private static void ApplyExtensionBlockTypeParameters(
9596
descriptor.ConstraintClauses ??= SyntaxFactory.List<TypeParameterConstraintClauseSyntax>();
9697
descriptor.ConstraintClauses = descriptor.ConstraintClauses.Value.Add(BuildConstraintClause(tp));
9798
}
99+
#endif
98100
}
99101

100102
/// <summary>

src/EntityFrameworkCore.Projectables.Generator/Interpretation/ProjectableInterpreter.MemberBodyResolver.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ x is IPropertySymbol xProperty &&
7878
{
7979
if (memberSymbol is IMethodSymbol exprCheckMethod)
8080
{
81+
#if ROSLYN_5_0_OR_LATER
8182
var isExtensionBlock = memberSymbol.ContainingType is { IsExtension: true };
83+
#else
84+
var isExtensionBlock = false;
85+
#endif
8286
var hasImplicitThis = !exprCheckMethod.IsStatic || isExtensionBlock;
8387
var expectedFuncArgCount = exprCheckMethod.Parameters.Length + (hasImplicitThis ? 2 : 1);
8488

@@ -88,9 +92,14 @@ x is IPropertySymbol xProperty &&
8892
ITypeSymbol? expectedReceiverType = null;
8993
if (hasImplicitThis)
9094
{
91-
expectedReceiverType = isExtensionBlock
92-
? exprCheckMethod.ContainingType.ExtensionParameter?.Type
93-
: exprCheckMethod.ContainingType;
95+
expectedReceiverType =
96+
#if ROSLYN_5_0_OR_LATER
97+
isExtensionBlock
98+
? exprCheckMethod.ContainingType.ExtensionParameter?.Type
99+
: exprCheckMethod.ContainingType;
100+
#else
101+
exprCheckMethod.ContainingType;
102+
#endif
94103
}
95104

96105
compatibleExprPropertyCandidates = exprPropertyCandidates.Where(x =>

0 commit comments

Comments
 (0)