繁体   English   中英

如何将额外的 dll 添加到 Roslyn 分析仪 NuGet package?

[英]How to add additional dll to Roslyn analyzer NuGet package?

简而言之:我在代码分析器中引用了额外的 dll。 单元测试一切都很好,而我通过 F5 调试了分析器。 但是当这个分析仪安装为 NuGet package 到实际项目时,它找不到额外的 dll 和崩溃。

#可选阅读-问题的详细版本:

我写了我的代码分析器(比如MyAnalyzer )。 它发现一些禁止在客户端代码中使用的类型。 它的 codeFix 用我的自定义 dll (比如myCustom.dll )中允许的替换类型替换这些类型。 我已将此 dll 添加为对MyAnalyzer.CodeFixes项目和分析仪解决方案的MyAnalyzer.Test单元测试项目的依赖项。

当由 F5 启动(在特殊打开的 VisualStudio 实例上)并成功通过所有单元测试时,它工作正常。

但是当我:

  • built NuGet package of analyzer (by building MyAnalyzer.Package - a special project from VS template to produce analyzer NuGet packege)
  • 然后将其复制到 NuGet 本地提要(在 NuGet 管理器作为包源注册的本地路径)
  • 然后由 NuGet 经理将这个 NuGet package 从本地提要安装到实际项目中,

诊断结果很好,但是当我尝试使用 codefix 时,它应该用myCustom.dll中的某个类型替换某些类型,我的分析器找不到myCustom.dll并引发异常

System.IO.FileNotFoundException : Could not load file or assembly 'myCustom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

我可能以错误的方式添加对我的 dll 的引用(不使用分析仪)? 或者可能是(它只是我的假设)NuGet package 的任何依赖关系应该是 NuGet ZEFE90A8E604A7C840EZ8 太?

#附加信息(可能很重要):

构建 output 看起来不错:构建 output 的文件夹包含我引用的 dll。

我将它按原样复制到 NuGet 本地提要文件夹。

但是安装 package 后出现的已安装 package 的缓存文件夹不包含myCustom.dll 它仅包含(除了一些小文件):

C:\Users\user1\.nuget\packages\myanalyzer\0.0.1\MyAnalyzer.0.0.1.nupkg

& 2个dll:

C:\Users\user1\.nuget\packages\myanalyzer\0.0.1\analyzers\dotnet\cs\MyAnalyzer.dll C:\Users\user1\.nuget\packages\myanalyzer\0.0.1\analyzers\dotnet\cs\MyAnalyzer.CodeFixes.dll

当我尝试手动将我的 dll 放在那里 - 到 package cahe 文件夹时,分析器仍然抛出异常。

当我尝试手动将我的 dll 放在那里 - 到 package cahe 文件夹时,分析器仍然抛出异常。

那不是正确的方法。 该问题描述您在全局缓存下有一些错误的旧版本 nuget。 Since you do not change the new release version to another, the old wrong nuget package is the same as the new release one, nuget package manager always install the version under the global cache first and if it does not find the same version of the nuget package by nuget package manager UI , it will then download the specific, different version from the nuget package feed into global cache C:\Users\xxx\.nuget\packages\ , and then install it into your project.

您应该尝试以下步骤进行仔细检查:

1)在本地提要下检查您新包装的 nuget package myanalyzer 您可以使用7zip工具解压缩myanalyzer.0.0.1.nupkg

检查myCustom.dll是否在文件夹myanalyzer.0.0.1\analyzers\dotnet\cs\下。

另外,我发现错误是

Could not load file or assembly 'MyCustomDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

我很好奇为什么 dll 不正确。 错误是您使用了MyCustomDll.dll而不是MyCustom.dll 您应该检查您的分析器文件并确保使用正确的命名文件。

然后,重新打包新的 nuget 项目,确保新版本 nuget package 是对的。

2)删除C:\Users\xxx\.nuget\packages\myanalyzer下的缓存文件夹。

如果您使用packages.config nuget 管理格式,还应删除解决方案文件夹<solution_folder>\packages\myanalyzer.0.0.1下的文件夹。

3)之后,在package manager UI下重新安装正确的版本。

更新

抱歉,我忽略了您的分析仪 nuget 项目的打包步骤。

实际上,如果您当前的 nuget 项目引用了 dll,则打包按钮没有义务将这些 dll 自动打包到nupkg文件中。

你得手动打包,不管怎么做,都得修改csproj文件。

解决方案

使用您的目标或另一个是在myanalyzer.package.csproj文件下添加这些:

<ItemGroup>
        <None Include ="$(TargetDir)mycustom.dll" pack="true" PackagePath="analyzers/dotnet/cs"></None>
</ItemGroup>

然后,重新打包项目,清理 nuget 缓存,然后安装新的。

我想知道,为什么所有 dll 在安装的 NuGet package: 作为单独的文件在 packages\myanalyzer\0.0.1\analyzers\dotnet\cs 和作为存档 in.nupkg 文件的一部分出现两次。 或者安装的package文件夹的所有内容,除了.nupkg文件,只是解压了.nupkg?

nupkg是文件是您的 nuget package 的原始版本。 它由您的包 nuget 项目生成。 包含 nupkg 的文件夹是本地 nuget 提要。

在此处输入图像描述

该文件夹类似于nuget.org源。 And this folder which contains it is the nuget package source(nuget download nuget files from this path and then download the nupkg and unpack it under C:\Users\xxx\.nuget\packages )

此外C:\Users\xxx\.nuget\packages全局 nuget 缓存文件夹

在此处输入图像描述

So it restores the download nuget packages from the local feed, and if you installed the old same version of the myanalyzer nuget package before, it will always install the old same version like 1.0.0 from the cache folder no matter you have packed a new发布相同的1.0.0版本。 因为版本相同,所以VS IDE会判断缓存文件夹下已经存在相同的nuget package,它总是安装旧的而不是新的。

就是为什么我建议您应该删除该文件夹下的所有缓存文件。 避免 VS 一直安装旧的缓存包。 或者您应该为更新的 nuget package 设置一个新版本,例如2.0.0

是这两个文件夹之间的解释和区别。

一种是原装本地 nuget package 饲料。 另一个是从本地nuget package feed下载的package的存放路径和解包路径,它会记录之前下载的包。 你需要注意这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM