I'm trying to make a project that is ultimately built using dotnet publish as a self-contained executable for multiple platforms (by specifying the runtime on the command line). This project has some different behavior (and native libraries) for each of my target platforms. I attempted to use the RuntimeIdentifier property as a condition in my csproj for DefineConstants as well as controlling which native libraries are copied over. However I found that referenced projects don't seem to have the RuntimeIdentifier property set during dotnet publish even though the entry point project does have that property set.
I've setup a repo that has a simple repro setup showing the issue. For reference, when I run dotnet --version it prints out 2.2.106. I'm also using the .NET SDK on macOS in case that makes a difference.
This feels like a bug to me, but it also might indicate that this isn't the correct way to handle native dependencies like this. I'm not trying to make a single cross-platform build (since I intend to distribute self-contained builds I don't need the .NET DLLs to be generic) so some of the advice in, for example, this issue aren't as important to me. I've simply ensured I have NativeLib.dll for Windows and libNativeLib.dylib for macOS which makes my DllImports work as intended (by specifying just 'NativeLib' as the assembly name and letting the runtime choose the prefix and suffix).
If this is a bug, hopefully it can be addressed. If there is a better way to handle things like configuring defines and copying the correct native libraries, I'd appreciate any pointers.
RuntimeIdentifier seems to be a question for project system: https://github.com/dotnet/project-system. cc @drewnoakes @davkean
I am not sure if interop will work cross-plat the way you plan to use it - @jeffschwMSFT @janvorli may know / help route ...
RuntimeIdentifier would be an SDK question, cc @dsplaisted @nguerrera
Try adding <RuntimeIdentifiers>win-x64;osx-x64<RuntimeIdentifiers> to the referenced projects. Put all the runtime identifiers that you use in publish. There is code in the process of resolving references that decides whether to flow runtime identifier based on whether the referenced project has rids or not.
That did in fact do the trick. Is that documented? It doesn't seem unreasonable but I had no idea I had to list those for the identifiers to resolve correctly.
Please feel free to close this issue unless you internally want it for something. I'm more than happy to add that property to my projects to resolve the issue.
I think we are probably missing documentation on this. It's relatively obscure. You can file an issue on https://github.com/dotnet/docs/issues and link here.
I will close this out, but happy to help on a docs issue with the text.
Most helpful comment
Try adding
<RuntimeIdentifiers>win-x64;osx-x64<RuntimeIdentifiers>to the referenced projects. Put all the runtime identifiers that you use in publish. There is code in the process of resolving references that decides whether to flow runtime identifier based on whether the referenced project has rids or not.