nette / tracy

😎 Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.
https://tracy.nette.org
Other
1.75k stars 218 forks source link

Uncaught ReferenceError: Tracy is not defined #579

Closed M4RtY closed 4 months ago

M4RtY commented 4 months ago

Version: 2.10

Bug Description

When I uploaded project to Linux hosted VPS witch Apache 2.4 everything works except the Tracy. I have checked the Apache configuration file but I do not see any difference that can cause this issue. I was trying to debug this Tracy source code and the stack trace says DeferredContent.php:76

1) I can see that there is a echo $str that breaks the code and it is trying to get built CSS + JS 2) I have dumped the $str and this looks fine - CSS + JS for tracy

Source code

if ($asset === 'js') {
    header('Content-Type: application/javascript; charset=UTF-8');
    header('Cache-Control: max-age=864000');
    header_remove('Pragma');
    header_remove('Set-Cookie');
    $str = $this->buildJsCss(); 
    header('Content-Length: ' . strlen($str));
    echo $str; <--- LINE 76 that returns error with sent header
    flush();
    return true;
}

Debug process

1) When I dump the headers_list()

array(3) {
  [0] =>
  string(51) "Content-Type: application/javascript; charset=UTF-8"
  [1] =>
  string(29) "Cache-Control: max-age=864000"
  [2] =>
  string(21) "Content-Length: 60317"
} 

Stack Trace

Nette\InvalidStateException: Cannot send header after HTTP headers have been sent (output started at /var/www/clients/client0/web7/web/vendor/tracy/tracy/src/Tracy/Debugger/DeferredContent.php:76). in /var/www/clients/client0/web7/web/vendor/nette/http/src/Http/Response.php:274
Stack trace:
#0 /var/www/clients/client0/web7/web/vendor/nette/http/src/Http/Response.php(85): Nette\Http\Response->checkHeaders()
#1 /var/www/clients/client0/web7/web/temp/cache/nette.configurator/Container_7376f82586.php(2652): Nette\Http\Response->setHeader('...', '...')
#2 /var/www/clients/client0/web7/web/temp/cache/nette.configurator/Container_7376f82586.php(2656): Container_7376f82586->{closure}()
#3 /var/www/clients/client0/web7/web/vendor/nette/bootstrap/src/Bootstrap/Configurator.php(241): Container_7376f82586->initialize()
#4 /var/www/clients/client0/web7/web/www/index.php(10): Nette\Bootstrap\Configurator->createContainer()
#5 {main}

Steps To Reproduce

URL: https://pisnickycz.pepiapp.sk/

Expected Behavior

The expecte behavior is to show the Tracy - it is enabled and should work.

Possible Solution

I do not know yet.

dg commented 4 months ago

After executing echo $str, an exit should be called in Debugger::dispatch() and the application should terminate. It's a mystery to me why it continues, why a DI container is created, and then that error occurs. Try stepping through it.

M4RtY commented 4 months ago

Yes, this is very strange, because other headers are fine, there is a problem with the Tracy only. I was trying to change PHP versions (FastCGI, PFM) and there is no difference.

I have found some configuration issue for Nginx, but there was also mentioned that Tracy works on "index", for me the Tracy does not work for whole website.

StackTrace #0 - Nette\Http\Response->checkHeaders()

$value = string(17) "Nette Framework 3" for both environments $name = string(12) "X-Powered-By" for both environments

if ($value === null) {
    header_remove($name);
} elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
    // ignore, PHP bug #44164
} else {
    header($name . ': ' . $value); <-- this line is executed (both environments - var_dump() here)
}

I dumped headers_list() here and there is one output both both environments:

array(1) {
  [0] =>
  string(31) "X-Powered-By: Nette Framework 3"
}

StackTrace #1 a #2 - Nette\Http\Response->setHeader('...', '...')

public function initialize(): void
{
// di.
(function () {
    $this->getService('tracy.bar')->addPanel(new Nette\Bridges\DITracy\ContainerPanel($this));
})();
// forms.
(function () {
    Nette\Forms\Validator::$messages[Nette\Forms\Form::EMAIL] = 'Zadejte platnou e-mailovou adresu.';
})();
// http.
(function () {
    $response = $this->getService('http.response');
    $response->setHeader('X-Powered-By', 'Nette Framework 3'); <--- LINE 2652
    $response->setHeader('Content-Type', 'text/html; charset=utf-8');
    $response->setHeader('X-Frame-Options', 'SAMEORIGIN');
    Nette\Http\Helpers::initCookie($this->getService('http.request'), $response);
})();  <--- LINE 2656

The thing is that there is nothing strange for me in this case and I am not sure how to continue with the debugging. At least, I can provide the affected environment or if you can gouide me what to do more, it would be great.

I was expecting that this "header" issue is for whole website, but only Tracy has this problem

M4RtY commented 4 months ago

Also I have noticed the output in the browser:

URL: view-source:https://pisnickycz.pepiapp.sk/?_tracy_bar=js&v=2.10.5&XDEBUG_SESSION_STOP=1

function stickyFooter() {
    let footer = document.querySelector('#tracy-bs footer');
    footer.classList.toggle('tracy-footer--sticky', false); // to measure footer.offsetTop
    footer.classList.toggle('tracy-footer--sticky', footer.offsetHeight + footer.offsetTop - window.innerHeight - document.documentElement.scrollTop < 0);
}

let Tracy = window.Tracy = window.Tracy || {};
Tracy.BlueScreen = Tracy.BlueScreen || BlueScreen;
})();Nette\InvalidStateException: Cannot send header after HTTP headers have been sent (output started at /var/www/clients/client0/web7/web/vendor/tracy/tracy/src/Tracy/Debugger/DeferredContent.php:76). in /var/www/clients/client0/web7/web/vendor/nette/http/src/Http/Response.php:273
Stack trace:
#0 /var/www/clients/client0/web7/web/vendor/nette/http/src/Http/Response.php(84): Nette\Http\Response->checkHeaders()
#1 /var/www/clients/client0/web7/web/temp/cache/nette.configurator/Container_a4279b605b.php(2652): Nette\Http\Response->setHeader('...', '...')
#2 /var/www/clients/client0/web7/web/temp/cache/nette.configurator/Container_a4279b605b.php(2656): Container_a4279b605b->{closure}()
#3 /var/www/clients/client0/web7/web/vendor/nette/bootstrap/src/Bootstrap/Configurator.php(241): Container_a4279b605b->initialize()
#4 /var/www/clients/client0/web7/web/www/index.php(10): Nette\Bootstrap\Configurator->createContainer()
#5 {main}

(stored in /var/www/clients/client0/web7/web/log/exception--2024-04-19--12-33--48522ffb8d.html)
dg commented 4 months ago

As I wrote, try to find out why the exit is not called https://github.com/nette/tracy/blob/master/src/Tracy/Debugger/Debugger.php#L259

M4RtY commented 4 months ago

Yes, thank you! As I have tried to step in to the code and dumped the exit, it is called, the output is true, so in this case I can presume the problem with headers is somewhere else.

Imho this can happen somewhere here $strategy->initialize();

If I am right - I have opened Container_{hash}.php file and there is initialize() method and there is a code:\

$response->setHeader('X-Powered-By', 'Nette Framework 3');
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
$response->setHeader('X-Frame-Options', 'SAMEORIGIN');

And here it looks like it fails.