dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.22k stars 1.35k forks source link

Directory.Build.props version tags not used correctly when multi-target building from VS2017 #3802

Open Ronzan opened 6 years ago

Ronzan commented 6 years ago

When building from Visual Studio 2017 (15.8.5), Directory.Build.props version tags are only used to generate assemblyinfo for first target when multi-targeting frameworks.

When having a project with multiple target frameworks, only the first target output assembly gets the correct version info when built from Visual Studio 2017. When building from the CLI with MSBuild, this works correctly and as expected.

I'm not sure if this is the right place to post this, if not, I appologize.

Update: With further investigation it seems to get a bit more confusing. After changing the Directory.Build.props file it fails like described. Restarting Visual Studio fixes it, until the next change to the props file. So it seems to be a caching issue. I now also see the same issue on our build server, where we use MSBuild, but there I'm not sure what to restart. I will continue to investigate this as a caching issue.

Steps to reproduce

Create a .NET Core Library project and setup multi-targeting, see project file below. Create a Directory.Build.props file one level above the project folder, see props file below. Build using Visual Studio 2017.

Directory.Build.props file

<Project>
...
<PropertyGroup>
  <Product>MyProduct</Product>
  <Company>MyCompany</Company>
  <Copyright>Copyright © 2018</Copyright>
  <Description>MyDescription</Description>

  <AssemblyVersion>8.8.5.0</AssemblyVersion>
  <FileVersion>8.8.50100.0</FileVersion>
  <InformationalVersion>Debug Build</InformationalVersion>
  <AssemblyTitle>Debug Build ($(TargetFramework)), ($(TargetFrameworkIdentifier))</AssemblyTitle>
</PropertyGroup>
...
</Project>

Project file

<Project Sdk="Microsoft.NET.Sdk">
...
<PropertyGroup>
  <OutputType>Library</OutputType>
  <TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
  <ApplicationIcon />
  <StartupObject />
  <AssemblyName>Namespace.MyAssembly</AssemblyName>
  <RootNamespace>Namespace.MyAssembly</RootNamespace>
  <LangVersion>latest</LangVersion>
  <OutputPath>..\bin\$(Configuration)</OutputPath>
  <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
...
</Project>

Expected behavior

Both output assemblies should have the same version info as specified in the Directory.Build.props file.

Actual behavior

Only the first output assembly generate for the first target listed in in the project file has the correct assembly version info.

The assemblyinfo.cs in the first target's obj folder is:

...
[assembly: System.Reflection.AssemblyCompanyAttribute("MyCompany")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyCopyrightAttribute("Copyright © 2018")]
[assembly: System.Reflection.AssemblyDescriptionAttribute("MyDescription")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("8.8.50100.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("Debug Build")]
[assembly: System.Reflection.AssemblyProductAttribute("MyProduct")]
[assembly: System.Reflection.AssemblyTitleAttribute("Debug Build (net472), ()")]
[assembly: System.Reflection.AssemblyVersionAttribute("8.8.5.0")]
...

The assemblyinfo.cs in the second target's obj folder is:

...
[assembly: System.Reflection.AssemblyCompanyAttribute("MyCompany")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyCopyrightAttribute("Copyright © 2018")]
[assembly: System.Reflection.AssemblyDescriptionAttribute("MyDescription")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("4.0.00100.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("Debug Build")]
[assembly: System.Reflection.AssemblyProductAttribute("MyProduct")]
[assembly: System.Reflection.AssemblyTitleAttribute("Debug Build (netcoreapp2.1), ()")]
[assembly: System.Reflection.AssemblyVersionAttribute("4.0.0.0")]
...

(Where this 4.0.00100.0 comes from I'm not quite sure, I did use that version at some point during testing.)

Everything works as expected if building from the CLI using MSBuild.

Environment data

msbuild /version output:

Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved.

15.8.169.51996

OS info: Visual Studio 2017 enterprise: 15.8.5+28010.2036 dotnet --version: 2.1.402

m00ners commented 4 years ago

same cache issue with vs2019 16.5.2

stagep commented 8 months ago

Same cache issue in VS2022 17.8.5 . In my scenario, it is the same target framework but a different OS. Windows for the local machine, Linux for deployment. I update Directory.Build.props and publish to an Azure Linux App Service. The updated Version is not used. If I close and restart Visual Studio before publishing then the updated Version is used.