Skip to content

Remove always searching executable directory for native libraries in single-file applications #114717

Closed

Description

@elinor-fung

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:

cc @dotnet/appmodel @dotnet/interop-contrib @GrabYourPitchforks

Activity

  1. added
    breaking-change
    on Apr 15, 2025
  2. added
    needs-breaking-change-doc-created
    on Apr 15, 2025
  3. dotnet-policy-service commented on Apr 15, 2025

    @dotnet-policy-service
    Contributor

    Added needs-breaking-change-doc-created label because this issue has the breaking-change label.

    1. Create and link to this issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.

    Tagging @dotnet/compat for awareness of the breaking change.

  4. added
    untriaged
    on Apr 15, 2025
  5. dotnet-policy-service commented on Apr 15, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @vitek-karas, @VSadov
    See info in area-owners.md if you want to be subscribed.

  6. jkotas commented on Apr 16, 2025

    @jkotas
    Member

    Do you plan to change the default for nativeaot too?

  7. elinor-fung commented on Apr 16, 2025

    @elinor-fung
    MemberAuthor

    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.AssemblyDirectory as the app directory, so I think updating single-file to do the same would actually help with consistency here.

  8. jkotas commented on Apr 16, 2025

    @jkotas
    Member

    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:

    <IlcRPath Condition="'$(IlcRPath)' == '' and '$(_IsApplePlatform)' != 'true'">$ORIGIN</IlcRPath>
    <IlcRPath Condition="'$(IlcRPath)' == '' and '$(_IsApplePlatform)' == 'true'">@executable_path</IlcRPath>

    On Windows, we opted to not set DEPENDENTLOADFLAG to prevent loading native libraries next to the app binary to match regular single-file behavior.

  9. jkotas commented on Apr 16, 2025

    @jkotas
    Member

    cc @MichalStrehovsky for NativeAOT

  10. elinor-fung commented on Apr 16, 2025

    @elinor-fung
    MemberAuthor

    That still means we respect any specified DllImportSearchPath specified (for Windows) though. So if I say DllImportSearchPath.System32 for a p/invoke in nativeaot, it won't find the library next to the app - unlike in single-file today.

  11. jkotas commented on Apr 17, 2025

    @jkotas
    Member

    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.

  12. elinor-fung commented on Apr 17, 2025

    @elinor-fung
    MemberAuthor

    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:

    • remove the executable directory from NATIVE_DLL_SEARCH_DIRECTORIES
    • add rpaths and don't set DEPENDENTLOADFLAG (like in NativeAOT)

    Then, regular p/invokes would respect DllImportSearchPath in 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.

  13. jkotas commented on Apr 17, 2025

    @jkotas
    Member

    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.".

  14. am11 commented on Apr 17, 2025

    @am11
    Member

    Single file apps won't try to load any native libraries (.dlls/.so/.dylib) from the application directory by default."

    Would that type of a change affect the following workflow, and if so, can we avoid that by confining executable directory in search paths to user-defined P/Invoke use-case?

      <None Include="runtimes\linux-x64\native\libexample.so"
        CopyToOutputDirectory="PreserveNewest" TargetPath="libexample.so"
        Pack="true" PackagePath="runtimes/linux-x64/native" />
      <!-- repeat for other RIDs -->

    CopyToOutputDirectory+TargetPath is for dotnet run/publish
    Pack/PackagePath is for dotnet pack, restore and then run/publish

    This setup conforms with the documented https://learn.microsoft.com/nuget/create-packages/native-files-in-net-packages#understanding-nuget-package-asset-selection and DllImport just 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/*.

  15. elinor-fung commented on Apr 17, 2025

    @elinor-fung
    MemberAuthor

    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.".

    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:

    [DllImport("lib")
    [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
    static extern void Method()

    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):

    [DllImport("lib")
    static extern void Method()

    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.

    Would that type of a change affect the following workflow

    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.

    add rpaths and don't set DEPENDENTLOADFLAG (like in NativeAOT)

    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.AssemblyDirectory for 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.

  16. elinor-fung commented on Apr 22, 2025

    @elinor-fung
    MemberAuthor

    @jkotas does the description in #114717 (comment) address your concerns with this?

  17. jkotas commented on Apr 23, 2025

    @jkotas
    Member

    In NativeAOT (non-DirectPInvoke), it will only look in System32 on Windows and use OS behaviour on non-Windows.

    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 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.

    I believe specifying DllImportSearchPath.System32 on non-Windows is going to search the system libraries only with FDD/FDE. For example:

        [LibraryImport("c")]
        [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
        public static partial void exit(int exitcode);
    

    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.

  18. elinor-fung commented on Apr 23, 2025

    @elinor-fung
    MemberAuthor

    Does this match your understanding?

    I did not think we do anything with DllImportSearchPath.System32 on non-Windows. I believe we just ignore any flags other than AssemblyDirectory:

    hmod = PAL_LoadLibraryDirect(name);

    And basically just do a dlopen(<library>)

    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 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.

  19. jkotas commented on Apr 24, 2025

    @jkotas
    Member

    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.

    breaking change for NativeAOT

    If we are doing a breaking change for SingleFile, I do not see a problem with doing same breaking change for NativeAOT too.

  20. changed the title [-]Remove executable directory from `NATIVE_DLL_SEARCH_DIRECTORIES` for single-file applications[/-] [+]Remove always searching executable directory for native libraries in single-file applications[/+] on Apr 24, 2025
  21. elinor-fung commented on Apr 24, 2025

    @elinor-fung
    MemberAuthor

    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.

  22. added this to the 10.0.0 milestone on Apr 24, 2025
  23. removed
    untriaged
    on Apr 24, 2025
  24. elinor-fung commented on May 22, 2025

    @elinor-fung
    MemberAuthor

    Breaking change doc issue: dotnet/docs#46356

  25. removed
    needs-breaking-change-doc-created
    on May 22, 2025
  26. locked and limited conversation to collaborators on Jun 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-change

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions