Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.ML.OnnxRuntime Nuget package should have a 'native' dependency group #24130

Open
benjamin-hodgson opened this issue Mar 21, 2025 · 0 comments
Labels
api:CSharp issues related to the C# API .NET Pull requests that update .net code

Comments

@benjamin-hodgson
Copy link

benjamin-hodgson commented Mar 21, 2025

Describe the issue

I would like to use the (native) Microsoft.ML.OnnxRuntime package in a C++ project via PackageReference. However, the package's nuspec does not include a dependency group for a native TFM. Nuget interprets this as meaning the package does not support native consumers. This results in the package being restored using a fallback target framework, along with a (transitive, undesirable) dependency on the managed package and a NU1701 warning.

The package's nuspec should include an empty dependency group for the native target framework. This tells nuget that it's valid to install the package into native projects, and that the dependency on the managed package is not required for such projects.

diff --git a/Microsoft.ML.OnnxRuntime.nuspec b/Microsoft.ML.OnnxRuntime.nuspec
index ffa2d6419d2..b471160b523 100644
--- a/Microsoft.ML.OnnxRuntime.nuspec
+++ b/Microsoft.ML.OnnxRuntime.nuspec
@@ -20,6 +20,7 @@
     <tags>native ONNX Runtime ONNXRuntime Machine Learning MachineLearning</tags>
     <repository type="git" url="https://github.com/Microsoft/onnxruntime.git" commit="e0b66cad282043d4377cea5269083f17771b6dfc" />
     <dependencies>
+      <group targetFramework="native0.0" />
       <group targetFramework=".NETCoreApp0.0">
         <dependency id="Microsoft.ML.OnnxRuntime.Managed" version="1.21.0" />
       </group>

This allows it to be restored in a native project without using a fallback TFM.


Some additional context, as to why I have this problem: I have a large repo with a mixture of C#, C++, and C++CLI projects. To facilitate interoperating the vcxproj and csproj projects, we've configured the vcxproj projects to restore using PackageReference and set up a global AssetTargetFallback:

<!-- Directory.Build.props -->
<PropertyGroup>
  <TargetFrameworkMoniker>native,Version=v0.0</TargetFrameworkMoniker>
  <NuGetTargetMoniker>native,Version=v0.0</NuGetTargetMoniker>
  <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  <EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>
  <AssetTargetFallback>$(AssetTargetFallback);native;net472;net8.0</AssetTargetFallback>
</PropertyGroup>

<ItemGroup>
  <ProjectCapability Include="PackageReferences" />
</ItemGroup>

(It seems that these props are needed in order to fully reproduce the issue - see below.) I acknowledge that this is hacky!

To reproduce

Create a default vcxproj project in visual studio. Add the config I mentioned above (the two sections labelled "PackageReference configuration" below) and add a PackageReference to Microsoft.ML.OnnxRuntime. I trimmed some stuff for my example:

<!-- Project1.vcxproj -->
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
  </ItemGroup>

  <PropertyGroup Label="Globals">
    <VCProjectVersion>17.0</VCProjectVersion>
    <Keyword>Win32Proj</Keyword>
    <ProjectGuid>{a09a1457-4b77-4094-91da-66e07a20135b}</ProjectGuid>
    <RootNamespace>Project1</RootNamespace>
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>

  <PropertyGroup Label="PackageReference configuration">
    <TargetFrameworkMoniker>native,Version=v0.0</TargetFrameworkMoniker>
    <NuGetTargetMoniker>native,Version=v0.0</NuGetTargetMoniker>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    <EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>
    <AssetTargetFallback>$(AssetTargetFallback);native;net472;net8.0</AssetTargetFallback>
  </PropertyGroup>

  <ItemGroup Label="PackageReference configuration">
    <ProjectCapability Include="PackageReferences" />
  </ItemGroup>

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>false</UseDebugLibraries>
    <PlatformToolset>v143</PlatformToolset>
    <WholeProgramOptimization>true</WholeProgramOptimization>
    <CharacterSet>Unicode</CharacterSet>
  </PropertyGroup>

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />

  <ItemGroup>
    <PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.21.0" />
  </ItemGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <SDLCheck>true</SDLCheck>
      <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <ConformanceMode>true</ConformanceMode>
    </ClCompile>
    <Link>
      <SubSystem>Console</SubSystem>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <OptimizeReferences>true</OptimizeReferences>
      <GenerateDebugInformation>true</GenerateDebugInformation>
    </Link>
  </ItemDefinitionGroup>

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>

Restore this project using msbuild /t:restore (from a developer command prompt) and observe the NU1701 warnings for the managed dependencies:

D:\test\Project1> msbuild /t:restore .\Project1.vcxproj
MSBuild version 17.13.19+0d9f5a35a for .NET Framework
Build started 21/03/2025 11:03:41.

Project "D:\test\Project1\Project1.vcxproj" on node 1 (Restore target(s)).
_GetAllRestoreProjectPathItems:
  Determining projects to restore...
Restore:
  X.509 certificate chain validation will use the default trust store selected by .NET for code signing.
  X.509 certificate chain validation will use the default trust store selected by .NET for timestamping.
