ohmybash / oh-my-bash

A delightful community-driven framework for managing your bash configuration, and an auto-update tool so that makes it easy to keep up with the latest updates from the community.
https://ohmybash.github.io
MIT License
5.53k stars 624 forks source link

Completion broken when sourcing file #547

Closed RoggeHaj closed 3 months ago

RoggeHaj commented 3 months ago

In Bash, at least on version 4.4.20(1)-release (Ubuntu 18.04), I get an error when trying to source a file with completion. I've tried with a clean install of Oh My Bash, just to rule out any custom modifications, with the same results.

Steps to reproduce

  1. Enter leading dot followed by a space character
  2. Press <TAB>

Expected behavior

Completion on file names and directories in current directory.

Actual behavior

user1@computer1:~$ . bash: .: syntax error: operand expected (error token is ".")

OSH version

6c1bde9779841b096a88e96f43fda210b78e5ddc

akinomyoga commented 3 months ago

Thanks for the report. What are the results of the following commands (after attempting the completion for the command .)?

$ . [TAB] # <-- attempt the completion for the command . to intentionally produce error
$ complete -p .
$ declare -p BASH_COMPLETION_VERSINFO
RoggeHaj commented 3 months ago
$ complete -p .
bash: complete: .: no completion specification
$ declare -p BASH_COMPLETION_VERSINFO
declare -a BASH_COMPLETION_VERSINFO=([0]="2" [1]="8")
akinomyoga commented 3 months ago

Does the problem reproduce after running unalias -a?

$ unalias -a
$ . [TAB] <--- attempt completion
RoggeHaj commented 3 months ago

Yes, same as before

$ unalias -a
$ . bash: .: syntax error: operand expected (error token is ".")
akinomyoga commented 3 months ago

Thank you. Hmm, what is the result of the following command?

$ complete -p -D
RoggeHaj commented 3 months ago
$ complete -p -D
complete -F _completion_loader -D
akinomyoga commented 3 months ago

Thanks. I suspect _completion_loader of bash-completion-2.8. I'll check later the behavior with bash-completion-2.8. If possible, you can try using the latest version of bash-completion-2.12.

akinomyoga commented 3 months ago

I now tried OMB + bash-completion-2.8 + bash-4.4, but the problem doesn't seem to arise. I'd like to get a debug trace. Could you run the following commands?

$ exec 3> debug.txt; BASH_XTRACEFD=3; set -x
$ . [TAB] <-- attempt completion
$ set +x

Then, a file debug.txt is created. Could you attach the file in reply? You can drag and drop the file into the textarea to attach a file in GitHub.

RoggeHaj commented 3 months ago

debug.txt

akinomyoga commented 3 months ago

Thank you. I've checked its content. I think _xspecs is an indexed array in your environment (instead of an associated array) for some reason.

(edit: Now this is fine since I could reproduce the problem) What are printed on the session start when you put the line `declare -p _xspecs >&2` around the line `source "$OSH"/oh-my-bash.sh` in your `~/.bashrc`? More specifically, what are output on the session start when you add the following changes to `~/.bashrc`? ```diff diff --git a/templates/bashrc.osh-template b/templates/bashrc.osh-template index 4f1851d..c9ffac0 100644 --- a/bashrc +++ b/bashrc @@ -113,7 +113,9 @@ plugins=( # plugins+=(tmux-autoattach) # fi +declare -p _xspecs >&2 source "$OSH"/oh-my-bash.sh +declare -p _xspecs >&2 # User configuration # export MANPATH="/usr/local/man:$MANPATH" ```
akinomyoga commented 3 months ago

OK, I could reproduce the problem. The problem arises when bash-completion-2.8 is loaded inside OMB. This happens because OMB sources modules inside a shell function, while bash-completion-2.8 declares the associative array _xspecs as declare -A _xspecs. When declare -A _xspecs is executed inside source called from a function, it creates a local variable _xspecs, which disappears after the initialization. This causes the problem.

There is also a report at https://github.com/scop/bash-completion/issues/360. The fix to bash-completion was given by https://github.com/scop/bash-completion/pull/210 and included in the release 2.9.

akinomyoga commented 3 months ago

A workaround for bash-completion-2.8 is to source bash-completion at the top level of ~/.bashrc:

diff --git a/templates/bashrc.osh-template b/templates/bashrc.osh-template
index 4f1851d..40e85c2 100644
--- a/templates/bashrc.osh-template
+++ b/templates/bashrc.osh-template
@@ -4,6 +4,8 @@ case $- in
     *) return;;
 esac

+source /usr/share/bash-completion/bash_completion
+
 # Path to your oh-my-bash installation.
 export OSH=~/.oh-my-bash
RoggeHaj commented 3 months ago

Works like a charm. Many thanks!