Skip to content

Commit 914fc27

Browse files
authored
Calculate the current C# compiler version
This change allows us to calculate the current C# compiler version in a way that will work on all platforms and only relies on the `Microsoft.CSharp.CurrentVersion.targets` target (specifically [this line](https://github.com/dotnet/msbuild/blob/main/src/Tasks/Microsoft.CSharp.CurrentVersion.targets#L315)) being imported. `Microsoft.CSharp.CurrentVersion.targets` is the core msbuild logic for finding the C# compiler and is used universally so this fix should work everywhere there is a C# compiler to be found.
1 parent 0e150d5 commit 914fc27

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@
99

1010
<!-- Remove the analyzer if using Roslyn 3.x (incremental generators require Roslyn 4.x) -->
1111
<Target Name="_MVVMToolkitRemoveAnalyzersForRoslyn3"
12-
Condition="'$(SupportsRoslynComponentVersioning)' != 'true'"
1312
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
1413
DependsOnTargets="_MVVMToolkitGatherAnalyzers">
15-
<ItemGroup>
14+
15+
<!-- Use the CSharpCoreTargetsPath property to find the version of the compiler we are using.
16+
This is the same mechanism MSBuild uses to find the compiler.
17+
We could check the assembly version for any compiler assembly (since they all have the same version)
18+
but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if someone is
19+
getting creative with msbuild tasks/targets this is the "most correct" assembly to check. -->
20+
<GetAssemblyIdentity
21+
AssemblyFiles="$([System.IO.Path]::Combine(`$([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath)))`,`Microsoft.Build.Tasks.CodeAnalysis.dll`))">
22+
<Output TaskParameter="Assemblies" ItemName="CurrentCompilerAssemblyIdentity"/>
23+
</GetAssemblyIdentity>
24+
25+
<PropertyGroup>
26+
<!-- Transform the resulting item from GetAssemblyIdentity into a property and check if the assembly version is less than 4.0 -->
27+
<CurrentCompilerVersion>@(CurrentCompilerAssemblyIdentity->'%(Version)')</CurrentCompilerVersion>
28+
<CurrentCompilerVersionIsNotNewEnough Condition="$([MSBuild]::VersionLessThan($(CurrentCompilerVersion), 4.0))">true</CurrentCompilerVersionIsNotNewEnough>
29+
<CurrentCompilerVersionIsNotNewEnough Condition="$(CurrentCompilerVersionIsNotNewEnough) == ''">false</CurrentCompilerVersionIsNotNewEnough>
30+
</PropertyGroup>
31+
32+
<ItemGroup Condition ="$(CurrentCompilerVersionIsNotNewEnough) == 'true'">
1633
<Analyzer Remove="@(_MVVMToolkitAnalyzer)"/>
1734
</ItemGroup>
18-
<Warning Text="The MVVM Toolkit source generators have been disabled on the current configuration, as they need Roslyn 4.x in order to work. The MVVM Toolkit will work just fine, but features relying on the source generators will not be available."/>
35+
<Warning Condition ="$(CurrentCompilerVersionIsNotNewEnough) == 'true'" Text="The MVVM Toolkit source generators have been disabled on the current configuration, as they need Roslyn 4.x in order to work. The MVVM Toolkit will work just fine, but features relying on the source generators will not be available."/>
1936
</Target>
2037

21-
</Project>
38+
</Project>

0 commit comments

Comments
 (0)