Different nuget-packages are used when compiling using VS and compiling using MSBuild

Steven Degrendele 21 Reputation points
2021-01-28T19:11:55.103+00:00

Consider following .csproj file:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">  
  <PropertyGroup>  
    <OutputType>WinExe</OutputType>  
    <TargetFrameworks>netcoreapp3.1;net5.0-windows</TargetFrameworks>  
    <UseWPF>true</UseWPF>  
  </PropertyGroup>  
  
 <ItemGroup>  
 <PackageReference Include="Newtonsoft.Json" Version="12.0.3" Condition="'$(TargetFramework)' == 'net5.0-windows'"/>  
 <PackageReference Include="Newtonsoft.Json" Version="11.0.1" />  
 </ItemGroup>  
</Project>  

Except for the files generated by visual studio when creating the project, there are no other files in this project. The generated files (except the .csproj file) are not altered in any way.

61489-image.png

As you can see in the image, visual studio will - for both projects - reference the 11.0.1 version, and also build the executables using the 11.0.1 version.

When using the Command Line with MSBuild

<msbuildpath> <projectpath> /restore  

The executables will be generated with the 12.0.3 version for the .net5.0 target and with the 11.0.1 version for the other target(s)

When using nuget.exe /restore from the command line it will also generate the assets file to use 12.0.3 for .net5.0 and 11.0.1 for the other targets.

*As a side note, the problem does fix itself when using choose/when tags

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">  
  <PropertyGroup>  
    <OutputType>WinExe</OutputType>  
    <TargetFrameworks>netcoreapp3.1;net5.0-windows</TargetFrameworks>  
    <UseWPF>true</UseWPF>  
  </PropertyGroup>  
  
 <Choose>  
 <When Condition="'$(TargetFramework)' == 'net5.0-windows'">  
 <ItemGroup>  
 <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />  
 </ItemGroup>  
 </When>  
 <Otherwise>  
 <ItemGroup>  
 <PackageReference Include="Newtonsoft.Json" Version="11.0.1" />  
 </ItemGroup>  
 </Otherwise>  
 </Choose>  
</Project>  
  • No matter which build-method generates the correct output, i would expect for MSBuild and VS to generate the same output. Or am i wrong?
  • Why is there a difference between the packages using visual studio and nuget/msbuild?
  • Is this expected behavior or is this a bug?
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,464 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,395 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lex Li (Microsoft) 4,742 Reputation points Microsoft Employee
    2021-01-29T04:32:53.267+00:00

    Visual Studio has difficulty handling conditions in MSBuild scripts, which is known for years. Of course you can report to Microsoft as bugs, but I doubt it will be improved quick enough.

    BTW, it is also weird to use different versions of JSON.NET that way, as the latest version clearly works on both.


0 additional answers

Sort by: Most helpful