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

Fatal error rendering text segment: runtime error: invalid memory address or nil pointer dereference #1907

Closed mdnava closed 2 years ago

mdnava commented 2 years ago

Code of Conduct

What happened?

Note1: I use the same Posh config file for Cmd, Pwsh, Git bash, Msys2 Bash, Fish, Zsh

My prompt usually works fine, but since the last update to v7.33+ (and today also happened with v7.36) I get this error from time to time (it seems to happen randomly):

oh-my-posh fatal error rendering text segment:runtime error: invalid memory address or nil pointer dereference

goroutine 20 [running]: runtime/debug.Stack() /opt/hostedtoolcache/go/1.17.7/x64/src/runtime/debug/stack.go:24 +0x65 oh-my-posh/engine.(Segment).renderText.func1() /home/runner/work/oh-my-posh/oh-my-posh/src/engine/segment.go:332 +0x58 panic({0x156fbe0, 0x202db50}) /opt/hostedtoolcache/go/1.17.7/x64/src/runtime/panic.go:1038 +0x215 strings.Count({0x0, 0xc0000340c0}, {0x160423d, 0xc000034090}) /opt/hostedtoolcache/go/1.17.7/x64/src/strings/strings.go:47 +0x50 strings.Replace({0x0, 0x0}, {0x160423d, 0x1}, {0x1604227, 0x1}, 0xffffffffffffffff) /opt/hostedtoolcache/go/1.17.7/x64/src/strings/strings.go:930 +0xcc strings.ReplaceAll(...) /opt/hostedtoolcache/go/1.17.7/x64/src/strings/strings.go:964 oh-my-posh/environment.DirMatchesOneOf({0x1722a38, 0xc0000d8de0}, {0x0, 0xe}, {0x2099080, 0x0, 0x0}) /home/runner/work/oh-my-posh/oh-my-posh/src/environment/shell.go:699 +0x76 oh-my-posh/engine.(Segment).cwdExcluded(0xc0001d7c20) /home/runner/work/oh-my-posh/oh-my-posh/src/engine/segment.go:200 +0xde oh-my-posh/engine.(Segment).shouldIncludeFolder(0xc0001d7c20) /home/runner/work/oh-my-posh/oh-my-posh/src/engine/segment.go:173 +0x2c oh-my-posh/engine.(Segment).renderText(0xc0001d7c20, {0x1722a38, 0xc0000d8de0}) /home/runner/work/oh-my-posh/oh-my-posh/src/engine/segment.go:338 +0x7b oh-my-posh/engine.(Block).renderSegmentsText.func1(0x0) /home/runner/work/oh-my-posh/oh-my-posh/src/engine/block.go:87 +0x65 created by oh-my-posh/engine.(Block).renderSegmentsText /home/runner/work/oh-my-posh/oh-my-posh/src/engine/block.go:85 +0x9d

Theme

No theme, my own prompt..

What OS are you seeing the problem on?

Windows

Which shell are you using?

zsh, bash, fish, powershell, other (please specify)

Log output

$ posh --debug --shell uni --config="%EnvScripts%\AutoRun\posh-prompt.json"

Version: 7.36.1

Segments:

ConsoleTitle(false) -   0 ms -
text(false) -   0 ms -
text(true)  -   0 ms -  CMD
path(true)  -   1 ms -  \Desktop 
time(true)  -   0 ms -  22:37:38 
text(false) -   0 ms -
git(false)  -  14 ms -
text(true)  -   1 ms - ϟ

Run duration: 18.0005ms

Cache path: C:\Users\USERNAME\AppData\Local\oh-my-posh

Logs:

2022/03/11 22:37:38 Args duration: 0s, args:
2022/03/11 22:37:38 Args duration: 0s, args:
2022/03/11 22:37:38 Args duration: 0s, args:
2022/03/11 22:37:38 Shell duration: 0s, args:
2022/03/11 22:37:38 debug: Getenv

