Skip to content
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

Failed to call GetHistoryItems #3948

Open
3 tasks done
segrey opened this issue Feb 16, 2024 · 2 comments
Open
3 tasks done

Failed to call GetHistoryItems #3948

segrey opened this issue Feb 16, 2024 · 2 comments
Labels
Needs-Triage 🔍 It's a new issue that core contributor team needs to triage.

Comments

@segrey
Copy link

segrey commented Feb 16, 2024

Prerequisites

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

Exception report

Exception calling "GetHistoryItems" with "0" argument(s): "Object reference not set to an instance of an object."
At C:\Users\Sergey.Simonchik\sandbox\init.ps1:3 char:3
+   $HistoryItems=[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryI ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException

Screenshot

image

Environment data

PS Version: 7.4.1
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.3.4
PSReadLine EditMode: Windows
OS: 10.0.22621.1 (WinBuild.160101.0800)
BufferWidth: 163
BufferHeight: 45

Steps to reproduce

I would like to run a small init script before a PowerShell session starts to access PSReadLine command history there. Unfortunately, [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() fails with the error.

pwsh.exe -NoExit -ExecutionPolicy Bypass -File .\init.ps1
#init.ps1
if (Get-Module -Name PSReadLine) {
  $HistoryItems=[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()
  if ($HistoryItems -eq $null) {
    [Console]::WriteLine("got null")
  }
  else {
    [Console]::WriteLine("got not-null")
  }
}

Expected behavior

History items can be accessed successfully, no exception happens and not null is printed to the console.

Actual behavior

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\Sergey.Simonchik> cd .\sandbox\
PS C:\Users\Sergey.Simonchik\sandbox> pwsh.exe -NoExit -ExecutionPolicy Bypass -File .\init.ps1
PowerShell 7.4.1
MethodInvocationException: C:\Users\Sergey.Simonchik\sandbox\init.ps1:3
Line |
   3 |    $HistoryItems=[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryI …
     |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "GetHistoryItems" with "0" argument(s): "Object reference not set to an instance of an object."
got null
PS C:\Users\Sergey.Simonchik\sandbox>
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Feb 16, 2024
@segrey
Copy link
Author

segrey commented Feb 16, 2024

Also, asked here: #3930

@segrey
Copy link
Author

segrey commented Feb 16, 2024

Please note that if I run the same script inside PowerShell session, GetHistoryItems() works fine. But I need to run it in the init.ps1 script passed as described above. Please let me know if there is a workaround. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage 🔍 It's a new issue that core contributor team needs to triage.
Projects
None yet
Development

No branches or pull requests

1 participant