2

This is related to this question.

I've got a packaged source generator which requires YamlDotNet to be bundled as a private asset for the analyzer. To achieve this, I have this 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>

Now, if I just build the project with dotnet pack -c Release it seems as though everything I'd expect to be included, is:

❯ 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

However, if I build and pack with dotnet build -c Release && dotnet pack -c Release -p:PackageVersion=0.0.2 I find that these dlls have been stripped out:

❯ 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

Now, thanks to the answer on the above linked question, I at least know that this is happening and I can work around it by just relying on GeneratePackageOnBuild but my real question is:

  1. Why is this generating two different packages?
  2. How can I make dotnet pack include the private assets?

(And yes, I know source generators technically should target netstandard 2.0 but that's a different matter and I have my reasons for ignoring that)

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • the answer to why pack is different to build is that your target in the csproj says `AfterTargets="ResolvePackageDependenciesForBuild"`. If that target only runs as part of build, not pack, as the name suggests, then your custom target is adding the ` – zivkan Jun 25 '23 at 20:08
  • 1
    In case you don't already know, MSBuild debugging involves adding `-bl` to any msbuild command (like build or pack), and it will write msbuild.binlog, which can be viewed with https://msbuildlog.com. MSbuild doesn't have private targets, so anything starting with an underscore should be considered private that you shouldn't use. Otherwise, you can see what targets were run, in what order, and what items and properties were created. You can use this info to figure out how to rewrite the `GetDependencyTargetPaths` target to work in pack. – zivkan Jun 25 '23 at 20:13

0 Answers0