PackageReference condition is ignored for old project format

646 Views Asked by At

I'm trying to centralize some of our project configurations by using the Directory.Builds.props/Directory.Build.targets files. Unfortunately we have a mixture of the old and new (sdk-style) project format. And not all of our projects can be converted to the new format due to some features not available in in the new format.

The issues that I'm running into is that I would like to have all our test projects automatically reference certain nuget packages. For projects in the new format, this works fine. However projects in the old format seem to ignore any conditions for the PackageReferences that I specify. It doesn't seem to matter what I use for the condition.

Here is an example of a very simple Directory.Build.Targets file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
  <ItemGroup>
    <PackageReference Include="Moq" Version="4.12.0" Condition="False" />
  </ItemGroup>
</Project>

In this case I should never see the Moq package included in any project. For all the projects in the old format it is included regardless.

I have also tried to use the condition on:

  • ItemGroup itself
  • Choose/When block

Putting a condition on a property group or property works as expected on the other hand.

I haven't see any mention in the documentation that conditions are not supported for ItemGroup or PackageReference. Is there something I'm missing?

Thanks

2

There are 2 best solutions below

5
weir On

I don't believe non-SDK projects support PackageReference; I suspect they are being ignored regardless of any condition you specify. Check for a packages.config file in the same directory as the project file-- if I am right it is present and references the package(s) in question.

0
Johann On

The Nuget docs currently state:

You can use a condition to control whether a package is included, where conditions can use any MSBuild variable or a variable defined in the targets or props file. However, at presently, only the TargetFramework variable is supported.

My experience was that the Condition="" was ignored by Visual Studio, but respected by the msbuild command line. However, in my testing the <Choose><When Condition=""> block did seem to be respected by both tools.