logo

パレット - カラフルに🎨

Palette — ビジュアルページビルダー、デザインの専門知識は不要です。

ライブデモ パレットをダウンロード

Scroll

Drush がエラーを表示する:bash で実行されない場合に $0 をフォールバックとして 'source' をサポートする

04/06/2025, by Ivan

私は膨大な数の設定を持っており、通常は php -d memory_limit=-1(PHP のメモリ制限を無効化)で設定インポートを実行します:

 php -d memory_limit=-1 ./vendor/bin/drush config-import -y

しかし今回はエラーが出ました:
 

# Support bash to support `source` with fallback on $0 if this does not run with bash
# https://stackoverflow.com/a/35006505/6512
selfArg="$BASH_SOURCE"
if [ -z "$selfArg" ]; then
    selfArg="$0"
fi

self=$(realpath $selfArg 2> /dev/null)
if [ -z "$self" ]; then
    self="$selfArg"
fi

dir=$(cd "${self%[/\\]*}" > /dev/null; cd ../drush/drush && pwd)

if [ -d /proc/cygdrive ]; then
    case $(which php) in
        $(readlink -n /proc/cygdrive)/*)
            # We are in Cygwin using Windows php, so the path must be translated
            dir=$(cygpath -m "$dir");
            ;;
    esac
fi

export COMPOSER_RUNTIME_BIN_DIR="$(cd "${self%[/\\]*}" > /dev/null; pwd)"

# If bash is sourcing this file, we have to source the target as well
bashSource="$BASH_SOURCE"
if [ -n "$bashSource" ]; then
    if [ "$bashSource" != "$0" ]; then
        source "${dir}/drush" "$@"
        return
    fi
fi

"${dir}/drush" "$@"

memory_limit=-1 を php-options として渡せば、このエラーを避けられます:

./vendor/bin/drush --php-options='-d memory_limit=-1' config-import -y

なぜ php -d memory_limit=-1 vendor/bin/drush … が今や破綻するのか

ファイルを php 経由で強制的に実行することで、PHP インタプリタに bash スクリプトを解析するよう指示していることになります:

php -d memory_limit=-1 ./vendor/bin/drush status

PHP は律儀に最初の非 PHP 行(# Support bash …)を実行しようとして即座に停止し、ラッパーのソースを画面に表示します。それがまさに、あなたが貼り付けた出力です。この変更は Drush 13.3 で導入され、アップストリームの issue「Running drush as php script fails after updating from 13.2.0」で議論されています。