Skip to content

VS Code Integrated Terminal Reports Error From Script NOT Being Debugged #4343

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

Closed
6 tasks done
Abax378 opened this issue Dec 21, 2022 · 6 comments · Fixed by PowerShell/PowerShellEditorServices#1982
Closed
6 tasks done
Assignees
Labels
Area-Engine Issue-Bug A bug to squash.

Comments

@Abax378
Copy link

Abax378 commented Dec 21, 2022

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

Type: Bug

While debugging a script that has a breakpoint set and which raises an error from the cmdlet Start-ThreadJob, the integrated terminal reports an error from a script not being debugged. The error reads:

`ParentContainsErrorRecordException:
Line |
22 | if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The property 'Id' cannot be found on this object. Verify that the property exists.

`

This code appears to be similar to code found here:
microsoft/terminal#14341

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.3.1
PSEdition                      Core
GitCommitId                    7.3.1
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}       
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.74.1
1ad8d514439d5077d2b0b7ee64d2ce82a9308e5a
x64

Extension Version

Steps to Reproduce

SETUP

The demo script tries to divide 1 by 0, raising an error.
Download the demo script 'demo_2.ps1' from https://github.com/Abax378/Temp

STEPS TO REPRODUCE

  1. Open the script 'demo_2.ps1' with VS Code. The Powershell extension terminal should be visible (by default).
  2. Kill the Powershell extension terminal. Answer no to the Restart dialog and kill the "Go to output window" dialog.
  3. Close the script.
  4. Close VS Code.
  5. Open VS Code.
  6. Open the script 'demo_2.ps1'. A PowerShell Extension v2022.12.1 terminal should automatically open.
  7. Set a breakpoint on line 29. ( Line 29 reads "If ($arrJobs.Count -gt 0) {" ).
  8. Start debug by placing the cursor anywhere in the script and pressing F5.
  9. An error about Line 22 should appear in the integrated terminal.

Visuals

See 'demo_2_video.mp4' https://github.com/Abax378/Temp

Logs

See 'logs.zip' at https://github.com/Abax378/Temp

@Abax378 Abax378 added the Issue-Bug A bug to squash. label Dec 21, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label Dec 21, 2022
@andyleejordan
Copy link
Member

andyleejordan commented Dec 21, 2022

That error is coming from VS Code's shell integration. Can you try disabling shell integration and try again?

/cc @Tyriar seems like an edge case here https://github.com/PowerShell/PowerShellEditorServices/blob/2e456d1ec35132c82a6deae9ffb89ae2e56b5d78/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs#L529-L532, perhaps when Get-History returns nothing.

@andyleejordan andyleejordan added Needs: Author Feedback Please give us the requested feedback! and removed Needs: Triage Maintainer attention needed! labels Dec 21, 2022
@Abax378
Copy link
Author

Abax378 commented Dec 22, 2022

With Terminal › Integrated › Shell Integration: Enabled, the Line 22 error pops up.

With Terminal › Integrated › Shell Integration: Disabled, the Line 22 error does not occur.

@ghost ghost added Needs: Maintainer Attention Maintainer attention needed! and removed Needs: Author Feedback Please give us the requested feedback! labels Dec 22, 2022
@arievanderwende
Copy link

arievanderwende commented Dec 22, 2022

This issue can be reproduced more easily I believe; simply add only the following two lines of code to your PowerShell 7 profile:

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

(No other code is required in your PowerShell 7 profile.)
PowerShell 7 will continue to work, but you'll get a similar error in the PowerShell Extension in Visual Studio Code;

ParentContainsErrorRecordException: 
Line |
  57 |      $Global:__LastHistoryId = $LastHistoryEntry.Id
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The property 'Id' cannot be found on this object. Verify that the property exists.

This used to work fine before; the issue was only introduced lately.

The issue may be caused by line 79 in the file "C:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\contrib\terminal\browser\media\shellIntegration.ps1", shown in the code block below.

$Global:__LastHistoryId = $LastHistoryEntry.Id

If there is no history retrieved on line 39, shown in the code block below, then the Id property of $LastHistoryEntry does not exist, which could cause this error to occur.

$LastHistoryEntry = Get-History -Count 1

@Abax378
Copy link
Author

Abax378 commented Dec 22, 2022

I've updated the sample .ps1 file with much simpler code that raises the Line 22 error. The logs are also updated to show the result of running the updated script.

@atyourservers - Your method of raising the error seems to have Set-StrictMode in common with my demo script. Your method apparently encounters an error on Line 57, while Set-StrictMode -Version 2.0 in my script encounters an error on Line 22.

@arievanderwende
Copy link

@Abax378, that is because the issue is caused by setting strict mode. Maybe I should have explicitly written that in my previous reply for clarity. Because of strict mode, references to nonexistent properties result in an error message instead of returning $null. In this case it errors out on the Id property of $LastHistoryEntry because it does not exist.

The workaround is to simply not set strict mode (it's disabled by default, but if it's enabled you can disable it using Set-StrictMode -Off), but it's a best practice to have it enabled. All code should work with it enabled. If it does not work, then it's bad code. ;) Setting the strict mode version to Latest ensures that you're always using the latest version (currently version 3.0), even when new versions are released. The latest version is also the most restrictive, so it should be used for testing things such as the PowerShell Extension before releasing an update for it.

Hopefully they can fix this soon.

@Abax378
Copy link
Author

Abax378 commented Dec 23, 2022

@atyourservers - I appreciate you highlighting other paths to a similar error. Let me rearrange your reply a little bit . . .

@Abax378, that is because the issue is caused by . . .
. . . Id property of $LastHistoryEntry because it does not exist.

I definitely agree with that. The purpose of my comment was to highlight for developers trying to solve this problem that there are likely multiple paths to this error (as evidenced by at least three different line numbers with the same code, which in itself seems like a problem if you're having to maintain identical code in multiple modules).

I'm sure the developers are smart enough to realize this, but when resources are short, fixing problems sometimes gets limited to the smallest subset of a complaint.

The workaround is to simply not set strict mode

It's easier than that. In many cases, the second attempt at debugging doesn't raise the error since $LastHistoryEntry.Id is populated at that point. That's why it took me a while to figure out when the bug was happening - I had to perform actions that were akin to what you might expect from Clear-History (although Clear-History won't replicate the bug).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Engine Issue-Bug A bug to squash.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants