Skip to content

pwsh.exe $env:PSModulePath Retained In 5.1 Console When code.cmd Called From pwsh.exe #3886

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
5 of 6 tasks
mattcargile opened this issue Mar 23, 2022 · 14 comments
Open
5 of 6 tasks
Assignees
Labels
Area-Startup Bug: PowerShell 5.1 Bugs when using PowerShell 5.1. Issue-Bug A bug to squash.

Comments

@mattcargile
Copy link

mattcargile commented Mar 23, 2022

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all 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

Issue found when Import-PowerShellDataFile was not recognized because Microsoft.PowerShell.Utility 7.0.0 is loaded in 5.1 Integrated Console. The libraries version is incorrect within the Visual Studio Code Host ( e.g. $Host ) on powershell.exe 5.1 when user launches code.cmd from pwsh.exe. The $env:PSModulePath is retained from the parent pwsh.exe session and causes issues in the powershell.exe session within code.exe. powershell.exe on it's own purges the pwsh.exe paths from the module path ( e.g. C:\Program Files\PowerShell\Modules and C:\program files\powershell\7\Modules ). See this comment on the code links to the PowerShell code base.

Modules like Terminal-Icons error out. I still need to use this version due to SOAP endpoints like SQL Server Reporting Services ( e.g. ReportingServicesTools module ).

PowerShell Version

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.18362.1171
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1171
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Visual Studio Code Version

PS> code --version
1.65.2
c722ca6c7eed3d7987c0d5c3df5c45f6b15e77d1
x64

Extension Version

PS> code --list-extensions --show-versions | Select-String powershell7:58  

[email protected]

Steps to Reproduce

Set Integrated Console in VS Code to Windows Powershell (x64). Close all instances of VS Code.

pwsh
code file.ps1

Execute below in VS Code Integrated Terminal

Get-Command 'Import-PowershellDataFile'
Get-Command -Module 'Microsoft.PowerShell.Utility' | Select-Object -Unique -Property Version

Visuals

N/A

Logs

Terminal Log Output

DEBUG: Logging started
DEBUG: Beginning EndProcessing block
VERBOSE: Removing PSReadLine        
DEBUG: Creating host configuration  
DEBUG: Determining REPL kind
DEBUG: REPL configured as PSReadLine
DEBUG: Configuring LSP transport  
DEBUG: Configuring debug transport
DEBUG: Session file writer created
VERBOSE: Adding AssemblyResolve event handler for dependency loading
VERBOSE: Loading EditorServices
VERBOSE: PID: 37124
VERBOSE: 
== Build Details ==
- Editor Services version: 3.1.5
- Build origin:            Release
- Build commit:            6e602caf43c37381f42fc4d47b680a9d96271949
- Build time:              3/10/2022 12:00:00 AM
VERBOSE: 
== Host Startup Configuration Details ==
 - Host name:                 Visual Studio Code Host
 - Host version:              2022.3.0
 - Host profile ID:           Microsoft.VSCode
 - PowerShell host type:      System.Management.Automation.Internal.Host.InternalHost

 - REPL setting:              PSReadLine
 - Session details path:
c:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\sessions\PSES-VSCode-35772-736086
 - Bundled modules path:      c:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules
 - Additional modules:        PowerShellEditorServices.VSCode
 - Feature flags:

 - Log path:
c:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\logs\1648066708-6efde32c-da6d-479b-a9dc-f595fa3e5cae16480667 
05294\EditorServices.log
 - Minimum log level:         Diagnostic

 - Profile paths:
   + AllUsersAllHosts:       C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
   + AllUsersCurrentHost:    C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.VSCode_profile.ps1
   + CurrentUserAllHosts:    C:\Users\user\OneDrive - Organization\Documents\WindowsPowerShell\profile.ps1
   + CurrentUserCurrentHost: C:\Users\user\OneDrive - Organizaton
\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1
DEBUG: Loaded 'Microsoft.PowerShell.Commands.Diagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Diagnostics\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerSh 
ell.Commands.Diagnostics.dll'
DEBUG: Loaded 'System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration.Install\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.Install.d 
ll'
DEBUG: Loaded 'Microsoft.PowerShell.Commands.Utility, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell. 
Commands.Utility.dll'
DEBUG: Loaded 'Microsoft.PowerShell.Commands.Management, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShe 
ll.Commands.Management.dll'
DEBUG: Loaded 'Microsoft.WSMan.Management, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.WSMan.Management\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.WSMan.Management.dll'  
DEBUG: Assembly resolve event fired for Microsoft.PowerShell.Security.resources, Version=3.0.0.0, Culture=en-US, 
PublicKeyToken=31bf3856ad364e35
DEBUG: Assembly resolve event fired for Microsoft.PowerShell.Security.resources, Version=3.0.0.0, Culture=en-US, 
PublicKeyToken=31bf3856ad364e35
DEBUG: Assembly resolve event fired for Microsoft.PowerShell.EditorServices, Version=3.1.5.0, Culture=neutral, PublicKeyToken=null     
DEBUG: Assembly resolve event fired for Microsoft.WSMan.Management.resources, Version=3.0.0.0, Culture=en-US, 
PublicKeyToken=31bf3856ad364e35
DEBUG: Loading Microsoft.PowerShell.EditorServices, Version=3.1.5.0, Culture=neutral, PublicKeyToken=null from PSES dependency dir 
into LoadFile context
DEBUG: Assembly resolve event fired for Microsoft.WSMan.Management.resources, Version=3.0.0.0, Culture=en-US, 
PublicKeyToken=31bf3856ad364e35
DEBUG: Loaded 'Microsoft.PowerShell.EditorServices, Version=3.1.5.0, Culture=neutral, PublicKeyToken=null' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Microsoft.Power 
Shell.EditorServices.dll'
DEBUG: Loaded 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\netstandard\v4.0_2.0.0.0__cc7b13ffcd2ddd51\netstandard.dll'
DEBUG: Assembly resolve event fired for Microsoft.Extensions.Logging.Abstractions, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=adb9793829ddae60
DEBUG: Loading Microsoft.Extensions.Logging.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 from PSES  
dependency dir into LoadFile context
VERBOSE: 
== Console Details ==
 - Console input encoding: OEM United States
 - Console output encoding: OEM United States
 - PowerShell output encoding: US-ASCII
DEBUG: Loaded 'Microsoft.Extensions.Logging.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Microsoft.Exten 
sions.Logging.Abstractions.dll'
VERBOSE: 
== PowerShell Details ==
- PowerShell version: 5.1.18362.1171
- Language mode:      FullLanguage
DEBUG: Loaded 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'   
from
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.InteropServices.RuntimeInformation\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Ru 
ntime.InteropServices.RuntimeInformation.dll'
VERBOSE: 
== Environment Details ==
 - OS description:  Microsoft Windows 10.0.18363
 - OS architecture: X64
 - Process bitness: 64
