10BC0 Add support for XDebug path mapping by fabpot · Pull Request #4733 · twigphp/Twig · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@fabpot
Copy link
Contributor
@fabpot fabpot commented Jan 12, 2026

Closes #4697

@fabpot fabpot force-pushed the xdebug-support branch 4 times, most recently from f531331 to 5a46aa6 Compare January 13, 2026 08:52
}

foreach (glob($mapPath.'/*.map') as $mapFile) {
xdebug_set_source_map($mapFile);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not available yet but this is the cleaneast way to do it IMHO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any public discussion on the Xdebug side about this function ?

Btw, the verb set in the name is confusing if it registers a new source map without removing the previous one, as that's not the usual meaning of set in function names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a feature that I haven't added yet, I am more than happy to discuss how it should work. I agree that using xdebug_add_source_map is probably a better choice. Right now, the internals will reject a new source map if it touches the same files though, and that seems not so easy to change.

Alternatively, I could add a new function xdebug_add_source_map_directory, which you would only have to call once before the compiled template files are loaded into PHP's memory through include.

Then Xdebug can (additionally) scan this directory to pick up any source maps that exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xdebug_add_source_map_directory might be better for the Twig use case indeed (and might maybe have less overhead for requests not using breakpoints by skipping the filesystem scanning, depending on when xdebug actually traverses those directories)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It currently scans the files (if path mapping is enabled) at the start of every request. Although for now, it only works with the step debugger, in the future I want to extend the support to maps to other features too: https://xdebug.org/funding/001-native-path-mapping#future-scope

@fabpot
Copy link
Contributor Author
fabpot commented Jan 13, 2026

@derickr I would love to get your feedback on how I've integrated the source map feature with Twig.

$directories = [];
foreach ($this->caches as $cache) {
if ($cache instanceof DirectoryCacheInterface) {
$directories []= $cache->getDirectories();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$directories []= $cache->getDirectories();
$directories[] = $cache->getDirectories();

Copy link
Contributor
@derickr derickr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this!

I left some comments and questions to ask how things can be improved.

I'm also still waiting on https://youtrack.jetbrains.com/issue/WI-81344/Cant-Set-Breakpoints-in-Template-Files-Even-Though-I-Added-The-File-Name-Pattern-to-PHP to make it into a release of PhpStorm (it's already fine with VS Code's Plugin)

}

foreach (glob($mapPath.'/*.map') as $mapFile) {
xdebug_set_source_map($mapFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a feature that I haven't added yet, I am more than happy to discuss how it should work. I agree that using xdebug_add_source_map is probably a better choice. Right now, the internals will reject a new source map if it touches the same files though, and that seems not so easy to change.

Alternatively, I could add a new function xdebug_add_source_map_directory, which you would only have to call once before the compiled template files are loaded into PHP's memory through include.

Then Xdebug can (additionally) scan this directory to pick up any source maps that exist.

$endLine,
basename($templatePath),
$debugInfo[$startLine],
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine, this gets large, as there is a separate entry in the source file for each line.

I am planning on optimising this, using how JavaScript source maps approach the line-to-line mappings, if possible.

I would also recommend that if $startLine and $endline are the same, you should not include the $endLine in the mapping. It saves some processing on the Xdebug side (runs many times), where doing the test here needs to be done once per map-write.

$startLine = $compiledLines[$i];
// End line is exclusive, so use next start line minus 1
// Use a reasonable max for last range (Xdebug can't handle PHP_INT_MAX)
$endLine = isset($compiledLines[$i + 1]) ? $compiledLines[$i + 1] - 1 : 999999;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it that you need to use a random largish constant (999999) here. Perhaps it would make sense if I add an EOF marker to the parser, so that you can do:

compiled_file.php:5-EOF = orig_file.twig:4

Would that be helpful?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would definitely be helpful here.

@derickr
Copy link
Contributor
derickr commented Jan 15, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add support for XDebug native path mapping for compiled templates

5 participants

0