3

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 Answers2

0

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.

weir
  • 4,521
  • 2
  • 29
  • 42
  • 1
    non-SDK project do support PackageReference. It's just not the default for these projects. All our projects are using PackageReferences and no package.config files. – Jörg Penndorf Aug 19 '19 at 12:05
  • My mistake. And thanks for confirming there is no `packages.config`. I’m stumped. – weir Aug 19 '19 at 16:51
  • Consider using the excellent open-source MSBuild Binary and Structured Log Viewer (http://www.msbuildlog.com/) to see if you can spot how that package reference is being added. – weir Aug 19 '19 at 16:55
  • I tried investigating the log files, but with no luck. I couldn't find any information on where this PackageReference comes from. – Jörg Penndorf Aug 26 '19 at 12:23
  • Bummer. I’m afraid I’m out of ideas ATM. – weir Aug 26 '19 at 19:45
0

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.

Johann
  • 4,107
  • 3
  • 40
  • 39