JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
16.79k stars 2.35k forks source link

pwsh - `$PWD` Lost Owner Or Inaccessible UNC Path Prompt Failure #1770

Closed mattcargile closed 2 years ago

mattcargile commented 2 years ago

Code of Conduct

What happened?

Not sure if there is anything I can do about this. I was thinking there might be some resolver in source? Maybe this should be a discussion as I could potentially use the template property?

While on VPN, my $PWD may be a UNC. If my connection expires, then when I use the Terminal ( i.e. wt.exe ) my prompt becomes PS> upon next command run or <ENTER> key press. This behavior also occurs if I Set-Location to a location without an owner ( or owned by only the [NT AUTHORITY]\System ).

I started a pwsh.exe -NoProfile and then I ran the below to make sure I didn't have some other $PROFILE problem. This was prior to testing the same expressed above.

$OhMyPoshConfig = "$env:OneDrive\Documents\oh-my-posh\themes\jandedobbeleer_mac.omp.json"
oh-my-posh.exe --init --shell pwsh --config $OhMyPoshConfig | Invoke-Expression

Is there a way to more gracefully fail and show empty text for the path component?

Theme

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "segments": [
        {
          "background": "#c386f1",
          "foreground": "#ffffff",
          "leading_diamond": "\ue0b6",
          "properties": {
            "template": " {{ .UserName }} "
          },
          "style": "diamond",
          "trailing_diamond": "\ue0b0",
          "type": "session"
        },
        {
          "background": "#ff479c",
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "folder_separator_icon": " \ue0b1 ",
            "home_icon": "~",
            "style": "folder",
            "template": " \uf74a  {{ .Path }} "
          },
          "style": "powerline",
          "type": "path"
        },
        {
          "background": "#fffb38",
          "background_templates": [
            "{{ if or (.Working.Changed) (.Staging.Changed) }}#FF9248{{ end }}",
            "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#ff4500{{ end }}",
            "{{ if gt .Ahead 0 }}#B388FF{{ end }}",
            "{{ if gt .Behind 0 }}#B388FF{{ end }}"
          ],
          "foreground": "#193549",
          "leading_diamond": "\ue0b6",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "branch_max_length": 25,
            "fetch_stash_count": true,
            "fetch_status": true,
            "fetch_upstream_icon": true,
            "template": " {{ .UpstreamIcon }}{{ .HEAD }}{{ .BranchStatus }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \uf692 {{ .StashCount }}{{ end }} "
          },
          "style": "powerline",
          "trailing_diamond": "\ue0b4",
          "type": "git"
        },
        {
          "background": "#6CA35E",
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "fetch_version": true,
            "template": " \uf898 {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} "
          },
          "style": "powerline",
          "type": "node"
        },
        {
          "background": "#8ED1F7",
          "foreground": "#111111",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "fetch_version": true,
            "template": " \ue626 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          },
          "style": "powerline",
          "type": "go"
        },
        {
          "background": "#4063D8",
          "foreground": "#111111",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "fetch_version": true,
            "template": " \ue624 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          },
          "style": "powerline",
          "type": "julia"
        },
        {
          "background": "#FFDE57",
          "foreground": "#111111",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "display_mode": "files",
            "fetch_virtual_env": false,
            "template": " \ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          },
          "style": "powerline",
          "type": "python"
        },
        {
          "background": "#AE1401",
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "display_mode": "files",
            "fetch_version": true,
            "template": " \ue791 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          },
          "style": "powerline",
          "type": "ruby"
        },
        {
          "background": "#FEAC19",
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "display_mode": "files",
            "fetch_version": false,
            "template": " \uf0e7{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          },
          "style": "powerline",
          "type": "azfunc"
        },
        {
          "background_templates": [
            "{{if contains \"default\" .Profile}}#FFA400{{end}}",
            "{{if contains \"jan\" .Profile}}#f1184c{{end}}"
          ],
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "display_default": false,
            "template": " \ue7ad {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} "
          },
          "style": "powerline",
          "type": "aws"
        },
        {
          "background": "#ffff66",
          "foreground": "#111111",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "template": " \uf0ad "
          },
          "style": "powerline",
          "type": "root"
        },
        {
          "background": "#fbc718",
          "foreground": "#679c2f",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "template": " {{ .Env.OMP_VISERVER }} "
          },
          "style": "powerline",
          "type": "text"
        },
        {
          "background": "#105aa7",
          "foreground": "#74b05c",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "template": " {{ .Env.OMP_ANYCONNECT }} "
          },
          "style": "powerline",
          "type": "text"
        },
        {
          "background": "#83769c",
          "foreground": "#ffffff",
          "properties": {
            "always_enabled": true,
            "template": "<transparent>\ue0b0</> \ufbab{{ .FormattedMs }}\u2800"
          },
          "style": "plain",
          "type": "executiontime"
        },
        {
          "background": "#00897b",
          "background_templates": [
            "{{ if gt .Code 0 }}#e91e63{{ end }}"
          ],
          "foreground": "#ffffff",
          "properties": {
            "always_enabled": true,
            "template": "<parentBackground>\ue0b0</> \ue23a "
          },
          "style": "diamond",
          "trailing_diamond": "\ue0b4",
          "type": "exit"
        }
      ],
      "type": "prompt"
    },
    {
      "segments": [
        {
          "background": "#0077c2",
          "foreground": "#ffffff",
          "properties": {
            "template": "<#0077c2,transparent>\ue0b6</> \uf489 {{ .Name }} <transparent,#0077c2>\ue0b2</>"
          },
          "style": "plain",
          "type": "shell"
        },
        {
          "background": "#1BD760",
          "foreground": "#111111",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b2",
          "properties": {
            "paused_icon": "\uf04c ",
            "playing_icon": "\uf04b ",
            "template": "\uf9c6 {{ .Icon }}{{ if ne .Status \"stopped\" }}{{ .Artist }} - {{ .Track }}{{ end }} "          
          },
          "style": "powerline",
          "type": "spotify"
        },
        {
          "background": "#f36943",
          "background_templates": [
            "{{if eq \"Charging\" .State.String}}#40c4ff{{end}}",
            "{{if eq \"Discharging\" .State.String}}#ff5722{{end}}",
            "{{if eq \"Full\" .State.String}}#4caf50{{end}}"
          ],
          "foreground": "#ffffff",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b2",
          "properties": {
            "charged_icon": "\ue22f ",
            "charging_icon": "\ue234 ",
            "discharging_icon": "\ue231 ",
            "template": " {{ if not .Error }}{{ .Icon }}{{ .Percentage }}{{ end }}{{ .Error }}\uf295 "
          },
          "style": "powerline",
          "type": "battery"
        },
        {
          "background": "#2e9599",
          "foreground": "#111111",
          "invert_powerline": true,
          "leading_diamond": "\ue0b2",
          "properties": {
            "template": " {{ .CurrentDate | date .Format }} "
          },
          "style": "diamond",
          "trailing_diamond": "\ue0b4",
          "type": "time"
        }
      ],
      "type": "rprompt"
    }
  ],
  "console_title": true,
  "console_title_style": "template",
  "console_title_template": "{{ .Shell }} in {{ .Folder }}",
  "final_space": true,
  "version": 1
}

