cloudbase / powershell-yaml

PowerShell CmdLets for YAML format manipulation
Apache License 2.0
434 stars 78 forks source link

This Module conflicts with Az.Aks #109

Closed matzter closed 1 year ago

matzter commented 1 year ago

I don't understand enough how this works, but I have this conflict for over an year now and finally figured what is causing it. If powershell-yaml module is loaded, Az.Aks can not be imported anymore.

PowerShell 7.3.4

> get-module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Script     2.2.6                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

> import-module powershell-yaml
> import-module az.aks
Import-Module: Assembly with same name is already loaded

> get-module
ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     0.0                   Load-Assemblies
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script     0.4.7                 powershell-yaml                     {ConvertFrom-Yaml, ConvertTo-Yaml, cfy, cty}
Script     2.2.6                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

> Get-AzAksCluster
Get-AzAksCluster: The 'Get-AzAksCluster' command was found in the module 'Az.Aks', but the module could not be loaded due to the following error: [Assembly with same name is already loaded]
For more information, run 'Import-Module Az.Aks'.

This has been executed on a fresh VM, no other tools or modules have been installed, and the issue can be reproduced.

gabriel-samfira commented 1 year ago

Hi @matzter ,

Yes, both powershell-yaml and Az.Aks load YamlDotNet.dll, although different versions. This module however, checks before attempting to load the assembly. That way, if the assembly is already loaded and has all the required types, it does nothing. Once an assembly is loaded in the current AppDomain it can't really be unloaded without unloading the AppDomain (closing the powershell process).

This is not an issue I can fix in this module. This is something that the Az.Aks module needs to address. YamlDotNet is a third party library that can already exist on a system and may be of a different version then what ships with a module. If the version we try to load differs from the one that is already loaded in the current app domain, you get the error you mentioned.

The version Az.Aks loads is:

YamlDotNet, Version=5.0.0.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e

The version that powershell-yaml tries to load is:

YamlDotNet, Version=13.0.0.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e

hence the error.

This is why I ended up using the Load-Assemblies.ps1 script instead of using RequiredAssemblies directive in the psd1 file that Az.Aks uses. It made no sense to require a strict version of YamlDotNet.dll (as long as some basic guardrails were used, like checking for needed types), especially considering that users may install their own library and load it before loading powershell-yaml. It also made sense to use a method that offered some sort of idempotency, which RequiredAssemblies didn't seem to offer in cases where the assembly version differed.

A really shaky workaround would be to load Az.Aks first, then load powershell-yaml. Powershell-yaml should work with that version and skips trying to load the assembly if it's already loaded.

gabriel-samfira commented 1 year ago

I suggest also opening an issue against Az.Aks. They use a really old version of the YamlDotNet.dll assembly.

matzter commented 1 year ago

Thank you very much for the detailed and prompt response. I created an incident for az.aks https://github.com/Azure/azure-powershell/issues/21895