只有累積,沒有奇蹟

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 內容如下
  1. System.AggregateException: One or more errors occurred. ---> Microsoft.Assumes+InternalErrorException: File should not depend on itself.
  2. at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectFactory.Prefetcher.<LoadProjectAsync>d__20.MoveNext()
  3. --- End of stack trace from previous location where exception was thrown ---
  4. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  5. at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  6. at Microsoft.VisualStudio.Threading.JoinableTask.CompleteOnCurrentThread()
  7. at Microsoft.VisualStudio.Threading.JoinableTask`1.CompleteOnCurrentThread()
  8. at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectFactory.CreateProject(String fileName, String location, String name, UInt32 flags, Guid& projectIid, IntPtr& project, Int32& canceled)
  9. 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)
  10. --- End of inner exception stack trace ---
  11. ===================
從錯誤訊息中看不出可能異常的原因,由於專案是全新的剛剛才 commit 進版控,因此也無法從版控更改紀錄來確認是異動哪裡的 Code 造成異常, .csproj  主要是用來定義專案的內容,是由數組 XML 元素組合而成,像是此專案所用到的 TargetFramework 就會定義在  .csproj  中,因此遇到此情況只能逐一查看專案檔  .csproj  內的 XML 元素是否有異常而造成 Visual Studio 無法讀取,經過一段時間的抽絲剝繭之後發現問題在專案檔中的下列區塊
  1. <Project>
  2. <!--省略-->
  3. <ItemGroup>
  4. <None Update="appsettings.Development.yml">
  5. <DependentUpon>appsettings.yml</DependentUpon>
  6. <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  7. </None>
  8. <None Update="appsettings.Production.yml">
  9. <DependentUpon>appsettings.yml</DependentUpon>
  10. <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  11. </None>
  12. <None Update="appsettings.yml">
  13. <DependentUpon>appsettings.yml</DependentUpon>
  14. <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  15. </None>
  16. </ItemGroup>
  17. </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

Related Posts:

  • [UnitTest] Visual Studio 2017 按右鍵無法建立單元測試 ? 問題 最近心血來潮使用家中舊電腦小白寫 Code,在練習測試中發現竟然有點怪異,在要測試的 method 按下右鍵沒有 建立單元測試 Create Unit Test 選項,但相同練習專案拿到公司筆電就是正常的,經比對後發現舊筆電 Visual Studio 版本少安裝測試功能,以下簡單紀錄解決問題的過程 解決方案 在 Visual Studio 2017 早期版本這是已知問題,有開發者在 vs community 回報給開… Read More
  • [VS2017] Visual Studio 顯示代辦清單 - TODO List前言  最近在與同事討論問題時,主管看到代碼中有 // TODO 當下詢問同事 Visual Studio 該如何看到 TODO comment 清單,一問之下發現同事不太清楚 Visual Studio 該如何看到 TODO 清單,或許是因為不常用沒太多人知道或是不熟悉,自己也是因為之前上過 Will 保哥的課程才明白,這篇就簡單介紹在 Visual Studio 如何透過 Task List 找到 TODO co… Read More
  • [UnitTest] Visual Studio 2017 新增單元測試時沒有 NUnit 選項 ?前言 新筆電總是讓人充滿驚喜,在使用上除了要重新安裝常用的開發工具外,像是 Visual Studio & SQL Server..等必須工具,還有些常用的套件與小工具也要一併安裝,NUnit 是蠻常用的 Test Framwroek 之一,在寫測試時在要測試的 method 下按下右鍵 create Unit Tests,選擇要使用測試的 Test Framework 即可,在測試專案中預設只有 MSTest 選項,如果需要 NUnit… Read More
  • [VS2017] 提升 Resharper 在 Visual Studio IDE 執行速度前言  Visual Studio 是地表上最強的開發工具,最近發現在安裝完 Resharper 後開啟專案時都會遇到 載入專案的速度變慢的問題,Visual Studio 會在上方提示 JetBrains ReSharper Ultimate’ likely caused 8 seconds of unresponsiveness. Disabling it may improve your experi… Read More
  • [UnitTest] Reflect.cs not found in NUnit問題 最近在新的專案寫 UnitTest 過程中,執行完後 Visual Studio 卻跳出 Source Not Fount : Reflect.cs not found 錯誤,但單元測試還是可以成功,如下圖 且透過 debug UnitTest 時發現測試的值也沒問題,經搜尋後發現是 Visual Studio 2017 IDE 設定問題,將,這篇文章簡單紀錄處理問題過程 處理方式 釐清問題 按下… Read More

0 意見:

張貼留言

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

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com