Skip to content
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

Use PackageDownloadAndReference to avoid including msbuild dependencies #75561

Merged
merged 5 commits into from
Oct 23, 2024
Merged
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
35 changes: 35 additions & 0 deletions eng/targets/PackageDownloadAndReference.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project>

<!--
Based on https://github.com/dotnet/runtime/blob/9893d145ebb61ae592afa1a64baa2f6f094b1f6b/eng/PackageDownloadAndReference.targets

These file supports using PackageDownloadAndReference items.

The PackageDownloadAndReference item is used to download a package and reference it in the project, without restoring the package's dependency closure.

When using PackageDownloadAndReference you are responsible for selecting the correct assets from the package and ensuring that the package and it's
dependencies are available at runtime.
tmat marked this conversation as resolved.
Show resolved Hide resolved

The PackageDownloadAndReference item has the following metadata:
- Folder: The folder in the package where the assembly is located.
- AssemblyName: The name of the assembly to reference.
- Private: Whether the reference should be private (copied to the output directory) or not. Default is false.

A common use case for PackageDownloadAndReference is to reference assemblies provided by MSBuild or the .NET SDK.
-->

<ItemDefinitionGroup>
<PackageDownloadAndReference>
<Folder>lib/$(TargetFramework)</Folder>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is guaranteed to be lower case so no need for a defensive ToLowerInvariant() call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set the TFM, so I'd hope we use lower case.

<Private>false</Private>
</PackageDownloadAndReference>
</ItemDefinitionGroup>

<ItemGroup>
<PackageDownload Include="@(PackageDownloadAndReference)" />
<PackageDownload Update="@(PackageDownloadAndReference)" Version="[%(Version)]"/>
<PackageDownloadAndReference Update="@(PackageDownloadAndReference)" PackageFolder="$([System.String]::new(%(Identity)).ToLowerInvariant())" AssemblyName="%(Identity)" />
<Reference Include="@(PackageDownloadAndReference->'$(NuGetPackageRoot)%(PackageFolder)/%(Version)/%(Folder)/%(AssemblyName).dll')" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@
-->
<PlatformTarget>AnyCPU</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<!--
We specifically reference Microsoft.Build 17.3.4, since it's the oldest version still compatible $(NetRoslynBuildHostNetCoreVersion).
The version targeted is only used for build time, since we use MSBuildLocator to discover the proper version at runtime.
-->
<_MsbuildVersion>17.3.4</_MsbuildVersion>
</PropertyGroup>
<ItemGroup Condition="'$(DotNetBuildSourceOnly)' == 'true'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason why you need PackageReference for sourcebuild. It should work just the same there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind filing an issue? We can remove the condition here once fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned that source build isn't really meant to have .NETFramework targets so that might not have been the root cause, but I bet there is something like that - where the SBRP packages differ from the real packages in a way that causes an error

<PackageReference Include="Microsoft.Build" VersionOverride="$(_MsbuildVersion)" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Framework" VersionOverride="$(_MsbuildVersion)" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" VersionOverride="$(_MsbuildVersion)" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Utilities.Core" VersionOverride="$(_MsbuildVersion)" ExcludeAssets="Runtime" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(DotNetBuildSourceOnly)' != 'true'">
<!-- Reference MSBuild directly to avoid redistributing its package closure and including the dependencies in deps.json file. -->
<PackageDownloadAndReference Include="Microsoft.Build" Version="$(_MsbuildVersion)" Folder="ref/$(TargetFramework)" />
<PackageDownloadAndReference Include="Microsoft.Build.Framework" Version="$(_MsbuildVersion)" Folder="ref/$(TargetFramework)" />
<PackageDownloadAndReference Include="Microsoft.Build.Tasks.Core" Version="$(_MsbuildVersion)" Folder="ref/$(TargetFramework)" />
<PackageDownloadAndReference Include="Microsoft.Build.Utilities.Core" Version="$(_MsbuildVersion)" Folder="ref/$(TargetFramework)" />
</ItemGroup>
<ItemGroup>
<!-- We specifically reference Microsoft.Build 17.3.4, since it's the oldest version still compatible net6.0. The version targeted is only used for build time, since
we use MSBuildLocator to discover the proper version at runtime. -->
<PackageReference Include="Microsoft.Build" VersionOverride="17.3.4" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Framework" VersionOverride="17.3.4" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" VersionOverride="17.3.4" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Utilities.Core" VersionOverride="17.3.4" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Locator" PrivateAssets="All" />
<PackageReference Include="System.Text.Json" VersionOverride="8.0.4" PrivateAssets="All" Condition="'$(DotNetBuildSourceOnly)' != 'true'"/>
<PackageReference Include="System.CommandLine" />
<PackageReference Include="System.Collections.Immutable" />
<PackageReference Include="Newtonsoft.Json" />
Expand All @@ -39,6 +51,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\*.cs" Link="InternalUtilities\%(FileName).cs" />

<!-- Exclude utilities that use System.Reflection.Metadata -->
<Compile Remove="..\..\..\Compilers\Core\Portable\InternalUtilities\BlobBuildingStream.cs" />
<Compile Remove="..\..\..\Compilers\Core\Portable\InternalUtilities\IncrementalHashExtensions.cs" />
tmat marked this conversation as resolved.
Show resolved Hide resolved

<Compile Include="..\..\..\Compilers\Core\Portable\Collections\ArrayBuilderExtensions.cs" Link="Collections\ArrayBuilderExtensions.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\Collections\ImmutableArrayExtensions.cs" Link="Collections\ImmutableArrayExtensions.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\Collections\TemporaryArray`1.cs" Link="Collections\TemporaryArray`1.cs" />
Expand Down Expand Up @@ -94,4 +111,5 @@
</Target>
<Import Project="..\..\..\Dependencies\PooledObjects\Microsoft.CodeAnalysis.PooledObjects.projitems" Label="Shared" />
<Import Project="..\..\..\Dependencies\Collections\Microsoft.CodeAnalysis.Collections.projitems" Label="Shared" />
<Import Project="$(RepositoryEngineeringDir)targets\PackageDownloadAndReference.targets"/>
tmat marked this conversation as resolved.
Show resolved Hide resolved
</Project>
Loading