posh-git icon indicating copy to clipboard operation
posh-git copied to clipboard

Prompt breaks if you execute a script with strict mode

Open gsscoder opened this issue 4 years ago • 4 comments

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.

gsscoder avatar Apr 07 '20 07:04 gsscoder

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

Maybe we should always run tests in strict mode?

dahlbyk avatar Apr 07 '20 14:04 dahlbyk

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 avatar Apr 07 '20 16:04 rkeithhill

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 avatar Jul 11 '20 21:07 rkeithhill

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?

rkeithhill avatar Jul 12 '20 21:07 rkeithhill