Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PackageReference condition is ignored

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?

like image 679
Bobrovsky Avatar asked Feb 02 '18 20:02

Bobrovsky


People also ask

What is Privateassets?

Private assets are investments that are typically not publicly listed and traded.

What is package reference in Csproj?

Package references, using <PackageReference> MSBuild items, specify NuGet package dependencies directly within project files, as opposed to having a separate packages. config file. Use of PackageReference doesn't affect other aspects of NuGet; for example, settings in NuGet.


2 Answers

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>
like image 158
Luke Schoen Avatar answered Sep 30 '22 11:09

Luke Schoen


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.

like image 23
Leo Liu-MSFT Avatar answered Sep 30 '22 13:09

Leo Liu-MSFT