Repository navigation
Description
Currently, in single-file .NET applications, the directory of the single-file executable is added to the NATIVE_DLL_SEARCH_DIRECTORIES property during startup. This means that the application directory is always probed when loading unmanaged libraries. This includes for p/invoke library load in the runtime itself, which the developer cannot control via the resolution extension points (DllImportResolver, AssemblyLoadContext.LoadUnmanagedDll).
This means that the following p/invoke will actually look in the application directory first and load lib from there if it exists:
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern void Method()In NativeAOT on non-Windows, we set the rpath to the application directory, such that the OS search will always look in the application directory. As a result, the above p/invoke on non-Windows will still look in the application directory.
At the time this was done, self-contained, single-file publish on Windows still had separate native runtime libraries next to the app (such that it would always have the executable location in the search directories anyways). This is no longer the case and the behaviour has caused confusion for developers. NativeAOT behaviour was made to match the regular single-file behaviour.
We should make single-file stop always adding the executable directory and properly handle bundled assembly p/invoke resolution in the runtime to have assembly directory mean next to the bundled app - the option not taken in #42772. NativeAOT should also stop setting the rpath.
This should result in:
// Look in System32 on Windows, OS search on non-Windows
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern void Method()
// Look next to the single-file app because assembly directory means application directory for single-file
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
static extern void Method()
// Look next to the single-file app (because we search assembly directory by default), then default OS search
[DllImport("lib")
static extern void Method()Related:
- Dll Search Path Behavior for Windows System Dlls for certain dotnet apps (WPF?) #101857
- [Single-File] DllImport fails to find native library #42772
cc @dotnet/appmodel @dotnet/interop-contrib @GrabYourPitchforks
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Apr 15, 2025
Added
needs-breaking-change-doc-createdlabel because this issue has thebreaking-changelabel.needs-breaking-change-doc-createdlabel.Tagging @dotnet/compat for awareness of the breaking change.
dotnet-policy-service commentedon Apr 15, 2025
Tagging subscribers to this area: @agocke, @vitek-karas, @VSadov
See info in area-owners.md if you want to be subscribed.
jkotas commentedon Apr 16, 2025
Do you plan to change the default for nativeaot too?
elinor-fung commentedon Apr 16, 2025
No - if I understand correctly, that is based on OS behaviour. I'm not suggesting we try to change that portion for single-file either, just stop explicitly adding it to our own default search (that is, it will be searched for p/invokes regardless of what 'DllImportSearchPath` is specified).
NativeAOT does already consider
DllImportSearchPath.AssemblyDirectoryas the app directory, so I think updating single-file to do the same would actually help with consistency here.jkotas commentedon Apr 16, 2025
The OS behavior is configurable. We tried to align the defaults in native AOT with the regular single-file defaults:
On Unix, we set custom RPath in NativeAOT to make the native libraries next to app binary just work:
runtime/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets
Lines 67 to 68 in 40f209c
On Windows, we opted to not set
DEPENDENTLOADFLAGto prevent loading native libraries next to the app binary to match regular single-file behavior.jkotas commentedon Apr 16, 2025
cc @MichalStrehovsky for NativeAOT
elinor-fung commentedon Apr 16, 2025
That still means we respect any specified
DllImportSearchPathspecified (for Windows) though. So if I sayDllImportSearchPath.System32for a p/invoke in nativeaot, it won't find the library next to the app - unlike in single-file today.jkotas commentedon Apr 17, 2025
It is going to look for it next to the app if the import is opted into DirectPInvoke. We have warning about DirectPInvoke at https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/interop#direct-pinvoke-calls to wash our hands. I think our story is going to have cracks if we make regular single file stricter by default but keep Native AOT defaults as is.
elinor-fung commentedon Apr 17, 2025
For user-defined p/invokes that is an opt-in though - if they don't want that behaviour, they still have the option of a regular p/invoke. Regular single-file today has no option to respect only the specified
DllImportSearchPath.I think it would actually make the two match better for regular single-file to:
NATIVE_DLL_SEARCH_DIRECTORIESThen, regular p/invokes would respect
DllImportSearchPathin both single-file and NativeAOT. Default would be able to find libraries next to the application in both. And regular single-file just doesn't have a DirectPInvoke equivalent.jkotas commentedon Apr 17, 2025
I may be misunderstanding the underlying motivation for this change. What's a simple user facing description of what we want to achieve by this change? I thought we want to be able to say something like "Single file apps won't try to load any native libraries (.dlls/.so/.dylib) from the application directory by default.".
am11 commentedon Apr 17, 2025
Would that type of a change affect the following workflow, and if so, can we avoid that by confining
executable directory in search pathsto user-defined P/Invoke use-case?CopyToOutputDirectory+TargetPath is for
dotnet run/publishPack/PackagePath is for
dotnet pack, restore and then run/publishThis setup conforms with the documented https://learn.microsoft.com/nuget/create-packages/native-files-in-net-packages#understanding-nuget-package-asset-selection and
DllImportjust works without custom steps; the native files will be copied to output or publish directory from include path and in case of package restore, it will be copied from{package path}/runtimes/{rid}/native/*.elinor-fung commentedon Apr 17, 2025
Sorry for the lack of clarity. I think it is more "Single-file apps won't always to try load native libraries from the application directory."
In regular single-file, this p/invoke will actually look in the application directory:
In NativeAOT (non-DirectPInvoke), it will only look in System32 on Windows and use OS behaviour on non-Windows.
The assembly directory should mean the application directory. The default case should still look in the application directory (since we look in the assembly directory by default):
NativeAOT handles assembly directory meaning application directory. Regular single-file does not - making the runtime handle that would be part of this issue.
I updated the description to hopefully clarify a bit.
I don't think so - for the change I have in mind described above. Since assembly directory is part of the default, updating the runtime to treat that as app directory for regular single-file should cover that workflow.
I'm actually not sure what setting the rpath would get us. Assembly directory is searched by default, so we'd already look next to the app by default. Since run path is non-transitive, I think it would just make it so that on non-Windows, setting DllImportSearchPaths without including
DllImportSearchPath.AssemblyDirectoryfor whatever reason would mean we still look in the assembly/app directory. I think that would seem weird - but it admittedly also seems odd to specify non-assembly directory search on non-Windows, since only the assembly directory flag means anything on non-Windows.elinor-fung commentedon Apr 22, 2025
@jkotas does the description in #114717 (comment) address your concerns with this?
jkotas commentedon Apr 23, 2025
We do influence the OS behavior on non-Windows by setting RPath.
On Linux, your example won't look into the application directory with FDD/FDE, but it will look into the application directory with both SingleFile and NativeAOT. If we are going to change it so that SingleFile does not look into the application directory anymore, we should make matching changes so that NativeAOT behaves the same way by default.
I believe specifying
DllImportSearchPath.System32on non-Windows is going to search the system libraries only with FDD/FDE. For example:Does this match your understanding?
I agree that System32 is a weird name to use on non-Windows. We may want to consider introducing an alias that looks less weird on non-Windows.
elinor-fung commentedon Apr 23, 2025
I did not think we do anything with
DllImportSearchPath.System32on non-Windows. I believe we just ignore any flags other thanAssemblyDirectory:runtime/src/coreclr/vm/nativelibrary.cpp
Line 187 in a5d6473
And basically just do a
dlopen(<library>)I agree that matching makes sense. I think my question was more whether both should set rpath or both should not. Based on my understanding above of non-AssemblyDirectory flags on non-Windows, I thought the scenario was a generally odd one - I could see either being reasonable: set rpath on both - always searches assembly directory regardless of flags, no breaking change for NativeAOT, don't set rpath - doesn't search assembly directory if only non-AssemblyDirectory is specified, breaking change for NativeAOT.
jkotas commentedon Apr 24, 2025
I think the behavior should be same between all FDD/FDE. SingleFile and NativeAOT. Ideally, non-assembly directory should have same default behavior across all of them on given OS.
We prefer more secure defaults these days. Omitting Rpath is more secure default so it would be my choice.
If we are doing a breaking change for SingleFile, I do not see a problem with doing same breaking change for NativeAOT too.
[-]Remove executable directory from `NATIVE_DLL_SEARCH_DIRECTORIES` for single-file applications[/-][+]Remove always searching executable directory for native libraries in single-file applications[/+]elinor-fung commentedon Apr 24, 2025
Thanks - that makes sense to me. I updated the title and description to better reflect current behaviour and desired changes for both regular single-file and NativeAOT.
elinor-fung commentedon May 22, 2025
Breaking change doc issue: dotnet/docs#46356