TL;DR:
Having duplicated commented-out assembly attributes may cause output attribute duplication when using gitversion to update assembly infos. This causes build failures but the root cause is annoying to spot. Introduced when upgrading from 4.0.0 to 5.0.0.
Samples:
Initial AssemblyInfo.cs
using System.Reflection;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
gitversion.exe /updateassemblyinfo
Resulting AssemblyInfo.cs
using System.Reflection;
// [assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0-rc.1+Branch.release-2.0.0.Sha.some-sha-here")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0-rc.1+Branch.release-2.0.0.Sha.some-sha-here")]
If the rows are in inverted order, it works somewhat correctly.
Initial AssemblyInfo.cs
using System.Reflection;
[assembly: AssemblyVersion("1.0.*")]
// [assembly: AssemblyVersion("1.0.*")]
gitversion.exe /updateassemblyinfo
Resulting AssemblyInfo.cs
using System.Reflection;
[assembly: AssemblyVersion("2.0.0.0")]
// [assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0-rc.1+Branch.release-2.0.0.Sha.some-sha-here")]
This used to work correctly in 4.0.0, broke in 5.0.0. Seems that the problem was introduced in was commit 797d45d00439
I wrote a few tests to reproduce the issues. These should be in ./src/GitVersionCore.Tests/AssemblyInfoUpdaterTests.cs
[TestCase("cs", "// [assembly: AssemblyVersion(\"1.0.*\")]\r\n[assembly: AssemblyVersion(\"1.0.*\")]")]
[TestCase("cs", "[assembly: AssemblyVersion(\"1.0.*\")]\r\n// [assembly: AssemblyVersion(\"1.0.*\")]")]
public void ShouldNotTouchCommentedOutAttributes(string fileExtension, string assemblyFileContent)
{
var workingDir = Path.GetTempPath();
var assemblyInfoFile = "AssemblyInfo." + fileExtension;
var fileName = Path.Combine(workingDir, assemblyInfoFile);
VerifyAssemblyInfoFile(assemblyFileContent, fileName, AssemblyVersioningScheme.None, verify: (fileSystem, variables) =>
{
using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, false))
{
assemblyInfoFileUpdater.Update();
fileSystem.Received().WriteAllText(fileName, Arg.Is<string>(s =>
// The commented-out attribute should remain
s.Contains(@"// [assembly: AssemblyVersion(""1.0.*"")]") &&
s.Contains(@"AssemblyVersion(""2.3.0.0"")") &&
s.Contains(@"AssemblyInformationalVersion(""2.3.1+3.Branch.foo.Sha.hash"")") &&
s.Contains(@"AssemblyFileVersion(""2.3.1.0"")")));
}
});
}
[TestCase("cs", "// [assembly: AssemblyVersion(\"1.0.*\")]\r\n[assembly: AssemblyVersion(\"1.0.*\")]")]
[TestCase("cs", "[assembly: AssemblyVersion(\"1.0.*\")]\r\n// [assembly: AssemblyVersion(\"1.0.*\")]")]
public void ShouldNotDuplicateAttributesIfThereDuplicateAttributesCommentedOut(string fileExtension, string assemblyFileContent)
{
var workingDir = Path.GetTempPath();
var assemblyInfoFile = "AssemblyInfo." + fileExtension;
var fileName = Path.Combine(workingDir, assemblyInfoFile);
VerifyAssemblyInfoFile(assemblyFileContent, fileName, AssemblyVersioningScheme.None, verify: (fileSystem, variables) =>
{
using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFile, workingDir, variables, fileSystem, false))
{
assemblyInfoFileUpdater.Update();
fileSystem.Received().WriteAllText(fileName, Arg.Is<string>(s =>
new System.Text.RegularExpressions.Regex(@"\s+\[assembly: AssemblyVersion").Matches(s).Count == 1 &&
new System.Text.RegularExpressions.Regex(@"\s+\[assembly: AssemblyInformationalVersion").Matches(s).Count == 1 &&
new System.Text.RegularExpressions.Regex(@"\s+\[assembly: AssemblyFileVersion").Matches(s).Count == 1
));
}
});
}
@espu Can you create a PR with the failing test? Later we can have a look.
This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs. Thank you for your contributions.
@espu, can we please have a PR with the failling test?
@asbjornu, I'll try to take some time to create a PR. Missed the previous request by @arturcic, sorry about that.
O well, that was easy. PR created with failing tests. FYI @asbjornu.
This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs. Thank you for your contributions.
The issue is still present in 5.3.7. #2150 definitely didn't fix this
Are you able to provide a pull request with a failing test, @Pinolo?
Are you able to provide a pull request with a failing test, @Pinolo?
I don't have a dev environment set up at the moment (I was debugging a pipeline when I faced the bug). I will see if I can arrange something during weekend. BTW, isn't one of the open PRs in this issue's sidebar supposed to provide the needed tests?
@Pinolo, yes it looks like #2102 contains a failing test. Now we just need a fix. 馃槄
Most helpful comment
@asbjornu, I'll try to take some time to create a PR. Missed the previous request by @arturcic, sorry about that.