Skip to content

Commit 09fecd6

Browse files
authored
Merge pull request dotnet#699 from dsplaisted/remove-duplicate-package-check-before-restore
Don't run CheckForImplicitPackageReferenceOverrides before NuGet restore
2 parents b616fc4 + 60a638a commit 09fecd6

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.DefaultItems.targets

+36-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Copyright (c) .NET Foundation. All rights reserved.
9393
<UsingTask TaskName="CheckForImplicitPackageReferenceOverrides" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
9494

9595
<!-- Remove package references with metadata IsImplicitlyDefined = true, if there are other PackageReference items with the same identity -->
96-
<Target Name="CheckForImplicitPackageReferenceOverrides" BeforeTargets="_GetRestoreProjectStyle;_CheckForInvalidConfigurationAndPlatform;CollectPackageReferences">
96+
<Target Name="CheckForImplicitPackageReferenceOverrides" BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CollectPackageReferences">
9797
<PropertyGroup>
9898
<ImplicitPackageReferenceInformationLink>https://aka.ms/sdkimplicitrefs</ImplicitPackageReferenceInformationLink>
9999
</PropertyGroup>
@@ -112,6 +112,41 @@ Copyright (c) .NET Foundation. All rights reserved.
112112

113113
</Target>
114114

115+
<!-- Running an SDK task before the NuGet restore task causes issues when running on .NET Framework because it causes the
116+
.NET Standard NuGet DLLs to be loaded from the SDK path rather than the .NET Framework versions from the NuGet targets
117+
path. To avoid this, we create a separate target to run before NuGet restore which deduplicates the items without
118+
causing the SDK tasks to be loaded (but doesn't generate a warning message, because we need to load the tasks for that). -->
119+
<PropertyGroup Condition="'$(DisableImplicitFrameworkReferences)' != 'true'">
120+
<_ImplicitPackageName Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'">NETStandard.Library</_ImplicitPackageName>
121+
<_ImplicitPackageName Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">Microsoft.NETCore.App</_ImplicitPackageName>
122+
</PropertyGroup>
123+
124+
<ItemGroup Condition="'$(_ImplicitPackageName)' != ''">
125+
<!-- Filter PackageReference to items where the ItemSpec matches the implicit package name, and add IsImplicitlyDefined metadata
126+
for items that don't have it-->
127+
<_ImplicitPackageReferenceCheck
128+
Include="@(PackageReference->WithMetadataValue('Identity', '$(_ImplicitPackageName)'))">
129+
<IsImplicitlyDefined Condition="'%(IsImplicitlyDefined)' != 'true' ">false</IsImplicitlyDefined>
130+
</_ImplicitPackageReferenceCheck>
131+
132+
<!-- Now filter down to an item with just the implicit reference and another one with just the overriding reference -->
133+
<_ImplicitPackageReference Include="@(_ImplicitPackageReferenceCheck->WithMetadataValue('IsImplicitlyDefined', 'true'))"/>
134+
<_OverridingPackageReference Include="@(_ImplicitPackageReferenceCheck->WithMetadataValue('IsImplicitlyDefined', 'false'))"/>
135+
</ItemGroup>
136+
137+
<Target Name="CheckForImplicitPackageReferenceOverridesBeforeRestore" BeforeTargets="_GetRestoreProjectStyle">
138+
<ItemGroup>
139+
<!-- Remove both the implicit and the override item, if there was both an implicit and an override item -->
140+
<PackageReference Remove="@(PackageReference->WithMetadataValue('Identity', '$(_ImplicitPackageName)'))"
141+
Condition="'@(_ImplicitPackageReference)' != '' And '@(_OverridingPackageReference)' != ''"
142+
/>
143+
144+
<!-- Add the override item back -->
145+
<PackageReference Include="@(_OverridingPackageReference)"
146+
Condition="'@(_ImplicitPackageReference)' != '' And '@(_OverridingPackageReference)' != ''" />
147+
</ItemGroup>
148+
</Target>
149+
115150
<UsingTask TaskName="CheckForDuplicateItems" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
116151

117152
<Target Name="CheckForDuplicateItems" BeforeTargets="_CheckForInvalidConfigurationAndPlatform;CoreCompile">

0 commit comments

Comments
 (0)