2022/03/11 22:37:38 Getenv duration: 0s, args: OMP_CACHE_DISABLED
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 Root duration: 0s, args:
2022/03/11 22:37:38 Shell duration: 0s, args:
2022/03/11 22:37:38 ErrorCode duration: 0s, args:
2022/03/11 22:37:38 IsWsl duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 User duration: 0s, args:
2022/03/11 22:37:38 Host duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 TemplateCache duration: 998.6µs, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 TemplateCache duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 Args duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 PathSeparator duration: 0s, args:
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 IsWsl duration: 0s, args:
2022/03/11 22:37:38 StackCount duration: 0s, args:
2022/03/11 22:37:38 TemplateCache duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 TemplateCache duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 TemplateCache duration: 0s, args:
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 GOOS duration: 0s, args:
2022/03/11 22:37:38 HasCommand duration: 14.9992ms, args: git.exe
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 error: HasParentFilePath
CreateFile D:\.git: The system cannot find the file specified.
2022/03/11 22:37:38 HasParentFilePath duration: 0s, args: .git
2022/03/11 22:37:38 debug: Pwd
D:\USERNAME\Desktop
2022/03/11 22:37:38 Pwd duration: 0s, args:
2022/03/11 22:37:38 debug: Home
D:\USERNAME
2022/03/11 22:37:38 TemplateCache duration: 0s, args:
2022/03/11 22:37:38 debug: Getenv
C:\Users\USERNAME\AppData\Local
2022/03/11 22:37:38 Getenv duration: 0s, args: LOCALAPPDATA
2022/03/11 22:37:38 CachePath duration: 0s, args:
JanDeDobbeleer commented 2 years ago

@mdnava that's a weird place for a nil pointer dereference. Can you share your theme? It tries to replaces some string values, which fails, but that should be possible. I need some more insights into what could go wrong here as I don't immediately see it.

JanDeDobbeleer commented 2 years ago

@mdnava found a potential cause. Will push a fix later today.

mdnava commented 2 years ago

This is my config file in case it's of any use:

Note: I still have pending to read the newer documentation and remove those "type": "text" from segments. But since it's been working fine I've taken my time, but it's in my ToDo list..

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "version": 1,
  "blocks": [

    // =====================
    // Prompt: First Line
    // =====================

    {
          "type": "prompt",
          "alignment": "left",
          "segments": [
        {
          "type": "text",
          "style": "plain",
          "properties": {
              "template": "{{if .Env.PROMPT_PRE_LNBR}}\n{{end}}"
          }
        },
        {
          "type": "text",
          "style": "plain",
          "background": "#FF00FF",
          "foreground": "#ffffff",
          "properties": {
            "template": " {{if .Env.WSYSTEM}}{{.Env.WSYSTEM}}{{end}}{{if .Env.MSYSTEM}}{{.Env.MSYSTEM}}{{end}}{{if .Root}}\uf997{{end}}{{if not .Root}} {{end}}"
          }
        },
        {
          "type": "path",
          "style": "diamond",
          "background": "#FFE50A",
          "foreground": "#000000",
          "trailing_diamond": "<#FFE50A, #008000>\ue0b0</>",
          "leading_diamond": "<#FF00FF, #FFE50A>\ue0b0</>",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "home_icon": "\ue617",
            "mapped_locations": {
              "C:\\Program Files": "$PROGRAMFILES",
              "C:\\Program Files (x86)": "$PROGRAMFILES32",
              "C:\\Users": "\uf007",
              "C:\\Windows": "\ue70f",
              "D:\\USERNAME": "\ue617",
              "D:\\USERNAME\\Dropbox": "\uf16b",
              "D:\\USERNAME\\Google Drive": "\ue731",
              "D:\\USERNAME\\OneDrive": "\ue762",
              "D:\\USERNAME\\OneDrive\\Images\\": "\uf1c5\\",
              "D:\\USERNAME\\OneDrive\\Music\\": "\uf001\\"
            },
            "stack_count_enabled:": true,
            "style": "full",
            "template": " {{ .Path }} "
          }
        },
        {
          "type": "time",
          "style": "diamond",
          "trailing_diamond": "\ue0b4",
          "background": "#008000",
          "foreground": "#FFFFFF",
          "properties": {
            "template": " {{ .CurrentDate | date .Format }} ",
            "time_format": "15:04:05"
          }
        },
        {   // Line ending space (Alt+255) to fix prompt mess after window resizing
            "type": "text",
            "style": "plain",
            "properties": { "text": " " }
        }
      ]
    },

    // =====================
    // Prompt: Second Line
    // =====================

    {
          "alignment": "left",
          "newline": true,
          "type": "prompt",
          "segments": [
          {
            "type": "git",
            "style": "diamond",
            "trailing_diamond": "\ue0bc",
            "background": "#95ffa4",
            "foreground": "#193549",
            "properties": {
              "branch_icon": "\ue0a0",
              "fetch_stash_count": true,
              "fetch_status": true,
              "fetch_upstream_icon": true,
              "template": " {{ .HEAD }}{{ .BranchStatus }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0}} \uf692 {{ .StashCount }}{{ end }}{{ if gt .WorktreeCount 0}} \uf1bb {{ .WorktreeCount }}{{ end }} "
          }
        },
        {
          "type": "text",
          "style": "plain",
          "properties": {
          "template": "ϟ "
          }          
        }
      ]
    }
  ]
}
JanDeDobbeleer commented 2 years ago

@mdnava thx, the text segments are perfectly fine ;-)

mdnava commented 2 years ago

@JanDeDobbeleer not sure if I this merits to open a new issue.. let me know if I should.. I'm getting an extra line with the % symbol in Zsh since the v7.33 update. I don't think it was there before:

2022-03-12 14 23 45

I'm trying to figure out if it's something I did.. But it only happens in Zsh.

This is the startup code in the .zshrc file (the theme's config file is the same used for all shells):

export PROMPT_PRE_LNBR="TRUE"
export PROMPT_POST_LNBR="TRUE"
eval "$(posh --init --shell zsh --config $EnvScripts/AutoRun/posh-prompt.json)"
JanDeDobbeleer commented 2 years ago

@mdnava I don't have that with your config in zsh:

image
mdnava commented 2 years ago

@mdnava I don't have that with your config in zsh:

But it seems you're getting the "zsh>" prompt (instead of %) ?.. It would be essentially the same issue.. that shouldn't appear.. shouldn't it? Press enter and see if the "zsh>" appears again..

mdnava commented 2 years ago

I upgraded to v7.37.1 and I got this error on the Cmd Prompt:

> 2022-03-12 19:15 @ v7.37.1

Error: unknown command "D:\\USERNAME\\OneDrive\\Command\\EnvScripts\\AutoRun\\posh-prompt.json" for "oh-my-posh"
Run 'oh-my-posh --help' for usage.
unknown command "D:\\USERNAME\\OneDrive\\Command\\EnvScripts\\AutoRun\\posh-prompt.json" for "oh-my-posh

This happens when the Cmd prompt starts and becomes unusable, but it works in other shells.. I downgraded to v7.37.0 the prompt started working again.

I should also mention that I got the next error earlier today when I upgraded to v7.37.0 a few hours ago: This error happened at the end of a successful gallery-dl.exe command, but the prompt became unusable.

> 2022-03-12 17:30 @ v7.37.0

Error: unknown flag: --error
Usage:
  oh-my-posh [flags]
  oh-my-posh [command]
System.Management.Automation.RemoteException
Available Commands:
  completion  Generate the autocompletion script for the specified shell
  config      Interact with the configuration
  help        Help about any command
  prompt      Setup the prompt for your shell
  version     Print the version
System.Management.Automation.RemoteException
Flags:
  -c, --config string   config (required)
  -h, --help            help for oh-my-posh
  -i, --init            init (deprecated)
  -s, --shell string    shell (deprecated)
System.Management.Automation.RemoteException
Use "oh-my-posh [command] --help" for more information about a command.
System.Management.Automation.RemoteException
unknown flag: --error
JanDeDobbeleer commented 2 years ago

@mdnava the zsh> prompt is the default transient prompt (I have that enabled in zsh).

mdnava commented 2 years ago

But in your test you were using my config file.. it shouldn't be enabled in that test. Anyway.. have a great weekend.

JanDeDobbeleer commented 2 years ago

@mdnava enabling that is done outside of the config file. It requires additional shell functionality which can't be set from the config. In my shell config, this feature is enabled, meaning I get the default transient prompt with your config as it isn't specified.

mdnava commented 2 years ago

In case anyone else encounters the percent sign (%) issue in Zsh.. I fixed it by adding to .zshrc the following line:

unsetopt PROMPT_SP

github-actions[bot] commented 7 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.