Repository navigation
Refactor module assembly loading logic to allow the whole module to be loaded in a custom ALC - #16889
Conversation
|
Dongbo Wang (@daxian-dbw) Is it all only about binary modules? |
|
This is only for binary module, and only for binary modules that do not use types from the conflicting assembly as parameter type or return type. We are not going with the "Import-Module -Isolated" approach because of the type-identity issue. An end user won't really understand how the module is supposed to work, and allowing the end user to choose whether or not to import a module to a custom ALC just won't work -- the end user will run into the type-identity issue without a clue of what's going on, and will just believe it's a broken feature. We have discussed this in the RFC in length, and it's clear that it's the module author's responsibility to avoid conflicts, and our job is to make that easier. |
Can we replace the dll with custom test dll? Newtonsoft is updated frequently (with security fixes too) and we will have to track and update it. |
|
No need to update the version in the test. The test is deliberately using an old version |
I hope MSFT compliance CIs pass this :-) |
Paul Higinbotham (PaulHigin)
left a comment
There was a problem hiding this comment.
Overall, changes LGTM. I just have a few comments/questions, mainly about making sure comments are clear.
|
Paul Higinbotham (@PaulHigin) Thanks for the review! I will review the comments to make them more descriptive and with more details. |
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
Dongbo Wang (@daxian-dbw) I forget ask about processing native dll-s. I don't see the scenarios in description and tests. Do you consider this? |
|
No special handling to native dlls. A module author can register the |
|
Great! |
…e loaded in a custom ALC (PowerShell#16889)
PR Summary
Related to #2083
Refactor module assembly loading logic to allow the whole module to be loaded in a custom ALC.
PR Context
The document Loading through .NET Core Assembly Load Contexts introduces a technique to allow a module author to fully control the potentially conflicting dependencies by leveraging a custom
AssemblyLoadContext. This technique was used by the Bicep module, and have a great example documented in this blog post: Resolving PowerShell Module Conflicts.This technique requires the use of dependency assemblies to be wrapped in a "bridge" assembly. The true module assemblies will need to reference the "bridge" assembly, depending on which to pull in the real dependencies. It's usually not easy to satisfy that requirement, especially for an existing module, that often means significant refactoring.
This PR refactors how assembly loading works during module loading. It leverages the existing-but-not-really-used
AssemblyCacheto make sure a nested module assembly or a root module assembly will not be loaded into the default ALC when they are designed to be loaded by a custom ALC of the module.With the changes in this PR, we don't need the "bridge" assembly anymore, the real nested/root module assemblies can be directly loaded in a custom ALC, which handles all their dependencies in an isolated way.
Example
The
IsolatedModulelocated at<repo-root>\test\tools\Modules\IsolatedModuleserves as an example of such a module, and it's also used in our test.The module assemblies are published by
<repo-root>\test\tools\TestAlc. Simply rundotnet publishin this folder to populate the assemblies to the folder<repo-root>\test\tools\Modules\IsolatedModulewith the pre-defined folder structure.The layout of files in
IsolatedModuleis:The content of
IsolatedModule.psd1is:The nested module
Test.Isolated.Init.dllis the only one that PowerShell can discover under the module base folder. It contains a custom ALC implementation and will set up theResolvinghandler to handle loading the real module assembliesTest.Isolated.Nested.dllandTest.Isolated.Root.dll.The processing order for module loading is
RequiredAssemblies->NestedModules->RootModule.In this example,
Test.Isolated.Init.dllis the first nested module and it gets processed first. After that,Resovlinghandler has been setup to deal withTest.Isolated.Nested.dllandTest.Isolated.Root.dll. PowerShell won't be able to locate those 2 assemblies, so it's up to theResovlinghandler to load them in the custom ALC.Test.Isolated.Nested.dlldepends on the10.0.0.0version ofNewtonsoft.Json.dll, which will be loaded by the custom ALC when requested.All module assemblies will be put in the
AssemblyCache, and PowerShell type resolution always searchesAssemblyCachefirst (existing behavior). So, public types exposed byTest.Isolated.Nested.dllandTest.Isolated.Root.dllwill be resolvable, but the specificNewtonsoft.Json.dllunder theDependenciesfolder will always be invisible to PowerShell.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.