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

Prompt breaks if you execute a script with strict mode #758

Open
gsscoder opened this issue Apr 7, 2020 · 4 comments
Open

Prompt breaks if you execute a script with strict mode #758

gsscoder opened this issue Apr 7, 2020 · 4 comments

Comments

@gsscoder
Copy link

gsscoder commented Apr 7, 2020

First of all thank you for this very useful and well done project!

If I execute a script with this strict mode enabled:

Set-StrictMode -Version Latest

posh-git prompt breaks:

$ [PoshGitVcsPrompt error: The property 'Length' cannot be found on this object. Verify that the property exists.]> 

It seems that strict mode is extended to loaded modules. Do you think it's better to post this issue in PowerShell repo?

Environment: macOS Catalina, PowerShell installd with brew.

Regards,
Giacomo S. S.

@dahlbyk
Copy link
Owner

dahlbyk commented Apr 7, 2020

We've fixes strict mode issues before…seems like something we should just fix.

Maybe we should always run tests in strict mode?

@rkeithhill
Copy link
Collaborator

We definitely should fix the strict mode error. Setting strict mode to latest caused 4 failures but not the missing Length error:

   [-] Tab completes when there is one alias of a given name 74ms (72ms|2ms)
    PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists.
    at <ScriptBlock>, C:\Users\Keith\GitHub\dahlbyk\posh-git\test\TabExpansion.Tests.ps1:270

 Context Get-PromptConnectionInfo
   [-] Returns null if Env:SSH_CONNECTION is not set 8ms (6ms|2ms)
    RuntimeException: The variable '$ssh_connection' cannot be retrieved because it has not been set.
    at <ScriptBlock>, C:\Users\Keith\GitHub\dahlbyk\posh-git\test\Utils.Tests.ps1:116
   [-] Returns null if Env:SSH_CONNECTION is empty 4ms (2ms|2ms)
    RuntimeException: The variable '$ssh_connection' cannot be retrieved because it has not been set.
    at <ScriptBlock>, C:\Users\Keith\GitHub\dahlbyk\posh-git\test\Utils.Tests.ps1:116
   [-] Returns "[username@hostname]: " if Env:SSH_CONNECTION is set 5ms (3ms|2ms)
    RuntimeException: The variable '$ssh_connection' cannot be retrieved because it has not been set.
    at <ScriptBlock>, C:\Users\Keith\GitHub\dahlbyk\posh-git\test\Utils.Tests.ps1:116

FYI, the line numbers are for my Pester v5 port branch.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jul 11, 2020

Nevermind. Need to set strict mode in the module and yeah, that shows plenty (37) of potential issues.

OK, I've fixed the failing Pester tests this caused.

@gsscoder Which version of posh-git are you using?

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jul 12, 2020

BTW I don't think we want to turn this on in the shipping module since user configuration of $GitPromptSettings can trigger strict mode errors and it's quite easy to do without realizing it. For example, this setting triggered an error:

$global:GitPromptSettings.DefaultPromptSuffix.Text = ' $((Get-History -Count 1).id + 1)$(">" * ($nestedPromptLevel + 1)) '

The above errors because when you first start PowerShell, history is empty so Get-History -Count 1 returns $null and then I try to deref $null to get the Id property.

Maybe we only enable this during Pester tests?

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

No branches or pull requests

3 participants