Skip to content

Commit e1a70c6

Browse files
committed
fix: source files using absolute paths for absolute BASH_SOURCE
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.
1 parent 6f03827 commit e1a70c6

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

bash_completion

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,6 +3125,18 @@ _comp_complete_minimal()
31253125
# https://lists.gnu.org/archive/html/bug-bash/2012-01/msg00045.html
31263126
complete -F _comp_complete_minimal ''
31273127
3128+
# Initialize the variable "_comp__base_directory"
3129+
# @var[out] _comp__base_directory
3130+
_comp__init_base_directory()
3131+
{
3132+
local REPLY
3133+
_comp_abspath "${BASH_SOURCE[0]-./bash_completion}"
3134+
_comp__base_directory=${REPLY%/*}
3135+
[[ $_comp__base_directory ]] || _comp__base_directory=/
3136+
unset -f "$FUNCNAME"
3137+
}
3138+
_comp__init_base_directory
3139+
31283140
# @since 2.12
31293141
_comp_load()
31303142
{
@@ -3177,11 +3189,7 @@ _comp_load()
31773189
# we want to prefer in-tree completions over ones possibly coming with a
31783190
# system installed bash-completion. (Due to usual install layouts, this
31793191
# often hits the correct completions in system installations, too.)
3180-
if [[ $BASH_SOURCE == */* ]]; then
3181-
dirs+=("${BASH_SOURCE%/*}/completions")
3182-
else
3183-
dirs+=(./completions)
3184-
fi
3192+
dirs+=("$_comp__base_directory/completions")
31853193
31863194
# 3) From bin directories extracted from the specified path to the command,
31873195
# the real path to the command, and $PATH
@@ -3323,12 +3331,10 @@ _comp__init_collect_startup_configs()
33233331
# run-in-place-from-git-clone setups. Notably we do it after the
33243332
# system location here, in order to prefer in-tree variables and
33253333
# functions.
3326-
if [[ ${base_path%/*} == */share/bash-completion ]]; then
3327-
compat_dir=${base_path%/share/bash-completion/*}/etc/bash_completion.d
3328-
elif [[ $base_path == */* ]]; then
3329-
compat_dir="${base_path%/*}/bash_completion.d"
3334+
if [[ $_comp__base_directory == */share/bash-completion ]]; then
3335+
compat_dir=${_comp__base_directory%/share/bash-completion}/etc/bash_completion.d
33303336
else
3331-
compat_dir=./bash_completion.d
3337+
compat_dir=$_comp__base_directory/bash_completion.d
33323338
fi
33333339
[[ ${compat_dirs[0]} == "$compat_dir" ]] ||
33343340
compat_dirs+=("$compat_dir")

0 commit comments

Comments
 (0)