Skip to content

Rebuilding project fails with duplicate generated AssemblyInfo #254

Closed

Description

@MattKotsenas

I'm not sure if this is an issue in this project, or in the SDK itself (or if I'm doing something wrong), so starting with an issue here to get your thoughts. Thanks in advance!

Repro steps

  1. Create a simple SDK-style project
  2. Add a reference to the NuGet API to allow restore
  3. Try building the project, should succeed
  4. Try building the project again, should succeed but fails

Here's a full repro case: https://github.com/MattKotsenas/repro-duplicate-assemblyinfo you should be able to just clone and dotnet test to repro the behavior.

Repro

Here's the interesting part of the repro:

public class UnitTest1 : MSBuildTestBase
{
    private readonly ITestOutputHelper _output;

    public UnitTest1(ITestOutputHelper output)
    {
        _output = output;
    }

    [Fact]
    public void Test1()
    {
        const string projectName = "ClassLibraryA";
        FileSystem fs = new();

        using (fs.CreateDisposableDirectory(out IDirectoryInfo temp))
        {
            Uri[] feeds = [ new Uri("https://api.nuget.org/v3/index.json") ];

            using (PackageRepository.Create(temp.FullName, feeds))
            {
                ProjectCreator projectCreator = ProjectCreator.Templates.SdkCsproj()
                    // Add this commented out code to prevent automatic generation of Assembly info and see the test pass.
                    //.Property("GenerateAssemblyInfo", "false")
                    .Save(Path.Combine(temp.FullName, projectName, $"{projectName}.csproj"));

                _output.WriteLine("Building for the first time:");
                projectCreator.TryBuild(restore: true, out bool firstResult, out BuildOutput firstOutput);
                Assert.True(firstResult, "This build should always succeed.");
                PrintAssemblyInfo(projectCreator);

                _output.WriteLine("Building for the second time:");
                projectCreator.TryBuild(restore: false, out bool secondResult, out BuildOutput secondOutput);
                PrintAssemblyInfo(projectCreator);
                Assert.True(secondResult, "This rebuild _should_ also succeed, but fails with duplicate version info.");
            }
        }
    }

    private void PrintAssemblyInfo(ProjectCreator projectCreator)
    {
        string objRelativePath = projectCreator.Project.GetPropertyValue("IntermediateOutputPath");
        string objFullPath = Path.GetFullPath(Path.Combine(projectCreator.Project.FullPath, "..", objRelativePath));

        string[] assemblyInfoFiles = Directory.GetFiles(objFullPath, "*AssemblyInfo.cs");

        if (assemblyInfoFiles.Length != 1)
        {
            throw new Exception($"Unable to determine AssemblyInfo.cs file. Found: {string.Join(", ", assemblyInfoFiles)}");
        }

        string contents = File.ReadAllText(assemblyInfoFiles.Single());

        _output.WriteLine("Contents of generated AssemblyInfo.cs:");
        _output.WriteLine(contents);
        _output.WriteLine("----------");
    }
}

Repro Output

Here's the output when I run the test:

 Repro.UnitTest1.Test1
   Source: UnitTest1.cs line 17
   Duration: 21.9 sec

  Message: 
This rebuild _should_ also succeed, but fails with duplicate version info.

  Stack Trace: 
UnitTest1.Test1() line 41
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

  Standard Output: 
Building for the first time:
Contents of generated AssemblyInfo.cs:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyTitleAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.


----------
Building for the second time:
Contents of generated AssemblyInfo.cs:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyTitleAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyCompanyAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyTitleAttribute("ClassLibraryA")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.


----------

Note that all the assembly attributes are duplicated.

Why it matters

In my case I'm authoring NuGet packages that add .props and .target files, and I wanted to build and rebuild to verify proper incremental build behavior. Today I'm working around the issue by setting GenerateAssemblyInfo to false in my test project.

Activity

  1. jeffkl commented on Nov 30, 2023

    @jeffkl
    Owner

    Thanks for filing this, I think I can fix it in #255

  2. added
    bug
    on Nov 30, 2023
  3. self-assigned this
    on Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions