PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
44.55k stars 7.2k forks source link

Can't import v1.0.2 to pwsh.exe v7.4.1 with error "Import-Module: Assembly with same name is already loaded" #21201

Open o-l-a-v opened 7 months ago

o-l-a-v commented 7 months ago

Prerequisites

Steps to reproduce

Using pwsh.exe v7.4.1

Using VSCode

Expected behavior

Module should load without problems, without workarounds like having to modify PSModulePath in process context to put preferred module directory first.

Actual behavior

PS C:\Users\olav.birkeland> Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2 -Verbose -Debug
VERBOSE: Loading module from path 'C:\program files\powershell\7\Modules\Microsoft.PowerShell.PSResourceGet\Microsoft.PowerShell.PSResourceGet.psd1'.
VERBOSE: Loading module from path 'C:\Users\olav.birkeland\AppData\Local\Microsoft\PowerShell\Modules\Microsoft.PowerShell.PSResourceGet\1.0.2\Microsoft.PowerShell.PSResourceGet.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Users\olav.birkeland\AppData\Local\Microsoft\PowerShell\Modules\Microsoft.PowerShell.PSResourceGet\1.0.2\PSGet.Format.ps1xml'.
VERBOSE: Loading module from path 'C:\Users\olav.birkeland\AppData\Local\Microsoft\PowerShell\Modules\Microsoft.PowerShell.PSResourceGet\1.0.2\Microsoft.PowerShell.PSResourceGet.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Users\olav.birkeland\AppData\Local\Microsoft\PowerShell\Modules\Microsoft.PowerShell.PSResourceGet\1.0.2\PSGet.Format.ps1xml'.
VERBOSE: Populating RepositorySourceLocation property for module Microsoft.PowerShell.PSResourceGet.
VERBOSE: Loading module from path 'C:\Users\olav.birkeland\AppData\Local\Microsoft\PowerShell\Modules\Microsoft.PowerShell.PSResourceGet\1.0.2\Microsoft.PowerShell.PSResourceGet.psm1'.
VERBOSE: Exporting function 'Import-PSGetRepository'.
VERBOSE: Importing function 'Import-PSGetRepository'.
VERBOSE: Loading module from path 'C:\Users\olav.birkeland\AppData\Local\Microsoft\PowerShell\Modules\Microsoft.PowerShell.PSResourceGet\1.0.2\Microsoft.PowerShell.PSResourceGet.dll'.
Import-Module: Assembly with same name is already loaded
PS C:\Users\olav.birkeland>

Error details

Exception             : 
    Type       : System.IO.FileLoadException
    Message    : Assembly with same name is already loaded
    TargetSite : 
        Name          : LoadBinaryModule
        DeclaringType : Microsoft.PowerShell.Commands.ModuleCmdletBase
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Source     : System.Management.Automation
    HResult    : -2146232799
    StackTrace : 
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean& found, String shortModuleName, Boolean disableFormatUpdates)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleNamedInManifest(PSModuleInfo parentModule, ModuleSpecification moduleSpecification, String moduleBase, Boolean searchModulePath, String prefix, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, Object privateData, Boolean& found, String shortModuleName, Nullable`1 manifestLanguageMode)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath, ExternalScriptInfo manifestScriptInfo, Hashtable data, Hashtable localizedData, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable`1 requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingExtensions(PSModuleInfo parentModule, String moduleName, String fileBaseName, String extension, String moduleBase, String prefix, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingMultiVersionModuleBase(String moduleBase, ManifestProcessingFlags manifestProcessingFlags, ImportModuleOptions importModuleOptions, Boolean& found)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingModulePath(PSModuleInfo parentModule, IEnumerable`1 modulePath, String name, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, PSModuleInfo& module)
   at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)
   at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String name)
   at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Import-Module], FileLoadException
FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand
InvocationInfo        : 
    MyCommand        : Import-Module
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 1
    Line             : Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2 -Verbose -Debug
    Statement        : Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2 -Verbose -Debug
    PositionMessage  : At line:1 char:1
                       + Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVer …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Import-Module
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Visuals

No response

o-l-a-v commented 7 months ago

Now I can't load it into pwsh.exe v7.4.1 either. 🤔

image

But it works from pwsh.exe v7.2.18:

image

o-l-a-v commented 7 months ago

Seems it has something to do with PSResourceGet v1.0.1 being included in PowerShell v7.4.1.

I deleted the whole %ProgramW6432%\PowerShell\7\Modules\Microsoft.PowerShell.PSResourceGet directory, then v7.4.1 managed to Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2.

o-l-a-v commented 7 months ago

It also works as expected if adding a version directory to the v7.4.1 included PSResourceGet. So was originally:

%ProgramW6432%\PowerShell\7\Modules\Microsoft.PowerShell.PSResourceGet\<content>

Became:

%ProgramW6432%\PowerShell\7\Modules\Microsoft.PowerShell.PSResourceGet\1.0.1\<content>

Additional info:

This might be an issue with how Import-Module works? Related issues:

And PScriptAnalyzer might be relevant here too? Simply opening a given script in VSCode loads PSResourceGet assemblies. I think this can be a related issue:

alerickson commented 7 months ago

Can you run Get-Module after starting a new session?

o-l-a-v commented 7 months ago

@alerickson

PowerShell 7.4.1
PS C:\Users\olavb> Get-Module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.3.4                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

PS C:\Users\olavb>

I could repro with another module too, so does not seem to be the issue of PSResourceGet?

alerickson commented 7 months ago

@o-l-a-v Thanks for the info, we're going to send this over to PowerShell/PowerShell

adityapatwardhan commented 7 months ago

The issue is about loading modules and not specific to PowerShellResourceGet. Transferring over to PowerShell/PowerShell repository.