简体   繁体   English

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

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

In short : I referenced additional dll in my code-analyzer.简而言之:我在代码分析器中引用了额外的 dll。 All good with unit-tests & while I debugged analyzer by F5.单元测试一切都很好,而我通过 F5 调试了分析器。 But when this analyzer was installed as NuGet package to real project, it can't find that additional dll & crashes.但是当这个分析仪安装为 NuGet package 到实际项目时,它找不到额外的 dll 和崩溃。

#Optional reading - detailed version of question: #可选阅读-问题的详细版本:

I've wrote my code-analyzer (say MyAnalyzer ).我写了我的代码分析器(比如MyAnalyzer )。 It finds some types forbidden to use in client code.它发现一些禁止在客户端代码中使用的类型。 And its codeFix replaces these types by allowed replacement-types from my custom dll (say myCustom.dll ).它的 codeFix 用我的自定义 dll (比如myCustom.dll )中允许的替换类型替换这些类型。 I've added this dll as dependency to MyAnalyzer.CodeFixes project & to MyAnalyzer.Test unit tests project of analyzer solution.我已将此 dll 添加为对MyAnalyzer.CodeFixes项目和分析仪解决方案的MyAnalyzer.Test单元测试项目的依赖项。

And it works fine when started by F5 (at special opened VisualStudio instance) & successfully passes all unit tests.当由 F5 启动(在特殊打开的 VisualStudio 实例上)并成功通过所有单元测试时,它工作正常。

But when I:但是当我:

  • built NuGet package of analyzer (by building MyAnalyzer.Package - a special project from VS template to produce analyzer NuGet packege) built NuGet package of analyzer (by building MyAnalyzer.Package - a special project from VS template to produce analyzer NuGet packege)
  • then coppied it to NuGet local feed (local path registered at NuGet manager as packages source)然后将其复制到 NuGet 本地提要(在 NuGet 管理器作为包源注册的本地路径)
  • & then install this NuGet package from that local feed to real project by NuGet manager,然后由 NuGet 经理将这个 NuGet package 从本地提要安装到实际项目中,

diagnostics rise fine, but when I try use codefix, which should replace some type by one from that myCustom.dll , my analyzer can't find myCustom.dll & throws an exception :诊断结果很好,但是当我尝试使用 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.

I probably add reference to my dll at wrong way (not working with analyzers)?我可能以错误的方式添加对我的 dll 的引用(不使用分析仪)? Or may be (its only my assumption) any dependency of NuGet package should be NuGet package too?或者可能是(它只是我的假设)NuGet package 的任何依赖关系应该是 NuGet ZEFE90A8E604A7C840EZ8 太?

#Additional info (may be it matters): #附加信息(可能很重要):

Build output looks Ok: folder with build output contains that my referenced dll.构建 output 看起来不错:构建 output 的文件夹包含我引用的 dll。

I copied it as is to NuGet local feed folder.我将它按原样复制到 NuGet 本地提要文件夹。

But cache folder of installed package, which appears after installing package, doesn't contain that myCustom.dll .但是安装 package 后出现的已安装 package 的缓存文件夹不包含myCustom.dll It contains (except for a few small files) only:它仅包含(除了一些小文件):

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

& 2 dlls: & 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 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

And when I try to manually put my dll there - to package cahe folder, analyzer still throws exception.当我尝试手动将我的 dll 放在那里 - 到 package cahe 文件夹时,分析器仍然抛出异常。

And when I try to manually put my dll there - to package cahe folder, analyzer still throws exception.当我尝试手动将我的 dll 放在那里 - 到 package cahe 文件夹时,分析器仍然抛出异常。

That is not a right way.那不是正确的方法。 The issue describes that you have some wrong old version of the nuget under the global cache.该问题描述您在全局缓存下有一些错误的旧版本 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. 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.

You should try the following steps to make a careful check:您应该尝试以下步骤进行仔细检查:

1) check your new packed nuget package myanalyzer under the local feed. 1)在本地提要下检查您新包装的 nuget package myanalyzer And you can use 7zip tool to unpack myanalyzer.0.0.1.nupkg .您可以使用7zip工具解压缩myanalyzer.0.0.1.nupkg

Check whether the myCustom.dll is under the folder myanalyzer.0.0.1\analyzers\dotnet\cs\ .检查myCustom.dll是否在文件夹myanalyzer.0.0.1\analyzers\dotnet\cs\下。

Also , I found the error is另外,我发现错误是

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

I am curious why the dll is not right.我很好奇为什么 dll 不正确。 And the error is that you have used MyCustomDll.dll rather than MyCustom.dll .错误是您使用了MyCustomDll.dll而不是MyCustom.dll You should check your Analyzers file and make sure that use the right named file.您应该检查您的分析器文件并确保使用正确的命名文件。

