php / php-src

The PHP Interpreter
https://www.php.net
Other
37.93k stars 7.72k forks source link

Show symlinks in stack trace file paths #15000

Open lc1337 opened 1 month ago

lc1337 commented 1 month ago

Description

Summary

Stack trace (from debug_backtrace(), exceptions, errors ..) shows always absolute/real/evaluated file paths, even though the script was run from symlink context.

How to reproduce

  1. Create a directory "d2" and a symlink to that directory "d1"
    mkdir D:\d2
    mklink /D "D:\d1" "D:\d2"
  2. Create a "D:\d2\test.php" script
    
    <?php

var_dump($argv);

(function(){ var_dump(debug_backtrace()); })();

3. Run the script using symlink path
`php.exe D:\d1\test.php`

#### Current behavior

array(1) { [0]=> string(14) "D:\d1\test.php" } array(1) { [0]=> array(4) { ["file"]=> string(14) "D:\d2\test.php" ["line"]=> int(8) ["function"]=> string(9) "{closure}" ["args"]=> array(0) { } } }


#### Expected behavior

array(1) { [0]=> string(14) "D:\d1\test.php" } array(1) { [0]=> array(4) { ["file"]=> string(14) "D:\d1\test.php" ["line"]=> int(8) ["function"]=> string(9) "{closure}" ["args"]=> array(0) { } } } ...



### PHP Version

8.3.9

### Operating System

Windows
lc1337 commented 1 month ago

Just found out that this may actually be a correct behaviour.

According to https://www.php.net/manual/en/function.debug-backtrace.php , "file" of debug_backtrace() result is related to __FILE__ constant which, according to https://www.php.net/manual/en/language.constants.magic.php#constant.file, should really be a full path with symlinks resolved.

However is this really wanted to have a resolved path in stack traces? For example it causes troubles when debuging in IDE, which opens the related files in out of project context.

SakiTakamachi commented 1 month ago

However is this really wanted to have a resolved path in stack traces? For example it causes troubles when debuging in IDE, which opens the related files in out of project context.

Personally, I think it's necessary. If use symbolic links for traces, I'm not sure what should happen if use unlink to remove the symbolic links in the process.

edit: I think can probably come up with a consistent specification, but can expect the implementation to be more complicated than it is now. If you think the change is worth the complexity, it's probably best to discuss it on the mailing list.

cmb69 commented 1 month ago

Just found out that this may actually be a correct behaviour.

Yeah, so I'm changing this ticket to a feature request.

If you think the change is worth the complexity, it's probably best to discuss it on the mailing list.

I agree.

And for clarification: I don't think this behavior is Windows specific. Can anybody confirm?

devnexen commented 1 month ago

assuming the mklink /D (/d ?) means soft link. this is what I get on mac

array(1) {
  [0]=>
  string(11) "d1/test.php"
}
array(1) {
  [0]=>
  array(4) {
    ["file"]=>
    string(24) "/private/tmp/d2/test.php"
    ["line"]=>
    int(7)
    ["function"]=>
    string(9) "{closure}"
    ["args"]=>
    array(0) {
    }
  }
}

so yes same outcome.

lc1337 commented 1 month ago

For example there could be an option to show stack traces with both symlinks and evaluated paths like this:

D:\d1\test.php (symlink) =>
D:\d3\test.php (symlink) =>
D:\d2\test.php:45

That would provide some additional debug info and also solved the IDE issue which would be now able to work with the files in right context.