Home: PackageReference ignores $Platform condition

Created on 12 Mar 2019  路  2Comments  路  Source: NuGet/Home

Details about Problem

NuGet product used (VS UI | Package Manager Console | dotnet.exe):

dotnet.exe --version (if appropriate): 3.0.100-preview3-010431

VS version (if appropriate): 16.0.0 Preview 4.1 SVC1 / 15.9.8

Detailed repro steps so we can see the same problem

  1. Create new C# project:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.0;netcoreapp2.2;net472</TargetFrameworks>
    <Platforms>x64;x86</Platforms>
  </PropertyGroup>

  <!--<ItemGroup Condition="'$(Platform)' == 'x64'">-->
  <!--<ItemGroup Condition="'$(Platform)' == 'x64' AND ('$(TargetFramework)' == 'netcoreapp3.0' Or '$(TargetFramework)' == 'netcoreapp2.2')">-->
  <ItemGroup Condition="'$(TargetFramework)' != 'net472' AND '$(Platform)' != 'x86'">
      <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-27413-02" />
  </ItemGroup>
</Project>
  1. Try to publish x86:
    dotnet publish -c Release -f netcoreapp3.0 -r win-x86

  2. Error:
    C:\Users\Me.nuget\packages\microsoft.dotnet.ilcompiler\1.0.0-alpha-27413-02\build\Microsoft.NETCore.Native.Publish.targets(65,5): error : win-x86 not supported yet. https://github.com/dotnet/corert/issues/4589 [C:\Development\Test\Test.csproj]

The commented out conditions all don't work. Either they result in both x86 and x64 builds trying to use the referenced package or neither of them using the referenced package. Either way it seems impossible to specify a PackageReference specific to the platform. Also tried adding the condition on the PackageReference rather than ItemGroup with the same results.

Restore Duplicate PackageReference Bug

All 2 comments

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>

Closing this as duplicate of https://github.com/NuGet/Home/issues/5895.

TargetFramework is the only project configuration pivot support.

Platform/Architecture/Configuration and the likes are not supported.

Was this page helpful?
0 / 5 - 0 ratings