Bash-it / bash-it

A community Bash framework.
MIT License
14.3k stars 2.29k forks source link

[Bug]: bash: /tmp/alias_completion-23769qilp1o: line 978: unexpected EOF while looking for matching `'' #2254

Open phil-markarian opened 5 months ago

phil-markarian commented 5 months ago

Expected behavior

source .bashrc doesn't work without errors.

Current behavior

Currently when I do source .bashrc it gives me the following error, bash: /tmp/alias_completion-23769qilp1o: line 978: unexpected EOF while looking for matching''`

Possible solution

No response

Context

I have a fresh install plus I have ble.sh plugin turned on and ble.sh installed.

Steps to reproduce

Install bash-it with --silent flag. Install ble.sh and then enable add-on. Do source .bashrc.

Bash-it version

d985e4c

List of enabled plugins, themes and aliases

base, oh-my-posh

Bash version

5.2.15

Operating system and version

Debian GNU/Linux 12

bash-it doctor output

# How to get: bash-it doctor

Your ~/.bashrc

#!/usr/bin/env bash

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

# Path to the bash it configuration
export BASH_IT="/root/.bash_it"

# Lock and Load a custom theme file.
# Leave empty to disable theming.
# location /.bash_it/themes/
export BASH_IT_THEME='oh-my-posh'

# Some themes can show whether `sudo` has a current token or not.
# Set `$THEME_CHECK_SUDO` to `true` to check every prompt:
#THEME_CHECK_SUDO='true'

# (Advanced): Change this to the name of your remote repo if you
# cloned bash-it with a remote other than origin such as `bash-it`.
# export BASH_IT_REMOTE='bash-it'

# (Advanced): Change this to the name of the main development branch if
# you renamed it or if it was changed for some reason
# export BASH_IT_DEVELOPMENT_BRANCH='master'

# Your place for hosting Git repos. I use this for private repos.
export GIT_HOSTING='git@git.domain.com'

# Don't check mail when opening terminal.
unset MAILCHECK

# Change this to your console based IRC client of choice.
export IRC_CLIENT='irssi'

# Set this to the command you use for todo.txt-cli
export TODO="t"

# Set this to the location of your work or project folders
#BASH_IT_PROJECT_PATHS="${HOME}/Projects:/Volumes/work/src"

# Set this to false to turn off version control status checking within the prompt for all themes
export SCM_CHECK=true
# Set to actual location of gitstatus directory if installed
#export SCM_GIT_GITSTATUS_DIR="$HOME/gitstatus"
# per default gitstatus uses 2 times as many threads as CPU cores, you can change this here if you must
#export GITSTATUS_NUM_THREADS=8

# Set Xterm/screen/Tmux title with only a short hostname.
# Uncomment this (or set SHORT_HOSTNAME to something else),
# Will otherwise fall back on $HOSTNAME.
#export SHORT_HOSTNAME=$(hostname -s)

# Set Xterm/screen/Tmux title with only a short username.
# Uncomment this (or set SHORT_USER to something else),
# Will otherwise fall back on $USER.
#export SHORT_USER=${USER:0:8}

# If your theme use command duration, uncomment this to
# enable display of last command duration.
#export BASH_IT_COMMAND_DURATION=true
# You can choose the minimum time in seconds before
# command duration is displayed.
#export COMMAND_DURATION_MIN_SECONDS=1

# Set Xterm/screen/Tmux title with shortened command and directory.
# Uncomment this to set.
#export SHORT_TERM_LINE=true

# Set vcprompt executable path for scm advance info in prompt (demula theme)
# https://github.com/djl/vcprompt
#export VCPROMPT_EXECUTABLE=~/.vcprompt/bin/vcprompt

# (Advanced): Uncomment this to make Bash-it reload itself automatically
# after enabling or disabling aliases, plugins, and completions.
# export BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE=1

# Uncomment this to make Bash-it create alias reload.
# export BASH_IT_RELOAD_LEGACY=1

# Load Bash It
source "$BASH_IT"/bash_it.sh

alias sourceme="source ~/.bashrc"
alias bashconf="nvim ~/.bashrc"
alias nv='nvim'

# Enable fuzzy history search with Ctrl+R
if [[ $- == *i* ]]; then
    bind '"\C-r": "$(fzf-history-widget)"'
fi

