Skip to content

Add error handling for interactive #requires - #6469

Merged
Dongbo Wang (daxian-dbw) merged 9 commits into
PowerShell:masterfrom
rjmholt:interactive-requires-error-msg-3803
Apr 26, 2018
Merged

Dongbo Wang (daxian-dbw) merged 9 commits into
PowerShell:masterfrom
rjmholt:interactive-requires-error-msg-3803

Conversation

@rjmholt

@rjmholt Rob Holt (rjmholt) commented Mar 23, 2018 •

Copy link
Copy Markdown
Collaborator

PR Summary

Fix for #3803. Restores the tokenizer state more fully to prevent the null dereference causing the original error. Also changed the name of a variable to express that it doesn't actually mean that the PowerShell session is interactive or not.

PR Checklist

@rjmholt

Copy link
Copy Markdown
Collaborator Author

I've opened an RFC on updating the behaviour of #requires. Please feel free to comment in the PR.

@lzybkr Jason Shirk (lzybkr) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test?

@lzybkr

Jason Shirk (lzybkr) commented Mar 26, 2018 •

Copy link
Copy Markdown
Contributor

Rob Holt (@rjmholt) - I meant can you add a test for the bug that was fixed - the fix isn't obvious I guess - so maybe check first/last tokens that were supposed to be set but caused the exception.

@rjmholt

Copy link
Copy Markdown
Collaborator Author

(Clicked the wrong button just then). Ok will do. The test I wrote traverses the code path I fixed -- just doesn't check the tokenizer state specifically.

I assume the thing to do is to add a tokenizer test somewhere in Parser.Tests.ps1? Is there a straightforward way to access the tokenizer's state?

I've been assuming the contract on FirstToken and LastToken is that they represent the first and last token produced by the tokenizer as it moves over a script, and that nested scans should just save and restore the parent scan's state. Is that correct?

@lzybkr

Copy link
Copy Markdown
Contributor

Rob Holt (@rjmholt) - First/Last token are exposed through PowerShell variables $^ and $$, so your test should check the variables.

Parser.Tests.ps1 seems like a good place for this test.

param($script, $firstToken, $lastToken)

$ps.AddScript($script)
$ps.AddScript("(`$^,`$`$)")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little surprised this works, 2 calls to AddScript is I think like piping the first call to the second. That said, it neatly drops any results from the first call.

To be slightly more like the interactive scenario, I think you would call $ps.Invoke twice, clearing the commands between calls.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried that and it didn't seem to do what I wanted. To be honest, I wasn't entirely certain how I should be using the embedded PowerShell session to recreate the input. But I spent some time playing with it, tried the "script, invoke, tokens, invoke" way and seemed to get $null for everything.

Comment thread ex.ps1 Outdated
@@ -0,0 +1,4 @@
Write-Host "Hello"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run git commit -a? It seems like this file was accidentally added.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, I thought I checked that first. One sec

@daxian-dbw

Copy link
Copy Markdown
Member

Rob Holt (@rjmholt) Can you please address Jason Shirk (@lzybkr)'s comments (if there is any pending)?

@rjmholt

Copy link
Copy Markdown
Collaborator Author

Dongbo Wang (@daxian-dbw) I think this is all addressed. I just tried $ps.Invoke() again, but it's still as I describe in the comment above.

$settings = [System.Management.Automation.PSInvocationSettings]::new()
$settings.AddToHistory = $true

$ps = [powershell]::Create()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a AfterAll block to dispose $ps.


$ps = [powershell]::Create()
$ps.AddScript("#requires`n10")
$ps.Invoke(@(), $settings) | Should -BeExactly 10

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please dispose $ps after you done with it.
Also, I think it's not necessary to use PSInvocationSettings in this test. Just $ps = [powershell]::Create(); $ps.AddScript("#requiresn10").Invoke() | Should -BeExactly 10` will do.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to set AddToHistory so we hit this line and test it. At least, that's why I wrote it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, we need to hit the $^/$$ setting code. Never mind.

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 63c0d8d into PowerShell:master Apr 26, 2018
Rob Holt (rjmholt) added a commit that referenced this pull request Apr 27, 2018
@rjmholt
Rob Holt (rjmholt) deleted the interactive-requires-error-msg-3803 branch June 22, 2018 22:55
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants