PackageReference condition is ignored
Asked Answered
K

4

16

In my VS 2017 project I reference docfx.console package and I want it to be used only when certain condition is met. But the package gets used for all builds.

Here is a part of my project. I want docfx.console to be used when configuration is Installer/AnyCPU and VS is building net40 flavor.

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net40;netstandard1.3;netstandard2.0</TargetFrameworks>
    <!-- ... -->
    <Configurations>Debug;Release;Installer</Configurations>
  </PropertyGroup>

  <ItemGroup Condition=" '$(TargetFramework)'=='net40' ">
    <!-- ... -->
    <PackageReference Include="docfx.console" Version="2.30.0" Condition="'$(Configuration)|$(Platform)'=='Installer|AnyCPU'" />
  </ItemGroup>

    <!-- ... -->
</Project>

Is there a way to use docfx.console in Installer build for net40 only?

Kanter answered 2/2, 2018 at 20:17 Comment(3)
Possible duplicate of Conditional reference in Visual Studio Community 2017Catherine
@MarcLaFleur-MSFT No, it is not a duplicate. At least the Choose/When approach don't help.Kanter
known and documented here: "at presently, only the TargetFramework variable is supported"Tippler
Y
16

Even i was looking for referencing nuget packages on condition based (load only when expected constant is set in DefineConstants). Though @Luke Schoen Solution worked for me, I could make it work without the external targets file.

Solution is to include your PackageReference using Choose > When

Make sure to have this block after your PropertyGroup which has DefineConstants.

<Choose>
    <When Condition="$(DefineConstants.Contains('My_CONST'))">
        <ItemGroup>
            <PackageReference Include="MyPackage">
                <Version>1.0.6</Version>
            </PackageReference>
        </ItemGroup>
    </When>
</Choose>
Yardman answered 24/5, 2020 at 19:49 Comment(0)
A
15

To summarize, even with the condition "false", the package will be imported.

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.0;netcoreapp2.2;net472</TargetFrameworks>
    <Platforms>x64;x86</Platforms>
  </PropertyGroup>
  <ItemGroup Condition="false">
      <PackageReference Include="MyPackage" Version="1.0.0" />
  </ItemGroup>
</Project>

We found that we can work around this issue by putting the packagereference in a different file, and making the import of the file conditional.

Separate file: packagerefs.targets

<Project Sdk="Microsoft.NET.Sdk">    
  <ItemGroup>
      <PackageReference Include="MyPackage" Version="1.0.0" />
  </ItemGroup>
</Project>

Project file:

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.0;netcoreapp2.2;net472</TargetFrameworks>
    <Platforms>x64;x86</Platforms>
  </PropertyGroup>
  <Import Project="packagerefs.targets" Condition="false" />
</Project>
Ania answered 7/5, 2019 at 18:59 Comment(3)
Wow great way to fix it! I've created a GitHub issue in msdocs page where it clearly says this should be working as I wasn't sure where to create it! github.com/NuGet/learn.microsoft.com-nuget/issues/1840, upvote it or any future linked issues if anyone sees this!Denesedengue
Also worth noting, the <Import Project=".." /> has to be at the root level, it cannot be nested inside a <PropertyGroup />Denesedengue
After a nights of sleep, and creating a new solution + project to try and reproduce this, the condition seems to be working this morning! Not sure what happened yesterday, but this definitely fixed at the time! I also tried reproducing on original project but ItemGroup Condition seems to be getting respected. Anyhow this did somehow manage to get me around my original issue!Denesedengue
A
3

PackageReference condition is ignored

This is an known issue about the new style csproj PackageReference to work with content/Tools files in a nuget package.

In the package docfx.console, it looks like docfx.console has "content", "build" and "tools" without .NET code in it, just random files:

enter image description here

In this case, when we install this nuget package, nuget does not do anything. So it seems gets used for all builds. That because:

NuGet packages that work with Packages.config, don't always work in transitive NuGet environments (projects using Project.json or PackageReferences). Packages that work in transitive NuGet environments must use "contentFiles" instead of "content" -- you can have both, if a package would like to work in both environments. Also, install.ps1/uninstall.ps1 doesn't execute in transitive NuGet environments -- however, init.ps1 will work in both Packages.config and transitive environments.

At this moment, there is not a perfect solution, so the issue 4837 is still open.

To resolve this issue, the NuGet docfx.console package needs to be changed to use contentFiles and define targets and would typically reference a tool by using $(MSBuildThisFileDirectory)..\tools\MyTool.exe. If you put this PackageName.targets file into the a build directory, it will be automatically included into the project referencing the NuGet package.

Hope this helps.

Alteration answered 6/2, 2018 at 5:53 Comment(2)
Thank you for the detailed answer. I ended up using console docfx tool instead of the NuGet package.Kanter
I have asked a DocFX question here if you are interested to take a look: #50018971.Essequibo
F
0

In my case I had also same kind of problem - but root cause was that some of msbuild properties were not defined when nuget package building was performed - in particular $(SolutionName) was not defined. Condition still gets evaluated, only it's returning true for some reason. (You can test this by putting Condition="false" - it will be omitted).

Solution for me was to check if property is defined, for example like this:

  <ItemGroup Condition="'$(SolutionName)' != '' and $(SolutionName.Contains('SolutionCustomTag'))">
    <Reference Include="...">

Te first statement '$(SolutionName)' != '' and - tests that property is defined.

Feign answered 24/3, 2021 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.