Skip to content

Commit

Permalink
馃悰 Load from domain BaseDirectory
Browse files Browse the repository at this point in the history
This cover cases where the executable is run with dotnet host.
  • Loading branch information
pleonex committed Nov 29, 2023
1 parent 97d6c77 commit 1880ec9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void LoadingInvalidAssemblyReturnsNull()
[Test]
public void LoadingIgnoreSystemLibraries()
{
IEnumerable<Assembly> loaded = TypeLocator.Instance.LoadContext.TryLoadFromExecutingDirectory();
IEnumerable<Assembly> loaded = TypeLocator.Instance.LoadContext.TryLoadFromBaseLoadDirectory();

Assert.That(loaded.Select(a => a.GetName().Name), Does.Not.Contain("testhost"));
}
Expand All @@ -80,7 +80,7 @@ public void LoadingIgnoreSystemLibraries()
public void LoadingExecutingDirGetsYarhl()
{
// We cannot use ConverterLocator as it will load Yarhl as it uses some of its types.
IEnumerable<Assembly> loaded = TypeLocator.Instance.LoadContext.TryLoadFromExecutingDirectory();
IEnumerable<Assembly> loaded = TypeLocator.Instance.LoadContext.TryLoadFromBaseLoadDirectory();

Assert.That(loaded.Select(a => a.GetName().Name), Does.Contain("Yarhl"));

Expand Down
7 changes: 4 additions & 3 deletions src/Yarhl.Plugins/AssemblyLoadContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public static IEnumerable<Assembly> TryLoadFromDirectory(this AssemblyLoadContex
}

/// <summary>
/// Try to load every .NET assembly in the directory of the current process.
/// Try to load every .NET assembly in the current domain base directory.
/// This is usually the process path or the entry assembly path.
/// </summary>
/// <param name="loader">The load context to use to load.</param>
/// <returns>A collection of assemblies that could be loaded.</returns>
Expand All @@ -97,9 +98,9 @@ public static IEnumerable<Assembly> TryLoadFromDirectory(this AssemblyLoadContex
/// a security risk by running arbitrary code.
/// If an assembly fails to load it will be silently skipped.
/// </remarks>
public static IEnumerable<Assembly> TryLoadFromExecutingDirectory(this AssemblyLoadContext loader)
public static IEnumerable<Assembly> TryLoadFromBaseLoadDirectory(this AssemblyLoadContext loader)
{
string programDir = Path.GetDirectoryName(Environment.ProcessPath) ??
string programDir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) ??
throw new ArgumentException("Cannot determine process directory");

string[] libraryAssemblies = Directory.GetFiles(programDir, "*.dll");
Expand Down

0 comments on commit 1880ec9

Please sign in to comment.