Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Import-Module : Could not load file or assembly Microsoft.Bcl.AsyncInterfaces

Open fullenw1 opened this issue 2 years ago • 40 comments

I have this error only on PowerShell Core and not on Windows PowerShell 5.1 It was working fine until the latest release...

Import-Module: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Here is my PowerShell configuration:

PS> $psversionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

fullenw1 avatar Jan 04 '24 09:01 fullenw1

I confirm, we face the same issue on Linux (Ubuntu 20.04 LTS)

FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Tested with

  • Posh-SSH v 3.1.1
  • Posh-SSH v 3.1.2
  • Powershell Core 7.4.0
  • Powershell Core 7.5.0

doanduyhai avatar Jan 04 '24 10:01 doanduyhai

When you look at the $error what does the loadexception says? Sent from my iPhoneOn Jan 4, 2024, at 6:12 AM, DuyHai DOAN @.***> wrote: I confirm, we face the same issue

FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Tested with

Posh-SSH v 3.1.1 Posh-SSH v 3.1.2 Powershell Core 7.4.0 Powershell Core 7.5.0

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

darkoperator avatar Jan 04 '24 10:01 darkoperator

When you look at the $error what does the loadexception says? Sent from my iPhoneOn Jan 4, 2024, at 6:12 AM, DuyHai DOAN @.> wrote: I confirm, we face the same issue FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Tested with Posh-SSH v 3.1.1 Posh-SSH v 3.1.2 Powershell Core 7.4.0 Powershell Core 7.5.0 —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.>

Here is my test code


Write-Host "*** Installing Posh SSH Module 3.1.2 ***"
Install-Module -Name Posh-SSH -Force -RequiredVersion 3.1.2
Write-Host "*** Import Posh SSH Module ***"
try {
  Import-Module Posh-SSH
} catch {
  Write-Host "error = $($Error | Out-String)"
}

And here is the display

*** Installing Posh SSH Module 3.1.2 *** *** Import Posh SSH Module *** error = Import-Module: /home/docker/actions-runner/_work/_temp/55116c9b-0848-4837-ba77-fa8c3aa0f3ee.ps1:17 Line | 17 | Import-Module Posh-SSH | ~~~~~~~~~~~~~~~~~~~~~~ | Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, | Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

doanduyhai avatar Jan 04 '24 10:01 doanduyhai

I unlisted the module. It seems the last SSH.Net update added a dependency, so I can make it only work with one or the other at the moment. https://github.com/sshnet/SSH.NET/issues/1287

darkoperator avatar Jan 04 '24 11:01 darkoperator

(I know nothing about powershell modules) Is it possible to package Microsoft.Bcl.AsyncInterfaces.dll alongside Renci.SshNet.dll?

Rob-Hague avatar Jan 04 '24 12:01 Rob-Hague

I unlisted the module. It seems the last SSH.Net update added a dependency, so I can make it only work with one or the other at the moment. sshnet/SSH.NET#1287

Will you do a new release for the fix ?

Regards

doanduyhai avatar Jan 04 '24 12:01 doanduyhai

I did and I get error’s depending on the version of .net used still trying to figure what broken with this latest nugget Sent from my iPhoneOn Jan 4, 2024, at 8:01 AM, Rob Hague @.***> wrote: (I know nothing about powershell modules) Is it possible to package Microsoft.Bcl.AsyncInterfaces.dll alongside Renci.SshNet.dll?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

darkoperator avatar Jan 04 '24 13:01 darkoperator

I unlisted the module. It seems the last SSH.Net update added a dependency, so I can make it only work with one or the other at the moment. sshnet/SSH.NET#1287

Will you do a new release for the fix ?

Regards

If I can fix it, seems the dependency messes stuff up when running under .ner standard 2.0

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0   master ?9 ~1                        09:51:58 
╰─❯ New-SSHSession v0.local pi

PowerShell credential request
Enter your credentials.
Password for user pi: ********

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0   master ?9 ~1                        09:53:15 
╰─❯ ls

        Directory: C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a---        10/31/2023  11:00 AM          26784   Microsoft.Bcl.AsyncInterfaces.dll
-a---          1/4/2024   9:44 AM           5439   PoshSSH.deps.json
-a---          1/4/2024   9:44 AM          46080   PoshSSH.dll
-a---          1/4/2024   9:44 AM          22320   PoshSSH.pdb

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0   master ?9 ~1                        09:53:19 
╰─❯ add-type -Path .\microsoft.Bcl.AsyncInterfaces.dll
Add-Type: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

darkoperator avatar Jan 04 '24 13:01 darkoperator

I copied the files straight from the .Netstandard 2.0 nugget for the library it will not even let me load it by hand

darkoperator avatar Jan 04 '24 13:01 darkoperator

Where is Renci.SshNet.dll and SshNet.Security.Cryptography.dll? Should Microsoft.Bcl.AsyncInterfaces.dll be alongside those?

Rob-Hague avatar Jan 04 '24 14:01 Rob-Hague

all of the files from the dependecies json file

─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:09 
╰─❯ ls

        Directory: C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----          1/4/2024   9:35 AM                  Assembly
d----        10/22/2023   1:56 PM                  en-US
d----          6/2/2021   5:35 PM                  Format
-a---        10/31/2023  11:00 AM          26784   Microsoft.Bcl.AsyncInterfaces.dll
-a---         3/17/2021   8:03 PM         701992   Newtonsoft.Json.dll
-a---          1/4/2024   9:37 AM           5342 󰞷  Posh-SSH.psd1
-a---        10/23/2023   7:40 AM          93572 󰞷  Posh-SSH.psm1
-a---        12/31/2023   9:37 AM          46080   PoshSSH.dll
-a---        12/29/2023   9:57 AM         839168   Renci.SshNet.dll
-a---        10/22/2017   8:53 PM          34816   SshNet.Security.Cryptography.dll
-a---         1/25/2021   2:43 PM          20856   System.Buffers.dll
-a---         12/2/2020   4:59 AM        1608160   System.Management.Automation.dll
-a---         1/25/2021   2:43 PM         141184   System.Memory.dll
-a---         1/25/2021   2:43 PM         115856   System.Numerics.Vectors.dll
-a---         2/19/2020   6:05 AM          25984   System.Threading.Tasks.Extensions.dll

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:11 
╰─❯ add-type -Path .\microsoft.Bcl.AsyncInterfaces.dll
Add-Type: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:21 
╰─❯

darkoperator avatar Jan 04 '24 14:01 darkoperator

the .net standard 2.0 files load no problems under PS version 5.1

PS C:\Users\Carlos Perez> cd "C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH"
PS C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH> add-type -Path .\microsoft.Bcl.AsyncInterfaces.dll
PS C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH>

darkoperator avatar Jan 04 '24 14:01 darkoperator

Any more error details from $error.Exception?

Rob-Hague avatar Jan 04 '24 14:01 Rob-Hague

$error[0].exception | fl *

Message        : Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral,
                 PublicKeyToken=cc7b13ffcd2ddd51'.
