Skip to content

Updates from MSBuild 17.x and NuGet 6.x #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@
<ProduceReferenceAssembly Condition="'$(ProduceReferenceAssembly)' == '' OR '$(ProduceOnlyReferenceAssembly)' == 'true'">false</ProduceReferenceAssembly>
</PropertyGroup>

<!--
By default, we produce reference assemblies in their own `ref` folder under `BuildDir`.
But we also want to preserve the previous way of producing a reference assembly in the `OutputPath` too!
-->
<PropertyGroup Condition="'$(ProduceReferenceAssembly)' == 'true'">
<ProduceReferenceAssemblyInOutputPath Condition="'$(ProduceReferenceAssemblyInOutputPath)' == ''">false</ProduceReferenceAssemblyInOutputPath>
</PropertyGroup>

<!-- Introduce `BaseReferenceOutputPath` and `ReferenceOutputPath` as the goto destination for reference assemblies -->
<PropertyGroup Condition="'$(ProduceReferenceAssembly)' == 'true' AND '$(ProduceReferenceAssemblyInOutputPath)' == 'false'">
<BaseReferenceOutputPath Condition="'$(BaseReferenceOutputPath)' == ''">$(BuildDir)ref\</BaseReferenceOutputPath>
<BaseReferenceOutputPath Condition="!HasTrailingSlash('$(BaseReferenceOutputPath)')">$(BaseReferenceOutputPath)\</BaseReferenceOutputPath>
<ReferenceOutputPath Condition="'$(ReferenceOutputPath)' == '' AND '$(Platform)' == 'AnyCPU'">$(BaseReferenceOutputPath)$(Configuration)\</ReferenceOutputPath>
<ReferenceOutputPath Condition="'$(ReferenceOutputPath)' == '' AND '$(Platform)' != 'AnyCPU'">$(BaseReferenceOutputPath)$(Platform)\$(Configuration)\</ReferenceOutputPath>
<ReferenceOutputPath Condition="!HasTrailingSlash('$(ReferenceOutputPath)')">$(ReferenceOutputPath)\</ReferenceOutputPath>
</PropertyGroup>

<!--
For Legacy projects that define properties per Configuration/Platform combination, the validity of an
invalid combination is determined by the non-empty value of the 'OutputPath' property specified under
Expand Down Expand Up @@ -239,8 +256,13 @@
<TargetDir Condition="'$(OutDir)' != ''">$([System.IO.Path]::Combine('$(MSBuildProjectDirectory)', '$(OutDir)'))</TargetDir>
<!-- Example, C:\MyProjects\MyProject\bin\Debug\MyAssembly.dll -->
<TargetPath Condition="'$(TargetPath)' == ''">$(TargetDir)$(TargetFileName)</TargetPath>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetRefPath)' == '' AND '$(ProduceReferenceAssembly)' == 'true'">
<!-- Example, C:\MyProjects\MyProject\bin\Debug\ref\MyAssembly.dll -->
<TargetRefPath Condition="'$(TargetRefPath)' == '' AND '$(ProduceReferenceAssembly)' == 'true'">$([MSBuild]::NormalizePath('$(TargetDir)', 'ref', '$(TargetFileName)'))</TargetRefPath>
<TargetRefPath Condition="'$(ProduceReferenceAssemblyInOutputPath)' != 'false'">$([MSBuild]::NormalizePath('$(TargetDir)', 'ref', '$(TargetFileName)'))</TargetRefPath>
<!-- Example, C:\MyProjects\MyProject\ref\Debug\MyAssembly.dll -->
<TargetRefPath Condition="'$(ProduceReferenceAssemblyInOutputPath)' == 'false'">$([MSBuild]::NormalizePath('$(ReferenceOutputPath)', '$(TargetFileName)'))</TargetRefPath>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -361,7 +383,8 @@
<ItemGroup Condition="'$(ProduceReferenceAssembly)' == 'true'">
<IntermediateRefAssembly Include="$(IntermediateOutputPath)ref\$(TargetName)$(TargetExt)" Condition="@(IntermediateRefAssembly->Count()) == 0"/>
<CreateDirectory Include="@(IntermediateRefAssembly->'%(RootDir)%(Directory)')"/>
<CreateDirectory Include="$(OutDir)ref"/>
<CreateDirectory Include="$(OutDir)ref" Condition="'$(ProduceReferenceAssemblyInOutputPath)' != 'false'"/>
<CreateDirectory Include="$(ReferenceOutputPath)" Condition="'$(ProduceReferenceAssemblyInOutputPath)' == 'false'"/>
</ItemGroup>

<ItemGroup Condition="'$(_DebugSymbolsProduced)' == 'true'">
Expand Down
128 changes: 116 additions & 12 deletions Sources/NuGet.Build.Sdk/Core/NuGet.Restore.targets
Original file line number Diff line number Diff line change
Expand Up @@ -174,33 +174,111 @@
CollectPackageReferences

Gathers all PackageReference items from the project.
This target may be used as an extension point to modify
package references before NuGet reads them.
This target may be used as a pivot point for 'BeforeTargets'
extension point (do not use 'AfterTargets') to modify
package downloads before NuGet reads them.
============================================================
-->
<Target Name="CollectPackageReferences" Returns="@(PackageReference)"/>
<Target Name="CollectPackageReferences" Returns="@(PackageReference)">

<!-- Design-Time builds should not produce a build error. So, we need to ensure that we continue on error -->
<PropertyGroup>
<CollectPackageReferencesContinueOnError>$([MSBuild]::ValueOrDefault($(ContinueOnError), 'false'))</CollectPackageReferencesContinueOnError>
<DisableCheckingDuplicatePackageReferences>$([MSBuild]::ValueOrDefault($(DisableCheckingDuplicateNuGetItems), $(DisableCheckingDuplicatePackageReferences)))</DisableCheckingDuplicatePackageReferences>
</PropertyGroup>

<CheckForDuplicateNuGetItemsTask
LogCode="NU1504"
NoWarn="$(NoWarn)"
Items="@(PackageReference)"
ItemName="PackageReference"
WarningsAsErrors="$(WarningsAsErrors)"
TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
MSBuildProjectFullPath="$(MSBuildProjectFullPath)"
ContinueOnError="$(CollectPackageReferencesContinueOnError)"
Condition="'$(DisableCheckingDuplicatePackageReferences)' != 'true'">
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedPackageReference"/>
</CheckForDuplicateNuGetItemsTask>

<ItemGroup Condition="'@(DeduplicatedPackageReference)' != ''">
<PackageReference Remove="@(PackageReference)"/>
<PackageReference Include="@(DeduplicatedPackageReference)"/>
</ItemGroup>
</Target>

<!--
============================================================
CollectCentralPackageVersions

Gathers all PackageVersion items from the central package versions file.
This target may be used as an extension point to modify
package versions before NuGet reads them.
This target may be used as a pivot point for 'BeforeTargets'
extension point (do not use 'AfterTargets') to modify
package downloads before NuGet reads them.
============================================================
-->
<Target Name="CollectCentralPackageVersions" Returns="@(PackageVersion)"/>
<Target Name="CollectCentralPackageVersions" Returns="@(PackageVersion)">

<!-- Design-Time builds should not produce a build error. So, we need to ensure that we continue on error -->
<PropertyGroup>
<CollectCentralPackageVersionsContinueOnError>$([MSBuild]::ValueOrDefault($(ContinueOnError), 'false'))</CollectCentralPackageVersionsContinueOnError>
<DisableCheckingDuplicatePackageVersions>$([MSBuild]::ValueOrDefault($(DisableCheckingDuplicateNuGetItems), $(DisableCheckingDuplicatePackageVersions)))</DisableCheckingDuplicatePackageVersions>
</PropertyGroup>

<CheckForDuplicateNuGetItemsTask
LogCode="NU1505"
NoWarn="$(NoWarn)"
Items="@(PackageVersion)"
ItemName="PackageVersion"
WarningsAsErrors="$(WarningsAsErrors)"
TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
MSBuildProjectFullPath="$(MSBuildProjectFullPath)"
ContinueOnError="$(CollectCentralPackageVersionsContinueOnError)"
Condition="'$(DisableCheckingDuplicatePackageVersions)' != 'true'">
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedPackageVersion"/>
</CheckForDuplicateNuGetItemsTask>

<ItemGroup Condition="'@(DeduplicatedPackageVersion)' != ''">
<PackageVersion Remove="@(PackageVersion)"/>
<PackageVersion Include="@(DeduplicatedPackageVersion)"/>
</ItemGroup>
</Target>

<!--
============================================================
CollectPackageDownloads

Gathers all PackageDownload items from the project.
This target may be used as an extension point to modify
This target may be used as a pivot point for 'BeforeTargets'
extension point (do not use 'AfterTargets') to modify
package downloads before NuGet reads them.
============================================================
-->
<Target Name="CollectPackageDownloads" Returns="@(PackageDownload)"/>
<Target Name="CollectPackageDownloads" Returns="@(PackageDownload)">

<!-- Design-Time builds should not produce a build error. So, we need to ensure that we continue on error -->
<PropertyGroup>
<CollectPackageDownloadsContinueOnError>$([MSBuild]::ValueOrDefault($(ContinueOnError), 'false'))</CollectPackageDownloadsContinueOnError>
<DisableCheckingDuplicatePackageDownloads>$([MSBuild]::ValueOrDefault($(DisableCheckingDuplicateNuGetItems), $(DisableCheckingDuplicatePackageDownloads)))</DisableCheckingDuplicatePackageDownloads>
</PropertyGroup>

<CheckForDuplicateNuGetItemsTask
LogCode="NU1505"
NoWarn="$(NoWarn)"
Items="@(PackageDownload)"
ItemName="PackageDownload"
WarningsAsErrors="$(WarningsAsErrors)"
TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
MSBuildProjectFullPath="$(MSBuildProjectFullPath)"
ContinueOnError="$(CollectPackageDownloadsContinueOnError)"
Condition="'$(DisableCheckingDuplicatePackageDownloads)' != 'true'">
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedPackageDownload"/>
</CheckForDuplicateNuGetItemsTask>

<ItemGroup Condition="'@(DeduplicatedPackageDownload)' != ''">
<PackageDownload Remove="@(PackageDownload)"/>
<PackageDownload Include="@(DeduplicatedPackageDownload)"/>
</ItemGroup>
</Target>

<!--
============================================================
Expand Down Expand Up @@ -519,18 +597,20 @@
============================================================
-->
<Target Name="_GetRestoreTargetFrameworksOutput"
DependsOnTargets="_GetRestoreProjectStyle"
DependsOnTargets="_GetRestoreProjectStyle;_CheckIfGlobalTargetFramework"
Returns="@(_RestoreTargetFrameworksOutputFiltered)">

<PropertyGroup>
<_RestoreProjectFramework/>
<_PreferredTargetFrameworks Condition="'$(_IsGlobalTargetFramework)' == 'true'">$(TargetFramework)</_PreferredTargetFrameworks>
<_PreferredTargetFrameworks Condition="'$(_IsGlobalTargetFramework)' != 'true'">$(TargetFrameworks)</_PreferredTargetFrameworks>
</PropertyGroup>

<!-- For project.json projects target frameworks will be read from project.json -->
<GetProjectTargetFrameworksTask
Condition="'$(RestoreProjectStyle)' != 'ProjectJson'"
ProjectPath="$(MSBuildProjectFullPath)"
TargetFrameworks="$(TargetFrameworks)"
TargetFrameworks="$(_PreferredTargetFrameworks)"
TargetFramework="$(TargetFramework)"
TargetFrameworkMoniker="$(TargetFrameworkMoniker)"
TargetPlatformIdentifier="$(TargetPlatformIdentifier)"
Expand All @@ -552,13 +632,16 @@

Read $(TargetFrameworks) from the project as items.
Projects that do not have $(TargetFrameworks) will noop.
If $(TargetFramework) is specified globally, it'll be preferred over $(TargetFrameworks)
============================================================
-->
<Target Name="_GetRestoreTargetFrameworksAsItems"
DependsOnTargets="_GetRestoreProjectStyle"
DependsOnTargets="_GetRestoreProjectStyle;_CheckIfGlobalTargetFramework"
Returns="@(_RestoreTargetFrameworkItems)">
<ItemGroup Condition="'$(TargetFrameworks)' != ''">
<_RestoreTargetFrameworkItems Include="$(TargetFrameworks.Split(';'))"/>
<_RestoreTargetFrameworkItems Include="$(TargetFrameworks.Split(';'))" Condition="'$(_IsGlobalTargetFramework)' != 'true'"/>
<!-- Use $(TargetFramework) instead of $(TargetFrameworks) when it is specified globally (i.e., on the command-line) -->
<_RestoreTargetFrameworkItems Include="$(TargetFramework)" Condition="'$(_IsGlobalTargetFramework)' == 'true'"/>
</ItemGroup>
</Target>

Expand Down Expand Up @@ -1298,6 +1381,27 @@
</PropertyGroup>
</Target>

<!--
============================================================
_CheckIfGlobalTargetFramework
============================================================
-->
<Target Name="_CheckIfGlobalTargetFramework"
Condition="'$(_DisableNuGetRestoreTargetFrameworksOverride)' != 'true'"
Returns="$(_IsGlobalTargetFramework)">

<!-- Check if $(TargetFramework) is a global property (i.e., specified on the command-line) -->
<GetGlobalPropertyValueTask
PropertyName="TargetFramework"
Condition="'$(TargetFramework)' != ''">
<Output TaskParameter="GlobalPropertyValue" PropertyName="_GlobalTargetFramework"/>
</GetGlobalPropertyValueTask>

<PropertyGroup Condition="'$(_GlobalTargetFramework)' != ''">
<_IsGlobalTargetFramework Condition="'$(_GlobalTargetFramework)' == '$(TargetFramework)'">true</_IsGlobalTargetFramework>
</PropertyGroup>
</Target>

<!--
============================================================
_IsProjectRestoreSupported
Expand Down
4 changes: 2 additions & 2 deletions Sources/NuGet.Build.Sdk/Extensions/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
<Import Project="$(DirectoryPackagesProps)" Condition="Exists('$(DirectoryPackagesProps)')"/>

<PropertyGroup Condition="Exists('$(DirectoryPackagesProps)')">
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageVersionsFileImported>true</CentralPackageVersionsFileImported>
<RestoreEnableGlobalPackageReference>true</RestoreEnableGlobalPackageReference>
<ManagePackageVersionsCentrally Condition="'$(ManagePackageVersionsCentrally)' == ''">true</ManagePackageVersionsCentrally>
<RestoreEnableGlobalPackageReference Condition="'$(RestoreEnableGlobalPackageReference)' == ''">true</RestoreEnableGlobalPackageReference>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions Sources/NuGet.Build.Sdk/Tasks/NuGet.Restore.tasks
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

<!-- NuGet's Common Tasks -->
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.NuGetMessageTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.GetGlobalPropertyValueTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.CheckForDuplicateNuGetItemsTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.WarnForInvalidProjectsTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.GetCentralPackageVersionsTask"/>
<UsingTask AssemblyFile="$(NuGetRestoreTasksAssemblyFile)" TaskName="NuGet.Build.Tasks.GetProjectTargetFrameworksTask"/>
Expand Down