bethink
bethink

Reputation: 23

Can't add a breakpoint to Twig files in VS Code

I am trying to debug a Twig template in my Drupal 9 site. I am using VSCode as my IDE and using Xdebug for debugging. I have setup/configured the installation based on the instructions outlined here - https://www.drupal.org/docs/develop/development-tools/configuring-visual-studio-code

I am able to set breakpoints in PHP files (step thru and watch variables successfully) however I just can't seem to do the same for Twig template files (just so I can see what variables I have access to in that template). What I mean by when I say I am unable to add the breakpoint is that clicking on the gutters of a Twig file within VSCode does not result in addition of any breakpoint.

I'd like to take advantage of Xdebug instead of using debugging functions like breakpoint() and dump() offered by the Devel and Twig_xdebug modules within Drupal. I am pretty sure I am doing something wrong or have missed a step but I can't seem to figure out what it is.

Some more details on my env:

I'd appreciate any pointers on what I am doing wrong and/or what I can try. Also, I hope I provided enough info on my use case.

Upvotes: 2

Views: 2147

Answers (2)

NikLP
NikLP

Reputation: 101

Came here looking for an answer to this very question.

You may be interested to know that there are a couple of ways to achieve this. You can either use a built-in feature of the devel.module (see: https://www.drupal.org/node/2788089) or there is also a contrib module called twig_xdebug, which lives here: https://www.drupal.org/project/twig_xdebug

I think the mechanism behind both of these is probably very similar, to be honest. The latter of those links provides further links to several guides on the subject matter. HTH! :)

Upvotes: 0

Derick
Derick

Reputation: 36784

Twig compiles the template files into PHP files. You need to set breakpoints in these Twig generated PHP files in order to debug them.

On this page, I also see that there is caching involved, which you will need to disable. That documentation seems to be quite comprehensive.

Upvotes: 1

Related Questions