Home: Directory.Build.props file with PackageReference incorrectly attempts install into non-compatible projects

Created on 26 Feb 2019  路  2Comments  路  Source: NuGet/Home

Details about Problem

NuGet product used:
MSBuild

NuGet version:
4.6.0

VS version:
15.9.6

Repro

  1. Create a solution with new format C# csproj and an old format C++ static library
  2. Add a Directory.Build.props file with a PackageReference to a nuget package which is not compatible with C++ projects, e.g. <PackageReference Include="StyleCop.Analyzers" Version="1.1.1-rc.108" PrivateAssets="all" />
  3. Build fails using MSBuild from the command line, error:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets(186,5): 
error : Your project does not reference ".NETFramework,Version=v4.0" framework. 
Add a reference to ".NETFramework,Version=v4.0" in the "TargetFrameworks" property of your 
project file and then re-run NuGet restore. [C:\Git\src\github.com\xxx\xxx.vcxproj]

Expected outcome

That nuget would only install packages into projects that are compatible.

For my own reference, I suspect there is some kind of workaround whereby I wrap the PackageReference in some kind of condition to prevent it being applied to C++ projects, something like this (I havent found the right runes yet)

<Project>
  <Choose>
    <When Condition=" '$(CustomCondition)' == 'true' ">
      <ItemGroup>
        <PackageReference Include="StyleCop.Analyzers" Version="1.1.1-rc.108" PrivateAssets="all" />
      </ItemGroup>
    </When>
  </Choose>
</Project>

(Referenced from https://github.com/dotnet/project-system/issues/4175)

Question

Most helpful comment

Some properties, like TargetFramework are defined by the SDK after Directory.Build.props are imported. Here are some alternate ideas:

  • Use a condition like '$(MSBuildProjectExtension)' == '.csproj'
  • Define the PackageReference in Directory.Build.targets in order to use properties such as TargetFramework.
  • I believe that MSBuild only imports the first Directory.Build.Props it finds, not all of them, so create an empty Directory.Build.props file in the same directory as any project that doesn't support PackageReference

All 2 comments

Directory.build.props is imported into all files.
Yes, you'll have to own conditioning it out. (perhaps on TargetFramework?)

<ItemGroup Condition="...">
    <PackageReference ... />
</ItemGroup>

Some properties, like TargetFramework are defined by the SDK after Directory.Build.props are imported. Here are some alternate ideas:

  • Use a condition like '$(MSBuildProjectExtension)' == '.csproj'
  • Define the PackageReference in Directory.Build.targets in order to use properties such as TargetFramework.
  • I believe that MSBuild only imports the first Directory.Build.Props it finds, not all of them, so create an empty Directory.Build.props file in the same directory as any project that doesn't support PackageReference
Was this page helpful?
0 / 5 - 0 ratings