Skip to content

Commit

Permalink
Fix wrapper script to handle CRYSTAL variable pointing to itself (#…
Browse files Browse the repository at this point in the history
…13032)

This patch ignores `CRYSTAL` if it's a path and points to the wrapper script itself. The fallback is `crystal`.
  • Loading branch information
straight-shoota authored Feb 1, 2023
1 parent e561a50 commit eca47ab
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions bin/crystal
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,16 @@ export CRYSTAL_HAS_WRAPPER=true

export CRYSTAL="${CRYSTAL:-"crystal"}"

# check if the crystal command refers to this script
if [ "$(realpath "$(command -v "$CRYSTAL")")" = "$SCRIPT_PATH" ]; then
PARENT_CRYSTAL="$CRYSTAL"

# check if the parent crystal command is a path that refers to this script
if [ -z "${PARENT_CRYSTAL##*/*}" -a "$(realpath "$PARENT_CRYSTAL")" = "$SCRIPT_PATH" ]; then
# ignore it and use `crystal` as parent compiler command
PARENT_CRYSTAL="crystal"
fi

# check if the parent crystal command refers to this script
if [ "$(realpath "$(command -v "$PARENT_CRYSTAL")")" = "$SCRIPT_PATH" ]; then
# remove the path to this script from PATH
NEW_PATH="$(remove_path_item "$(remove_path_item "$PATH" "$SCRIPT_ROOT")" "bin")"
# if the PATH did not change it will lead to an infinite recursion => display error
Expand All @@ -161,17 +169,17 @@ if [ "$(realpath "$(command -v "$CRYSTAL")")" = "$SCRIPT_PATH" ]; then
fi

if [ -z "$CRYSTAL_CONFIG_LIBRARY_PATH" ] || [ -z "$CRYSTAL_LIBRARY_PATH" ]; then
CRYSTAL_INSTALLED_LIBRARY_PATH="$($CRYSTAL env CRYSTAL_LIBRARY_PATH || echo "")"
CRYSTAL_INSTALLED_LIBRARY_PATH="$($PARENT_CRYSTAL env CRYSTAL_LIBRARY_PATH || echo "")"
export CRYSTAL_LIBRARY_PATH=${CRYSTAL_LIBRARY_PATH:-$CRYSTAL_INSTALLED_LIBRARY_PATH}
export CRYSTAL_CONFIG_LIBRARY_PATH=${CRYSTAL_CONFIG_LIBRARY_PATH:-$CRYSTAL_INSTALLED_LIBRARY_PATH}
fi

if [ -x "$CRYSTAL_DIR/crystal" ]; then
__warning_msg "Using compiled compiler at ${CRYSTAL_DIR#"$PWD/"}/crystal"
exec "$CRYSTAL_DIR/crystal" "$@"
elif ! command -v $CRYSTAL > /dev/null; then
elif ! command -v $PARENT_CRYSTAL > /dev/null; then
__error_msg 'You need to have a crystal executable in your path! or set CRYSTAL env variable'
exit 1
else
exec $CRYSTAL "$@"
exec $PARENT_CRYSTAL "$@"
fi

0 comments on commit eca47ab

Please sign in to comment.