FileName       :
FusionLog      :
TargetSite     : System.Reflection.Assembly LoadFromAssemblyPath(System.String)
Data           : {}
InnerException :
HelpLink       :
Source         : System.Private.CoreLib
HResult        : -2146232799
StackTrace     :    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
                    at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                    at Microsoft.PowerShell.Commands.AddTypeCommand.LoadAssemblies(IEnumerable`1 assemblies)
                    at System.Management.Automation.CommandProcessorBase.Complete()

darkoperator avatar Jan 04 '24 14:01 darkoperator

The HResult is 0x80131621 which has a few results on google, including https://github.com/pnp/powershell/discussions/1814

Perhaps there is some conflict somewhere. Does this show anything?

[System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property FullName, Location | fl

Rob-Hague avatar Jan 04 '24 14:01 Rob-Hague

Tried it at startup, after loading SSH.NET, after importing the module. Seems to error when it is actually used. DLL is in the same directory


╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:21 
╰─❯ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property
 FullName, Location | fl
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:14:26 
╰─❯ Add-Type -Path .\Renci.SshNet.dll
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:01 
╰─❯ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property
 FullName, Location | fl
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:03 
╰─❯ import-module -verbose .\PoshSSH.dll
VERBOSE: Importing cmdlet 'Get-SSHHostKey'.
VERBOSE: Importing cmdlet 'Get-SCPItem'.
VERBOSE: Importing cmdlet 'Get-SFTPItem'.
VERBOSE: Importing cmdlet 'New-SFTPSession'.
VERBOSE: Importing cmdlet 'New-SSHSession'.
VERBOSE: Importing cmdlet 'Set-SCPItem'.
VERBOSE: Importing cmdlet 'Set-SFTPItem'.
VERBOSE: Importing cmdlet 'New-SSHMemoryKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHJsonKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHOpenSSHKnownHost'.
VERBOSE: Importing alias 'Get-SSHJsonKnowHost'.
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:31 
╰─❯ New-SSHSession v0.local pi

PowerShell credential request
Enter your credentials.
Password for user pi: ********

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:41 
╰─❯ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property
 FullName, Location | fl
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1 

darkoperator avatar Jan 04 '24 15:01 darkoperator

I set on the project <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to have it bundle all dependencies and still not working on PS7, it has to be some bug or something changed in PS. I did notice that when I look at [System.AppDomain]::CurrentDomain.GetAssemblies() | where {$_.location -like "user"} I see the module is loaded and SSH.Net assembly, but if I look at all assemblies some are blank for some reason

darkoperator avatar Jan 04 '24 15:01 darkoperator

Now I'm completely lost. I can load it by hand, it is in the CAG but it can not find it .. .

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~2                                                                                                                             14:44:30 
╰─❯ [Reflection.Assembly]::LoadFile('C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH\Microsoft.Bcl.AsyncInterfaces.dll')

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH\Microsoft.Bcl.AsyncInterfaces.dll

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~2                                                                                                                             14:44:46 
╰─❯ New-SSHSession v0.local pi

PowerShell credential request
Enter your credentials.
Password for user pi: ********

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)

darkoperator avatar Jan 04 '24 18:01 darkoperator

I have a C:\Program Files\PowerShell\7\Microsoft.Bcl.AsyncInterfaces.dll which is version 5.0.0.0

The problem could be that the SSH.NET nuspec defines an exact dependency (with square brackets) on 8.0.0 and because PS7 has a dependency on a different version already, it refuses to load another.

You could try the following diff in SSH.NET:


diff --git a/src/Renci.SshNet/Renci.SshNet.csproj b/src/Renci.SshNet/Renci.SshNet.csproj
index 877b3b89..334df508 100644
--- a/src/Renci.SshNet/Renci.SshNet.csproj
+++ b/src/Renci.SshNet/Renci.SshNet.csproj
@@ -14,7 +14,7 @@
   </ItemGroup>

   <ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
-    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
+    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" /> <!-- or a higher version -->
   </ItemGroup>

   <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">

and try again with a local build of the library.

If things are still not getting anywhere, I would be in favour of removing the dependency from the library to unblock your module. It is not a necessary part of the library. cc @wojciechnagorski

Rob-Hague avatar Jan 04 '24 20:01 Rob-Hague

@Rob-Hague Thanks!! it worked now on both versions of PS, I will do more tests before I update my module

darkoperator avatar Jan 05 '24 14:01 darkoperator

Great

It would be useful if you could do a quick check with a local build using version 8.0.0 and see whether it still works (which would imply the problem would be fixed by https://github.com/sshnet/SSH.NET/pull/1288), or if it only works with a version <= 5.0.0 or whatever PS7 seems to be using (in which case, we should probably remove the dependency altogether)

Rob-Hague avatar Jan 05 '24 16:01 Rob-Hague

@darkoperator @Rob-Hague I'm confused. If the csproj diff fixes it, wouldn't this mean the problem is version 8.0.0? In this case https://github.com/sshnet/SSH.NET/pull/1288 wouldn't fix it, since SSH.NET would still need 8.0.0 (it would just also accept 9.0 etc.).

If PowerShell can't handle the newer Microsoft.Bcl.AsyncInterfaces Version for whatever reason, it would probably be best for SSH.NET to require >=1.0.0 instead >=8.0.0 . I doubt that SSH.NET actually needs an up-to-date version since it's just a compat package for .NET Framework / .NET Standard.

mus65 avatar Jan 05 '24 17:01 mus65

yep, with version 1.1.0 it worked in terms of loading and establishing a SSH connection. Version 8.0.0 loading was the version causing the problem in PS 7

darkoperator avatar Jan 05 '24 17:01 darkoperator

Can you prepare a PR?

WojciechNagorski avatar Jan 05 '24 17:01 WojciechNagorski

I think this one is covering it https://github.com/sshnet/SSH.NET/pull/1288

darkoperator avatar Jan 05 '24 17:01 darkoperator

@mus65 I'm not so clear on the nuances of dll loading 🙂

Can you can modify your PR to take version >= 1.0.0?

Rob-Hague avatar Jan 05 '24 17:01 Rob-Hague

@WojciechNagorski @Rob-Hague @darkoperator I updated my PR to fix both issues: https://github.com/sshnet/SSH.NET/pull/1288

mus65 avatar Jan 05 '24 17:01 mus65

@darkoperator PowerShell distributes Microsoft.Bcl.AsyncInterfaces, so if you want to load another version, that creates a conflict. Every time the PowerShell team changes the version of this assembly, it will break your module.

The technical explanation is here: https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.4

In the quick version (this is contained in the previous document), if you define a class that implements IModuleAssemblyInitializer , when the module loads, you can insert code that allows you to load a library with the version of your choice (via an AssemblyLoadContext)

I'm giving you the repository of the person who worked on the subject at the time (same use case as the doc) https://github.com/rjmholt/ModuleDependencyIsolationExample/tree/master

A random example : https://github.com/PSBicep/BicepNet/blob/main/BicepNet.PS/LoadContext/ModuleInitializer.cs

fMichaleczek avatar Jan 09 '24 23:01 fMichaleczek

Hello @darkoperator

Any update on the fix ? Should I use the latest release of the module ?

doanduyhai avatar Jan 18 '24 08:01 doanduyhai

You can use the latest available in the gallery, at the moment waiting to see when they will push a new nugget with the fixed version. If in a couple of weeks they do not push one I will push my own version by compiling from the development branch.

darkoperator avatar Jan 18 '24 17:01 darkoperator