D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'Microsoft.ML.OnnxRuntime.Managed 1.21.0' was restored usin
g 'native,Version=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v
0.0'. This package may not be fully compatible with your project.
D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Buffers 4.5.1' was restored using 'native,Version=v
0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v0.0'. This package
may not be fully compatible with your project.
D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Memory 4.5.5' was restored using 'native,Version=v0
.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v0.0'. This package m
ay not be fully compatible with your project.
D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Numerics.Vectors 4.5.0' was restored using 'native,
Version=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v0.0'. This
 package may not be fully compatible with your project.
D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Runtime.CompilerServices.Unsafe 4.5.3' was restored
 using 'native,Version=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Vers
ion=v0.0'. This package may not be fully compatible with your project.
  Assets file has not changed. Skipping assets file writing. Path: D:\test\Project1\obj\project.assets.json
  Restored D:\test\Project1\Project1.vcxproj (in 160 ms).

  NuGet Config files used:
      C:\Users\behodgson\AppData\Roaming\NuGet\NuGet.Config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.FallbackLocation.config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

  Feeds used:
      https://api.nuget.org/v3/index.json
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
  All projects are up-to-date for restore.
Done Building Project "D:\test\Project1\Project1.vcxproj" (Restore target(s)).

Deferred Messages

  Detailed Build Summary
  ======================


  ============================== Build Hierarchy (IDs represent configurations) =======================================
  ==============
  Id                  : Exclusive Time   Total Time   Path (Targets)
  ---------------------------------------------------------------------------------------------------------------------
  --------------
  0                   : 0.647s           0.859s       D:\test\Project1\Project1.vcxproj (Restore)
  | 1                 : 0.027s           0.027s       D:\test\Project1\Project1.vcxproj (_IsProjectRestoreSupported)
  | 2                 : 0.002s           0.002s       D:\test\Project1\Project1.vcxproj (_GenerateRestoreProjectPathWal
  k)
  | 3                 : 0.113s           0.113s       D:\test\Project1\Project1.vcxproj (_GenerateRestoreGraphProjectEn
  try)
  . 4                 : 0.070s           0.070s       D:\test\Project1\Project1.vcxproj (_GenerateProjectRestoreGraph)

  ============================== Node Utilization (IDs represent configurations) ======================================
  ==============
  Timestamp:            1        Duration   Cumulative
  ---------------------------------------------------------------------------------------------------------------------
  --------------
  638781662216290522:   0        0.165s     0.165s ...
  638781662217940984:   1        0.027s     0.193s
  638781662218215952:   0        0.017s     0.210s
  638781662218385954:   2        0.002s     0.212s
  638781662218405954:   0        0.002s     0.214s
  638781662218425956:   3        0.113s     0.326s ..
  638781662219551060:   0        0.001s     0.327s
  638781662219561077:   4        0.070s     0.397s .
  638781662220261289:   0        0.462s     0.859s .........
  ---------------------------------------------------------------------------------------------------------------------
  --------------
  Utilization:          100.0    Average Utilization: 100.0

Build succeeded.

"D:\test\Project1\Project1.vcxproj" (Restore target) (1) ->
(Restore target) ->
  D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'Microsoft.ML.OnnxRuntime.Managed 1.21.0' was restored us
ing 'native,Version=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version
=v0.0'. This package may not be fully compatible with your project.
  D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Buffers 4.5.1' was restored using 'native,Version
=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v0.0'. This packag
e may not be fully compatible with your project.
  D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Memory 4.5.5' was restored using 'native,Version=
v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v0.0'. This package
 may not be fully compatible with your project.
  D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Numerics.Vectors 4.5.0' was restored using 'nativ
e,Version=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Version=v0.0'. Th
is package may not be fully compatible with your project.
  D:\test\Project1\Project1.vcxproj : warning NU1701: Package 'System.Runtime.CompilerServices.Unsafe 4.5.3' was restor
ed using 'native,Version=v0.0, .NETFramework,Version=v4.7.2, net8.0' instead of the project target framework 'native,Ve
rsion=v0.0'. This package may not be fully compatible with your project.

    5 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.87

You can also find the undesirable managed dependencies in the generated assets file:

// obj/project.assets.json
{
  "version": 3,
  "targets": {
    "native,Version=v0.0": {
      // ...
      "Microsoft.ML.OnnxRuntime.Managed/1.21.0": {
        "type": "package",
        "dependencies": {
          "System.Memory": "4.5.5"
        },
        "compile": {
          "lib/netstandard2.0/Microsoft.ML.OnnxRuntime.dll": {
            "related": ".pdb"
          }
        },
        // ...
      }
    }
  }
}

If I edit the nuspec file in my machine-level package cache, to add the empty dependency group as detailed above, the issue is resolved.

Urgency

Not urgent as it's just a build warning, but probably an easy fix 😉

Platform

Windows

OS Version

11

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.21.0

ONNX Runtime API

C++

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

@benjamin-hodgson benjamin-hodgson changed the title Microsoft.ML.OnnxRuntime package should have a 'native' dependency group Microsoft.ML.OnnxRuntime Nuget package should have a 'native' dependency group Mar 21, 2025
@github-actions github-actions bot added .NET Pull requests that update .net code api:CSharp issues related to the C# API labels Mar 21, 2025
benjamin-hodgson added a commit to benjamin-hodgson/onnxruntime that referenced this issue Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:CSharp issues related to the C# API .NET Pull requests that update .net code
Projects
None yet
Development

No branches or pull requests

1 participant