Skip to content

Commit e6e0940

Browse files
authored
Merge pull request #592 from CommunityToolkit/dev/fix-vb-net-projects
Fix build error from VB.NET projects
2 parents 9127b61 + 6a7cc7a commit e6e0940

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@
77
</ItemGroup>
88
</Target>
99

10+
<!-- Remove the analyzer if Roslyn is missing -->
11+
<Target Name="MVVMToolkitRemoveAnalyzersForRosynNotFound"
12+
Condition="'$(CSharpCoreTargetsPath)' == ''"
13+
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
14+
DependsOnTargets="MVVMToolkitGatherAnalyzers">
15+
16+
<!-- If no Roslyn assembly could be found, just remove the analyzer without emitting a warning -->
17+
<ItemGroup>
18+
<Analyzer Remove="@(MVVMToolkitAnalyzer)"/>
19+
</ItemGroup>
20+
</Target>
21+
1022
<!-- Remove the analyzer if using Roslyn 3.x (incremental generators require Roslyn 4.x) -->
1123
<Target Name="MVVMToolkitRemoveAnalyzersForRoslyn3"
1224
Condition="'$(CSharpCoreTargetsPath)' != ''"
1325
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
1426
DependsOnTargets="MVVMToolkitGatherAnalyzers">
1527

16-
<!-- Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism
17-
MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have
18-
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if
19-
someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check. -->
28+
<!--
29+
Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism
30+
MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have
31+
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if
32+
someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check.
33+
-->
2034
<GetAssemblyIdentity AssemblyFiles="$([System.IO.Path]::Combine(`$([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath)))`,`Microsoft.Build.Tasks.CodeAnalysis.dll`))">
2135
<Output TaskParameter="Assemblies" ItemName="MVVMToolkitCurrentCompilerAssemblyIdentity"/>
2236
</GetAssemblyIdentity>
@@ -35,25 +49,39 @@
3549
<Analyzer Remove="@(MVVMToolkitAnalyzer)"/>
3650
</ItemGroup>
3751

38-
<!-- If the source generators are disabled, also emit a warning. This would've been produced by MSBuild itself as well, but
39-
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been
40-
disabled, and that the rest of the MVVM Toolkit will still keep working as intended, just without additional features. -->
52+
<!--
53+
If the source generators are disabled, also emit a warning. This would've been produced by MSBuild itself as well, but
54+
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been
55+
disabled, and that the rest of the MVVM Toolkit will still keep working as intended, just without additional features.
56+
-->
4157
<Warning Condition ="'$(MVVMToolkitCurrentCompilerVersionIsNotNewEnough)' == '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."/>
58+
59+
<PropertyGroup>
60+
61+
<!--
62+
Setup the checks for the MVVMToolkitRemoveDuplicateAnalyzersWhenRoslynComponentVersioningIsNotSupported target below.
63+
These are done here so that the following target can run only if it's sure this one has run, which is a necessary
64+
condition to ensure all dependent MSBuild properties defined here will also be available there whenever needed.
65+
-->
66+
<MVVMToolkitIsManualRoslynMultiTargetingLogicNeeded Condition="'$(MVVMToolkitCurrentCompilerVersionIsNotNewEnough)' != 'true' AND '$(SupportsRoslynComponentVersioning)' != 'true'">true</MVVMToolkitIsManualRoslynMultiTargetingLogicNeeded>
67+
</PropertyGroup>
4268
</Target>
4369

4470
<!--
4571
Manually remove duplicate analyzers if Roslyn component versioning is not supported (ie. if a legacy .csproj project is used).
4672
This target is only run if Roslyn 4.0 or greater is present, as otherwise all analyzers would have already been removed anyway.
73+
Additionally, skip this target if MVVMToolkitRemoveAnalyzersForRoslyn3 has been skipped (ie. if $(CSharpCoreTargetsPath) is not
74+
defined, which will be the case on VB.NET projects). In these cases, MVVMToolkitRemoveAnalyzersForRosynNotFound will run at the
75+
end and will remove all source generators (as they're only supported in C# projects), so there's nothing left to do here.
4776
-->
4877
<Target Name="MVVMToolkitRemoveDuplicateAnalyzersWhenRoslynComponentVersioningIsNotSupported"
49-
Condition="'$(MVVMToolkitCurrentCompilerVersionIsNotNewEnough)' != 'true' AND '$(SupportsRoslynComponentVersioning)' != 'true'"
50-
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
51-
DependsOnTargets="MVVMToolkitRemoveAnalyzersForRoslyn3">
78+
Condition="'$(MVVMToolkitIsManualRoslynMultiTargetingLogicNeeded)' == 'true'"
79+
AfterTargets="MVVMToolkitRemoveAnalyzersForRoslyn3">
5280

53-
<!--
54-
This switch manually implements Roslyn component versioning. That is, it checks the current version of Roslyn and
55-
removes and removes all analyzers except the highest version that is supported. The fallback is just Roslyn 4.0.
56-
-->
81+
<!--
82+
This switch manually implements Roslyn component versioning. That is, it checks the current version of Roslyn and
83+
removes and removes all analyzers except the highest version that is supported. The fallback is just Roslyn 4.0.
84+
-->
5785
<PropertyGroup>
5886
<MVVMToolkitSelectedRoslynAnalyzerDirectoryName Condition="$([MSBuild]::VersionGreaterThanOrEquals($(MVVMToolkitCurrentCompilerVersion), 4.3))">roslyn4.3</MVVMToolkitSelectedRoslynAnalyzerDirectoryName>
5987
<MVVMToolkitSelectedRoslynAnalyzerDirectoryName Condition="'$(MVVMToolkitSelectedRoslynAnalyzerDirectoryName)' == ''">roslyn4.0</MVVMToolkitSelectedRoslynAnalyzerDirectoryName>
@@ -71,16 +99,4 @@
7199
</ItemGroup>
72100
</Target>
73101

74-
<!-- Remove the analyzer if Roslyn is missing -->
75-
<Target Name="MVVMToolkitRemoveAnalyzersForRosynNotFound"
76-
Condition="'$(CSharpCoreTargetsPath)' == ''"
77-
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
78-
DependsOnTargets="MVVMToolkitGatherAnalyzers">
79-
80-
<!-- If no Roslyn assembly could be found, just remove the analyzer without emitting a warning -->
81-
<ItemGroup>
82-
<Analyzer Remove="@(MVVMToolkitAnalyzer)"/>
83-
</ItemGroup>
84-
</Target>
85-
86102
</Project>

0 commit comments

Comments
 (0)