Skip to content

Commit

Permalink
fix: source files using absolute paths for absolute BASH_SOURCE
Browse files Browse the repository at this point in the history
Some completion functions use BASH_SOURCE to identify the path to the
file where the functions are defined.  However, if the file was
sourced with the relative path (e.g. `. ./completions/make`),
BASH_SOURCE referenced by the function contains the relative path.
This causes the problem after the current working directory is changed
from the one where the file was sourced.  To make BASH_SOURCE
available to the completion files, we should replace a relative path
to the absolute path before passing the path to `source` or `.`.

To supply the absolute path, we add a new global variable
`_comp__base_directory`, which contains the directory where
`bash_completion` is located.
  • Loading branch information
akinomyoga committed Aug 23, 2024
1 parent 6f03827 commit e1a70c6
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -3125,6 +3125,18 @@ _comp_complete_minimal()
# https://lists.gnu.org/archive/html/bug-bash/2012-01/msg00045.html
complete -F _comp_complete_minimal ''
# Initialize the variable "_comp__base_directory"
# @var[out] _comp__base_directory
_comp__init_base_directory()
{
local REPLY
_comp_abspath "${BASH_SOURCE[0]-./bash_completion}"
_comp__base_directory=${REPLY%/*}
[[ $_comp__base_directory ]] || _comp__base_directory=/
unset -f "$FUNCNAME"
}
_comp__init_base_directory
# @since 2.12
_comp_load()
{
Expand Down Expand Up @@ -3177,11 +3189,7 @@ _comp_load()
# we want to prefer in-tree completions over ones possibly coming with a
# system installed bash-completion. (Due to usual install layouts, this
# often hits the correct completions in system installations, too.)
if [[ $BASH_SOURCE == */* ]]; then
dirs+=("${BASH_SOURCE%/*}/completions")
else
dirs+=(./completions)
fi
dirs+=("$_comp__base_directory/completions")
# 3) From bin directories extracted from the specified path to the command,
# the real path to the command, and $PATH
Expand Down Expand Up @@ -3323,12 +3331,10 @@ _comp__init_collect_startup_configs()
# run-in-place-from-git-clone setups. Notably we do it after the
# system location here, in order to prefer in-tree variables and
# functions.
if [[ ${base_path%/*} == */share/bash-completion ]]; then
compat_dir=${base_path%/share/bash-completion/*}/etc/bash_completion.d
elif [[ $base_path == */* ]]; then
compat_dir="${base_path%/*}/bash_completion.d"
if [[ $_comp__base_directory == */share/bash-completion ]]; then
compat_dir=${_comp__base_directory%/share/bash-completion}/etc/bash_completion.d
else
compat_dir=./bash_completion.d
compat_dir=$_comp__base_directory/bash_completion.d
fi
[[ ${compat_dirs[0]} == "$compat_dir" ]] ||
compat_dirs+=("$compat_dir")
Expand Down

0 comments on commit e1a70c6

Please sign in to comment.