forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolveReferences.targets
335 lines (292 loc) · 19.3 KB
/
ResolveReferences.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!--
The targets in this file are used to implement custom <Reference> resolution.
For more details, see /docs/ReferenceResolution.md.
Properties which can be set by projects. If unset, these will be inferred.
* UseLatestPackageReferences = resolve `<Reference>` items to the latest version of PackageReferences in eng/Dependencies.props.
* UseProjectReferences = prefer project references to packages
* IsProjectReferenceProvider = when true, the assembly in this project should be available as a ProjectReferenceProvider (see below).
Items used by the resolution strategy:
* BaselinePackageReference = a list of packages that were referenced in the last release of the project currently building
- mainly used to ensure references do not change in servicing builds unless $(UseLatestPackageReferences) is not true.
* LatestPackageReference = a list of the latest versions of packages
* Reference = a list of the references which are needed for compilation or runtime
* ProjectReferenceProvider = a list which maps of assembly names to the project file that produces it
-->
<Project>
<PropertyGroup>
<EnableCustomReferenceResolution
Condition="'$(EnableCustomReferenceResolution)' == '' AND ('$(DotNetBuildFromSource)' != 'true' OR '$(ExcludeFromSourceBuild)' != 'true')">true</EnableCustomReferenceResolution>
<ResolveReferencesDependsOn>
ResolveCustomReferences;
$(ResolveReferencesDependsOn);
</ResolveReferencesDependsOn>
</PropertyGroup>
<PropertyGroup>
<!--
Projects should use the latest package references when:
* preparing a new major or minor release i.e. a non-servicing builds
* when a project is a test or sample project
* when a package is releasing a new patch (we like to update external dependencies in patches when possible)
That is, use latest package references unless this is a servicing build, the project is normally packable, and
the package is not included in this release. The "unless" cases are extremely unlikely because both
$(IsPackableInNonServicingBuild) and $(IsPackageInThisPatch) are either undefined or true.
-->
<UseLatestPackageReferences
Condition=" '$(UseLatestPackageReferences)' == '' AND '$(IsServicingBuild)' != 'true' ">true</UseLatestPackageReferences>
<UseLatestPackageReferences
Condition=" '$(UseLatestPackageReferences)' == '' AND '$(IsPackableInNonServicingBuild)' != 'true' ">true</UseLatestPackageReferences>
<UseLatestPackageReferences
Condition=" '$(UseLatestPackageReferences)' == '' AND '$(IsPackageInThisPatch)' == 'true' ">true</UseLatestPackageReferences>
<UseLatestPackageReferences Condition=" '$(UseLatestPackageReferences)' == '' ">false</UseLatestPackageReferences>
<!-- Projects should use project references (instead of baseline packages) in almost all cases. -->
<UseProjectReferences Condition=" '$(UseProjectReferences)' == '' ">true</UseProjectReferences>
</PropertyGroup>
<ItemDefinitionGroup>
<Reference>
<IsSharedSource />
</Reference>
</ItemDefinitionGroup>
<ItemGroup Condition="'$(EnableCustomReferenceResolution)' == 'true'">
<Reference Update="@(Reference)">
<IsSharedSource
Condition="'%(IsSharedSource)' == '' AND $([System.String]::new('%(Identity)').EndsWith('.Sources'))">true</IsSharedSource>
</Reference>
<!-- Packages which are implicitly defined by the .NET Core SDK. -->
<_ImplicitPackageReference Include="@(PackageReference->WithMetadataValue('IsImplicitlyDefined', 'true'))" />
<!-- Capture a list of references which were set explicitly in the project. -->
<_AllowedExplicitPackageReference Include="@(PackageReference->WithMetadataValue('AllowExplicitReference', 'true'))" />
<_AllowedExplicitPackageReference Include="FSharp.Core" Condition="'$(MSBuildProjectExtension)' == '.fsproj'" />
<_ExplicitPackageReference Include="@(PackageReference)"
Exclude="@(_ImplicitPackageReference);@(_AllowedExplicitPackageReference)" />
<_CompilationOnlyReference Include="@(Reference->WithMetadataValue('NuGetPackageId','NETStandard.Library'))"
Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<_InvalidReferenceToNonSharedFxAssembly Condition="'$(IsAspNetCoreApp)' == 'true'"
Include="@(Reference)"
Exclude="
@(AspNetCoreAppReference);
@(AspNetCoreAppReferenceAndPackage);
@(ExternalAspNetCoreAppReference);
@(_CompilationOnlyReference);
@(Reference->WithMetadataValue('IsSharedSource', 'true'));
@(Reference->WithMetadataValue('PrivateAssets', 'All'))" />
<_OriginalReferences Include="@(Reference)" />
</ItemGroup>
<!--
Turn Reference items into a ProjectReference when UseProjectReferences is true. Order matters; this
comes before package resolution because projects should be used when possible instead of packages.
-->
<ItemGroup Condition=" '$(EnableCustomReferenceResolution)' == 'true' AND '$(UseProjectReferences)' == 'true' ">
<!--
For the _CheckForReferenceBoundaries target, mark project reference providers that _should_ be referenced with
Reference items but weren't. General principle is to use only Reference items when referencing a provider.
This simplifies project moves and shortens files.
-->
<ProjectReferenceProvider Update="@(ProjectReference->'%(Filename)')" DirectUse="1" />
<!-- Find Reference items satisfied using project reference providers. -->
<Reference Update="@(ProjectReferenceProvider)" ProjectPath="%(ProjectReferenceProvider.ProjectPath)" />
<ProjectReference Include="@(Reference->Distinct()->'%(ProjectPath)')" />
<Reference Remove="@(Reference->HasMetadata('ProjectPath'))" />
</ItemGroup>
<!--
This target helps ensure projects within the shared framework do not unintentionally add new references, and that
assemblies outside the shared framework reference the framework as a whole instead of using individual assemblies.
In addition, enforce use of Reference items for projects reference providers.
-->
<Target Name="_CheckForReferenceBoundaries" BeforeTargets="CollectPackageReferences;ResolveReferences">
<!-- Dependency graph checks may include unexpected packages. Ignore this because it's not an error. -->
<Error
Condition=" '$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)' AND
'$(MSBuildRestoreSessionId)' != '' AND
@(_InvalidReferenceToNonSharedFxAssembly->Count()) != 0 "
Text="Cannot reference "%(Identity)". This dependency is not in the shared framework. See docs/SharedFramework.md for instructions on how to modify what is in the shared framework." />
<Error
Condition=" '$(EnableCustomReferenceResolution)' == 'true' AND @(ProjectReferenceProvider->WithMetadataValue('DirectUse', '1')->Count()) != 0 "
Text="Cannot reference "%(Identity)" with a ProjectReference item; use a Reference item." />
</Target>
<Target Name="_WarnAboutRedundantRef" AfterTargets="ResolveFrameworkReferences;ProcessFrameworkReferences">
<Warning
Condition="@(FrameworkReference->WithMetadataValue('Identity', 'Microsoft.AspNetCore.App')->Count()) > 1"
Text="Redundant <FrameworkReference>. If you have an explicit item in the project file, you might be able to remove it. Some SDKs, like Microsoft.NET.Sdk.Web, add this implicitly." />
</Target>
<!--
This target resolves remaining Reference items to Packages, if possible. If not, they are left as Reference
items for the SDK to resolve. This executes on NuGet restore and during DesignTimeBuild. It should not run in
outer, cross-targeting build.
-->
<Target Name="ResolveCustomReferences"
BeforeTargets="CheckForImplicitPackageReferenceOverrides;CollectPackageReferences;ResolvePackageAssets"
Condition=" '$(TargetFramework)' != '' AND '$(EnableCustomReferenceResolution)' == 'true' ">
<ItemGroup>
<!-- Ensure only content assets are consumed from .Sources packages. -->
<Reference>
<IncludeAssets Condition="'%(IsSharedSource)' == 'true'">ContentFiles;Build</IncludeAssets>
<PrivateAssets Condition="'%(IsSharedSource)' == 'true'">All</PrivateAssets>
</Reference>
<!-- Identify if any references were present in the last release of this package, but have been removed. -->
<UnusedBaselinePackageReference Include="@(BaselinePackageReference)"
Exclude="@(Reference);@(PackageReference);@(ProjectReference->'%(Filename)')" />
<!-- Handle suppressions needed because above Exclude is not aware of references added in .nuspec files. -->
<UnusedBaselinePackageReference Remove="@(SuppressBaselineReference->WithMetadataValue('InNuspecFile', 'true'))"
Condition=" '$(IsServicingBuild)' == 'true' " />
<!-- Allow suppressions of any baseline changes in non-servicing builds. -->
<UnusedBaselinePackageReference Remove="@(SuppressBaselineReference)"
Condition=" '$(IsServicingBuild)' != 'true' " />
</ItemGroup>
<JoinItems Left="@(Reference)" Right="@(LatestPackageReference)" LeftMetadata="*" RightMetadata="Version"
Condition=" '$(UseLatestPackageReferences)' == 'true' ">
<Output TaskParameter="JoinResult" ItemName="_LatestPackageReferenceWithVersion" />
</JoinItems>
<ItemGroup>
<PackageReference Include="@(_LatestPackageReferenceWithVersion)" IsImplicitlyDefined="true" />
<!-- Remove reference items that have been resolved to a LatestPackageReference item. -->
<Reference Remove="@(_LatestPackageReferenceWithVersion)" />
</ItemGroup>
<!-- Resolve references from BaselinePackageReference for servicing builds in corner cases. May be unused. -->
<JoinItems Left="@(Reference)" Right="@(BaselinePackageReference)" LeftMetadata="*" RightMetadata="Version"
Condition=" '$(IsServicingBuild)' == 'true' OR '$(UseLatestPackageReferences)' != 'true' ">
<Output TaskParameter="JoinResult" ItemName="_BaselinePackageReferenceWithVersion" />
</JoinItems>
<ItemGroup>
<PackageReference Include="@(_BaselinePackageReferenceWithVersion)" IsImplicitlyDefined="true" />
<!-- Remove reference items that have been resolved to a BaselinePackageReference item. -->
<Reference Remove="@(_BaselinePackageReferenceWithVersion)" />
</ItemGroup>
<!-- For PrivateAssets=All references, like .Sources packages, fallback to LatestPackageReferences. -->
<JoinItems Left="@(Reference->WithMetadataValue('PrivateAssets', 'All'))"
Right="@(LatestPackageReference)"
LeftMetadata="*"
RightMetadata="Version">
<Output TaskParameter="JoinResult" ItemName="_PrivatePackageReferenceWithVersion" />
</JoinItems>
<ItemGroup>
<PackageReference Include="@(_PrivatePackageReferenceWithVersion)" IsImplicitlyDefined="true" />
<!-- Remove reference items that have been resolved to a LatestPackageReference item. -->
<Reference Remove="@(_PrivatePackageReferenceWithVersion)" />
<!-- Free up memory for unnecessary items -->
<_LatestPackageReferenceWithVersion Remove="@(_LatestPackageReferenceWithVersion)" />
<_BaselinePackageReferenceWithVersion Remove="@(_BaselinePackageReferenceWithVersion)" />
<_PrivatePackageReferenceWithVersion Remove="@(_PrivatePackageReferenceWithVersion)" />
<_ImplicitPackageReference Remove="@(_ImplicitPackageReference)" />
</ItemGroup>
<Error
Condition="'$(DisablePackageReferenceRestrictions)' != 'true' AND @(_ExplicitPackageReference->Count()) != 0"
Text="PackageReference items are not allowed. Use <Reference> instead to replace the reference to @(_ExplicitPackageReference, ', '). See docs/ReferenceResolution.md for more details." />
<ItemGroup>
<_ExplicitPackageReference Remove="@(_ExplicitPackageReference)" />
</ItemGroup>
<Warning
Condition=" '$(IsServicingBuild)' != 'true' AND '%(UnusedBaselinePackageReference.Identity)' != '' "
Code="BUILD001"
Text="Reference to '%(UnusedBaselinePackageReference.Identity)' was removed since the last stable release of this package. This could be a breaking change. See docs/ReferenceResolution.md for instructions on how to update changes to references or suppress this warning if the error was intentional." />
<Error
Condition=" '$(IsServicingBuild)' == 'true' AND @(UnusedBaselinePackageReference->Count()) != 0 "
Code="BUILD002"
Text="Package references changed since the last release. This could be a breaking change and is not allowed in a servicing update. References removed:%0A - @(UnusedBaselinePackageReference, '%0A - ')" />
<Error
Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework' AND '%(Reference.Identity)' != '' AND ! Exists('%(Reference.Identity)') AND '$(DisablePackageReferenceRestrictions)' != 'true'"
Code="MSB3245"
Text="Could not resolve this reference. Could not locate the package or project for "%(Reference.Identity)". Did you update baselines and dependencies lists? See docs/ReferenceResolution.md for more details." />
</Target>
<PropertyGroup>
<_CompileTfmUsingReferenceAssemblies>false</_CompileTfmUsingReferenceAssemblies>
<_CompileTfmUsingReferenceAssemblies
Condition=" '$(CompileUsingReferenceAssemblies)' != false AND '$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)' ">true</_CompileTfmUsingReferenceAssemblies>
</PropertyGroup>
<!--
If we have a ref/ assembly from dotnet/runtime for an Extension package, use that when compiling but do not reference its assemblies.
-->
<ItemGroup Condition=" '$(MSBuildProjectName)' != 'Microsoft.AspNetCore.App.Runtime' AND
'$(MSBuildProjectName)' != 'RepoTasks' AND
($(_CompileTfmUsingReferenceAssemblies) OR
('$(IsTargetingPackBuilding)' != 'false' AND '$(MSBuildProjectName)' == 'Microsoft.AspNetCore.App.Ref')) ">
<PackageReference Include="Microsoft.AspNetCore.Internal.Transport"
Version="$(MicrosoftAspNetCoreInternalTransportVersion)"
IsImplicitlyDefined="true"
IncludeAssets="Compile"
PrivateAssets="All"
GeneratePathProperty="true" />
</ItemGroup>
<!--
Remove compile-time assets for packages that overlap Microsoft.AspNetCore.Internal.Transport. Serviced packages
may otherwise increase the referenced version. Avoid this because change reduces compatible runtime versions.
That's not a big deal within the shared framework but can cause problems for shipped packages. Leave test
projects and Ignitor alone because they may transitively reference newer netstandard assemblies and need a
net5.0 assembly with the same version. (This can happen in implementation projects but is less likely.)
-->
<Target Name="RemoveExtensionsCompileAssets"
AfterTargets="ResolvePackageAssets"
Condition=" '$(PkgMicrosoft_AspNetCore_Internal_Transport)' != '' AND
'$(IsServicingBuild)' == 'true' AND
'$(IsImplementationProject)' == 'true' AND
'$(MSBuildProjectName)' != 'Ignitor' AND
'$(MSBuildProjectName)' != 'Microsoft.AspNetCore.App.Runtime' AND
($(_CompileTfmUsingReferenceAssemblies) OR
('$(IsTargetingPackBuilding)' != 'false' AND '$(MSBuildProjectName)' == 'Microsoft.AspNetCore.App.Ref')) ">
<ItemGroup>
<ResolvedCompileFileDefinitions Remove="@(ResolvedCompileFileDefinitions)"
Condition=" '%(NuGetPackageId)' != 'Microsoft.AspNetCore.Internal.Transport' AND
EXISTS('$(PkgMicrosoft_AspNetCore_Internal_Transport)\ref\$(TargetFramework)\%(Filename).dll') AND
$([System.String]::new('%(Directory)').Contains('$(TargetFramework)')) " />
</ItemGroup>
</Target>
<!-- These targets are used to generate the map of assembly name to project files. See also the /t:GenerateProjectList target in build/repo.targets. -->
<Target Name="GetReferencesProvided" Returns="@(ProvidesReference)">
<ItemGroup>
<_TargetFramework Remove="@(_TargetFramework)" />
<_TargetFramework Include="$(TargetFramework)" Condition="'$(TargetFramework)' != '' "/>
<_TargetFramework Include="$(TargetFrameworks)" Condition="'$(TargetFramework)' == '' "/>
</ItemGroup>
<MSBuild Projects="$(MSBuildProjectFullPath)"
Targets="_GetReferencesProvided"
Properties="TargetFramework=%(_TargetFramework.Identity)">
<Output TaskParameter="TargetOutputs" ItemName="ProvidesReference" />
</MSBuild>
</Target>
<Target Name="_GetReferencesProvided" Returns="@(ProvidesReference)">
<Error
Condition=" '$(IsAspNetCoreApp)' == 'true' AND '$(IsImplementationProject)' == 'true' AND
!$(HasReferenceAssembly) AND '$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)' "
Text="All assemblies which have set IsAspNetCoreApp=true should produce a reference assembly for default TFM." />
<Error Condition=" '$(IsAspNetCoreApp)' == 'true' AND '$(IsImplementationProject)' != 'true' "
Text="Only implementation projects should set IsAspNetCoreApp=true." />
<Error Condition=" '$(IsAspNetCoreApp)' != 'true' AND $(HasReferenceAssembly) "
Text="Only projects in the shared framework i.e. IsAspNetCoreApp==true should produce a reference assembly." />
<Warning Condition=" '$(IsProjectReferenceProvider)' == 'true' AND '$(AssemblyName)' != '$(MSBuildProjectName)' "
Text="Project name "$(MSBuildProjectName)" is confusing; assembly is named "$(AssemblyName)"." />
<ItemGroup Condition=" '$(IsProjectReferenceProvider)' == 'true' ">
<ProvidesReference Include="$(AssemblyName)">
<IsAspNetCoreApp>$([MSBuild]::ValueOrDefault($(IsAspNetCoreApp),'false'))</IsAspNetCoreApp>
<IsPackable>$([MSBuild]::ValueOrDefault($(IsPackable),'false'))</IsPackable>
<ProjectFileRelativePath>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectFullPath)))</ProjectFileRelativePath>
</ProvidesReference>
</ItemGroup>
</Target>
<!-- This is used by the eng/scripts/AddAllProjectRefsToSolution.ps1 script to traverse the ProjectRef graph -->
<PropertyGroup>
<_CustomCollectProjectReferenceDependsOn
Condition="'$(TargetFramework)' != ''">ResolveProjectReferences</_CustomCollectProjectReferenceDependsOn>
</PropertyGroup>
<Target Name="_CustomCollectProjectReference"
DependsOnTargets="$(_CustomCollectProjectReferenceDependsOn)"
Returns="$(MSBuildProjectFullPath);@(_MSBuildProjectReferenceExistent)">
<ItemGroup>
<_TargetFrameworks Include="$(TargetFrameworks)" />
</ItemGroup>
<MSBuild Condition="'$(TargetFramework)' == ''"
Targets="_CustomCollectProjectReference"
BuildInParallel="true"
Projects="$(MSBuildProjectFullPath)"
Properties="TargetFramework=%(_TargetFrameworks.Identity)"
RebaseOutputs="True">
<Output TaskParameter="TargetOutputs" ItemName="_MSBuildProjectReferenceExistent" />
</MSBuild>
<MSBuild Condition="'$(TargetFramework)' != ''"
Targets="_CustomCollectProjectReference"
BuildInParallel="true"
SkipNonexistentTargets="true"
Projects="@(_MSBuildProjectReferenceExistent)"
RebaseOutputs="True">
<Output TaskParameter="TargetOutputs" ItemName="_MSBuildProjectReferenceExistent" />
</MSBuild>
</Target>
</Project>