nushell

Why does this nushell closure return an empty string even when the control path leads to a literal?


I'm trying to configure nushell to print my current git branch as part of my shell prompt. To start simple on the problem, I'm just getting it to check whether or not I'm in a git repository or not, and put that into the prompt.

It's working perfectly when I am in a git repo, but if not, then it just gives me an empty prompt (except for the angle bracket it always puts).

Here's the code I'm using (well, I've hidden my real name, but it's close enough):

def in_git_repo [] {
     if (git rev-parse --is-inside-work-tree err> /dev/null) == "true" {
        true
     } else {
        false
     }
}

$env.PROMPT_COMMAND = {
    let time = (ansi green) + (date now | format date '%H:%M') + (ansi reset);

    let dir = if (pwd) == "/Users/redacted" {
        "~"
    } else {
        (ansi blue) + (pwd | path basename) + (ansi reset);
    }

    let git = if (in_git_repo) {
        "git"
    } else {
        ""
    }
    
    if $git != "" {
        $time + " " + $dir + " " + $git
    } else {
        $time + " " + $dir
    }   
}

I can't find a way to put any debug information in this closure either, echo statements don't seem to be printed, so I'm just guessing that it is hopefully reaching the else clauses, but even when it does, nothing happens.

If anything else about the way I'm doing stuff seems wrong/weird, let me know about that as well.

Thanks!


Solution

  • git rev-parse returns a non-zero exit code when it's run outside of a git repo, and nushell converts that to an error:

    ❯ pwd
    /home/weirdan/src
    ❯ try { git rev-parse --is-inside-work-tree err> /dev/null } catch {|e| $e }
    ╭───────────┬────────────────────────────────────────────────────────────────────╮
    │ msg       │ External command had a non-zero exit code                          │
    │ debug     │ NonZeroExitCode { exit_code: 128, span: Span { start: 170381, end: │
    │           │  170384 } }                                                        │
    │ raw       │ NonZeroExitCode { exit_code: 128, span: Span { start: 170381, end: │
    │           │  170384 } }                                                        │
    │ rendered  │ Error: nu::shell::non_zero_exit_code                               │
    │           │                                                                    │
    │           │   × External command had a                                         │
    │           │ non-zero exit code                                                 │
    │           │    ╭─[entry #15:1:7]                                               │
    │           │  1 │ try { git rev-parse                                           │
    │           │ --is-inside-work-tree err> /dev/null } catch {|e| $e }             │
    │           │    ·                                                               │
    │           │ ─┬─                                                                │
    │           │    ·        ╰── exited with code 128                               │
    │           │    ╰────                                                           │
    │           │                                                                    │
    │ json      │ {"msg":"External command had a non-zero exit                       │
    │           │ code","labels":[{"text":"exited with code 128","span":{"start":170 │
    │           │ 381,"end":170384}}],"code":"nu::shell::non_zero_exit_code","url":n │
    │           │ ull,"help":null,"inner":[]}                                        │
    │ exit_code │ 128                                                                │
    ╰───────────┴────────────────────────────────────────────────────────────────────╯
    

    It's likely that errors in a prompt command are suppressed

    So, instead of that if you need something along the lines of:

    def in_git_repo [] {
      try { git rev-parse --is-inside-work-tree e> /dev/null } 
      | default "false" 
      | $in == "true"
    }