Then , repack the new nuget project, ensure the new release nuget package is right.然后,重新打包新的 nuget 项目,确保新版本 nuget package 是对的。

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

If you use packages.config nuget management format, and you should also delete the folder under your solution folder <solution_folder>\packages\myanalyzer.0.0.1 .如果您使用packages.config nuget 管理格式,还应删除解决方案文件夹<solution_folder>\packages\myanalyzer.0.0.1下的文件夹。

3) after that, reinstall the right version under package manager UI . 3)之后,在package manager UI下重新安装正确的版本。

Update更新

Sorry for that I ignored the pack steps of your analyzer nuget project.抱歉,我忽略了您的分析仪 nuget 项目的打包步骤。

Actually , if the dlls are referenced by your current nuget project, the pack button has no duty to pack these dlls into the nupkg file automatically.实际上,如果您当前的 nuget 项目引用了 dll,则打包按钮没有义务将这些 dll 自动打包到nupkg文件中。

You have to pack it manually, no matter how you did, you have to modify the csproj file.你得手动打包,不管怎么做,都得修改csproj文件。

Solution解决方案

Use your target or another is to add these under myanalyzer.package.csproj file:使用您的目标或另一个是在myanalyzer.package.csproj文件下添加这些:

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

Then , repack the project, clean the nuget caches, then install the new one.然后,重新打包项目,清理 nuget 缓存,然后安装新的。

I wonder, why all dlls are present twice at installed NuGet package: as separate files at packages\myanalyzer\0.0.1\analyzers\dotnet\cs & as part of archive in.nupkg file.我想知道,为什么所有 dll 在安装的 NuGet package: 作为单独的文件在 packages\myanalyzer\0.0.1\analyzers\dotnet\cs 和作为存档 in.nupkg 文件的一部分出现两次。 Or all content of installed package folder, except.nupkg file, is just unpacked.nupkg?或者安装的package文件夹的所有内容,除了.nupkg文件,只是解压了.nupkg?

The nupkg is the file is the original version of your nuget package. nupkg是文件是您的 nuget package 的原始版本。 It is generated by your pack nuget project.它由您的包 nuget 项目生成。 And the folder which contains the nupkg is a local nuget feed.包含 nupkg 的文件夹是本地 nuget 提要。

在此处输入图像描述

The folder is like nuget.org source.该文件夹类似于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 ) 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 )

Besides , the C:\Users\xxx\.nuget\packages is the global nuget cache folder .此外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 release same 1.0.0 version. 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版本。 Because the version is the same, so VS IDE will judge that there is already has a same nuget package under the cache folder, it always install the old one rather than the new one.因为版本相同,所以VS IDE会判断缓存文件夹下已经存在相同的nuget package,它总是安装旧的而不是新的。

So that is why I recommended that you should delete all cache files under that folder.就是为什么我建议您应该删除该文件夹下的所有缓存文件。 Avoid VS installing older cache packages all the time.避免 VS 一直安装旧的缓存包。 Or you should set a new version for the updated nuget package like 2.0.0 .或者您应该为更新的 nuget package 设置一个新版本,例如2.0.0

This is the explanation and the difference between these two Folders.是这两个文件夹之间的解释和区别。

One is the original local nuget package feed.一种是原装本地 nuget package 饲料。 Another is the storage path and unpacking path of the package downloaded from the local nuget package feed, and it will record the previously downloaded packages.另一个是从本地nuget package feed下载的package的存放路径和解包路径,它会记录之前下载的包。 You need to pay attention to this.你需要注意这一点。

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

相关问题 如何将现有的用于CL的NuGet软件包与Roslyn分析仪结合使用? - How do I combine an existing NuGet package for a CL with a Roslyn analyzer? Roslyn 分析器 package 的 NuGet 依赖项是否不可能存在? - Is it impossible to have NuGet-dependencies at .nuspec of a Roslyn analyzer package? 如何将其他文件添加到 nuget package 并在 nuget ZEFE90A8E604A7C67D803A 代码中引用这些文件? - How to add additional files to a nuget package and reference those files within the nuget package code? 如何在NuGet包中使用其他文件? - How to use additional files in NuGet package? 如何使用 Stylecop Analyzer 和自定义规则集创建 nuget 包? - How to create nuget package with Stylecop Analyzer and custom ruleset? Nuget Package Explorer没有为引用添加* .dll(不是* .Resources.dll) - Nuget Package Explorer didnt add a *.dll (not *.Resources.dll) to references 如何在 Visual Studio 2017 中向 NuGet 包添加其他文件? - How do you add additional files to a NuGet package in Visual Studio 2017? 如何从Nuget包创建.dll? - How to create .dll from Nuget package? 如何在没有 nuget 或 VSIX 的情况下将自定义代码分析器添加到项目中? - How to add a custom code analyzer to a project without nuget or VSIX? 如何将标签添加到 nuget package? - How to add tags to a nuget package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM