JanDeDobbeleer / oh-my-posh

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

CMD.exe sets a hidden `=ExitCode` envvar, which the ExitCode module could use #4478

Closed chrisant996 closed 10 months ago

chrisant996 commented 10 months ago

Code of Conduct

What would you like to see changed/added?

This isn't a "request" per se -- It's just a courtesy "heads-up" in case you didn't yet know about the following:

Whenever cmd.exe runs a program, it sets a hidden envvar named =ExitCode to an 8 digit hexadecimal representation of the exit code from the program.

While the set command doesn't list hidden envvars whose names begin with =, programs written in Go or C/C++ or etc are able to get the hidden envvars by name. And even echo %=exitcode% prints the value.

Since on Windows launching OMP inherits the environment from its parent, maybe the ExitCode module could automatically get the exit code from %=ExitCode% when the -e flag isn't specified?

Note, however, that the =ExitCode envvar only seems to be set for programs that were executed, but not for internal commands.

bombenbodo commented 10 months ago

Could this be the reason why the Exitcode is always 1 when starting cmd.exe with injected clink the first time?

cmd_exitc(1)

Once cmd is loaded Exitcodes work as expected with OMP.

cmd_exitc(0)

my status snippet looks like this:

        {
          "background": "#2e9599",
          "background_templates": [
            "{{ if gt .Code 0 }}red{{ end }}"
          ],
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "always_enabled": true
          },
          "style": "diamond",
          "template": " {{ if gt .Code 0 }}\uf421{{ else }}\uf469{{ end }}\u2003",
          "trailing_diamond": "\ue0c0",
          "type": "status"
        }

Is there something I can do to fix this cosmetic issue?

chrisant996 commented 10 months ago

@bombenbodo No, what you asked about has nothing to do with what I mentioned.

When I start cmd.exe with Clink injected and oh-my-posh enabled, the exit code is 0, not 1.

The reason the exit code is 1 for you is because something was run in the cmd.exe session whose exit code was 1. You'll need to track down what that is. For example, maybe you have an initialization batch script, and maybe something it runs returns exit code 1.

JanDeDobbeleer commented 10 months ago

@bombenbodo yeah but no but what @chrisant996 said 😆

bombenbodo commented 10 months ago

Ah, ok I see. :) Any ideas whats wrong with my config then? I just use a modified oh-my-posh theme and my *.lua is basically the default.

load(io.popen('oh-my-posh --config="%POSH_THEMES_PATH%/monty.custom.omp.json" --init --shell cmd'):read("*a"))()

chrisant996 commented 10 months ago

It's not a "config" thing, it's not an oh-my-posh thing.

In between the invocation of cmd.exe and the moment when the input prompt is displayed for the first time, some batch file or program was run and returned 1 as the exit code.

Examine what batch scripts are being invoked and what they do, and what command line is given to cmd.exe, and what the autorun regkey includes.

You can examine the autorun regkey by running clink autorun show.

Also, what version of Clink are you using? I think a very old version might have returned 1 from clink inject, so if you're using a very old version then you should consider upgrading Clink.

It might also be that clink inject is returning 1 because it's being injected two times. That results in a non-fatal error, which returns 1. For example, that could happen if you've installed Clink for autorun and you're also explicitly invoking clink inject a second time.

bombenbodo commented 10 months ago

It might also be that clink inject is returning 1 because it's being injected two times.

Thanks that led me to the solution. I manually added clink for "All Users" to the registry.

Unsetting the Autorun String for "All Users" fixed it... :)

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