DEBUG: Checking that .NET Framework version is at least 4.6.1
VERBOSE: .NET registry version: 528040
VERBOSE: Updated PSModulePath to: 'C:\Users\user\OneDrive - Organization\Documents\PowerShell\Modules;C:\Program 
Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program
Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files\Veeam\Backup and
Replication\Explorers\Exchange\;C:\Program Files\Veeam\Backup and Replication\Explorers\SharePoint\;C:\Program Files\Veeam\Backup and  
Replication\Explorers\SQL\;C:\Program Files\Veeam\Backup and Replication\Explorers\ActiveDirectory\;C:\Program Files\Veeam\Backup and  
Replication\Explorers\Oracle\;c:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules'
DEBUG: Validating configuration
VERBOSE: Loading PowerShell Editor Services
VERBOSE: Starting EditorServices
DEBUG: Assembly resolve event fired for Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10
DEBUG: Loading Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10 from PSES dependency dir into LoadFile       
context
DEBUG: Loaded 'Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Serilog.dll'    
DEBUG: Assembly resolve event fired for Serilog.Sinks.Async, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10
DEBUG: Loading Serilog.Sinks.Async, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10 from PSES dependency dir into    
LoadFile context
DEBUG: Loaded 'Serilog.Sinks.Async, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Serilog.Sinks.A 
sync.dll'
DEBUG: Assembly resolve event fired for Microsoft.Extensions.Logging, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=adb9793829ddae60
DEBUG: Loading Microsoft.Extensions.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 from PSES dependency    
dir into LoadFile context
DEBUG: Loaded 'Microsoft.Extensions.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Microsoft.Exten 
sions.Logging.dll'
DEBUG: Assembly resolve event fired for Serilog.Extensions.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10  
DEBUG: Loading Serilog.Extensions.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10 from PSES dependency dir 
into LoadFile context
DEBUG: Loaded 'Serilog.Extensions.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Serilog.Extensi 
ons.Logging.dll'
DEBUG: Assembly resolve event fired for Microsoft.Extensions.Logging.Abstractions, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=adb9793829ddae60
DEBUG: Loading Microsoft.Extensions.Logging.Abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 from PSES  
dependency dir into LoadFile context
DEBUG: Assembly resolve event fired for Serilog.Sinks.File, Version=5.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10
DEBUG: Loading Serilog.Sinks.File, Version=5.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10 from PSES dependency dir into 
LoadFile context
DEBUG: Loaded 'Serilog.Sinks.File, Version=5.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Serilog.Sinks.F 
ile.dll'
DEBUG: Assembly resolve event fired for Microsoft.Extensions.Options, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=adb9793829ddae60
DEBUG: Loading Microsoft.Extensions.Options, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 from PSES dependency    
dir into LoadFile context
DEBUG: Loaded 'Microsoft.Extensions.Options, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\Microsoft.Exten 
sions.Options.dll'
DEBUG: Assembly resolve event fired for Microsoft.Extensions.Logging, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=adb9793829ddae60
DEBUG: Loading Microsoft.Extensions.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 from PSES dependency    
dir into LoadFile context
DEBUG: Assembly resolve event fired for OmniSharp.Extensions.LanguageServer, Version=0.19.0.0, Culture=neutral, 
PublicKeyToken=6d868dff454e6022
DEBUG: Loading OmniSharp.Extensions.LanguageServer, Version=0.19.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022 from PSES       
dependency dir into LoadFile context
DEBUG: Loaded 'OmniSharp.Extensions.LanguageServer, Version=0.19.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\OmniSharp.Exten 
sions.LanguageServer.dll'
DEBUG: Assembly resolve event fired for OmniSharp.Extensions.JsonRpc, Version=0.19.0.0, Culture=neutral, 
PublicKeyToken=6d868dff454e6022
DEBUG: Loading OmniSharp.Extensions.JsonRpc, Version=0.19.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022 from PSES dependency   
dir into LoadFile context
DEBUG: Loaded 'OmniSharp.Extensions.JsonRpc, Version=0.19.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\OmniSharp.Exten 
sions.JsonRpc.dll'
DEBUG: Assembly resolve event fired for MediatR, Version=8.0.0.0, Culture=neutral, PublicKeyToken=bb9a41a5e8aaa7e2
DEBUG: Loading MediatR, Version=8.0.0.0, Culture=neutral, PublicKeyToken=bb9a41a5e8aaa7e2 from PSES dependency dir into LoadFile       
context
DEBUG: Loaded 'MediatR, Version=8.0.0.0, Culture=neutral, PublicKeyToken=bb9a41a5e8aaa7e2' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\MediatR.dll'    
DEBUG: Assembly resolve event fired for OmniSharp.Extensions.LanguageProtocol, Version=0.19.0.0, Culture=neutral, 
PublicKeyToken=6d868dff454e6022
DEBUG: Loading OmniSharp.Extensions.LanguageProtocol, Version=0.19.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022 from PSES     
dependency dir into LoadFile context
DEBUG: Loaded 'OmniSharp.Extensions.LanguageProtocol, Version=0.19.0.0, Culture=neutral, PublicKeyToken=6d868dff454e6022' from 
'C:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules\PowerShellEditorServices\bin\Common\OmniSharp.Exten 
sions.LanguageProtocol.dll'
DEBUG: Loaded 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' from 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Newtonsoft.Json\v4.0_11.0.0.0__30ad4fe6b2a6aeed\Newtonsoft.Json.dll'
DEBUG: Creating/running editor services
DEBUG: Creating startup info object
VERBOSE: Starting server, deregistering host logger and registering shutdown listener
=====> PowerShell Preview Integrated Console v2022.3.0 <=====

Import-PowerShellDataFile : The term 'Import-PowerShellDataFile' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:210 char:33
+         $hash.Add($_.Basename, (Import-PowerShellDataFile $_.FullName ...
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Import-PowerShellDataFile:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Import-PowerShellDataFile : The term 'Import-PowerShellDataFile' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:196 char:22
+         $colorData = Import-PowerShellDataFile $_.FullName
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Import-PowerShellDataFile:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Index operation failed; the array index evaluated to null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:197 char:9
+         $hash[$colorData.Name] = $colorData
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex
 
Index operation failed; the array index evaluated to null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:198 char:9
+         $hash[$colorData.Name].Types.Directories[''] = $colorReset
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex
 
Index operation failed; the array index evaluated to null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:199 char:9
+         $hash[$colorData.Name].Types.Files['']       = $colorReset
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex
 
Import-PowerShellDataFile : The term 'Import-PowerShellDataFile' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:53 char:34
+                         $theme = Import-PowerShellDataFile $item.Full ...
+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Import-PowerShellDataFile:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Exception calling "ContainsKey" with "1" argument(s): "Key cannot be null.
Parameter name: key"
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:58 char:33
+ ...           if (-not $script:colorSequences.ContainsKey($theme.Name)) {
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
 
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:63 char:29
+ ...             $theme.Types.Directories.WellKnown.GetEnumerator().ForEac ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:67 char:29
+ ...                $theme.Types.Files.WellKnown.GetEnumerator().ForEach({
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:71 char:29
+ ...             $theme.Types.Files.GetEnumerator().Where({$_.Name -ne 'We ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Index operation failed; the array index evaluated to null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:76 char:25
+ ...              $script:userThemeData.Themes.$Type[$theme.Name] = $theme
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex
 
Save-Theme : Cannot bind argument to parameter 'Theme' because it is null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:77 char:43
+                         Save-Theme -Theme $theme -Type $Type
+                                           ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Save-Theme], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Save-Theme
 
Import-PowerShellDataFile : The term 'Import-PowerShellDataFile' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:53 char:34
+                         $theme = Import-PowerShellDataFile $item.Full ...
+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Import-PowerShellDataFile:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Index operation failed; the array index evaluated to null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:76 char:25
+ ...              $script:userThemeData.Themes.$Type[$theme.Name] = $theme
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex
 
Save-Theme : Cannot bind argument to parameter 'Theme' because it is null.
At C:\Program Files\WindowsPowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:77 char:43
+                         Save-Theme -Theme $theme -Type $Type
+                                           ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Save-Theme], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Save-Theme
 
 user    ~    ﮫ0ms⠀                                                                  powershell   98  15:18:46  

Attached are the corresponding logs files.
StartEditorServices.log
vscode-powershell.log
EditorServices.log

@mattcargile mattcargile added the Issue-Bug A bug to squash. label Mar 23, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label Mar 23, 2022
@mattcargile mattcargile changed the title Import-PowerShellDataFile - Not Recognized As The Name Of A Cmdlet In Ps Integrated Console 5.1 Import-PowerShellDataFile - Not Recognized In Ps Integrated Console 5.1 Mar 23, 2022
@mattcargile mattcargile changed the title Import-PowerShellDataFile - Not Recognized In Ps Integrated Console 5.1 Import-PowerShellDataFile - Not Recognized In Integrated Console 5.1 Mar 23, 2022
@mattcargile mattcargile changed the title Import-PowerShellDataFile - Not Recognized In Integrated Console 5.1 Import-PowerShellDataFile Not Recognized In Integrated Console 5.1 Mar 24, 2022
@andyleejordan
Copy link
Member

I can't repro this:
Screenshot 2022-03-24 092335

I was also able to install and import the linked Terminal-Icons module.

Do you have WMF 5.1 installed?

@andyleejordan andyleejordan added Needs: Repro Info 🤔 and removed Needs: Triage Maintainer attention needed! labels Mar 24, 2022
@mattcargile

This comment was marked as off-topic.

@mattcargile

This comment was marked as outdated.

@andyleejordan
Copy link
Member

This Windows is Version 21H2, OS Build 22000.556. Do you have any updates available?

@mattcargile mattcargile changed the title Import-PowerShellDataFile Not Recognized In Integrated Console 5.1 Import-PowerShellDataFile Not Recognized In Integrated Console 5.1 On Non-Elevated code.cmd Mar 24, 2022
@mattcargile

This comment was marked as outdated.

@mattcargile mattcargile changed the title Import-PowerShellDataFile Not Recognized In Integrated Console 5.1 On Non-Elevated code.cmd Import-PowerShellDataFile Not Recognized In Integrated Console 5.1 From Non-Elevated code.cmd Mar 24, 2022
@mattcargile

This comment was marked as outdated.

@mattcargile

This comment was marked as outdated.

@mattcargile

This comment was marked as outdated.

@mattcargile mattcargile changed the title Import-PowerShellDataFile Not Recognized In Integrated Console 5.1 From Non-Elevated code.cmd Microsoft.PowerShell.Utility 7.0.0 Loaded When code.cmd Called from pwsh.exe Mar 24, 2022
@mattcargile mattcargile changed the title Microsoft.PowerShell.Utility 7.0.0 Loaded When code.cmd Called from pwsh.exe Microsoft.PowerShell.Utility 7.0.0 Loaded In 5.1 Console When code.cmd Called from pwsh.exe Mar 24, 2022
@andyleejordan
Copy link
Member

Well man, it is working now. Opening the program as Admin must have kicked something into gear...

Strange...to answer your question earlier, no, I don't load Code in an admin prompt.

Does seem like a path issue if you're starting with pwsh.exe, then opening Code (with the PSModulePath set from pwsh.exe) and then starting powershell.exe. I would recommend either opening Code directly (not from pwsh.exe) or from the same version of PowerShell you then want to use in Code (so powershell.exe to Code back to powershell.exe).

@andyleejordan andyleejordan added Resolution-Answered Will close automatically. and removed Needs: Repro Info 🤔 labels Mar 24, 2022
@mattcargile
Copy link
Author

That is unfortunate. I prefer pwsh.exe as my main shell so I typically open processes from there.

If that is the solution and there is nothing more to debug, then ok. I would think there shouldn't be any sharing of paths when code.exe opens powershell.exe.

@SeeminglyScience
Copy link
Collaborator

Can you check [IntPtr]::Size in the pwsh window you are opening code.cmd from?

@andyleejordan
Copy link
Member

andyleejordan commented Mar 24, 2022

That's an operating system thing... so here's how environment variables work:

You start pwsh.exe, it sets the environment variable PSModulePath, on my system that value is:

> $env:PSModulePath
C:\Users\andschwa\OneDrive - Microsoft\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files
\windowsapps\microsoft.powershell_7.2.2.0_x64__8wekyb3d8bbwe\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDO
WS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft Azure Information Protection\Powershell

From that process (where the environment variable is set) you start code.cmd. Because it's a spawned process of pwsh.exe, it inherits that environment variable. Within Code, any new processes continue to inherit that environment variable (they may modify it, but they start with it), so here it is in a spawned pwsh.exe (non-extension terminal):

> $env:PSModulePath
C:\Users\andschwa\OneDrive - Microsoft\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\windowsapps\microsoft.powershell_7.2.2.0_x64__8wekyb3d8bbwe\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft Azure Information Protection\Powershell

and in a spawned powershell.exe from the extension (the process tree looks like: pwsh->code->powershell):

> $env:PSModulePath
C:\Users\andschwa\OneDrive - Microsoft\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\windowsapps\microsoft.powershell_7.2.2.0_x64__8wekyb3d8bbwe\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft Azure Information Protection\Powershell;c:\Users\andschwa\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules

Since we just take the environment variable PSModulePath and add the extension's folder to it.

https://github.com/PowerShell/PowerShellEditorServices/blob/2c879ca88703ee823df8629011b5e0b10165b3c3/src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs#L259-L276

Now that said, in my experimenting it seems that powershell.exe when run by itself (not by our loader) is trimming the pwsh portions out of the environment variable; essentially resolving this bug but in the general case and not in the extension. Starts here:

https://github.com/PowerShell/PowerShell/blob/7dc4587014bfa22919c933607bf564f0ba53db2e/src/System.Management.Automation/engine/NativeCommandProcessor.cs#L532

And then the actual trim logic here:

https://github.com/PowerShell/PowerShell/blob/7dc4587014bfa22919c933607bf564f0ba53db2e/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs#L1225-L1277

So I'm going to leave this open as a bug, because we could duplicate PowerShell's existing workaround and improve this behavior for you.

@andyleejordan andyleejordan added Area-Startup Bug: PowerShell 5.1 Bugs when using PowerShell 5.1. and removed Resolution-Answered Will close automatically. labels Mar 24, 2022
@andyleejordan andyleejordan added this to the Consider-vNext milestone Mar 24, 2022
@mattcargile
Copy link
Author

mattcargile commented Mar 24, 2022

Can you check [IntPtr]::Size in the pwsh window you are opening code.cmd from?

It is 8.
image

I'll spend some time reviewing your robust explanation soon, Andy. I really appreciate the large response! Additionally, I'm seeing my $env:PSModulePath within powershell.exe does reflect pwsh.exe directories for some reason. I was wrong about this. I need to step back and review it more.

EDIT: My $env:PSModulePath is below so somehow the pwsh.exe paths are getting inside the Visual Studio Code Host.

PS> $env:PSModulePath -split ';' 
C:\Users\user\OneDrive\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files\Veeam\Backup and Replication\Explorers\Exchange\
C:\Program Files\Veeam\Backup and Replication\Explorers\SharePoint\
C:\Program Files\Veeam\Backup and Replication\Explorers\SQL\
C:\Program Files\Veeam\Backup and Replication\Explorers\ActiveDirectory\
C:\Program Files\Veeam\Backup and Replication\Explorers\Oracle\
c:\Users\user\.vscode\extensions\ms-vscode.powershell-preview-2022.3.0\modules

EDIT 2: Sorry I got distracted and didn't see your response below.

@andyleejordan
Copy link
Member

So that's exactly why I left if open. If you open powershell.exe from a pwsh.exe (no Code involved), the PSModulePath gets "cleaned up" by powershell.exe. However, we don't have that same logic in the extension's PowerShell host, so you'll see those two different behaviors depending on if powershell.exe was spawned by itself, or through the extension.

@mattcargile mattcargile changed the title Microsoft.PowerShell.Utility 7.0.0 Loaded In 5.1 Console When code.cmd Called from pwsh.exe When code.cmd Called From pwsh.exe $env:PSModulePath Paths Retained In 5.1 Console Mar 24, 2022
@mattcargile mattcargile changed the title When code.cmd Called From pwsh.exe $env:PSModulePath Paths Retained In 5.1 Console When code.cmd Called From pwsh.exe $env:PSModulePath's Paths Retained In 5.1 Console Mar 24, 2022
@mattcargile mattcargile changed the title When code.cmd Called From pwsh.exe $env:PSModulePath's Paths Retained In 5.1 Console When code.cmd Called From pwsh.exe, Non-powershell.exe $env:PSModulePath Paths Retained In 5.1 Console Mar 24, 2022
@mattcargile mattcargile changed the title When code.cmd Called From pwsh.exe, Non-powershell.exe $env:PSModulePath Paths Retained In 5.1 Console When code.cmd Called From pwsh.exe, Non-powershell.exe $env:PSModulePath Retained In 5.1 Console Mar 24, 2022
@mattcargile mattcargile changed the title When code.cmd Called From pwsh.exe, Non-powershell.exe $env:PSModulePath Retained In 5.1 Console Non-powershell.exe $env:PSModulePath Retained In 5.1 Console When code.cmd Called From pwsh.exe Mar 24, 2022
@mattcargile mattcargile changed the title Non-powershell.exe $env:PSModulePath Retained In 5.1 Console When code.cmd Called From pwsh.exe pwsh.exe $env:PSModulePath Retained In 5.1 Console When code.cmd Called From pwsh.exe Mar 24, 2022
@SydneyhSmith SydneyhSmith moved this from Todo to P0 - Todo in American Pharoah May 9, 2022
@andyleejordan andyleejordan self-assigned this Aug 3, 2022
@SydneyhSmith SydneyhSmith removed this from the Consider-vNext milestone Aug 11, 2022
@SydneyhSmith SydneyhSmith moved this to Todo in Flying Fox Mar 7, 2023
@SydneyhSmith SydneyhSmith moved this from Todo to Wishlist in Flying Fox Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Startup Bug: PowerShell 5.1 Bugs when using PowerShell 5.1. Issue-Bug A bug to squash.
Projects
Status: Wishlist
Development

No branches or pull requests

4 participants