Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,27 @@ set -e

# Resolve the installation directory
# Handle both direct execution and symlinked scenarios
if [ -L "$0" ]; then
# Follow symlink
SCRIPT_PATH="$(readlink "$0" 2>/dev/null || readlink -f "$0" 2>/dev/null || echo "$0")"
else
SCRIPT_PATH="$0"
fi
# Uses POSIX shell syntax and avoids GNU-specific readlink -f/realpath (requires readlink(1))
# Ignores inherited CDPATH, then resolves the script directory and its parent path
SCRIPT_DIR="$(
CDPATH=
self=$0

# follow the symlink chain one hop at a time so relative targets resolve correctly
while [ -L "$self" ]; do
dir=${self%/*}; [ "$dir" = "$self" ] && dir=.
cd "$dir" >/dev/null 2>&1 || exit 1
self=$(readlink "${self##*/}") || exit 1
done

case "$self" in
(*/*) ;;
(*) self=./$self ;;
esac

# Get absolute path to script directory
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
INSTALL_DIR="$(dirname "$SCRIPT_DIR")"
cd -P -- "${self%/*}" >/dev/null && pwd

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think cd -- would work on POSIX/macOS/BSD environments.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cd -- (option terminator) has been part of systems since 1995 and I have just checked on zsh shell in my macos and it supports this.

To be precise, this is completely safe to use cd -- on any system from 2005 onward:

  • macOS 10.0+ (2001+)
  • FreeBSD 6.0+ (2005+)
  • OpenBSD 5.0+ (2008+)
  • NetBSD 1.5+ (1999+)

I am not aware of how much backward compatibility do we need to have, but it seems like a valid decision, considering readlink even the basic version is relatively more recent.

)"
INSTALL_DIR="$(dirname -- "$SCRIPT_DIR")"

# Paths to bundled components
NODE_BIN="$INSTALL_DIR/node/current/bin/node"
Expand Down
33 changes: 33 additions & 0 deletions scripts/install.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,37 @@ test_wrapper_via_symlink() {
teardown
}

# ── Tests: wrapper via relative symlink from different directory ──

test_wrapper_via_relative_symlink() {
printf '%b\n' "${C_BOLD}Wrapper works via relative symlink from different directory${C_RESET}"
setup

prefix="$TEST_TMPDIR/devcontainers"
cli_version="0.75.0"

sh "$INSTALL_SCRIPT" --prefix "$prefix" --version "$cli_version" >/dev/null 2>&1

# Create a RELATIVE symlink in a different directory
link_dir="$TEST_TMPDIR/bin"
mkdir -p "$link_dir"

# Create relative symlink that points back to the wrapper
ln -s ../devcontainers/bin/devcontainer "$link_dir/devcontainer"
assert_symlink "$link_dir/devcontainer" "relative symlink created"

# Call from a different directory
cwd_before=$(pwd)
cd "$TEST_TMPDIR"
version_output=$("$link_dir/devcontainer" --version 2>/dev/null) && wrc=0 || wrc=$?

cd "$cwd_before"
assert_exit_code "0" "$wrc" "relative symlink works from different directory"
assert_contains "$version_output" "$cli_version" "relative symlink wrapper reports version"

teardown
}

# ── Tests: install to path with spaces ────────────────────────────

test_path_with_spaces() {
Expand Down Expand Up @@ -561,6 +592,8 @@ test_wrapper_missing_cli
printf '\n'
test_wrapper_via_symlink
printf '\n'
test_wrapper_via_relative_symlink
printf '\n'
test_path_with_spaces
printf '\n'
test_version_equals_form
Expand Down