簡體   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