You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何dotnet build与dotnet pack生成的nupkg包不一致?

源代码生成器NuGet打包依赖缺失问题

我有一个打包为NuGet包的源代码生成器,需将YamlDotNet作为私有资产捆绑到分析器中,对应的csproj配置如下:

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

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <LangVersion>default</LangVersion>
        <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        <Version>0.0.1-alpha</Version>
        <Title>SuperFluid</Title>
        <Description>An incremental source generator for fluent APIs with grammar</Description>
        <PackageProjectUrl>https://github.com/hughesjs/SuperFluid</PackageProjectUrl>
        <PackageLicenseUrl>https://github.com/hughesjs/SuperFluid/blob/master/LICENSE</PackageLicenseUrl>
        <RepositoryUrl>https://github.com/hughesjs/SuperFluid.git</RepositoryUrl>
        <RepositoryType>git</RepositoryType>
        <PackageReadmeFile>./README.md</PackageReadmeFile>
        <IncludeBuildOutput>false</IncludeBuildOutput>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    </PropertyGroup>

    <ItemGroup>
        <None Include="../../README.md" Pack="true" PackagePath="\" />
    </ItemGroup>

    <ItemGroup>
        <InternalsVisibleTo Include="SuperFluid.Tests" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
        <PackageReference Include="YamlDotNet" Version="13.1.0" PrivateAssets="all" GeneratePathProperty="true" />
    </ItemGroup>
    
    <!-- Gross hack to let source generator use nuget packages -->
    <PropertyGroup>
        <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
    </PropertyGroup>
    
    <Target Name="GetDependencyTargetPaths" AfterTargets="ResolvePackageDependenciesForBuild">
            <ItemGroup>
                <TargetPathWithTargetPlatformMoniker Include="@(ResolvedCompileFileDefinitions)" IncludeRuntimeDependency="false" />
                <None Include="@(ResolvedCompileFileDefinitions)" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
            </ItemGroup>
    </Target>
    <!-- End Hack -->
    
    
    <ItemGroup>
        <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>
</Project>

使用命令dotnet pack -c Release生成的nupkg包包含所有预期的依赖DLL:

❯ unzip -l SuperFluid.0.0.1-alpha.nupkg                                                                                                   ─╯
Archive:  SuperFluid.0.0.1-alpha.nupkg
  Length      Date    Time    Name
---------  ---------- -----   ----
      500  2023-06-25 19:03   _rels/.rels
      720  2023-06-25 19:03   SuperFluid.nuspec
     1290  2023-06-04 19:59   README.md
    26112  2023-06-25 17:57   analyzers/dotnet/cs/SuperFluid.dll
  2828968  2022-11-12 03:59   analyzers/dotnet/cs/Microsoft.CodeAnalysis.dll
  6145664  2022-11-12 04:10   analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.dll
   190592  2021-10-22 23:39   analyzers/dotnet/cs/System.Collections.Immutable.dll
   462720  2020-10-19 18:45   analyzers/dotnet/cs/System.Reflection.Metadata.dll
    18024  2021-10-22 23:38   analyzers/dotnet/cs/System.Runtime.CompilerServices.Unsafe.dll
   756864  2021-10-22 23:46   analyzers/dotnet/cs/System.Text.Encoding.CodePages.dll
   265728  2023-04-16 20:31   analyzers/dotnet/cs/YamlDotNet.dll
      520  2023-06-25 19:03   [Content_Types].xml
      670  2023-06-25 19:03   package/services/metadata/core-properties/1567cf9005dc47bcab15315a2e2084cf.psmdcp
---------                     -------
 10698372                     13 files

但使用命令dotnet build -c Release && dotnet pack -c Release -p:PackageVersion=0.0.2生成的nupkg包中,这些依赖DLL被剥离:

❯ unzip -l SuperFluid.0.0.2.nupkg                                                                                                         ─╯
Archive:  SuperFluid.0.0.2.nupkg
  Length      Date    Time    Name
---------  ---------- -----   ----
      500  2023-06-25 19:05   _rels/.rels
      714  2023-06-25 19:05   SuperFluid.nuspec
     1290  2023-06-04 19:59   README.md
    26112  2023-06-25 17:57   analyzers/dotnet/cs/SuperFluid.dll
      520  2023-06-25 19:05   [Content_Types].xml
      664  2023-06-25 19:05   package/services/metadata/core-properties/49ced3920e7d47ff9d9c3ab4a107dc77.psmdcp
---------                     -------
    29800                     6 files

目前我可通过GeneratePackageOnBuild规避此问题,但仍有两个疑问:

  • 为何两种方式生成的nupkg包不一致?
  • 如何让dotnet pack命令包含私有资产?

(注:我知晓源代码生成器理论上应针对netstandard 2.0,但此为另一问题,我有自己的考量)

内容的提问来源于stack exchange,提问作者ScottishTapWater

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:24:59