fzf-history-widget() {
    local selected_command
    selected_command=$(history | fzf +s --tac --no-sort --preview="echo {} | cut -d' ' -f4-")
    READLINE_LINE=${selected_command#* }
    READLINE_POINT=${#READLINE_LINE}
}

# Function to change oh-my-posh theme
change_posh_theme() {
    if [ -z "$1" ]; then
        echo "Usage: change_posh_theme <theme-name>"
        return 1
    fi
    local THEME_URL="https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v$(oh-my-posh --version)/themes/$1.omp.json"
    if curl --output /dev/null --silent --head --fail "$THEME_URL"; then
        sed -i "s#export POSH_THEME=.*#export POSH_THEME=${THEME_URL}#" /root/.bash_it/themes/oh-my-posh/oh-my-posh.theme.bash
        source /root/.bash_it/themes/oh-my-posh/oh-my-posh.theme.bash
        echo "Theme changed to $1"
    else
        echo "Theme $1 not found. Please check the theme name and try again."
        return 1
    fi
}

Notes

No response

akinomyoga commented 5 months ago

Does the problem only happen when ble.sh is enabled?

What are the outputs of the following commands?

$ alias -p
$ ls -l /tmp/alias_completion-*
phil-markarian commented 5 months ago

Yeah, it only happens when ble.sh is enabled.

Here are my outputs:

 {  home }  alias -p
alias -- -='cd -'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias _='sudo'
alias aptinst='apt-get install -V'
alias aptpurge='apt-get remove --purge'
alias aptrm='apt-get remove'
alias apts='apt-cache search'
alias aptshow='apt-cache show'
alias aptupd='apt-get update'
alias aptupg='apt-get dist-upgrade -V && sudo apt-get autoremove'
alias aptupgd='apt-get update && sudo apt-get dist-upgrade -V && sudo apt-get autoremove'
alias babsh='bash-it'
alias bash_it='bash-it'
alias bash_ti='bash-it'
alias bashconf='nvim ~/.bashrc'
alias bashit='bash-it'
alias batbsh='bash-it'
alias batshit='bash-it'
alias bshena='bash-it enable alias'
alias bshenc='bash-it enable completion'
alias bshenp='bash-it enable plugin'
alias bshha='bash-it help aliases'
alias bshhc='bash-it help completions'
alias bshhp='bash-it help plugins'
alias bshsa='bash-it show aliases'
alias bshsc='bash-it show completions'
alias bshsch='bash-it search'
alias bshsp='bash-it show plugins'
alias c='clear'
alias cd..='cd ..'
alias chkboot='cat /var/run/reboot-required'
alias chkup='/usr/lib/update-notifier/apt-check -p --human-readable'
alias cls='clear'
alias dco='docker-compose'
alias dcofresh='docker-compose-fresh'
alias dcol='docker-compose logs -f --tail 100'
alias dcou='docker-compose up'
alias dcouns='dcou --no-start'
alias dk='docker'
alias dkbash='dkelc'
alias dkelc='docker exec -it $(dklcid) bash --login'
alias dkex='docker exec -it '
alias dki='docker images'
alias dkideps='docker-image-dependencies'
alias dkip='docker image prune -a -f'
alias dklc='docker ps -l'
alias dklcid='docker ps -l -q'
alias dklcip='docker inspect -f "{{.NetworkSettings.IPAddress}}" $(docker ps -l -q)'
alias dkps='docker ps'
alias dkpsa='docker ps -a'
alias dkre='docker-runtime-environment'
alias dkri='docker run --rm -i '
alias dkric='docker run --rm -i -v $PWD:/cwd -w /cwd '
alias dkrit='docker run --rm -it '
alias dkritc='docker run --rm -it -v $PWD:/cwd -w /cwd '
alias dkrmac='docker rm $(docker ps -a -q)'
alias dkrmall='docker-remove-stale-assets'
alias dkrmflast='docker rm -f $(dklcid)'
alias dkrmi='docker-remove-images'
alias dkrmlc='docker-remove-most-recent-container'
alias dkrmli='docker-remove-most-recent-image'
alias dkrmui='docker images -q -f dangling=true | xargs -r docker rmi'
alias dksp='docker system prune -a -f'
alias dkvp='docker volume prune -f'
alias edit='${EDITOR:-${ALTERNATE_EDITOR:-nano}}'
alias h='history'
alias ipy='ipython'
alias irc='${IRC_CLIENT:=irc}'
alias l='ls -a'
alias l1='ls -1'
alias la='ls -AF'
alias lf='ls -F'
alias ll='ls -al'
alias ls='ls --color=auto'
alias md='mkdir -p'
alias nv='nvim'
alias pager='${PAGER:=less}'
alias pass='passgen'
alias piano='pianobar'
alias pkgfiles='dpkg --listfiles'
alias py='python'
alias q='exit'
alias rb='ruby'
alias rd='rmdir'
alias reload_aliases='source '\''/root/.bash_it/scripts/reloader.bash'\'' '\''alias'\'' '\''aliases'\'''
alias reload_completion='source '\''/root/.bash_it/scripts/reloader.bash'\'' '\''completion'\'' '\''completion'\'''
alias reload_plugins='source '\''/root/.bash_it/scripts/reloader.bash'\'' '\''plugin'\'' '\''plugins'\'''
alias shit='bash-it'
alias sl='ls'
alias sourceme='source ~/.bashrc'
alias vbpf='${VISUAL:-vim} ~/.bash_profile'
alias vbrc='${VISUAL:-vim} ~/.bashrc'
alias xt='extract'

  root on Tuesday at 9:47 PM                                                                                                                                       0.035s    MEM: 19.57% (4/8GB)
  {  home }  ls -l /tmp/alias_completion-*
-rw------- 1 root root 55028 Jun 11 11:29 /tmp/alias_completion-10570esqXFv
-rw------- 1 root root 55028 Jun 11 12:38 /tmp/alias_completion-11493Ehdlsu
-rw------- 1 root root 55195 Jun 11 17:47 /tmp/alias_completion-1561GEWrR5
-rw------- 1 root root 30301 Jun 11 09:07 /tmp/alias_completion-17005XV1NtE
-rw------- 1 root root 30277 Jun 11 01:45 /tmp/alias_completion-17153n8ZEZf
-rw------- 1 root root 55028 Jun 11 10:56 /tmp/alias_completion-17581A88T4D
-rw------- 1 root root 55028 Jun 11 10:57 /tmp/alias_completion-17658DSJbnP
-rw------- 1 root root 55028 Jun 11 10:48 /tmp/alias_completion-18184p0DUXk
-rw------- 1 root root 30277 Jun 11 01:49 /tmp/alias_completion-19156vbnEv5
-rw------- 1 root root 30277 Jun 11 01:46 /tmp/alias_completion-19645MrbVC2
-rw------- 1 root root 30285 Jun 11 01:26 /tmp/alias_completion-22757jQ0sof
-rw------- 1 root root 55195 Jun 11 17:48 /tmp/alias_completion-23769qilp1o
-rw------- 1 root root 55028 Jun 11 12:54 /tmp/alias_completion-25114jdRhHK
-rw------- 1 root root 55028 Jun 11 11:28 /tmp/alias_completion-26183Oh4p41
-rw------- 1 root root 55028 Jun 11 12:54 /tmp/alias_completion-26950Z3G6iL
-rw------- 1 root root 55028 Jun 11 11:22 /tmp/alias_completion-2812dReNIN
-rw------- 1 root root 55028 Jun 11 11:19 /tmp/alias_completion-30083FsrPwh
-rw------- 1 root root 55028 Jun 11 11:26 /tmp/alias_completion-3313JJOO4J
-rw------- 1 root root 55028 Jun 11 11:04 /tmp/alias_completion-37394nH2tf
-rw------- 1 root root 30285 Jun 11 01:32 /tmp/alias_completion-40951Rc1Km
-rw------- 1 root root 30277 Jun 11 01:44 /tmp/alias_completion-4852oxiw2E
-rw------- 1 root root 30285 Jun 11 01:24 /tmp/alias_completion-5107wXIogD
-rw------- 1 root root 55028 Jun 11 11:26 /tmp/alias_completion-5902Ev29qU
-rw------- 1 root root 55028 Jun 11 15:26 /tmp/alias_completion-6036UQF9hR
-rw------- 1 root root 28249 Jun 11 01:43 /tmp/alias_completion-735AEMpmp
-rw------- 1 root root 55028 Jun 11 12:41 /tmp/alias_completion-7789yY101a
-rw------- 1 root root 55195 Jun 11 16:25 /tmp/alias_completion-856rJWVkL
-rw------- 1 root root 55028 Jun 11 11:18 /tmp/alias_completion-9647dFWCfl
akinomyoga commented 5 months ago

Thanks. What is the result of the following command?

$ ble/widget/display-shell-version

The temporary files seem to remain, so I think it would be easier to look into that. Could you attach the file /tmp/alias_completion-23769qilp1o here? You can copy the file to alias_completion-23769qilp1o.txt (with the filename extension .txt added) and drag & drop the file into the textarea of the GitHub interface.

Note: there seem to be many other similar files, but please make sure to choose 23769qilp1o because the other files might not have caused the problem.

phil-markarian commented 5 months ago

Here is the output:

ble.sh, version 0.4.0-devel4+365101cf (noarch) [git 2.39.2, GNU Make 4.3, GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.2.0, GNU MP 6.2.1)]
bash-completion, version 2.11 (hash:aefcfdd4b924e8b03db0d49be8bdb5881ad7d0cd, 77084 bytes) (noarch)
bash-preexec, (hash:5f1208c33e624859eea70e3843bd9b8c9a06819e, 13046 bytes) (noarch)
bash-it, version +d985e4c9 (noarch), alias(apt docker docker-compose general), completion(aliases bash-it system), plugin(base blesh docker-compose docker explain fzf git history-eternal thefuck)
locale: LANG=en_US.UTF-8
terminal: TERM=xterm-256color wcwidth=12.1-west/15.1-2+ri, xterm:276 (0;276;0)

Here is the contents for /tmp/alias_completion-23769qilp1o

alias.txt

What do you mean by choosing exactly? I thought that these files are autogeneriated when using any of the build in aliases.

akinomyoga commented 5 months ago

Thanks. I'll take a look.

What do you mean by choosing exactly?

Your ls result shows many temporary files /tmp/alias_completion-XXXXXXXXXXX where the part XXXXXXXXXXX are randomly generated. The sizes of those files seem to be different, so the file contents are different. I wanted the content of the file with XXXXXXXXXXX being exactly 23769qilp1o because that is the one you reported in your first post.

I thought that these files are autogeneriated when using any of the build in aliases.

Yes. I guess they would probably cause the same issue, but I can't say they cause the present problem 100%. Also the hint for the line number 978 in your first report cannot be applied to other files.

phil-markarian commented 5 months ago

Thanks. I'll take a look.

Thanks.

I wanted the content of the file with XXXXXXXXXXX being exactly 23769qilp1o because that is the one you reported in your first post.

Ah, I get what you were saying now. Had a brain fart lol.

akinomyoga commented 5 months ago

This is an issue of the aliases completion module. The following line is causing the problem:

https://github.com/Bash-it/bash-it/blob/d985e4c96b1eb4e7374b3ab53eea3513fc9f50ce/completion/available/aliases.completion.bash#L94

The variables $alias_cmd and $alias_args may contain an arbitrary string, but they are not properly quoted in generating the body of the dynamically defined function.

I'm not sure if it will be fixed in Bash-it because I don't see the activity of the Bash-it maintainers recent years. At least, I guess it would take months to have it fixed. If you don't use aliases module, you can disable the module by running the following command.

$ bash-it disable completion aliases
phil-markarian commented 5 months ago

Thanks for looking at it for me.

The variables $alias_cmd and $alias_args may contain an arbitrary string, but they are not properly quoted in generating the body of the dynamically defined function.

Hmm,I see.

I'm not sure if it will be fixed in Bash-it because I don't see the activity of the Bash-it maintainers recent years. At least, I guess it would take months to have it fixed.

I don't mind fixing a bug myself, but the bash-it system is kind of complex and I am not very well versed in bash yet. I'll give a crack at it and see if I can get something to work though. It doesn't really impact my productivity but it stands out haha.

phil-markarian commented 5 months ago

Used Claude and ChatGPT to help me get close to fixing the issue. The issue was making sure that the single quote is escaped properly. The code below fixes the error that occurs, but this code is buggy because when I tried to type say something like ls /tmp/ it will gave a bunch of strange suggestions for some reason. Anyway, I'll add updates as I go along.

echo "function $compl_wrapper {
    local compl_word=\${2?}
    local prec_word=\${3?}
    # check if prec_word is the alias itself. if so, replace it
    # with the last word in the unaliased form, i.e.,
    # alias_cmd + ' ' + alias_args.
    if [[ \$COMP_LINE == \"\$prec_word \$compl_word\" ]]; then
        prec_word='${alias_cmd} ${alias_args//\'/\'\\\\\'\'}' 
        prec_word=\${prec_word#* }
    fi
    (( COMP_CWORD += ${#alias_arg_words[@]} ))
    COMP_WORDS=(\"\${alias_cmd}\" \"\${alias_arg_words[@]//\'/\'\\\\\'\'}\" \"\${COMP_WORDS[@]:1}\")
    (( COMP_POINT -= \${#COMP_LINE} ))
    COMP_LINE=\${COMP_LINE//$alias_name/\${alias_cmd} \${alias_args//\'/\'\\\\\'\'}}
    (( COMP_POINT += \${#COMP_LINE} ))
    \"\${compl_func}\" \"\${alias_cmd}\" \"\$compl_word\" \"\$prec_word\"
}" >> "$tmp_file"