Skip to content

Commit fbaf62d

Browse files
authored
Merge pull request #698 from CommunityToolkit/dev/packages-config-warning
Add warning when using packages.config for MVVM Toolkit
2 parents 846f2ae + 0071d6a commit fbaf62d

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.targets

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been
5555
disabled, and that the rest of the MVVM Toolkit will still keep working as intended, just without additional features.
5656
-->
57-
<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."/>
57+
<Warning Condition ="'$(MVVMToolkitCurrentCompilerVersionIsNotNewEnough)' == 'true'"
58+
Code="MVVMTKCFG0001"
59+
HelpLink="https://aka.ms/mvvmtoolkit/errors/mvvmtkcfg0001"
60+
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."/>
5861

5962
<PropertyGroup>
6063

@@ -99,4 +102,48 @@
99102
</ItemGroup>
100103
</Target>
101104

105+
<!--
106+
Inform the user if packages.config is used (as the analyzers and the source generators
107+
won't work at all). Since packages.config can only be used with legacy-style projects,
108+
the entire package can be skipped if an SDK-style project is used.
109+
-->
110+
<Target Name="MVVMToolkitWarnForPackagesConfigUse"
111+
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
112+
Condition="'$(UsingMicrosoftNetSDK)' != 'true'">
113+
114+
<!--
115+
Check whether packages are being restored via packages.config, by reading the associated MSBuild property.
116+
This happens when either the project style is using packages.config, or when explicitly requested.
117+
See https://learn.microsoft.com/nuget/reference/msbuild-targets#restoring-packagereference-and-packagesconfig-projects-with-msbuild.
118+
-->
119+
<PropertyGroup>
120+
<MVVMToolkitIsTargetProjectUsingPackagesConfig Condition ="'$(RestorePackagesConfig)' == 'true' OR '$(RestoreProjectStyle)' == 'PackagesConfig'">true</MVVMToolkitIsTargetProjectUsingPackagesConfig>
121+
</PropertyGroup>
122+
123+
<!--
124+
If no packages.config properties are set, also try to manually find the packages.config file.
125+
This will be in the @(None) elements, if present. Doing so makes sure this works in builds as
126+
well, since the implicit targets populating the properties above only run when restoring.
127+
Since the packages.config file will always be in the root of the project, if present, we will
128+
match with the full item spec (see https://learn.microsoft.com/nuget/reference/packages-config).
129+
-->
130+
<FindInList ItemSpecToFind="packages.config"
131+
List="@(None)"
132+
MatchFileNameOnly="false"
133+
Condition="'$(MVVMToolkitIsTargetProjectUsingPackagesConfig)' != 'true'">
134+
<Output TaskParameter="ItemFound" PropertyName="MVVMToolkitPackagesConfigFile"/>
135+
</FindInList>
136+
137+
<!-- Make sure to update the MSBuild property if the above task did find something -->
138+
<PropertyGroup>
139+
<MVVMToolkitIsTargetProjectUsingPackagesConfig Condition ="'$(MVVMToolkitPackagesConfigFile)' == 'packages.config'">true</MVVMToolkitIsTargetProjectUsingPackagesConfig>
140+
</PropertyGroup>
141+
142+
<!-- Emit a warning in case packages.config is used -->
143+
<Warning Condition ="'$(MVVMToolkitIsTargetProjectUsingPackagesConfig)' == 'true'"
144+
Code="MVVMTKCFG0002"
145+
HelpLink="https://aka.ms/mvvmtoolkit/errors/mvvmtkcfg0002"
146+
Text="The MVVM Toolkit source generators might not be loaded correctly, as the current project is using the packages.config setup to restore NuGet packages. Source generators require PackageReference to be used (either in a legacy-style or SDK-style .csproj project, both are supported as long as PackageReference is used)."/>
147+
</Target>
148+
102149
</Project>

0 commit comments

Comments
 (0)