From 1880ec9559b04076d15d79086549bb4a1b7cc357 Mon Sep 17 00:00:00 2001 From: Benito Palacios Sanchez Date: Wed, 29 Nov 2023 13:58:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Load=20from=20domain=20BaseDirec?= =?UTF-8?q?tory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This cover cases where the executable is run with dotnet host. --- .../AssemblyLoadContextExtensionsTests.cs | 4 ++-- src/Yarhl.Plugins/AssemblyLoadContextExtensions.cs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Yarhl.IntegrationTests/AssemblyLoadContextExtensionsTests.cs b/src/Yarhl.IntegrationTests/AssemblyLoadContextExtensionsTests.cs index 7670301..fcbcb1c 100644 --- a/src/Yarhl.IntegrationTests/AssemblyLoadContextExtensionsTests.cs +++ b/src/Yarhl.IntegrationTests/AssemblyLoadContextExtensionsTests.cs @@ -71,7 +71,7 @@ public void LoadingInvalidAssemblyReturnsNull() [Test] public void LoadingIgnoreSystemLibraries() { - IEnumerable loaded = TypeLocator.Instance.LoadContext.TryLoadFromExecutingDirectory(); + IEnumerable loaded = TypeLocator.Instance.LoadContext.TryLoadFromBaseLoadDirectory(); Assert.That(loaded.Select(a => a.GetName().Name), Does.Not.Contain("testhost")); } @@ -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 loaded = TypeLocator.Instance.LoadContext.TryLoadFromExecutingDirectory(); + IEnumerable loaded = TypeLocator.Instance.LoadContext.TryLoadFromBaseLoadDirectory(); Assert.That(loaded.Select(a => a.GetName().Name), Does.Contain("Yarhl")); diff --git a/src/Yarhl.Plugins/AssemblyLoadContextExtensions.cs b/src/Yarhl.Plugins/AssemblyLoadContextExtensions.cs index 70ee9b7..642cfaa 100644 --- a/src/Yarhl.Plugins/AssemblyLoadContextExtensions.cs +++ b/src/Yarhl.Plugins/AssemblyLoadContextExtensions.cs @@ -88,7 +88,8 @@ public static IEnumerable TryLoadFromDirectory(this AssemblyLoadContex } /// - /// 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. /// /// The load context to use to load. /// A collection of assemblies that could be loaded. @@ -97,9 +98,9 @@ public static IEnumerable TryLoadFromDirectory(this AssemblyLoadContex /// a security risk by running arbitrary code. /// If an assembly fails to load it will be silently skipped. /// - public static IEnumerable TryLoadFromExecutingDirectory(this AssemblyLoadContext loader) + public static IEnumerable 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");