Repository navigation
Description
TL;DR: How can I make DllImport automatically load the right native library from the runtimes/<rid>/native path when it is in a project referred by ProjectReference, similar to what PackageReference enables by adding the appropriate resolution information in deps.json?
--
If I create a NuGet package and put native DLLs in runtimes/<rid>/native/, the deps.json file generated for the assembly referencing that package will get runtimeTargets entries for the native libraries, allowing DllImport to find the appropriate DLLs to load. That mechanism is very useful for easily targeting multiple platforms with "Any CPU" despite reliance on native libraries that are each specific to a platform/architecture combination.
Now I would like to unit-test the classes in that package. Usually I put the unit test project in the same solution, make it refer to the project containing the classes to test. Unfortunately, that doesn't bring the mechanisms from PackageReference, as it only uses a ProjectReference. Therefore the deps.json for the unit test assembly doesn't get hints for the native libraries, and when the unit test is run, it complains about not finding the native DLLs.
A workaround would be to have a separate NuGet package containg nothing more than the native libraries, making the project containing .NET code refer to it, similar to what libgit2sharp does. However that's not convenient when updating the native libraries because it requires publishing their package before being able to test their integration in the .NET code.
I know about the NativeLibrary class where one can register a custom resolver, but that seems tedious for true multiplatform. I didn't find a simple way to replicate the RID-based resolution.
Is there any other way? Any thoughts?
Activity
[-]Resolution of native librarcies when using DllImport[/-][+]Resolution of native libraries when using DllImport[/+]antoinebj commentedon Nov 19, 2020
I also noticed the existence of --additional-deps, but I don't know how to make unit test runners use that parameter.
benvillalobos commentedon Nov 25, 2020
/cc: @vitek-karas
vitek-karas commentedon Nov 25, 2020
I can only comment on the runtime side of things:
.deps.jsonruntime won't find it - so it's up to SDK to put it there. As you noted this works forPackageReference. I don't know if or how it should forProjectReference.--additional-depswon't help that much, it lets you specify additional.deps.json, but that assumes you have one. So you would have to "hand-craft" one.antoinebj commentedon Nov 25, 2020
This has been labeled as a question, which it is indeed, but only because I wouldn't want to make a feature request if there is already an existing solution.
If the conclusion to this question is that there is no simple solution for what I'm trying to do, then I would be inclined to request an improvement in how
.deps.jsonis generated whenPackageReferenceisn't involved, so as to have the same convenience for loading native libraries.wasabii commentedon Dec 16, 2020
I second this. Same issue. Building a .NET Core library that includes some native files (from a VCXPROJ). Works fine when I add it to the produced NuGet package: consumers generate .deps.json files referencing the libraries. However, doesn't work in a Console app within the same solution using ProjectReference, nor in a Unit Test. Though, it should. The only issue is that hte generate .deps.json files for those other projects do not fold in the native assets of the ProjectReference.
I would expect to be able to simply include Content (or None) item, and perhaps stick
<AssetType>native</AssetType>onto it and have it pulled through.wasabii commentedon Dec 16, 2020
Digging into it, DependencyContextBuilder just seems to consider NuGet origins. Would need to fix that, as well as the associated GenerateDeps MSBuild task. And then the MSBuild targets to consider these items.
rgebee commentedon Jan 27, 2021
Adding to this to say I have the same issue with my project Raylib-cs. Took me a while to notice the
.deps.jsonwas not includingruntimeTargetsso for manual installs it would fail to find the library. I am also not sure how to workaround/fix this issue so any suggestions or more information is appreciated.vitek-karas commentedon Jun 25, 2021
@marcpopMSFT I think this is very much an SDK issue.
.deps.json(it's basically the main reason why this file exists today).deps.jsonif the RID specific assets are coming from NuGet packages.deps.jsonalexrp commentedon Jun 26, 2021
FWIW, having a way to do this is essential for parts of vezel-dev/zig-sdk#8 as well.
marcpopMSFT commentedon Jun 28, 2021
@vitek-karas Isn't this an issue in deps.json creation which from our discussion, the runtime team controls (hence our decision to move it to runtime repo)?
7 remaining items
alexrp commentedon Dec 20, 2021
Is there a chance that this issue (or something like what @vitek-karas described) might be prioritized for .NET 7?
Having a way to include RID-specific assets combined with my upcoming work on project/package reference support in Zig.Sdk would be a game-changer for easily adding cross-compiled, in-box native components to any .NET app.
saucecontrol commentedon Jan 7, 2022
I think the native binary resolution is just a symptom of a larger problem, which is that when consuming a project that builds a NuGet package as a
ProjectReference, none of the package behaviors are brought in. That includes not just RID-specific binaries but also.propsand.targets. Perhaps the answer is that things that are expressed by convention (file name/path, etc) in NuGet packages need to be able to be made explicit in the project file so they can be consumed by the SDK. That could also drive the package definition so that less of it is based on magic folder names.webczat commentedon Jan 7, 2022
just went straight into that issue. how is it to be handled now? build a local nuget just to reference it later?
marcpopMSFT commentedon Jun 13, 2023
Moving to backlog. This relates to P2P references and native dependencies. Probably could use @richlander doing a pass through this as there isn't a good way to produce packages and projects that have native dependencies in a way that will work well for customers
wasabii commentedon Jun 13, 2023
We're doing something to solve this in IKVM. But it involves our own P2P implementation, and build time modification of deps.json. so, not great.
alexrp commentedon Jun 13, 2023
I'd really urge prioritizing this issue. .NET has an excellent interop story these days ... except when it comes to everything build-related. Resolving this issue would bring the story from excellent to amazing. It would also make life massively simpler for those of us who are maintaining MSBuild SDKs that bridge managed and native code.
ero-qt commentedon Jan 3, 2025
As a user of the aforementioned
Zig.Sdk, I would also like to voice my desire for this issue to receive some priority again.Zig.Sdkallows for building C, C++, and Zig code using the .NET CLI. Naturally, I want to pull in some native, header-only libraries, such asboostornlohmann.json. Today, I need to add a work-around in which I specify the paths to the packages' directories which should be part of theincludes.If the SDK were able to integrate with
PackageReferencesbetter, such a work around would not be necessary.MiKom commentedon Jan 3, 2025
This would also be very useful for embedded projects where an end-user application is likely using some DllImport-wrapped C++ libraries to talk to the board. Ideally, I'd like to have the wrappers in separate project and have the proper native libraries nicely copied to
runtimes/<rid>/nativedepending on the target platform.Now, I have to do some manual copying as post build steps.