Closed LuanVSO closed 1 year ago
@LuanVSO I have the same issue. I'm not an expert of pwsh, but a little investigation allowed me to run the script successfully if I removed . (dot) from my functions. I think it somehow connected with that issue https://stackoverflow.com/a/17881960/8413677. I tried to fix it in AstVisitor but I couldn't.
For example this is my function that failed:
function dotfilesEdit {
Set-Location $env:DOTFILES && nvim .
}
@IISResetMe hello 👋 Do you have any idea how to fix it even conceptually?
@bedware can you share the original profile script + the full error message you saw? Please include the version of PowerShell you're using :)
@bedware can you share the original profile script + the full error message you saw? Please include the version of PowerShell you're using :)
PowerShell 7.3.4
Loading personal and system profiles took 1977ms.
╭─ > pwsh ~ 16:28:14
╰─❯ $PSVersionTable
Name Value
---- -----
PSVersion 7.3.4
PSEdition Core
GitCommitId 7.3.4
OS Microsoft Windows 10.0.22621
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
╭─ > pwsh ~ 16:28:33
╰─❯ measure-Script -Path $PROFILE -Top 5
MethodInvocationException: C:\Users\dmitr\Documents\PowerShell\Modules\PSProfiler\1.0.5\AstVisitor7.class.ps1:15
Line |
15 | $res = $element.Visit($this)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "Visit" with "1" argument(s): "Method invocation failed because
| [System.Management.Automation.Language.TokenKind] does not contain a method named 'Visit'."
InvalidOperation: You cannot call a method on a null-valued expression.
InvalidOperation: The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script
block, or a CommandInfo object.
C:\Users\dmitr\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Count Line Time Taken Statement
----- ---- ---------- ---------
0 1 00:00.0000000 # $OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding =
[System.Text.UTF8Encoding]::new()
0 2 00:00.0000000 [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
0 3 00:00.0000000
0 4 00:00.0000000 $ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
0 5 00:00.0000000 if (Test-Path($ChocolateyProfile)) {
0 6 00:00.0000000 Import-Module "$ChocolateyProfile"
0 7 00:00.0000000 }
0 8 00:00.0000000
0 9 00:00.0000000 Import-Module "$env:LOCALAPPDATA\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1"
0 10 00:00.0000000 oh-my-posh init pwsh --config
"$env:LOCALAPPDATA\Programs\oh-my-posh\themes\bedware.omp.json"| Invoke-Expression
0 11 00:00.0000000
0 12 00:00.0000000 # replace 'Ctrl+t' and 'Ctrl+r' with your preferred bindings:
0 13 00:00.0000000 Set-PsFzfOption `
0 14 00:00.0000000 -PSReadlineChordProvider 'Ctrl+f' `
0 15 00:00.0000000 -PSReadlineChordReverseHistory 'Ctrl+r' `
0 16 00:00.0000000 -PSReadlineChordSetLocation 'Ctrl+g'
0 17 00:00.0000000
0 18 00:00.0000000 $env:EDITOR = 'nvim'
0 19 00:00.0000000 $env:DOTFILES = "$env:USERPROFILE\.dotfiles"
0 20 00:00.0000000 $env:ChocolateyToolsLocation = "$env:LOCALAPPDATA\tools"
0 21 00:00.0000000 $env:FZF_CTRL_T_COMMAND = 'fd --type f --path-separator / --hidden'
0 22 00:00.0000000 $env:FZF_ALT_C_COMMAND = 'fd --type d --path-separator /'
0 23 00:00.0000000
0 24 00:00.0000000 Set-Alias -Name vi -Value nvim
0 25 00:00.0000000 Set-Alias -Name .f -Value dotfiles
0 26 00:00.0000000 Set-Alias -Name .fe -Value dotfilesEdit
0 27 00:00.0000000
0 28 00:00.0000000 function dotfiles {
0 29 00:00.0000000 Set-Location $env:DOTFILES
0 30 00:00.0000000 }
0 31 00:00.0000000 function dotfilesEdit {
0 32 00:00.0000000 Set-Location $env:DOTFILES && nvim .
0 33 00:00.0000000 }
0 34 00:00.0000000
╭─ > pwsh ~ 16:29:51
╰─❯ vi $profile # comment out "&& nvim."
╭─ > pwsh ~ 16:30:18
╰─❯ measure-Script -Path $PROFILE -Top 5
C:\Users\dmitr\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Count Line Time Taken Statement
----- ---- ---------- ---------
0 1 00:00.0000000 # $OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
1 2 00:00.0022937 [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
0 3 00:00.0000000
1 4 00:00.0005926 $ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
0 5 00:00.0000000 if (Test-Path($ChocolateyProfile)) {
1 6 00:00.0008007 Import-Module "$ChocolateyProfile"
0 7 00:00.0000000 }
0 8 00:00.0000000
1 9 00:00.0012642 Import-Module "$env:LOCALAPPDATA\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1"
1 10 00:00.1799960 oh-my-posh init pwsh --config "$env:LOCALAPPDATA\Programs\oh-my-posh\themes\bedware.omp.json"| Invoke-Expression
0 11 00:00.0000000
0 12 00:00.0000000 # replace 'Ctrl+t' and 'Ctrl+r' with your preferred bindings:
1 13 00:00.0243277 Set-PsFzfOption `
0 14 00:00.0000000 -PSReadlineChordProvider 'Ctrl+f' `
0 15 00:00.0000000 -PSReadlineChordReverseHistory 'Ctrl+r' `
0 16 00:00.0000000 -PSReadlineChordSetLocation 'Ctrl+g'
0 17 00:00.0000000
1 18 00:00.0006962 $env:EDITOR = 'nvim'
1 19 00:00.0006824 $env:DOTFILES = "$env:USERPROFILE\.dotfiles"
1 20 00:00.0007021 $env:ChocolateyToolsLocation = "$env:LOCALAPPDATA\tools"
1 21 00:00.0006482 $env:FZF_CTRL_T_COMMAND = 'fd --type f --path-separator / --hidden'
1 22 00:00.0006952 $env:FZF_ALT_C_COMMAND = 'fd --type d --path-separator /'
0 23 00:00.0000000
1 24 00:00.0006434 Set-Alias -Name vi -Value nvim
1 25 00:00.0006170 Set-Alias -Name .f -Value dotfiles
1 26 00:00.0006356 Set-Alias -Name .fe -Value dotfilesEdit
0 27 00:00.0000000
0 28 00:00.0000000 function dotfiles {
0 29 00:00.0000000 Set-Location $env:DOTFILES
0 30 00:00.0000000 }
0 31 00:00.0000000 function dotfilesEdit {
0 32 00:00.0000000 Set-Location $env:DOTFILES # && nvim .
0 33 00:00.0000000 }
0 34 00:00.0000000
╭─ > pwsh ~ 16:30:27
╰─❯
Thanks!
I see the problem, the AST Visitor hasn't been updated to handle the short-circuit operators introduced in the latest versions of PowerShell. I'll get it updated and ship a new version sometime next week :)
Thanks! I will be appreciated =)
I just want to mention that in my case it was enough to merge the commands by ;
to get around this problem. And that worked, too. But I want to warn you that is not the same as using &&
which will apply the second command only if the first command was successful.
function dotfilesEdit {
Set-Location $env:DOTFILES ; nvim .
}
I'm aware they behave differently, it's a question of timing - the chaining operators didn't exist in PowerShell when the latest version of PSProfiler was released :)
@bedware @LuanVSO New release should fix this bug, please give it a spin: https://www.powershellgallery.com/packages/PSProfiler/1.0.5.0 :)
Working fine now, thanks