8000 Can't import v1.0.2 to pwsh.exe v7.4.1 with error "Import-Module: Assembly with same name is already loaded" · Issue #21201 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

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

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

Open
3 tasks done
o-l-a-v opened this issue Feb 8, 2024 · 10 comments
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@o-l-a-v
Copy link
o-l-a-v commented Feb 8, 2024

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues.

Steps to reproduce

Using pwsh.exe v7.4.1

  • Open pwsh.exe, enter Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.0.2 -Verbose -Debug

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

  • Windows 11 23H2
  • Visual Studio Code v1.86.0 x64
  • vscode-powershell extension v2024.0.0
  • Microsoft.PowerShell.PSResourceGet v1.0.3
  • PowerShell v7.4.1 x64

Visuals

No response

@o-l-a-v o-l-a-v added the Needs-Triage The issue is new and needs to be triaged by a work group. label Feb 8, 2024
@o-l-a-v
Copy link
Author
o-l-a-v commented Feb 8, 2024

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 o-l-a-v changed the title Can't import v1.0.2 to VSCode with error "Import-Module: Assembly with same name is already loaded" Can't import v1.0.2 to pwsh.exe v7.4.1 with error "Import-Module: Assembly with same name is already loaded" Feb 8, 2024
@o-l-a-v
Copy link
Author
o-l-a-v commented Feb 8, 2024

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
Copy link
Author
o-l-a-v commented Feb 8, 2024

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:

  • With Windows PowerShell, included modules are added to a version directory (<modulename>\<version>\<content>).
  • PSResourceGet, PowerShellGet and PackageManagement installs modules to <modulename>\<version>\<content>.
  • PowerShell Core does not have this version directory for included modules, so <modulename>\<content> instead of <modulename>\<version>\<content>.

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
Copy link
Member

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

@o-l-a-v
Copy link
Author
o-l-a-v commented Feb 8, 2024

@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
Copy link
Member

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

@adityapatwardhan
Copy link
Member

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

@o-l-a-v
Copy link
Author
o-l-a-v commented Feb 8, 2025

Here is an easy and reliable repro of this issue.

Prerequisite: Module of lower version must not be inside a versioned folder.

  • This is what happens when PSResourceGet is embedded inside a PowerShell release, but not in a versioned directory.

Does not repro:

ModulesAllVersioned
├─ Modules1
│  └─ Microsoft.PowerShell.PSResourceGet
│     └─ 1.0.6
│        └─ <content>
└─ Modules2
   └─ Microsoft.PowerShell.PSResourceGet
      └─ 1.1.0
         └─ <content>

Does repro:

ModulesFirstNotVersioned
├─ Modules1
│  └─ Microsoft.PowerShell.PSResourceGet
│     └─ <content>
└─ Modules2
   └─ Microsoft.PowerShell.PSResourceGet
      └─ 1.1.0
         └─ <content>

Repro:

  • Start PowerShell with no profile: pwsh -NoProfile
# Set PSModulePath in process scope
$env:PSModulePath = (
    '{0}\ModulesFirstNotVersioned\Modules1;{0}\ModulesFirstNotVersioned\Modules2' -f
    [System.Environment]::GetFolderPath('Desktop')
)

# Import Microsoft.PowerShell.PSResourceGet v1.1.0
Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.1.0 -Verbose -Debug

This gives:

PS > Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -RequiredVersion 1.1.0 -Verbose -Debug

VERBOSE: Loading module from path 'C:\Users\olavb\Desktop\ModulesFirstNotVersioned\Modules1\Microsoft.PowerShell.PSResourceGet\Microsoft.PowerShell.PSResourceGet.psd1'.
VERBOSE: Loading module from path 'C:\Users\olavb\Desktop\ModulesFirstNotVersioned\Modules2\Microsoft.PowerShell.PSResourceGet\1.1.0\Microsoft.PowerShell.PSResourceGet.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Users\olavb\Desktop\ModulesFirstNotVersioned\Modules2\Microsoft.PowerShell.PSResourceGet\1.1.0\PSGet.Format.ps1xml'.
VERBOSE: Loading module from path 'C:\Users\olavb\Desktop\ModulesFirstNotVersioned\Modules2\Microsoft.PowerShell.PSResourceGet\1.1.0\Microsoft.PowerShell.PSResourceGet.psm1'.
VERBOSE: Exporting function 'Import-PSGetRepository'.
VERBOSE: Importing function 'Import-PSGetRepository'.
VERBOSE: Loading module from path 'C:\Users\olavb\Desktop\ModulesFirstNotVersioned\Modules2\Microsoft.PowerShell.PSResourceGet\1.1.0\Microsoft.PowerShell.PSResourceGet.dll'.
Import-Module: Could not load file or assembly 'Microsoft.PowerShell.PSResourceGet, Version=1.1.0.1, Culture=neutral, PublicKeyToken=null'. Assembly with same name is already loaded

PS >

@danielniccoli
Copy link
danielniccoli commented May 22, 2025

I also run into this frequently and I have no idea how to solve it.

> Import-Module Microsoft.Graph.Authentication -MaximumVersion 2.24.0 -Verbose -Debug

VERBOSE: Loading module from path 'C:\Users\niccodan\AppData\Local\PowerShell\Modules\Microsoft.Graph.Authentication\2.24.0\Microsoft.Graph.Authentication.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Users\niccodan\AppData\Local\PowerShell\Modules\Microsoft.Graph.Authentication\2.24.0\Microsoft.Graph.Authentication.format.ps1xml'.
VERBOSE: Populating RepositorySourceLocation property for module Microsoft.Graph.Authentication.
VERBOSE: Loading module from path 'C:\Users\niccodan\AppData\Local\PowerShell\Modules\Microsoft.Graph.Authentication\2.24.0\Microsoft.Graph.Authentication.dll'.
Import-Module: Could not load file or assembly 'Microsoft.Graph.Authentication, Version=2.24.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Assembly with same name is already loaded

The .dll id loaded twice and the second time it displays an error.

@RPitt
Copy link
RPitt commented May 22, 2025

Trying to add some insight, this is my understanding:

Background:

PowerShell is supposed to be able to manage module versions side-by-side, loading the appropriate version as required.
However a fundamental limitation in the scheme is that PowerShell can't unload/reload .Net assemblies.

This is problematic for versioned modules that depends on versioned assemblies like so

  • 1.0.0\modulename.psd1 uses assemblyname.dll v1.0.0
  • 1.0.1\modulename.psd1 uses assemblyname.dll v1.0.1

Because once the modulename +assembly is loaded at one version, there's no way to switch to a different version in that session, that's what Assembly with same name is already loaded is saying.

Import-Module version searching bug?

I think the @o-l-a-v has rightly identified that there is something defficent in the version searching / loading logic.

My suspicion is that for modules in versioned subfolders (e.g. 1.0.1\) PowerShell uses the folder name as quick indicator of the version whilst building a list of candidates.
However for modules that are not in a versioned folder it seems to actually load the module even before it's determined if its the appropraite version.
This then causes the problem when it later tries to load the actually desired version and hits an error because the previous version is already loaded.

^ I'm hoping someone else can anaylse and confirm or disprove my suspcion.

@danielniccoli

Note, whilst your error has similarities I suspect your error is just down to a bad/mixed install of Microsoft.Graph components.
Check your versions with Get-Module -ListAvailable Microsoft.Graph* and if they're inconsistent I'd suggest uninstall/deleting them and re-installing Microsoft.Graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
Development

No branches or pull requests

5 participants
0