Commit 1a71a5f
authored
Fix NuGet DLL Loading on Linux and macOS (#27266)
## Summary
This PR addresses persistent native library loading issues in the ONNX
Runtime NuGet package, specifically on macOS and Linux, by implementing
a robust DllImportResolver. It also includes necessary pipeline and
packaging adjustments to ensure required macOS artifacts are correctly
located and validated during CI.
## Problem
#27263 reports that
`Unable to load shared library 'onnxruntime.dll' or one of its
dependencies`. It was caused by
#26415 since the commit
hard-coded onnxruntime.dll even for Linux and MacOS (The correct
filename shall be libonnxruntime.so for Linux, and libonnxruntime.dylib
for MacOS).
The Nuget test pipeline has been broken for a while, so we also need fix
the pipeline to test our change. It has the following issues:
* MacOS nuget is for arm64, but the vmImage `macOS-15` is x64.
* MacOS nuget test need libcustom_op_library.dylib, but it is not copied
from artifacts to test environment.
* MacOS artifact contains libonnxruntime.dylib and
libonnxruntime.1.24.1.dylib, where libonnxruntime.dylib is symlink. It
causes issue since the later is excluded by nuspec.
* MacOS nuget test use models from onnx repo. However, latest onnx has
some models with data types like float8 that are not supported by C#, so
those model test failed.
* Linux nuget test uses a docker Dockerfile.package_ubuntu_2404_gpu, but
docker build failed due to libnvinfer-headers-python-plugin-dev and
libnvinfer-win-builder-resource10 version.
## Changes
### 1. Robust C# DLL Resolution
The DllImportResolver has been enhanced to handle various deployment
scenarios where standard .NET resolution might fail:
- **Platform-Specific Naming**: Maps extension-less library names
(`onnxruntime`, `ortextensions`) to appropriate filenames
(`onnxruntime.dll`, `libonnxruntime.so`, `libonnxruntime.dylib`) based
on the OS.
- **Multi-Stage Probing**:
1. **Default Loading**: Attempts `NativeLibrary.TryLoad` with the mapped
name.
2. **NuGet `runtimes` Probing**: If the above fails, it probes the
`runtimes/{rid}/native/` subdirectories relative to the assembly
location, covering common RIDs (`win-x64`, `linux-arm64`, `osx-arm64`,
etc.).
3. **Base Directory Fallback**: As a final attempt, it looks in
`AppContext.BaseDirectory`.
- **Case-Sensitivity Handling**: Ensures lowercase extensions are used
on Windows to prevent lookup failures on case-sensitive filesystems.
### 2. macOS CI/Packaging Improvements
- **Templates (test_macos.yml)**:
- Updated to extract artifacts from TGZ files.
- Ensures `libcustom_op_library.dylib` is placed in the expected
location (`testdata/testdata`) for end-to-end tests.
- Initializes the ONNX submodule to provide required test data.
- **Node.js**:
- Restored the Node.js macOS test stage in
c-api-noopenmp-test-pipelines.yml, configured to run on the ARM64 pool
(`AcesShared`).
- Updated test_macos.yml template to support custom agent pools (similar
to the NuGet template).
- **Pipeline Config**: Adjusted agent pool selection and demands for
macOS jobs to ensure stable execution.
- **Binary Robustness**: The `copy_strip_binary.sh` script now ensures
`libonnxruntime.dylib` is a real file rather than a symlink, improving
NuGet packaging reliability.
### 3. Test Refinements
- **Inference Tests**: Skips a specific set of pretrained-model test
cases on macOS that are currently known to be flaky or unsupported in
that environment, preventing noise in the CI results.
## Verification
### Pipelines
- [x] Verified in `NuGet_Test_MacOS`.
- [x] Verified in `NuGet_Test_Linux`.
- [x] Verified in Windows test pipelines.
### Net Effect
The C# bindings are now significantly more resilient to different
deployment environments. The CI process for macOS is also more robust,
correctly handling the artifacts required for comprehensive NuGet
validation.1 parent d7f32e2 commit 1a71a5f
File tree
9 files changed
+243
-23
lines changed- csharp
- src/Microsoft.ML.OnnxRuntime
- test/Microsoft.ML.OnnxRuntime.Tests.NetCoreApp
- tools/ci_build/github
- azure-pipelines
- nodejs/templates
- nuget/templates
- templates
- linux
- docker
9 files changed
+243
-23
lines changedLines changed: 132 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
474 | 475 | | |
475 | 476 | | |
476 | 477 | | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
477 | 484 | | |
478 | 485 | | |
479 | 486 | | |
| |||
847 | 854 | | |
848 | 855 | | |
849 | 856 | | |
850 | | - | |
| 857 | + | |
851 | 858 | | |
852 | 859 | | |
853 | 860 | | |
| |||
872 | 879 | | |
873 | 880 | | |
874 | 881 | | |
875 | | - | |
876 | | - | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
877 | 886 | | |
878 | 887 | | |
879 | 888 | | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
880 | 1003 | | |
881 | 1004 | | |
882 | 1005 | | |
| |||
2644 | 2767 | | |
2645 | 2768 | | |
2646 | 2769 | | |
2647 | | - | |
| 2770 | + | |
2648 | 2771 | | |
2649 | 2772 | | |
2650 | 2773 | | |
| |||
2767 | 2890 | | |
2768 | 2891 | | |
2769 | 2892 | | |
2770 | | - | |
| 2893 | + | |
2771 | 2894 | | |
2772 | 2895 | | |
2773 | 2896 | | |
| |||
2952 | 3075 | | |
2953 | 3076 | | |
2954 | 3077 | | |
2955 | | - | |
2956 | | - | |
2957 | | - | |
| 3078 | + | |
| 3079 | + | |
| 3080 | + | |
| 3081 | + | |
2958 | 3082 | | |
2959 | 3083 | | |
2960 | 3084 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
604 | 627 | | |
605 | 628 | | |
606 | 629 | | |
| |||
934 | 957 | | |
935 | 958 | | |
936 | 959 | | |
| 960 | + | |
937 | 961 | | |
938 | 962 | | |
939 | 963 | | |
| |||
Lines changed: 12 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
108 | 110 | | |
109 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
110 | 119 | | |
111 | 120 | | |
112 | 121 | | |
| |||
117 | 126 | | |
118 | 127 | | |
119 | 128 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | 129 | | |
125 | 130 | | |
126 | 131 | | |
| |||
225 | 230 | | |
226 | 231 | | |
227 | 232 | | |
228 | | - | |
| 233 | + | |
229 | 234 | | |
230 | 235 | | |
231 | 236 | | |
| |||
246 | 251 | | |
247 | 252 | | |
248 | 253 | | |
249 | | - | |
| 254 | + | |
250 | 255 | | |
251 | 256 | | |
252 | 257 | | |
| |||
Lines changed: 10 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
15 | 23 | | |
16 | 24 | | |
17 | 25 | | |
| |||
38 | 46 | | |
39 | 47 | | |
40 | 48 | | |
41 | | - | |
| 49 | + | |
Lines changed: 10 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
3 | 7 | | |
4 | 8 | | |
5 | 9 | | |
| |||
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
14 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
15 | 24 | | |
16 | 25 | | |
17 | 26 | | |
| |||
0 commit comments