-
Notifications
You must be signed in to change notification settings - Fork 2
🐛 Fix plugin dependency loading using AssemblyLoadContext #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1b7bf8e
Initial plan
Copilot 9c0f2ac
Initial analysis: understand plugin dependency loading issue
Copilot eda9832
Implement PluginLoadContext for dependency resolution in plugin loading
Copilot 3b6cc4a
Final fix: Restore .NET 9.0 target framework and complete plugin depe…
Copilot a14d989
Address PR review feedback: restore .NET 9 collection expression synt…
Copilot b95ca87
Update PluginLoaderTests.cs
matt-goldman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System.Reflection; | ||
| using System.Runtime.Loader; | ||
|
|
||
| namespace Blake.BuildTools.Utils; | ||
|
|
||
| /// <summary> | ||
| /// A custom AssemblyLoadContext that provides isolated plugin loading with dependency resolution. | ||
| /// Each plugin gets its own load context to avoid dependency conflicts. | ||
| /// </summary> | ||
| internal class PluginLoadContext : AssemblyLoadContext | ||
| { | ||
| private readonly AssemblyDependencyResolver _resolver; | ||
|
|
||
| public PluginLoadContext(string pluginPath) : base(isCollectible: true) | ||
| { | ||
| _resolver = new AssemblyDependencyResolver(pluginPath); | ||
| } | ||
|
|
||
| protected override Assembly? Load(AssemblyName assemblyName) | ||
| { | ||
| string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); | ||
| if (assemblyPath != null) | ||
| { | ||
| return LoadFromAssemblyPath(assemblyPath); | ||
| } | ||
|
|
||
| // Return null to fall back to default load context for shared dependencies | ||
| return null; | ||
| } | ||
|
|
||
| protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
| { | ||
| string? libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName); | ||
| if (libraryPath != null) | ||
| { | ||
| return LoadUnmanagedDllFromPath(libraryPath); | ||
| } | ||
|
|
||
| return IntPtr.Zero; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using Blake.BuildTools.Utils; | ||
| using Microsoft.Extensions.Logging; | ||
| using Xunit; | ||
|
|
||
| namespace Blake.BuildTools.Tests.Utils; | ||
|
|
||
| public class PluginLoaderTests | ||
| { | ||
| [Fact] | ||
| public void LoadPluginDLLs_WithPluginWithDependencies_LoadsSuccessfully() | ||
| { | ||
| // Arrange | ||
| var logger = new TestLogger(); | ||
| var pluginPath = Path.GetFullPath(Path.Combine( | ||
| Directory.GetCurrentDirectory(), | ||
| "..", "..", "..", "..", "..", "tests", "Blake.IntegrationTests", | ||
| "TestPluginWithDependencies", "bin", "Debug", "net8.0", | ||
| "BlakePlugin.TestPluginWithDependencies.dll" | ||
| )); | ||
|
|
||
| // Skip test if plugin doesn't exist (build not run) | ||
| if (!File.Exists(pluginPath)) | ||
| { | ||
| Assert.True(true, "Plugin not built - skipping test"); | ||
| return; | ||
| } | ||
|
|
||
| var files = new List<string> { pluginPath }; | ||
| var plugins = new List<PluginContext>(); | ||
|
|
||
| // Act & Assert - should not throw exception | ||
| var exception = Record.Exception(() => | ||
| { | ||
| // Use reflection to call the private method | ||
| var method = typeof(PluginLoader).GetMethod("LoadPluginDLLs", | ||
| System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); | ||
| method?.Invoke(null, new object[] { files, plugins, logger }); | ||
| }); | ||
|
|
||
| // Assert | ||
| Assert.Null(exception); | ||
| Assert.Single(plugins); | ||
| Assert.Equal("BlakePlugin.TestPluginWithDependencies", plugins[0].PluginName); | ||
|
|
||
| // Ensure no errors were logged | ||
| Assert.Empty(logger.ErrorMessages); | ||
| } | ||
|
|
||
| private class TestLogger : ILogger | ||
| { | ||
| public List<string> ErrorMessages { get; } = new List<string>(); | ||
| public List<string> InfoMessages { get; } = new List<string>(); | ||
|
|
||
| public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null; | ||
| public bool IsEnabled(LogLevel logLevel) => true; | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
| { | ||
| var message = formatter(state, exception); | ||
| if (logLevel == LogLevel.Error) | ||
| { | ||
| ErrorMessages.Add(message); | ||
| } | ||
| else if (logLevel == LogLevel.Information) | ||
| { | ||
| InfoMessages.Add(message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...IntegrationTests/TestPluginWithDependencies/BlakePlugin.TestPluginWithDependencies.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <EnableDynamicLoading>true</EnableDynamicLoading> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Blake.BuildTools\Blake.BuildTools.csproj"> | ||
| <Private>false</Private> | ||
| <ExcludeAssets>runtime</ExcludeAssets> | ||
| </ProjectReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
39 changes: 39 additions & 0 deletions
39
tests/Blake.IntegrationTests/TestPluginWithDependencies/TestPluginWithDependencies.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using Blake.BuildTools; | ||
| using Microsoft.Extensions.Logging; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace BlakePlugin.TestPluginWithDependencies; | ||
|
|
||
| public class TestPluginWithDependencies : IBlakePlugin | ||
| { | ||
| public Task BeforeBakeAsync(BlakeContext context, ILogger? logger = null) | ||
| { | ||
| logger?.LogInformation("TestPluginWithDependencies: BeforeBakeAsync called"); | ||
|
|
||
| // Use Newtonsoft.Json to test dependency loading | ||
| var testObject = new { Message = "Plugin with dependencies loaded successfully", PageCount = context.MarkdownPages.Count }; | ||
| var serialized = JsonConvert.SerializeObject(testObject); | ||
|
|
||
| logger?.LogInformation("TestPluginWithDependencies: Serialized data: {SerializedData}", serialized); | ||
|
|
||
| // Create a marker file to prove the plugin ran with dependencies | ||
| var testFilePath = Path.Combine(context.ProjectPath, ".plugin-with-deps-before-bake.txt"); | ||
| File.WriteAllText(testFilePath, serialized); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public Task AfterBakeAsync(BlakeContext context, ILogger? logger = null) | ||
| { | ||
| logger?.LogInformation("TestPluginWithDependencies: AfterBakeAsync called with {PageCount} generated pages", context.GeneratedPages.Count); | ||
|
|
||
| // Use Newtonsoft.Json again to ensure dependency is still available | ||
| var testObject = new { Message = "Plugin dependencies working in AfterBakeAsync", GeneratedPageCount = context.GeneratedPages.Count }; | ||
| var serialized = JsonConvert.SerializeObject(testObject); | ||
|
|
||
| var testFilePath = Path.Combine(context.ProjectPath, ".plugin-with-deps-after-bake.txt"); | ||
| File.WriteAllText(testFilePath, serialized); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.