What OS are you seeing the problem on?

Windows

Which shell are you using?

powershell

Log Note

Write-PoshDebug fails when my $PWD is a UNC path that I can't connect to. Here is the error when I run the aforementioned function.

Log output

@{ErrorRecord=Program 'oh-my-posh.exe' failed to run: StandardOutputEncoding is only supported when standard output is redirected.At line:93 char:22
+ … dardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cle …
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.; WasThrownFromThrowStatement=False; TargetSite=System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject] Invoke(System.Collections.IEnumerable); Message=Program 'oh-my-posh.exe' failed to run: StandardOutputEncoding is only supported when standard output is redirected.At line:93 char:22
+ … dardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cle …
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.; Data=System.Collections.ListDictionaryInternal; InnerException=System.InvalidOperationException: StandardOutputEncoding is only supported when standard output is redirected.
   at System.Diagnostics.Process.Start()
   at System.Management.Automation.NativeCommandProcessor.InitNativeProcess(); HelpLink=; Source=System.Management.Automation; HResult=-2146233087; StackTrace=   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown, ExecutionOptions options)}
JanDeDobbeleer commented 2 years ago

@mattcargile in the first case (standard prompt failing), can you have a look of the contents of the $error variable ? This error comes from the init script and is a PowerShell issue, not one the comes from the oh-my-posh golang binary. That one isn't even invoked here and the error you provided is pretty generic and does not display the actual issue. We fetch the following two parameters to get the actual path which are then passed as a string to oh-my-posh (so that should always work, unless the string values can't be resolved OR the output is different?):

