Skip to content

Add error handling for interactive #requires #6469

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

Merged

Conversation

rjmholt
Copy link
Collaborator

@rjmholt rjmholt commented Mar 23, 2018

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
Collaborator Author

rjmholt commented Mar 23, 2018

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

lzybkr
lzybkr previously requested changes Mar 23, 2018
Copy link
Contributor

@lzybkr lzybkr left a comment

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
Copy link
Contributor

lzybkr commented Mar 26, 2018

@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 rjmholt closed this Mar 26, 2018
@rjmholt rjmholt reopened this Mar 26, 2018
@rjmholt
Copy link
Collaborator Author

rjmholt commented Mar 26, 2018

(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
Contributor

lzybkr commented Mar 26, 2018

@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
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
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.

ex.ps1 Outdated
@@ -0,0 +1,4 @@
Write-Host "Hello"
Copy link
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
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
Member

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

@rjmholt
Copy link
Collaborator Author

rjmholt commented Apr 24, 2018

@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
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
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
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
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 daxian-dbw dismissed lzybkr’s stale review April 24, 2018 23:42

Comments were addressed.

@daxian-dbw daxian-dbw merged commit 63c0d8d into PowerShell:master Apr 26, 2018
rjmholt added a commit that referenced this pull request Apr 27, 2018
@rjmholt rjmholt deleted the interactive-requires-error-msg-3803 branch June 22, 2018 22:55
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