Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xdebug with Visual Studio Code ignores some breakpoints

I am using Visual Studio Code with the PHP Debug extension to debug a Laravel project. But some breakpoints are ignored and I cannot figure out why. I insist on the fact that not all breakpoints are ignored. For instance all breakpoints at method declarations are ignored but those at variable declarations are hit.

The Xdebug part of my php.ini:

xdebug.profiler_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back=1
xdebug.remote_enable = 1
xdebug.remote_port = 9000

This is my launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9001,
            "log": true
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

What I tried:

  • Changing the default port 9000 to 9001
  • Uninstalling and reinstalling the PHP debug extension (you never know)

Example: the "•" indicates a line breakpoint. All the breakpoints in the code sample below are ignored.

 public function testCreateWhenAllParametersAreCorrectlySpecifiedReturnsCompany()
 {
•    $attributes = [
         'business_name' => 'DANONE'
     ];

     $address = factory(Address::class)->create();

     $company = Company::create($attributes, $address);

     $this->assertInstanceOf(Company::class, $company);

     $this->assertDatabaseHas('companies', [
•        'address_id' => $address->id,
         'business_name' => 'DANONE'
     ]);
 }

How to get Xdebug with Visual Studio Code hitting all breakpoints or is this normal behaviour? Thank you in advance for your help.

Update #1 (08/07/2019)

The Zend extension path is specified in my php.ini like below.

zend_extension = "/usr/local/Cellar/[email protected]/7.2.18/pecl/20170718/xdebug.so"

And I tried to add php.validate.executablePath in my settings.json.

Update #2 (08/08/2019)

According to the comments and the answers at the time of this update, Xdebug ignoring some lines is the normal behavior. My questions then are why are some lines ignored? What kinds of lines are ignored? Is there an official list?

like image 995
louisfischer Avatar asked Aug 02 '19 09:08

louisfischer


People also ask

Why breakpoint is not hitting with VS Code?

If a source file has changed and the source no longer matches the code you're debugging, the debugger won't set breakpoints in the code by default. Normally, this problem happens when a source file is changed, but the source code wasn't rebuilt. To fix this issue, rebuild the project.

How do you keep breakpoint in Visual Studio code?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

Why does Visual Studio not stop at breakpoint?

This problem occurs because ASP.NET debugging isn't enabled on the application.


1 Answers

Running it in PhpStorm, but as Xdebug runs on PHP, the behavior is the same.

Xdebug stops when stuff is "done", on the lines that things are "done". This can be function calls, variable assignments, data transformations, etc. All these have in common that they must be explicit. Implicit assignments are ignored.

Xdebug does not pause execution for breakpoints placed "in" an action. Which is why the first breakpoint in your code will not work, and the second will.

Explicit & implicit example:

$attributes = [                   // this is implicitly an array, no pausing execution
    'business_name' => 'DANONE'   // this is explicitly assigned a string, execution paused
];

With some screenshots (code is from Symfony 4 public/index.php, with some obvious additions - blue background is paused execution "current line"):

example 1

Clearly executing functions inside an if() statement - it pauses

example 2

As we can see, breakpoints on all 3 lines of this array. However, the only one it pauses on is the assignment of the key/value pair. This is explicitly done, the array itself is implicitly declared.

example 3

Here we explicitly declare $testArray to be an array. So: it pauses.

example 4

This is to be complete, could've added it above. Implicit setting of type array, but explicit assigning of key/value.


So: yes.

If you had placed your breakpoints slightly different, they would've paused the execution. Not pausing on the implicit ones is normal behavior.


To be complete:

On local Apache installation I've got the following config:

[XDEBUG]
zend_extension = C:\xampp\php\ext\php_xdebug-2.7.1-7.3-vc15-x86_64.dll

xdebug.remote_mode = req
xdebug.remote_connect_back = 1
xdebug.default_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_handler = dbgp
xdebug.max_nesting_level = 200;

But I normally run this in Docker. In the docker-compose PHP image:

environment:
  PHP_XDEBUG_ENABLED: ${XDEBUG_ENABLED:-0}
  PHP_IDE_CONFIG: ${PHP_IDE_CONFIG:-serverName=ProjectName}
  XDEBUG_CONFIG: >
    idekey=PHPSTORM 
    remote_host=${XDEBUG_REMOTE_HOST_IP:-host.docker.internal}
    remote_port=${XDEBUG_REMOTE_PORT:-9000}
    remote_enable=1
  • (to enable xdebug, set XDEBUG_ENABLED environment config to 1 in application when starting using docker-compose up)
  • (replace "ProjectName" with the name of the project, use that name in your "servers" config in PhpStorm settings for Xdebug)

and in the Dockerfile

pecl install xdebug-2.7.2 redis && \
docker-php-ext-enable xdebug redis && \
  • (make sure to replace xdebug-2.7.2 with a version you want / is compatible with your PHP version, check that here)

Edit: additional based on comments on OP's question.

There's plenty of bug reports (see those comments, credit to LazyOne for finding them).

The last URL he provided is interesting as it's about the upcoming V2.8.0 (currently in 2.8.0beta1, not general release), about which he comments in this ticket about Xdebug not pausing execution on the implicit assignments:

I've just merged this into the master branch, which will become part of 2.8.0. I will release 2.7.2 soon (this week, today?!), and then probably next week a 2.8alpha1 release so that people can try this out.

(quote from 2019-05-06 12:49 by Derick, writer of Xdebug)

You can have a look at the changelog page or the roadmap for Xdebug.

The roadmap shows all of the features / fixes which will be included.

For 2.8.0 it shows that support for IDE's will be added for IDE's to show whether or not a breakpoint can be resolved. It's current release date is set for 2019-09-30.

Released

like image 149
rkeet Avatar answered Oct 04 '22 10:10

rkeet