Search code examples
c#cakebuild

The Cake GetVersion - Duplicate 'AssemblyInformationalVersion' attribute


For some reason, there is an error during cakebuild.net task execution.

The root cause of error is UpdateAssemblyInfo = true property. It's look like attribute duplication happens.

But it's not obvious why it happens for me. Could you pls, reveal such kind of behavior

Prerequisites:

  • Net 4.7.2

  • Not AssemblyInformationalVersion attribute in .csproj

  • Not AssemblyInformationalVersion attribute in Properties\AssemblyInfo

    var gitVersion = GitVersion(new GitVersionSettings
                 {
                     OutputType = GitVersionOutput.Json,
                     NoFetch = false,
                     UpdateAssemblyInfo = true
                 });
    

Error:

Properties\AssemblyInfo.cs(41,12): error CS0579: Duplicate 'AssemblyInformationalVersion' attribute


Solution

  • For SDK style projects AssemblyInfo will always be generated (unless <GenerateAssemblyInfo>false</GenerateAssemblyInfo> is set). The generated AssemblyInfo will contain some version information, regardless whether you set <AssemblyInformationalVersion> explicitly, or not.

    So: Setting UpdateAssemblyInfo=true in GitVersion creates an AssemblyInfo, and your csproj also creates one. Hence, the error.

    What you can do: Get the version and set build properties accordingly, so the generated AssemblyInfo contains the information you want it to.

    Task("Build")
    .Does(() => {
       // get version
       var gitVersion = GitVersion();
       var version = gitVersion.SemVer; // or something other...
       Information($"Building version: {version}");
    
       // add version to settings
       var settings = new DotNetBuildSettings();
       settings.MSBuildSettings = new DotNetCoreMSBuildSettings();
       settings.MSBuildSettings.Properties.Add("AssemblyVersion", new[] { version });
       settings.MSBuildSettings.Properties.Add("AssemblyFileVersion", new[] { version });
       settings.MSBuildSettings.Properties.Add("AssemblyInformationalVersion", new [] { version });
       settings.MSBuildSettings.Properties.Add("Version", new [] { version });
    
       // build
       DotNetBuild("./console/console.csproj", settings);
    });