5

我正在研究源代码生成器,但我遇到了依赖问题:

It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Flurl.Http, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.'

有很多关于如何将依赖项打包到nuget中的信息,但我直接引用分析器项目是这样的:

<ProjectReference Include="SG.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />

在我添加的分析器项目<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>中,所有依赖项都在输出目录中可用,但 VS 没有使用该目录 - 它使用AppData\Local\Temp\VBCSCompiler\AnalyzerAssemblyLoader\[...]它并且只复制一个 DLL。

可以做些什么来完成这项工作?

4

3 回答 3

3

我找到了通过一些技巧使其或多或少可靠地工作的方法。

在此之前我也尝试过 ILMerge,但它不起作用(缺少方法异常)。

解决方案:

首先,我将依赖项嵌入到源生成器程序集中,如下所示:

<ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
    <EmbeddedResource Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" Visible="false" />
</ItemGroup>

然后我AssemblyResolveAppDomain(生成器类中的静态构造函数)创建了处理程序,如下所示:

AppDomain.CurrentDomain.AssemblyResolve += (_, args) =>
{
    AssemblyName name = new(args.Name);
    Assembly loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().FullName == name.FullName);
    if (loadedAssembly != null)
    {
        return loadedAssembly;
    }

    string resourceName = $"Namespace.{name.Name}.dll";

    using Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
    if (resourceStream == null)
    {
        return null;
    }
    
    using MemoryStream memoryStream = new MemoryStream();
    resourceStream.CopyTo(memoryStream);

    return Assembly.Load(memoryStream.ToArray());
};
于 2021-04-13T11:29:13.677 回答
1

应该工作的方式在source-generators cook-book中进行了概述,他们的例子是:

<Project>
  <PropertyGroup>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!-- Generates a package at build -->
    <IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency -->
  </PropertyGroup>

  <ItemGroup>
    <!-- Take a private dependency on Newtonsoft.Json (PrivateAssets=all) Consumers of this generator will not reference it.
         Set GeneratePathProperty=true so we can reference the binaries via the PKGNewtonsoft_Json property -->
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" PrivateAssets="all" GeneratePathProperty="true" />

    <!-- Package the generator in the analyzer directory of the nuget package -->
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

    <!-- Package the Newtonsoft.Json dependency alongside the generator assembly -->
    <None Include="$(PkgNewtonsoft_Json)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>
</Project>

但是,根据我的经验,这仍然不可靠或不可靠;这是公认的,我的印象是改善这种体验是未来计划的一部分,但是:老实说,现在最好不要使用核心框架和已经加载的 Roslyn 库之外的依赖项。如果您的依赖项与分析器/生成器位于同一存储库中,您可能可以在构建期间简单地吸收代码,例如从这里

<ItemGroup>
    <!-- compile what we need from protobuf-net directly; package refs cause pure pain in anaylizers-->
    <Compile Include="../protobuf-net.Core/**/*.cs" Link="protobuf-net.Core"/>
    <Compile Remove="../protobuf-net.Core/obj/**/*.cs" />
    <Compile Include="../protobuf-net.Reflection/**/*.cs"  Link="protobuf-net.Reflection"/>
    <Compile Remove="../protobuf-net.Reflection/obj/**/*.cs" />
</ItemGroup>
于 2021-04-13T08:46:26.483 回答
0

现在有一个明确的答案。Source Generator Samples包含一个.csproj具有所需设置的示例。

我用它用Sprache制作了一个源代码生成器,效果很好。

在此处粘贴示例.csproj以供参考:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftNetCompilersToolsetVersion)" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
    <!-- Generator dependencies -->
    <PackageReference Include="CsvTextFieldParser" Version="1.2.2-preview" GeneratePathProperty="true" PrivateAssets="all" />
    <PackageReference Include="Handlebars.Net" Version="1.10.1" GeneratePathProperty="true" PrivateAssets="all" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
    <GetTargetPathDependsOn>$(GetTargetPathDependsOn)</GetTargetPathDependsOn>;GetDependencyTargetPaths
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PKGCsvTextFieldParser)\lib\netstandard2.0\CsvTextFieldParser.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PKGHandlebars_Net)\lib\netstandard2.0\Handlebars.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
    </ItemGroup>
</Target>
</Project>
于 2021-12-06T09:24:39.033 回答