Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

starship init bash in vscode: integrated terminal crashes due to infinite loop #5816

Open
NeodymiumFerBore opened this issue Mar 2, 2024 · 0 comments · May be fixed by #5817
Open

starship init bash in vscode: integrated terminal crashes due to infinite loop #5816

NeodymiumFerBore opened this issue Mar 2, 2024 · 0 comments · May be fixed by #5817
Labels
🐛 bug Something isn't working as expected.

Comments

@NeodymiumFerBore
Copy link

NeodymiumFerBore commented Mar 2, 2024

pkg_version:1.17.1
branch:master
commit_hash:
build_time:2024-01-02 18:32:30 +00:00
build_env:rustc 1.75.0 (82e1608df 2023-12-21),

Current Behavior

When using eval "$(starship init bash)" in my bashrc, when I start Visual Studio Code integrated terminal, it loads correctly, but if I try to source my bashrc manually during the session, the terminal crashes (and closes).

Expected Behavior

The vscode integrated terminal does not crash when running eval "$(starship init bash)" twice in the same terminal.

More context

  • Tested on Windows 10 with vscode 1.87.0, with lot of extensions and WSL as the terminal backend
  • Tested on Ubuntu 22.04 with vscode 1.86.2, fresh install, no extensions
  • Both OS with same version of starship (1.17.1)

If eval "$(starship init bash)" is not run as part of bashrc (or any sourced file at terminal startup), running it multiple times in the terminal works perfectly. The problem appears when it is executed at terminal startup. May be linked to how vscode handles its shell integration.

My debug results

TL;DR: infinite loop caused by starship preserving PROMPT_COMMAND, and by vscode also preserving PROMPT_COMMAND.


Debugging details (click to unfold)

In vscode, with shell integration enabled (NOT the experimental ones), PROMPT_COMMAND is set to __vsc_prompt_cmd_original (after sourcing bashrc), and it overrides the value set by starship.

$ # This is a fresh terminal in vscode, with shell integration enabled, and with starship init in bashrc.

$ type -t starship_precmd
function

$ echo $PROMPT_COMMAND 
__vsc_prompt_cmd_original

$ type __vsc_prompt_cmd_original
__vsc_prompt_cmd_original is a function
__vsc_prompt_cmd_original () 
{ 
    __vsc_status="$?";
    __vsc_restore_exit_code "${__vsc_status}";
    for cmd in "${__vsc_original_prompt_command[@]}";
    do
        eval "${cmd:-}";
    done;
    __vsc_precmd
}

$ echo "${__vsc_original_prompt_command[@]}"
starship_precmd

$ type __vsc_precmd
__vsc_precmd is a function
__vsc_precmd () 
{ 
    __vsc_command_complete "$__vsc_status";
    __vsc_current_command="";
    __vsc_update_prompt
}

vscode preserves PROMPT_COMMAND and executes it before its own __vsc_precmd function.

But starship does the same: it preserves PROMPT_COMMAND and executes it before its own logic in starship_precmd.

We can see it if we try to echo $_PRESERVED_PROMPT_COMMAND in .bashrc. Because this is prompt related stuff, the bashrc runs to completion, and the infinite loop is triggered after that, when the prompt needs to be printed.

_PRESERVED_PROMPT_COMMAND:
• me@host:~ fancyprompt
$ # this was a new terminal opened in vscode

• me@host:~ fancyprompt
$ . ~/.bashrc

_PRESERVED_PROMPT_COMMAND: __vsc_prompt_cmd_original
# (then terminal crashes)

This is the source of the infinite loop.

  • At terminal startup, __vsc_prompt_cmd_original does not exist, and starship_precmd executes just fine
  • When evaluating starship init bash again:
    • PROMPT_COMMAND is set to __vsc_prompt_cmd_original (and it will execute starship_precmd)
    • starship saves this PROMPT_COMMAND as _PRESERVED_PROMPT_COMMAND
    • __vsc_prompt_cmd_original executes starship_precmd
    • starship_precmd executes __vsc_prompt_cmd_original as _PRESERVED_PROMPT_COMMAND
    • __vsc_prompt_cmd_original executes starship_precmd
    • infinite loop

Possible Solution

In starship.bash, add a verification specific to vscode:

  • check if PROMPT_COMMAND is set to __vsc_prompt_cmd_original
  • if not, then keep current behavior
  • if it is:
    • do not redefine PROMPT_COMMAND
    • if ${__vsc_original_prompt_command[@]} does NOT contain starship_precmd, add it

Environment

  • Starship version: 1.17.1
  • bash version: GNU bash, version 5.2.26(1)-release (x86_64-unknown-linux-gnu)
    Copyright (C) 2022 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

  • Operating system: Linux Unknown
  • Terminal emulator: vscode 1.86.2
  • Git Commit Hash:
  • Branch/Tag: master
  • Rust Version: rustc 1.75.0 (82e1608df 2023-12-21)
  • Rust channel: release
  • Build Time: 2024-01-02 18:32:30 +00:00

Relevant Shell Configuration

bashrc

# minimalist ~/.bashrc for testing

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

shopt -s histappend
shopt -s checkwinsize
HISTCONTROL=ignoreboth
HISTSIZE=1000
HISTFILESIZE=2000

# Add ~/.local/bin in PATH if not already present
case :$PATH:
  in *:$HOME/.local/bin:*) ;; # do nothing, it's there
     *) export PATH="$HOME/.local/bin:$PATH" ;;
esac

# Fallback PS1
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

command -v starship &>/dev/null && eval "$(starship init bash)"

alias ll='ls -alF'

Starship Configuration

Default (no file)

@NeodymiumFerBore NeodymiumFerBore added the 🐛 bug Something isn't working as expected. label Mar 2, 2024
NeodymiumFerBore added a commit to NeodymiumFerBore/starship that referenced this issue Mar 2, 2024
@NeodymiumFerBore NeodymiumFerBore linked a pull request Mar 3, 2024 that will close this issue
5 tasks
NeodymiumFerBore added a commit to NeodymiumFerBore/starship that referenced this issue Mar 3, 2024
NeodymiumFerBore added a commit to NeodymiumFerBore/starship that referenced this issue Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working as expected.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant