beyondcode / herd-community

82 stars 1 forks source link

[Bug]: Herd doesn't listen to breakpoints in PHPStorm when testing through CLI mode. #834

Closed renzocastillo closed 1 month ago

renzocastillo commented 3 months ago

Platform

macOS

Operating system version

MacOS Sonoma 14.5

System architecture

ARM64 (M1, M2, etc)

Herd Version

1.7.8 Build(28)

PHP Version

PHP 8.1.29

Bug description

When I try to debug a PHPUnit test, Herd doesn't capture the breakpoints I establish in my source code. However, it does capture them when I run my Laravel application using the web browser with the debugging tool. I frequently use CLI debugging for test-driven development to verify my application's functionality.

https://github.com/beyondcode/herd-community/assets/21372177/4518f2c2-ffb6-4671-82d4-338e272b9301

Steps to reproduce

image image image image image

Workaround solution:

So, for this reason, I checked Herd PHP configuration and found this debug.ini file

image

Main PHP ini file contains:

curl.cainfo=/Users/renzocastillo/Library/Application Support/Herd/config/php/cacert.pem
openssl.cafile=/Users/renzocastillo/Library/Application Support/Herd/config/php/cacert.pem
pcre.jit=0
output_buffering=4096

auto_prepend_file=/Applications/Herd.app/Contents/Resources/valet/dump-loader.php
memory_limit=-1

Debug ini file contains:

zend_extension=/Applications/Herd.app/Contents/Resources/xdebug/xdebug-81-arm64.so
xdebug.mode=debug,develop
xdebug.start_with_request=yes
xdebug.start_upon_error=yes

So I know currently have a bash script which basically adds all content from debug.ini into php.ini

# Dynamic path to PHP binary
PHP_PATH="$HOME/Library/Application Support/Herd/bin/php"

# Retrieve PHP version and remove the dot
PHP_VERSION=$("$PHP_PATH" -v | grep -oE '[0-9]+\.[0-9]+' | head -1 | tr -d '.')
PHP_INI_FILE="$HOME/Library/Application Support/Herd/config/php/${PHP_VERSION}/php.ini"
DEBUG_INI="$HOME/Library/Application Support/Herd/config/php/${PHP_VERSION}/debug/debug.ini"

# Check if the PHP ini file exists
if [[ ! -f "$PHP_INI_FILE" ]]; then
    echo "$PHP_INI_FILE does not exist."
    exit 1
fi

# Check if the script has write permissions on php.ini file
if [[ ! -w "$PHP_INI_FILE" ]]; then
    echo "The script does not have write permissions for $PHP_INI_FILE"
    exit 1
fi

# Loading debug.ini content
if [[ ! -f "$DEBUG_INI" ]]; then
    echo "$DEBUG_INI does not exist."
    exit 1
fi

DEBUG_CONTENT=$(printf "; START PHP INI FROM debug.ini\n%s\n; END PHP INI FROM debug.ini\n" "$(cat "$DEBUG_INI")")

# Use grep to check if the content from debug.ini is already in php.ini
if grep -Fq "; START PHP INI FROM debug.ini" "$PHP_INI_FILE"; then
    echo "Disabling Xdebug in $PHP_INI_FILE"
    # Use sed to remove the block from php.ini
    sed -i '' '/; START PHP INI FROM debug.ini/,/; END PHP INI FROM debug.ini/d' "$PHP_INI_FILE"
else
    echo "Enabling Xdebug in $PHP_INI_FILE"
    # Append debug.ini content to php.ini
    {
        # Print php.ini without trailing empty lines using sed
        sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$PHP_INI_FILE"
        # Then append debug.ini content
        printf "\n%s" "$DEBUG_CONTENT"
    } > temp_file && mv temp_file "$PHP_INI_FILE"
fi

So now I do this to be able to test through cli:

https://github.com/beyondcode/herd-community/assets/21372177/40a6604c-a837-42a5-9841-42f34f68e21c

It would be nice to have Herd handling this out of the box. I've followed the instructions stated here.

Maybe we could add a checkbox here to manually enable Xdebug by clicking it or using a keyboard shortcut. This would be useful for debugging scenarios in the command line interface.

image

I appreciate your help and thank you for building this cool product.

Relevant log output

No response

mpociot commented 3 months ago

We are currently talking with JetBrains about adding a Herd detection into PHPStorm to make this easier 🎉

mecca666 commented 3 months ago

I have the exact same issue with PhpStorm and I use Herd Pro, would be nice to get this working

wdavis commented 2 months ago

@renzocastillo your script works great! i added a raycast script command just like you did to toggle so I can debug my tests in PHPStorm.

@mpociot look forward to news regarding the direct detection. Thank you.

mpociot commented 1 month ago

CleanShot 2024-08-09 at 12 36 40@2x

In the meantime you can configure a custom "debug" interpreter in PHPStorm to use for CLI commands.