From 57cc3bce94e49ebc95fa666823a2f9abad4a0023 Mon Sep 17 00:00:00 2001 From: Rune Morling Date: Tue, 25 Jun 2024 17:04:26 +0200 Subject: [PATCH] bash: Build new PATH the smart way **Summary** The goal with this PR is to ensure that any existing user-set PATH is respected to the widest extent possible. Any elements of the default PATH which have not already been added, will be added to the new PATH in (what I hope to be) a sane way. In the case of no user-set PATH, prepare a sane default PATH. I have left the debug statements in for now to provide visibility to the various test cases. Naturally, these debug statements will need to disappear before the commit is merged to main. Signed-off-by: Rune Morling --- packages/b/bash/files/profile/10-path.sh | 68 +++++++++++++++++++----- packages/b/bash/package.yml | 2 +- packages/b/bash/pspec_x86_64.xml | 12 ++--- 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/packages/b/bash/files/profile/10-path.sh b/packages/b/bash/files/profile/10-path.sh index e17981d6761..6672cdaae26 100644 --- a/packages/b/bash/files/profile/10-path.sh +++ b/packages/b/bash/files/profile/10-path.sh @@ -1,20 +1,64 @@ # Begin /usr/share/defaults/etc/profile.d/10-path.sh -export PATH="/usr/sbin:/usr/bin" -if [ -d "/usr/local/sbin" ]; then - export PATH="$PATH:/usr/local/sbin" -fi +PREFIX_PATH= +POSTFIX_PATH= -if [ -d "/usr/local/bin" ]; then - export PATH="$PATH:/usr/local/bin" -fi +# PATH exists, only append missing stuff to it +if [[ -n "${PATH}" ]]; then -if [ -d "$HOME/bin" ]; then - export PATH="$HOME/bin:$PATH" -fi + echo "DEBUG: Starting \$PATH: ${PATH}" + + for d in "/usr/sbin" "/usr/bin" "/usr/local/sbin" "/usr/local/bin"; do + # respect user path ordering if the element is already in $PATH + if [[ "${PATH}" =~ "${d}" ]]; then + : + echo "DEBUG: Skipping ${d} as it is already in \$PATH" + else + POSTFIX_PATH="${POSTFIX_PATH}:${d}" + echo "DEBUG: Added ${d} to \$POSTFIX_PATH, \$POSTFIX_PATH is now: ${POSTFIX_PATH}" + fi + done + + for d in "${HOME}/.local/bin" "${HOME}/bin"; do + # respect user path ordering if the element is already in $PATH + if [[ "${PATH}" =~ "${d}" ]]; then + : + echo "DEBUG: Skipping ${d} as it is already in \$PATH" + else + PREFIX_PATH="${d}:${PREFIX_PATH}" + echo "DEBUG: Added ${d} to \$PREFIX_PATH, \$PREFIX_PATH is now: ${PREFIX_PATH}" + fi + done + + # this should adequately respect existing path definitions + PATH="${PREFIX_PATH}${PATH}${POSTFIX_PATH}" + +else +# PATH does NOT exist, build it from scratch. -if [ -d "$HOME/.local/bin" ]; then - export PATH="$HOME/.local/bin:$PATH" + echo "DEBUG: \$PATH is empty, building a default \$PATH ..." + + DEFAULT_PATH="/usr/sbin:/usr/bin" + if [[ -d "/usr/local/sbin" ]]; then + DEFAULT_PATH="${DEFAULT_PATH}:/usr/local/sbin" + fi + + if [[ -d "/usr/local/bin" ]]; then + DEFAULT_PATH="${DEFAULT_PATH}:/usr/local/bin" + fi + + if [[ -d "$HOME/bin" ]]; then + DEFAULT_PATH="${HOME}/bin:${DEFAULT_PATH}" + fi + + if [[ -d "$HOME/.local/bin" ]]; then + DEFAULT_PATH="${HOME}/.local/bin:${DEFAULT_PATH}" + fi + + PATH="${DEFAULT_PATH}" fi +export PATH="${PATH}" +echo "DEBUG: \$PATH is now: ${PATH}" + # End /usr/share/defaults/etc/profile.d/10-path.sh diff --git a/packages/b/bash/package.yml b/packages/b/bash/package.yml index ece6fc6bc33..7677d80bb63 100644 --- a/packages/b/bash/package.yml +++ b/packages/b/bash/package.yml @@ -1,6 +1,6 @@ name : bash version : 5.2.26 -release : 80 +release : 81 source : - https://ftp.gnu.org/gnu/bash/bash-5.2.21.tar.gz : c8e31bdc59b69aaffc5b36509905ba3e5cbb12747091d27b4b977f078560d5b8 license : diff --git a/packages/b/bash/pspec_x86_64.xml b/packages/b/bash/pspec_x86_64.xml index dfc68d71bed..bf9b1e7f7c6 100644 --- a/packages/b/bash/pspec_x86_64.xml +++ b/packages/b/bash/pspec_x86_64.xml @@ -3,8 +3,8 @@ bash https://www.gnu.org/software/bash - Troy Harvey - harveydevel@gmail.com + Rune Morling + ermo@serpentos.com GPL-3.0-or-later system.base @@ -133,7 +133,7 @@ programming.devel - bash + bash /usr/include/bash/alias.h @@ -199,12 +199,12 @@ - + 2024-06-25 5.2.26 Packaging update - Troy Harvey - harveydevel@gmail.com + Rune Morling + ermo@serpentos.com \ No newline at end of file