只有累積,沒有奇蹟

2019年10月1日 星期二

[VisualStudio] VisualStudio 2019 開啟專案失敗 InternalErrorException: File should not depend on itself.

問題 
今天在公司與同事共同開發專案時發生離奇的問題,同事反映要使用 Visual Studio 開啟版控的專案時發生 發生異常,Visual Studio 顯示錯誤訊息 'System.AggregateException: One or more errors occurred. ---> Microsoft.Assumes+InternalErrorException: File should not depend on itself. 這篇文章就針對此問題的解決方式做分享若是有不清楚或是錯誤的地方歡迎討論予糾正

案發現場
當使用 Visual Studio 開啟專案時會無法正常開啟,異常發生時畫面如下
點選確定之後,可以看到異常的方案檔在這時候出現 unloaded 的字眼出現,接著整個專案檔內容也因為讀取失敗,而無法看到專案檔底下的資訊,如下圖所示
接著開啟 log 檔案中可以看到加載失敗的錯誤訊息資訊,Log 內容如下
  System.AggregateException: One or more errors occurred. ---> Microsoft.Assumes+InternalErrorException: File should not depend on itself.
   at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectFactory.Prefetcher.<LoadProjectAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Threading.JoinableTask.CompleteOnCurrentThread()
   at Microsoft.VisualStudio.Threading.JoinableTask`1.CompleteOnCurrentThread()
   at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectFactory.CreateProject(String fileName, String location, String name, UInt32 flags, Guid& projectIid, IntPtr& project, Int32& canceled)
   at Microsoft.VisualStudio.Shell.Flavor.FlavoredProjectFactoryBase.Microsoft.VisualStudio.Shell.Interop.IVsProjectFactory.CreateProject(String fileName, String location, String name, UInt32 flags, Guid& projectGuid, IntPtr& project, Int32& canceled)
   --- End of inner exception stack trace ---
=================== 
從錯誤訊息中看不出可能異常的原因,由於專案是全新的剛剛才 commit 進版控,因此也無法從版控更改紀錄來確認是異動哪裡的 Code 造成異常, .csproj  主要是用來定義專案的內容,是由數組 XML 元素組合而成,像是此專案所用到的 TargetFramework 就會定義在  .csproj  中,因此遇到此情況只能逐一查看專案檔  .csproj  內的 XML 元素是否有異常而造成 Visual Studio 無法讀取,經過一段時間的抽絲剝繭之後發現問題在專案檔中的下列區塊
<Project>
  <!--省略-->
  <ItemGroup>
    <None Update="appsettings.Development.yml">
      <DependentUpon>appsettings.yml</DependentUpon>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Production.yml">
      <DependentUpon>appsettings.yml</DependentUpon>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.yml">
      <DependentUpon>appsettings.yml</DependentUpon>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>
可以看到在上述 ItemGroup 區塊中定義三個項目,主要用意是希望設定檔 appsettings 將前墜字串相同但環境不同的 Config 整合再一起,在 Visual Studio IDE 統一顯示在一個區塊中才不會在散落各地,因此在這使用  DependentUpon  屬性來定義所依賴的檔案,但也可以發現造成無法開啟專案檔的兇手在此,造成異常的原因為 appsettings.yml 內的 DependentUpon 屬性定義依賴的檔案為自己,在比對一開始錯誤訊息內容 File should not depend on itself.,就瞬間恍然大悟秒懂錯誤訊息所表達的意思 (在沒發現之前很難意會 XD),了解原因之後修復的動作也很簡單,將 appsettings.yml 中的 DependentUpon 移除,並 reload Project 就可以正常開啟專案,正常畫面如下
解決完畢宣告除蟲大成功 !!!

參考
Common MSBuild project items

0 意見:

張貼留言

Copyright © m@rcus 學習筆記 | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com