$cleanPWD = $PWD.ProviderPath
$cleanPSWD = $PWD.ToString()

Can you check what the values are for these two in this specific case? ProviderPath could the the issue.

mattcargile commented 2 years ago

Thanks for the notes. After I disconnected from my VPN while my $PWD is a UNC path, I started troubleshooting

Below is the $Error[0], which is what I attempted to copy before but I think when I piped it to Set-Clipboard, it got butchered. I also checked the $PWD property references and they returned without error.


ErrorRecord                 : Program 'oh-my-posh.exe' failed to run: StandardOutputEncoding is only supported when standard output is redirected.At line:93 char:22
                              + … dardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cle …
                              +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
WasThrownFromThrowStatement : False
TargetSite                  : System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject] Invoke(System.Collections.IEnumerable)
Message                     : Program 'oh-my-posh.exe' failed to run: StandardOutputEncoding is only supported when standard output is redirected.At line:93 char:22
                              + … dardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cle …
                              +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
Data                        : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException              : System.InvalidOperationException: StandardOutputEncoding is only supported when standard output is redirected.
                                 at System.Diagnostics.Process.Start()
                                 at System.Management.Automation.NativeCommandProcessor.InitNativeProcess()
HelpLink                    :
Source                      : System.Management.Automation
HResult                     : -2146233087
StackTrace                  :    at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
                                 at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown, ExecutionOptions options)

I guess it is complaining about this line?

https://github.com/JanDeDobbeleer/oh-my-posh/blob/b10b8dc2f0caf41d449f65941d43c031217aadad/src/engine/init/omp.ps1#L93

Side note: How does that $omp="::OMP::" work? That is interesting.

mattcargile commented 2 years ago

Did a little more troubleshooting and replaced that aforementioned line 93 in the omp.ps1 with the below.

$standardOut = @(&$omp --error="$errorCode" --pwd="$HOME" --pswd="$HOME" --execution-time="$executionTime" --stack-count="$stackCount" --config="$config" --terminal-width=$terminalWidth 2>&1)

Still broken and returns a PS> prompt. I tried to look around a little bit with sysinternals tool procmon.exe but I didn't have much luck.

JanDeDobbeleer commented 2 years ago

@mattcargile the more I look at the pattern it seems that PowerShell (or PSReadline) can't handle running an executable when in this use-case. The error you see has nothing to do with oh-my-posh, it's PowerShell that fails.

mattcargile commented 2 years ago

Yeah I see! I couldn't even run oh-my-posh.exe --version to output. conhost.exe pops up for a second and no output to the original terminal ( wt.exe ) occurred. Even ipconfig exhibits similar behavior. It's like the output is being pushed to a conhost.exe/cmd.exe and avoiding wt.exe altogether. I tried in powershell.exe too.

mattcargile commented 2 years ago

I guess we can close this out. I tried a quick Google for the issue and didn't have much success.

I was able to reproduce the issue using conhost.exe as my terminal instead of wt.exe.

Thanks for helping me understand it.

github-actions[bot] commented 6 months ago

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues. If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.