Skip to content

Implementation of AppContext.BaseDirectory and DllImportSearchPath.AssemblyDirectory handling on NativeAOT in the SharedLibrary scenario is unintuitive #112290

@jkoritzinsky

Description

@jkoritzinsky

On NativeAOT today, DllImportSearchPath.AssemblyDirectory tells the runtime to load the requested library from Path.Combine(AppContext.BaseDirectory, libName). On NativeAOT, AppContext.BaseDirectory is always defined as the directory of the process executable. As a result, trying to load a native shared library from a NativeAOT application will always look next to the process entry module.

This makes sense for NativeAOT applications that produce executables, but this behavior becomes unintuitive for SharedLibrary-based NativeAOT applications. In that scenario, using DllImportSearchPath.AssemblyDirectory will look next to the entry executable of the process, not next to the NativeAOT shared library.

This makes it extremely difficult to load any native shared libraries that are shipped alongside a NativeAOT shared library. Users are forced to manually use P/Invokes to GetModuleFileName and dladdr to determine their path (or, if loaded by another native library, passed these values) and manually call NativeLibrary.Load(string) with the full path to load, losing all of the niceties of LibraryImport/DllImport or the NativeLibrary.Load overload that takes DllImportSearchPath (like prepending lib and appending .so/.dylib on different platforms).

I propose that on NativeAOT SharedLibrary scenarios, the "Assembly directory" that's used is the directory that the NativeAOT shared library is installed in, not the directory where the executable that loads the library is located.

@agocke has also expressed that we should continue to have AppContext.BaseDirectory and the "Assembly directory" be the same, and if we decide to do this to change the AppContext.BaseDirectory path to always be the path to the NativeAOT-produced library or exe that is calling AppContext.BaseDirectory, not always the process executable path.

@max-charlamb hit this while working on the native unwinder work for the cDAC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-NativeAOT-coreclrin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions