From eca47ab041fac4539325d2c80307b63f53c6b766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Wed, 1 Feb 2023 20:24:59 +0100 Subject: [PATCH] Fix wrapper script to handle `CRYSTAL` variable pointing to itself (#13032) This patch ignores `CRYSTAL` if it's a path and points to the wrapper script itself. The fallback is `crystal`. --- bin/crystal | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bin/crystal b/bin/crystal index 4f0301544688..1f9566399984 100755 --- a/bin/crystal +++ b/bin/crystal @@ -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 @@ -161,7 +169,7 @@ 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 @@ -169,9 +177,9 @@ 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