Unable to load Az modules - Assembly with same name is already loaded · Issue #4727 · PowerShell/vscode-powershell · GitHub
Skip to content

Unable to load Az modules - Assembly with same name is already loaded #4727

Open

Description

@wsmelton

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

If you load a PowerShell file that has references to Azure PowerShell commands the assemblies are being loaded without loading the module fully.

This is repeat of #3012 which was closed with no solution. Extensions do not matter in this case, it is when a file that has Azure module references is opened the assemblies get loaded but not the actual modules.

PowerShell Version

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.13
PSEdition                      Core
GitCommitId                    7.2.13
OS                             Darwin 21.6.0 Darwin Kernel Version 21.6.0: Thu Jul  6 22:20:00 PDT 2023; root:xnu-8020.240.18.702.13~1/RELEASE_A…
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

> code-insiders --version 
1.82.0-insider
f1302be1e67e3af5fbeb8bbb2ea784de7bc96150
arm64

Extension Version

> code --list-extensions --show-versions | Select-String powershell

ms-vscode.powershell@2023.6.0

Steps to Reproduce

  1. Close all PowerShell files
  2. Restart session for PowerShell Extension
  3. Run following command to list loaded assemblies
[System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location | Sort-Object -Property FullName | Select-Object -Property FullName, Location, GlobalAssemblyCache, IsFullyTrusted | Where FullName -like '*azure*' | ft
  1. Note nothing is returned
  2. Run Get-Module Az*
  3. Nothing is returned
  4. Open file (any file) with Az PowerShell references
  5. Rerun step 3
  6. All assemblies for the Az.Accounts and related commands are loaded
  7. Rerun step 5
  8. Nothing is returned
  9. Run Import-Module Az.Accounts
  10. Error returned: Import-Module: Assembly with same name is already loaded

Visuals

VSCode-PowerShell-Az.mp4

Logs

ext_logs.zip

Activity

  1. SydneyhSmith commented on Sep 6, 2023

    @SydneyhSmith
    Collaborator

    Thanks Shawn Melton (@wsmelton) , to clarify is this a new issue for you or has this been happening on previous versions of the extension?

    "If you load a PowerShell file that has references to Azure PowerShell commands the assemblies are being loaded without loading the module fully." We think this is happening from a Get-Help call in tab completion

    It looks like the error itself may be coming from our resolve handler or something similar

  2. wsmelton commented on Sep 6, 2023

    @wsmelton
    Author

    Always happening since I started using an Apple device (M1), switched to this month ago. I can't recall if Windows device gives me this issue...I can test that shortly.

    Extension versions I tried v2023.6.0 back to v2023.3.3, issue exist in all of them.

  3. wsmelton commented on Sep 6, 2023

    @wsmelton
    Author

    So I cannot replicate this on Windows (Windows 10, VS Code Insiders, PowerShell 7.2, and v2023.9.1).

  4. moved this from Todo to In Progress in Flying Foxon Sep 6, 2023
  5. andyleejordan commented on Sep 6, 2023

    @andyleejordan
    Member

    Well, that's interesting. Usually it's the other way around (issue occurs on Windows but nowhere else).

  6. wsmelton commented on Sep 6, 2023

    @wsmelton
    Author

    I'll grab diagnostic logs for the windows device I used tomorrow and upload them.

  7. andyleejordan commented on Sep 7, 2023

    @andyleejordan
    Member

    Shawn Melton (@wsmelton) could you share your PSSA settings file? We noticed:

    2023-09-05 08:05:03.512 -05:00 [VRB] Using Script Analyzer settings path - '/Users/smelton/ldx/Agents/ScriptAnalyzerSettings.psd1'.

    and believe there is a PSSA rule that can cause modules to be imported when analyzing the file.

  8. wsmelton commented on Sep 7, 2023

    @wsmelton
    Author

    There is no PSSA settings file in the referenced project

    image

    We have this in most of the project's settings.json file:
    "powershell.scriptAnalysis.settingsPath": "./ScriptAnalyzerSettings.psd1"

  9. andyleejordan commented on Sep 7, 2023

    @andyleejordan
    Member

    Your log said it loaded '/Users/smelton/ldx/Agents/ScriptAnalyzerSettings.psd1', is there anything in that file? Could be a bug that it found it but might be what's going on.

  10. andyleejordan commented on Sep 7, 2023

    @andyleejordan
    Member

    Oh wait nevermind also in the log:

    [INF] Unable to find PSSA settings file at '/Users/smelton/ldx/Agents/ScriptAnalyzerSettings.psd1'. Loading default rules.

    And I misread that earlier.

  11. moved this from In Progress to Todo in Flying Foxon Sep 8, 2023
  12. 11 remaining items

  13. randriksen commented on Nov 26, 2023

    @randriksen

    I'm seeing the same issue with Az.Accounts. And it's really annoying when that module is the best way to get authorization tokens to work with any of the Azure related Rest API's. Should we also report it as a bug for the Az module?

  14. tropez1971 commented on Nov 28, 2023

    @tropez1971

    I'm seeing the same issue with Az.Accounts. And it's really annoying when that module is the best way to get authorization tokens to work with any of the Azure related Rest API's. Should we also report it as a bug for the Az module?

    Same here, pain in the butt. No current workaround.

  15. andyleejordan commented on Nov 29, 2023

    @andyleejordan
    Member

    Should we also report it as a bug for the Az module?

    Yes please.

  16. tropez1971 commented on Nov 30, 2023

    @tropez1971

    Found a workaround. If I do a forced update of both the Az.Storage and Az.Accounts modules, I’m back to working.

  17. PaulBendall commented on Mar 13, 2024

    @PaulBendall

    ...I would highly suggest just hiding the Extension Terminal and using normal pwsh instances in the VS Code terminal pane (click the + button and you can also set powershell.integratedConsole.startInBackground to hide the Extension Terminal at startup and just let it server its primary purpose in the background, which is powering the extension's features by hosting an LSP server).

    I'm seeing this issue with MS Graph SDK. Is there an idiot's guide to implementing Andy Jordan (@andyleejordan) recommendation? I found that when I want to Debug code the PIC is used regardless of having the powershell.integratedConsole.startInBackground to hide.

  18. lbouriez commented on Jun 7, 2024

    @lbouriez

    Found a workaround. If I do a forced update of both the Az.Storage and Az.Accounts modules, I’m back to working.

    I am trying to do a remove and import of these 2 but I still get the issue. Can you post your workaround, maybe i am missing something

  19. JustinGrote commented on Jun 10, 2024

    @JustinGrote
    Collaborator

    Paul Bendall (@PaulBendall) the issue has nothing to do with vscode specifically, basically what happens is that the Az modules have some common .NET libraries, and if one loads a lower version before another one that requires a higher version, that module will run into an error. It's even worse in PowerShell 5.1 vs 7 because assembly binding redirects aren't a thing.

    It appears to happen more in vscode because the engine that makes vscode work, despite hiding some things properly, still has some common DLLs it has to load that might conflict

    The best thing to do is to clean out all your Az modules, and then install specifically the ones you need at the same time with Install-PSResource to ensure you get the correct interdependencies, and then update all those modules together.

    Also, you may have other modules loaded that use the same DLLs as Az (such as Azure.Identity). Ideally you load those in your system after Az and on Powershell 7+ so that binding redirects can work. Also keep your vscode extension up to date.

    That's the best advice I can offer.

  20. o-l-a-v commented on Oct 1, 2024

    @o-l-a-v
    Contributor

    I made an issue in both the Azure PowerShell (Az) and the Microsoft Graph SDK PowerShell (Microsoft.Graph) repos.

    Could someone more knowledgeable than me about "proper assembly isolation" chime in in respective issues on how they should proceed to fix this? Is there a best practice reference guide? Or a reference module that does this right?


    Edit: Other modules:


    Edit 2: Created PowerShell discussion about this topic:

  21. JustinGrote commented on Oct 1, 2024

    @JustinGrote
    Collaborator

    It's called an Assembly Load Context, and I believe Az already has it partially implemented but they use it to hide only one assembly which doesn't really do the trick :). Also this would be for PS7+ only, wouldn't work for PS5.1

    https://learn.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext?view=net-8.0

  22. andyleejordan commented on Oct 1, 2024

    @andyleejordan
    Member

    It is very strange what Az does with their ALC. Some modules use it to hide a one (or a few) assemblies, some don't use it all. None of them use it correctly. Granted, "correct" is hard to get right with ALC and it's a ridiculous ask of module others, but is unfortunately just how .NET apps work.

  23. o-l-a-v commented on Oct 2, 2024

    @o-l-a-v
    Contributor

    As Microsoft seems to be focusing on security lately for many projects: Can ALC be an argument for better security?

  24. matthiashaldimann commented on Oct 6, 2024

    @matthiashaldimann

    Not a solution, but a workaround that may help some of you while we wait for a fix:
    I am able to avoid the conflict between Microsoft.Graph and vscode's Powershell extension by using the
    PowerShell Launch Current File in Temporary Console
    debug configuration instead of the
    PowerShell Launch Current File
    debug configuration. I am not using Az, but it's not unlikely that the same workaround works for this module too.

  25. JustinGrote commented on Oct 6, 2024

    @JustinGrote
    Collaborator

    Matthias Haldimann (@matthiashaldimann) that's a pretty good workaround as the temp console doesn't load PSES, so it won't have the same assembly conflicts.

  26. SamErde commented on Mar 12, 2026

    @SamErde

    I just released v1.0 of my DLL Pickle module that prevents most cases of these DLL version conflicts. Please try it out and let me know your results!

    Install-Module -Name 'DLLPickle' -Scope CurrentUser
    Import-Module -Name 'DLLPickle'
    Import-DPLibrary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions