Skip to content

Commit d34e588

Browse files
fix: rewrite wrapper symlink resolution for portability and backward compatibility
1 parent 0b472b7 commit d34e588

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

scripts/install.sh

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,16 +422,26 @@ set -e
422422
423423
# Resolve the installation directory
424424
# Handle both direct execution and symlinked scenarios
425-
if [ -L "$0" ]; then
426-
# Follow symlink
427-
SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || readlink "$0" 2>/dev/null || echo "$0")"
428-
else
429-
SCRIPT_PATH="$0"
430-
fi
425+
# Uses portable shell built-ins instead of GNU-specific readlink -f and realpath
426+
# Ignores inherited CDPATH, then resolves the script directory and its parent path
427+
SCRIPT_DIR="$(
428+
CDPATH=
429+
self=$0
430+
431+
# follow the symlink chain one hop at a time so relative targets resolve correctly
432+
while [ -L "$self" ]; do
433+
cd -- "${self%/*}" >/dev/null || :
434+
self=$(readlink "${self##*/}")
435+
done
436+
437+
case "$self" in
438+
(*/*) ;;
439+
(*) self=./$self ;;
440+
esac
431441
432-
# Get absolute path to script directory
433-
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
434-
INSTALL_DIR="$(dirname "$SCRIPT_DIR")"
442+
cd -P -- "${self%/*}" >/dev/null && pwd
443+
)"
444+
INSTALL_DIR="$(dirname -- "$SCRIPT_DIR")"
435445
436446
# Paths to bundled components
437447
NODE_BIN="$INSTALL_DIR/node/current/bin/node"

scripts/install.test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ test_wrapper_missing_cli
592592
printf '\n'
593593
test_wrapper_via_symlink
594594
printf '\n'
595+
test_wrapper_via_relative_symlink
596+
printf '\n'
595597
test_path_with_spaces
596598
printf '\n'
597599
test_version_equals_form

0 commit comments

Comments
 (0)