diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.GenerateAssemblyInfo.targets b/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.GenerateAssemblyInfo.targets index 948d8e5dd525..556b07e4128c 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.GenerateAssemblyInfo.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.GenerateAssemblyInfo.targets @@ -19,7 +19,6 @@ Copyright (c) .NET Foundation. All rights reserved. --> $(MSBuildAllProjects);$(MSBuildThisFileFullPath) - $(IntermediateOutputPath)$(MSBuildProjectName).AssemblyInfo$(DefaultLanguageSourceExtension) true @@ -48,10 +47,7 @@ Copyright (c) .NET Foundation. All rights reserved. DependsOnTargets="PrepareForBuild;GetAssemblyVersion;CoreGenerateAssemblyInfo" Condition="'$(GenerateAssemblyInfo)' == 'true'" /> - + <_Parameter1>$(Company) @@ -84,7 +80,29 @@ Copyright (c) .NET Foundation. All rights reserved. <_Parameter1>$(NeutralLanguage) + + + + + + + + + $(IntermediateOutputPath)$(MSBuildProjectName).AssemblyInfo.$(_AssemblyAttributeContentHash.SubString(0,6))$(DefaultLanguageSourceExtension) + $(IntermediateOutputPath)$(MSBuildProjectName).AssemblyInfo$(DefaultLanguageSourceExtension) + + + + diff --git a/test/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs b/test/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs index 866eaea0dc57..a2ecf63e0627 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections.Generic; +using System.Linq; using System.IO; using Microsoft.NET.TestFramework; using Microsoft.NET.TestFramework.Assertions; @@ -112,5 +113,56 @@ public void It_respects_version_prefix(string targetFramework) info["AssemblyFileVersionAttribute"].Should().Be("1.2.3.0"); info["AssemblyInformationalVersionAttribute"].Should().Be("1.2.3"); } + + [Theory] + [InlineData("netcoreapp1.1")] + [InlineData("net45")] + public void It_respects_version_changes_on_incremental_build(string targetFramework) + { + if (targetFramework == "net45" && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return; + } + + // Given a project that has already been built + var testAsset = _testAssetsManager + .CopyTestAsset("HelloWorld", identifier: targetFramework) + .WithSource() + .Restore("", $"/p:OutputType=Library;TargetFramework={targetFramework}"); + BuildProject(versionPrefix: "1.2.3"); + var fullBuildAssemblyInfo = FindAssemblyInfo(); + + // When the same project is built again using a different VersionPrefix proeprty + var incrementalBuildCommand = BuildProject(versionPrefix: "1.2.4"); + var incrementalBuildAssemblyInfo = FindAssemblyInfo(); + + // Then the version of the built assembly shall match the provided VersionPrefix + var assemblyPath = Path.Combine(incrementalBuildCommand.GetOutputDirectory(targetFramework).FullName, "HelloWorld.dll"); + var info = AssemblyInfo.Get(assemblyPath); + info["AssemblyVersionAttribute"].Should().Be("1.2.4.0"); + + // And the assembly info filename must have changed + incrementalBuildAssemblyInfo.Should().NotBe(fullBuildAssemblyInfo); + + // And the previous assembly info must have been deleted (by IncrementalClean) + File.Exists(fullBuildAssemblyInfo).Should().BeFalse(); + + BuildCommand BuildProject(string versionPrefix) + { + var command = new BuildCommand(Stage0MSBuild, testAsset.TestRoot); + command.Execute($"/p:OutputType=Library;TargetFramework={targetFramework};VersionPrefix={versionPrefix}") + .Should() + .Pass(); + return command; + } + + string FindAssemblyInfo() + { + var expectedAssemblyInfoLocation = Path.Combine(testAsset.TestRoot, "obj", "Debug", targetFramework); + var matches = Directory.EnumerateFiles(expectedAssemblyInfoLocation, "*AssemblyInfo*.cs", SearchOption.TopDirectoryOnly).ToList(); + matches.Count.Should().Be(1, "only a single assembly info file should exist"); + return matches[0]; + } + } } } diff --git a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs index 8434c3da33cf..713407e4d1c0 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs @@ -11,6 +11,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Xml.Linq; using Xunit; using static Microsoft.NET.TestFramework.Commands.MSBuildTest; @@ -561,7 +562,7 @@ void RemoveGeneratedCompileItems(List compileItems) // { AssemblyName}.AssemblyInfo.cs in the intermediate output path var itemsToRemove = compileItems.Where(i => i.EndsWith("AssemblyAttributes.cs", System.StringComparison.OrdinalIgnoreCase) || - i.EndsWith("AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase)) + Regex.IsMatch(i, ".*AssemblyInfo(.[0-9a-z]{6})?.cs$")) .ToList(); foreach (var itemToRemove in itemsToRemove)