在 Visual Studio 2019 想將 .NET Framework 專案設定版號自動跳號時卡住。

AssemblyInfo.cs [assembly: AssemblyVersion()] 上方的註解明明還提示可以寫成 1.0.* 讓系統自動跳號產生第三及第四段的 Build 及 Revison 號碼,卻冒出紅蚯蚓,顯示「The specified version string contains wildcards, which are not comptaible with determinism. Either remove wildcards from the version string, or disable determinism for this compilation.」錯誤。

註:若對 .NET Assembly 版號應用還不熟悉,推薦兩篇舊文:

先說一下 Determinism。一般 .NET 在編譯組件時會摻入 Timestamp 及隨機產生的 GUID,故每次產生的組件在二進位內容上一定會有一些 Byte 不同。而 .NET 編譯器有個 -deterministic 模式,改以原始碼內容、工作路徑... 等條件為依據,確保相同程式碼在相同環境編譯出來的組件二進位內容完全一致。這有助於識別組件是否來自可信任來源(例如:配合雜湊碼驗證),或是某些比對組件二進位差異決定編譯步驟的連續編譯系統。

由此不難想見,組件版號自動跳號會讓 Determinism 破功,而自 2018/4 起,.csproj 的 Deterministic 選項預設被設成 true,而 Visual Studio 沒提供 GUI 操作介面修改它,要調整需手工修改 .csproj:

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{0749D430-8CE3-45FD-8E83-B7DD4936FB85}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>ConsoleApp1</RootNamespace>
    <AssemblyName>ConsoleApp1</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>false</Deterministic>
  </PropertyGroup>

參考資料:The specified version string contains wildcards, which are not compatible with determinism

設定 <Deterministic>false</Deterministic> 後,就可以填入 2.0.* 並可編譯。順便練習用 PowerShell 檢查版號,驗證自動跳號成功。

[System.Reflection.Assembly]::LoadFile("$(pwd)\Foo.dll").GetName().Version

後話:開發社群一致覺得註解提示與結果矛盾、錯誤訊息過於生澀以及缺乏 GUI 介面調整讓這段使用體驗不佳,但這畢竟是個冷門問題,應該就是這樣了,遇上能快速找到解法就成了。

About VS2019's "The specified version string contains wildcards, which are not comptaible with determinism. Either remove wildcards from the version string, or disable determinism for this compilation." issue.


Comments

Be the first to post a comment

Post a comment