NuGet 不解析传递性依赖关系。

huangapple go评论68阅读模式
英文:

NuGet does not resolve transitive dependencies

问题

我正在尝试从一个项目(Sub.nupkg)构建一个NuGet包。这部分工作得相当不错。

问题从这个项目本身使用包含contentFiles的NuGet(Base.nupkg)开始,这些文件需要被“传递”,但未复制到目标目录。

问题可以通过使用IronPython.StdLib来复制包的方式来重现。一旦包含了这个包,当在另一个项目中使用时,contentFiles就不再被复制。

如果我们更改Sub.nupkg中的私有资产,我们可以强制将其复制到输出目录:

<PackageReference Include="pkg" Version="1.*">
  <PrivateAssets>none</PrivateAssets>
</PackageReference>

如果在Sub.nupkg中配置了这个,它将强制将contentFiles复制到输出文件夹。

问题是,我们无法创建自动配置资产的NuGet包。

我们可以在nuspec中指定developmentDependency,如下所示:

<developmentDependency>true</developmentDependency>

但这会导致以下用法:

<PackageReference Include="pkg" Version="1.*">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

这并不会将所有的contentFiles复制到目标目录。

我尝试使用*.props做一些尝试,但因为无法重新配置现有的PackageReference,所以没有成功。

我们的依赖关系类似于:

  • 使用项目
    • Sub.nupkg
      • Base.nupkg(包含需要复制到输出目录的contentFiles

我们该如何解决这个问题?

我找到了为什么依赖库中的本地dll不包含在构建输出中?,但它没有提供解决这个问题的解决方案。而且我不明白如何使用buildTransitive来满足我们的需求。

顺便说一下:我们只使用包引用。

IronPython.StdLib来重现这个问题的项目:

消费者(使用引用IronPython的包):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IronPythonConsumer" Version="1.0.0" />
  </ItemGroup>
   
</Project>

使用IronPython的项目:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net70</TargetFramework>
    <Authors />
    <Product>IronPythonConsumer</Product>
    <AssemblyVersion>1.0.0</AssemblyVersion>
    <FileVersion>1.0.0</FileVersion>
    <Version>1.0.0</Version>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>$(SolutionDir)bin$(Configuration)</OutputPath>
  </PropertyGroup>

  <PropertyGroup>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IronPython.StdLib" Version="2.7.12" />
  </ItemGroup>
</Project>

如果你将IronPython的PackageReference更改为以下方式:

<PackageReference Include="IronPython.StdLib" Version="2.7.12">
  <PrivateAssets>none</PrivateAssets>
</PackageReference>

消费者不需要更改。但之后所有的contentFiles都会被复制到输出文件夹。

重要的是不能每次使用这些资产时都调整资产。这会导致频繁出现错误,因为编译运行没有错误。问题只在运行时出现。

英文:

I am trying to build a NuGet package from a project (Sub.nupkg). This works quite well.

The problems start as soon as this project itself uses a NuGet (Base.nupkg) which contains contentFiles. These are needed "transitive", but are not copied to the target directory.

The problem can be reproduced with IronPython.StdLib. Once this package is included, the contentFiles are no longer copied when used in another project.

If we change the private assets in Sub.nupkg we're able to force the copy to output:

&lt;PackageReference Include=&quot;pkg&quot; Version=&quot;1.*&quot;&gt;
  &lt;PrivateAssets&gt;none&lt;/PrivateAssets&gt;
&lt;/PackageReference&gt;

If this is configured in Sub.nupkg it will foce a copy of the contentFiles to the output folder.

The problem is that we're not able to create a NuGet package that configures that assets automatically.

We can specify developmentDependency in nuspec like:

&lt;developmentDependency&gt;true&lt;/developmentDependency&gt;

But this leads to this usage:

&lt;PackageReference Include=&quot;pkg&quot; Version=&quot;1.*&quot;&gt;
  &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
  &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
&lt;/PackageReference&gt;

And this does not copy all the contentFiles to the target directory.

I tried something using *.props but didn't work because I'm not able to reconfigure an existing PackageReference.

Our dependencies are similar to:

  • Using-Project
    • Sub.nupkg
      • Base.nupkg (containing contentFiles needed to be copied to the output folder)

How do we solve that problem?

I found Why are native dll's from dependent libraries not included in build output? but it does not contain any solution to that problem. And I don't understand how to use buildTransitive to achieve our needs.

Btw: We're only using package references.

Projects for reproducing that issue using IronPython.StdLib:

Consumer (that uses a packages that references IronPython):

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;

  &lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
  &lt;/PropertyGroup&gt;

  &lt;PropertyGroup Condition=&quot;&#39;$(Configuration)|$(Platform)&#39;==&#39;Debug|AnyCPU&#39;&quot;&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;IronPythonConsumer&quot; Version=&quot;1.0.0&quot; /&gt;
  &lt;/ItemGroup&gt;
   
&lt;/Project&gt;

Project that consumes IronPython:

&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
  &lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;net70&lt;/TargetFramework&gt;
    &lt;Authors /&gt;
    &lt;Product&gt;IronPythonConsumer&lt;/Product&gt;
    &lt;AssemblyVersion&gt;1.0.0&lt;/AssemblyVersion&gt;
    &lt;FileVersion&gt;1.0.0&lt;/FileVersion&gt;
    &lt;Version&gt;1.0.0&lt;/Version&gt;
  &lt;/PropertyGroup&gt;

  &lt;PropertyGroup Condition=&quot;&#39;$(Configuration)|$(Platform)&#39;==&#39;Debug|AnyCPU&#39;&quot;&gt;
    &lt;OutputPath&gt;$(SolutionDir)bin$(Configuration)&lt;/OutputPath&gt;
  &lt;/PropertyGroup&gt;

  &lt;PropertyGroup&gt;
    &lt;GeneratePackageOnBuild&gt;True&lt;/GeneratePackageOnBuild&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;IronPython.StdLib&quot; Version=&quot;2.7.12&quot; /&gt;
  &lt;/ItemGroup&gt;
&lt;/Project&gt;

You can make that work if you change the PackageReference of IronPython to:

&lt;PackageReference Include=&quot;IronPython.StdLib&quot; Version=&quot;2.7.12&quot;&gt;
  &lt;PrivateAssets&gt;none&lt;/PrivateAssets&gt;
&lt;/PackageReference&gt;

The Consumer does not need to be changed. But after that all the contentFiles are copied to the output folder.

It is not an option to adjust the assets every time you use them. This leads too frequently to errors, because the compile runs without errors. The problems occur only at runtime.

答案1

得分: 0

我可以翻译以下部分:

"I was able to solve the problem by analyzing the package Stub.System.Data.SQLite.Core.NetFramework. This package manages that the ContentFiles transitively end up in the target directory. This package manages that the contentFiles will transitively be placed in the target directory."

"通过分析包Stub.System.Data.SQLite.Core.NetFramework,我成功解决了这个问题。这个包确保ContentFiles以传递方式最终进入目标目录。这个包确保contentFiles以传递方式放置在目标目录中。"

"这是通过在目标文件中指定必要的复制操作来实现的。这个文件由MSBuild包含,并扩展了现有的csproj文件。"

"完整的targets文件可能如下所示:"

"这是targets文件的一部分,用于复制文件。在此之前,会对是否可以复制进行自我解释的测试。此Target在后期构建事件中执行。"

"这部分收集要复制的文件。这是由Visual Studio触发的。"

"此条目注册了后期构建事件,并触发了复制文件的过程。"

"当打开项目时,将确定要复制的文件。"

"这个targets文件确保文件被复制到目标目录。但要触发这一过程,文件的基本名称必须与NuGet id匹配,并且它必须位于build文件夹中。"

"为了使整个过程能够以传递方式工作,必须将此文件放置在buildTransitive目录中。"

"在我的情况下,适当的nuspec文件如下所示:"

"使用这个nuspectargets文件,contentFiles文件夹中的文件将以传递方式复制到目标目录。"

"也许有一个更简单的解决方案,但不幸的是我没有找到。"

"在包Stub.System.Data.SQLite.Core.NetFramework中定义了清理操作。这确保在clean期间从目标目录中删除文件。"

"但是,我们使用了多目标 (net472; net7.0),它们由Visual Studio并行构建。这导致在clean期间偶尔出现错误,因为文件正在被另一个进程使用。我们决定不允许clean,并且文件保留在目标目录中。"

英文:

I was able to solve the problem by analyzing the package Stub.System.Data.SQLite.Core.NetFramework. This package manages that the ContentFiles transitively end up in the target directory. This package manages that the contentFiles will transitively be placed in the target directory.

This is achieved by specifying the necessary copy operations in a targets file. This file is included by MSBuild and extends the existing csproj.

The full targets file might look like this:

&lt;Project xmlns=&quot;http://schemas.microsoft.com/developer/msbuild/2003&quot;&gt;

  &lt;Import Condition=&quot;&#39;$(MSBuildThisFileFullPath)&#39; != &#39;&#39; And
                     Exists(&#39;$(MSBuildThisFileFullPath).user&#39;)&quot;
          Project=&quot;$(MSBuildThisFileFullPath).user&quot; /&gt;

  &lt;ItemGroup&gt;
    &lt;CustomInteropFiles Condition=&quot;&#39;$(MSBuildThisFileDirectory)&#39; != &#39;&#39; And
                                   HasTrailingSlash(&#39;$(MSBuildThisFileDirectory)&#39;)&quot;
                        Include=&quot;$(MSBuildThisFileDirectory)..\..\contentFiles\any\any\**&quot; /&gt;
  &lt;/ItemGroup&gt;

  &lt;ItemGroup Condition=&quot;&#39;$(ContentCustomInteropFiles)&#39; != &#39;&#39; And
                        &#39;$(ContentCustomInteropFiles)&#39; != &#39;false&#39; And
                        &#39;@(CustomInteropFiles)&#39; != &#39;&#39;&quot;&gt;
    &lt;Content Include=&quot;@(CustomInteropFiles)&quot;&gt;
      &lt;Link&gt;%(RecursiveDir)%(FileName)%(Extension)&lt;/Link&gt;
      &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
    &lt;/Content&gt;
  &lt;/ItemGroup&gt;

  &lt;Target Name=&quot;CopyCustomInteropFiles&quot;
          Condition=&quot;&#39;$(CopyCustomInteropFiles)&#39; != &#39;false&#39; And
                     &#39;$(OutDir)&#39; != &#39;&#39; And
                     HasTrailingSlash(&#39;$(OutDir)&#39;) And
                     Exists(&#39;$(OutDir)&#39;)&quot;
          Inputs=&quot;@(CustomInteropFiles)&quot;
          Outputs=&quot;@(CustomInteropFiles -&gt; &#39;$(OutDir)%(RecursiveDir)%(Filename)%(Extension)&#39;)&quot;&gt;
    &lt;Copy SourceFiles=&quot;@(CustomInteropFiles)&quot;
          DestinationFiles=&quot;@(CustomInteropFiles -&gt; &#39;$(OutDir)%(RecursiveDir)%(Filename)%(Extension)&#39;)&quot; /&gt;
  &lt;/Target&gt;

  &lt;Target Name=&quot;CollectCustomInteropFiles&quot;
          Condition=&quot;&#39;$(CollectCustomInteropFiles)&#39; != &#39;false&#39;&quot;&gt;
    &lt;ItemGroup&gt;
      &lt;FilesForPackagingFromProject Include=&quot;@(CustomInteropFiles)&quot;&gt;
        &lt;DestinationRelativePath&gt;bin\%(RecursiveDir)%(Filename)%(Extension)&lt;/DestinationRelativePath&gt;
      &lt;/FilesForPackagingFromProject&gt;
    &lt;/ItemGroup&gt;
  &lt;/Target&gt;

  &lt;PropertyGroup&gt;
    &lt;PostBuildEventDependsOn&gt;
      $(PostBuildEventDependsOn);
      CopyCustomInteropFiles;
    &lt;/PostBuildEventDependsOn&gt;
    &lt;BuildDependsOn&gt;
      $(BuildDependsOn);
      CopyCustomInteropFiles;
    &lt;/BuildDependsOn&gt;
  &lt;/PropertyGroup&gt;

  &lt;PropertyGroup Condition=&quot;&#39;$(VisualStudioVersion)&#39; == &#39;&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;10.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;11.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;12.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;14.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;15.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;16.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;17.0&#39;&quot;&gt;
    &lt;PipelineCollectFilesPhaseDependsOn&gt;
      CollectCustomInteropFiles;
      $(PipelineCollectFilesPhaseDependsOn);
    &lt;/PipelineCollectFilesPhaseDependsOn&gt;
  &lt;/PropertyGroup&gt;
&lt;/Project&gt;

Explanation

&lt;Import Condition=&quot;&#39;$(MSBuildThisFileFullPath)&#39; != &#39;&#39; And
                     Exists(&#39;$(MSBuildThisFileFullPath).user&#39;)&quot;
        Project=&quot;$(MSBuildThisFileFullPath).user&quot; /&gt;

If the per-user settings file exists, import it now. The contained settings, if any, will override the default ones provided below.

  &lt;ItemGroup&gt;
    &lt;CustomInteropFiles Condition=&quot;&#39;$(MSBuildThisFileDirectory)&#39; != &#39;&#39; And
                                   HasTrailingSlash(&#39;$(MSBuildThisFileDirectory)&#39;)&quot;
                        Include=&quot;$(MSBuildThisFileDirectory)..\..\contentFiles\any\any\**&quot; /&gt;
  &lt;/ItemGroup&gt;

This determines the files to be copied and stores them on the variable CustomInteropFiles. It is important to know that all NuGet packages are unpacked in the local cache. This directory structure can be accessed. The variable MSBuildThisFileDirectory contains the directory in the local NuGet cache where this targets file is located.

In my example this targets file is located in the build folder of the NuGet package and further in the target platform folder. For this reason we need to go two directories up to access the contentFiles. In my case all files should be copied to the destination directory.

  &lt;ItemGroup Condition=&quot;&#39;$(ContentCustomInteropFiles)&#39; != &#39;&#39; And
                        &#39;$(ContentCustomInteropFiles)&#39; != &#39;false&#39; And
                        &#39;@(CustomInteropFiles)&#39; != &#39;&#39;&quot;&gt;
    &lt;Content Include=&quot;@(CustomInteropFiles)&quot;&gt;
      &lt;Link&gt;%(RecursiveDir)%(FileName)%(Extension)&lt;/Link&gt;
      &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
    &lt;/Content&gt;
  &lt;/ItemGroup&gt;

Here it is checked if per-user settings prevent copying and if there are files to copy. Then these files are linked and marked for copying.

  &lt;Target Name=&quot;CopyCustomInteropFiles&quot;
          Condition=&quot;&#39;$(CopyCustomInteropFiles)&#39; != &#39;false&#39; And
                     &#39;$(OutDir)&#39; != &#39;&#39; And
                     HasTrailingSlash(&#39;$(OutDir)&#39;) And
                     Exists(&#39;$(OutDir)&#39;)&quot;
          Inputs=&quot;@(CustomInteropFiles)&quot;
          Outputs=&quot;@(CustomInteropFiles -&gt; &#39;$(OutDir)%(RecursiveDir)%(Filename)%(Extension)&#39;)&quot;&gt;
    &lt;Copy SourceFiles=&quot;@(CustomInteropFiles)&quot;
          DestinationFiles=&quot;@(CustomInteropFiles -&gt; &#39;$(OutDir)%(RecursiveDir)%(Filename)%(Extension)&#39;)&quot; /&gt;
  &lt;/Target&gt;

This is the Target in the csproj file, which is used to copy the files. Before that, self-explanatory tests are made whether copying is possible or not. This Target is executed in the post-build event.

  &lt;Target Name=&quot;CollectCustomInteropFiles&quot;
          Condition=&quot;&#39;$(CollectCustomInteropFiles)&#39; != &#39;false&#39;&quot;&gt;
    &lt;ItemGroup&gt;
      &lt;FilesForPackagingFromProject Include=&quot;@(CustomInteropFiles)&quot;&gt;
        &lt;DestinationRelativePath&gt;bin\%(RecursiveDir)%(Filename)%(Extension)&lt;/DestinationRelativePath&gt;
      &lt;/FilesForPackagingFromProject&gt;
    &lt;/ItemGroup&gt;
  &lt;/Target&gt;

This part collects the files to be copied. This is triggered by Visual Studio.

  &lt;PropertyGroup&gt;
    &lt;PostBuildEventDependsOn&gt;
      $(PostBuildEventDependsOn);
      CopyCustomInteropFiles;
    &lt;/PostBuildEventDependsOn&gt;
    &lt;BuildDependsOn&gt;
      $(BuildDependsOn);
      CopyCustomInteropFiles;
    &lt;/BuildDependsOn&gt;
  &lt;/PropertyGroup&gt;

This entry registers the post-build event and triggers the process of copying the files.

  &lt;PropertyGroup Condition=&quot;&#39;$(VisualStudioVersion)&#39; == &#39;&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;10.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;11.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;12.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;14.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;15.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;16.0&#39; Or
                            &#39;$(VisualStudioVersion)&#39; == &#39;17.0&#39;&quot;&gt;
    &lt;PipelineCollectFilesPhaseDependsOn&gt;
      CollectCustomInteropFiles;
      $(PipelineCollectFilesPhaseDependsOn);
    &lt;/PipelineCollectFilesPhaseDependsOn&gt;
  &lt;/PropertyGroup&gt;

When the project is opened, the files to be copied are determined.

This targets file ensures that the files are copied to the target directory. But to trigger this, the base name of the file must match the NuGet id and it must be located in the build folder.

In order to make the whole thing work transitively, this file has to be placed in the buildTransitive directory.

In my case the appropriate nuspec file looks something like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;package xmlns=&quot;http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd&quot;&gt;
  &lt;metadata&gt;
    &lt;id&gt;*****&lt;/id&gt;
    &lt;version&gt;*****&lt;/version&gt;
    &lt;authors*****&lt;/authors&gt;
    &lt;requireLicenseAcceptance&gt;false&lt;/requireLicenseAcceptance&gt;
    &lt;license type=&quot;file&quot;&gt;LICENSE.txt&lt;/license&gt;
    &lt;projectUrl&gt;*****&lt;/projectUrl&gt;
    &lt;description&gt;*****&lt;/description&gt;
    &lt;copyright&gt;*****&lt;/copyright&gt;
    &lt;dependencies&gt;
      &lt;group targetFramework=&quot;.NETFramework4.7.2&quot; /&gt;
      &lt;group targetFramework=&quot;net7.0&quot; /&gt;
    &lt;/dependencies&gt;
    &lt;contentFiles&gt;
      &lt;files include=&quot;**&quot; buildAction=&quot;None&quot; copyToOutput=&quot;false&quot; flatten=&quot;false&quot; /&gt;
    &lt;/contentFiles&gt;
  &lt;/metadata&gt;
  &lt;files&gt;
    &lt;file src=&quot;.doc\**&quot; target=&quot;.doc&quot; /&gt;
    &lt;file src=&quot;contentFiles\**&quot; target=&quot;contentFiles&quot; /&gt;
    &lt;file src=&quot;build\**&quot; target=&quot;build\net472&quot; /&gt;
    &lt;file src=&quot;build\**&quot; target=&quot;build\net7.0&quot; /&gt;
    &lt;file src=&quot;build\**&quot; target=&quot;buildTransitive\net472&quot; /&gt;
    &lt;file src=&quot;build\**&quot; target=&quot;buildTransitive\net7.0&quot; /&gt;
    &lt;file src=&quot;..\_._&quot; target=&quot;lib\net472&quot; /&gt;
    &lt;file src=&quot;..\_._&quot; target=&quot;lib\net7.0&quot; /&gt;
    &lt;file src=&quot;licenses\LICENSE.txt&quot; target=&quot;&quot; /&gt;
  &lt;/files&gt;
&lt;/package&gt;

With this nuspec and targets file the files from the contentFiles folder are copied transitively to the target directory.

It might be possible that there is a simpler solution for this, but unfortunately I have not found it.

In the package Stub.System.Data.SQLite.Core.NetFramework a cleanup is defined. This ensures that the files are deleted from the target directory during a clean.

However, we use multi targeting (net472; net7.0), which are built in parallel by Visual Studio. This causes a sporadic error during a clean because the files are in use by another process. We have decided not to allow a clean and the files remain in the target directory.

huangapple
  • 本文由 发表于 2023年7月17日 22:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定