Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xdebug shows different lines of source code when debugging #382

Open
pgee70 opened this issue Jul 26, 2021 · 9 comments
Open

Xdebug shows different lines of source code when debugging #382

pgee70 opened this issue Jul 26, 2021 · 9 comments
Labels

Comments

@pgee70
Copy link

pgee70 commented Jul 26, 2021

Hi using:

Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans

the Intellij ultimate IDE.
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.
i have version 3.0.1 of your framework installed in /vendor/kenjis/ci-phpunit-test

I find that when i run the unit tests from the command line the xdebug debugger opens the correct file, but the line numbers are out.
the line number being executed is in the 'shadow copy' which i have set application/tests/_
but this copy is not selected by the debugger when the tests are run.

i am not sure how the mapping between the 'shadow copy' and the code is maintained in your framework, but it seems off.

it is very difficult to debug unit tests now, previously this used to work fine.

are there any setup tricks/tips i can have to help here?

thanks.

@kenjis
Copy link
Owner

kenjis commented Jul 27, 2021

What do you mean by shadow copy?

If you use Monkey Patching, the line numbers are different from the actual line numbers.
Because it seems there is no way to patch without changing line numbers.
With the old PHP-Parser, it was possible to patch without changing line numbers.

are there any setup tricks/tips i can have to help here?

I don't know.

The best solution is not to use Monkey Patching. But if your code is not clean, it must be
impossible.

Monkey Patch hooks require and read the source file and modifies it on the fly.

@pgee70
Copy link
Author

pgee70 commented Jul 27, 2021

Thanks for your answer, it must be the monkey patching - I do use this. the code was written way before I did any unit testing and I have been bolted on unit testing on afterwards.
and the upgrade to the new parser must have caused the issue that I am experiencing.
thanks for your advice.

@pgee70 pgee70 closed this as completed Jul 27, 2021
@kenjis
Copy link
Owner

kenjis commented Jul 27, 2021

The feature of PHP-Parser may resolve this issue, but it is experimental and not yet complete.

Formatting-preserving pretty printing
https://github.com/nikic/PHP-Parser/blob/master/doc/component/Pretty_printing.markdown#formatting-preserving-pretty-printing

@kenjis kenjis changed the title xdebug help Xdebug shows different lines of source code when debugging Jul 27, 2021
@ipeevski
Copy link

I think this issue should be left open.
The strange thing is that even with monkey patching, it used to work fine until we upgraded PHP/Xdebug to newer versions. So, if it used to work, there is probably a way to make it work again.

There is another thing that doesn't work anymore - codecoverage (for the same reason - wrong lines are reported by xdebug)

@kenjis how does PHP-Parser apply here?

@kenjis kenjis reopened this Jan 17, 2022
@kenjis
Copy link
Owner

kenjis commented Jan 17, 2022

@ipeevski This is the problem in PHP-Parser. Xdebug has nothing to do with.
Monkey Patching reads a source file and parse it with PHP-Parser and modify it on the fly,
and PHP executes the modified source code.

Note: If you use Monkey Patching with PHP-Parser 4.6 or later, the line number when an error occurs is probably different from the actual source code. Please check the cache file of the source that Monkey Patching creates.
https://github.com/kenjis/ci-phpunit-test/blob/3.x/docs/HowToWriteTests.md#monkey-patching

@pgee70
Copy link
Author

pgee70 commented Jan 17, 2022

For my use-case, debugging is only needed while I am doing test-driven development and not when I run all unit tests.
so I turn off monkey patching, and then when I want to run all my unit-tests I turn it back on by manually editing the bootstrap file to toggle monkey patching
For the most part I monkey-patch ini-set php memory - my test environment uses lots of ram and generally the site only a few methods need their memory tweaked. All tests failed when a test was run that set the php memory limit to a lower amount than the test environment needed.
I refactored my code to not do ini-sets during the test environment - then I ran into other issues that opened up another can of worms, so I went back to manually editing the unit-test bootstrap file.

@ipeevski
Copy link

@kenjis That may be so (and explains why it happened after the upgrade, since as part of that we would have upgraded PHP-Parser to 4.13) and it helps with tracking things down when debugging manually, but still doesn't help with code coverage reports.
Are there any leads you can give us as to how we might address those?

For the record, I can confirm that disabling monkey patching fixes the code coverage reports.
We use MonkeyPatch in a few places to skip routing, recatcha, etc and won't be trivial to just turn off. Is our only option to rewrite those tests?

@ipeevski This is the problem in PHP-Parser. Xdebug has nothing to do with. Monkey Patching reads a source file and parse it with PHP-Parser and modify it on the fly, and PHP executes the modified source code.

Note: If you use Monkey Patching with PHP-Parser 4.6 or later, the line number when an error occurs is probably different from the actual source code. Please check the cache file of the source that Monkey Patching creates.
https://github.com/kenjis/ci-phpunit-test/blob/3.x/docs/HowToWriteTests.md#monkey-patching

@kenjis
Copy link
Owner

kenjis commented Jan 17, 2022

@ipeevski It seems there is no easy way to solve it.

  1. rewrite the test code to avoid using Monkey Patching.
  2. run Monkey Patching cached files (modified source files). You need to modify the Monkey Patching so that it does not change the source code on the fly.
  3. develop PHP-Parser's Formatting-preserving pretty printing.

We use MonkeyPatch in a few places

Excluding tests that use Monkey Patching from coverage (and turn Monkey Patching off) seems to be easy.

@ipeevski
Copy link

Excluding tests that use Monkey Patching seems like the best we could do until we rewrite tests I think
Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants