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

Internal error for which the description of the problem refers to a file that does not do what the error says #6428

Closed
benja-M-1 opened this issue Jan 21, 2022 · 9 comments

Comments

@benja-M-1
Copy link

benja-M-1 commented Jan 21, 2022

Bug report

Hi 👋🏻,

tl;dr: problem solved bug it might be some issue underlying, see my last comment

I am sorry for the long and unprecise title, I don't really know how to describe the issue in one sentence...

PHPStan fails in our CI with the following error:
Screenshot 2022-01-21 at 09 57 10

I tried to reproduce it locally but I can't, PHPStan succeeds.
edit: I cleared PHPStan's cache and now it fails but does not with the --debug flag.

I tried to run the command on the container in the CI via SSH, it fails with the sameerror as expected. Then I tried to add the --debug flag and this time it succeeds. I tried again without the flag, it still fails. I removed the cache and tried again, it still fails.

What I don't understand is that there is no call to a "name" property in this file. So maybe it is rather PHPStan that raises the error internally when analyzing this file.

Code snippet that reproduces the problem

I don't have any way to reproduce it locally, I don't understand the issue enough, I am sorry.

Expected output

I expected to not have error on every environment.

Did PHPStan help you today? Did it make you happy in any way?

Ooooh yes! It does a lot. Thank you so much for building it 🙏🏻

@mergeable
Copy link

mergeable bot commented Jan 21, 2022

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@benja-M-1
Copy link
Author

benja-M-1 commented Jan 21, 2022

I managed to find what causing the issue.
In a Symfony service definition I use the string env var processor :

    App\Security\Authenticator\Handler\ZoomOAuthAuthenticationFailureHandler:
        arguments:
            $webAppUrl: "%env(string:WEB_APP_FRONTEND_URL)%"

When I remove the string processor, PHPStan does not fail anymore:

    App\Security\Authenticator\Handler\ZoomOAuthAuthenticationFailureHandler:
        arguments:
            $webAppUrl: "%env(WEB_APP_FRONTEND_URL)%"

I don't really understand why it fails because I use many env var processors in my app but this is one is the first one to fail.

@benja-M-1
Copy link
Author

benja-M-1 commented Jan 21, 2022

It is getting weirder and weirder...

Here is the service on which PHPStan says it fails:

use App\OAuth\OAuthStateDecoderInterface;
use Symfony\Component\Routing\RouterInterface;

class ZoomOAuthAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
{
    public function __construct(
        private string $webAppUrl,
        private OAuthStateDecoderInterface $stateDecoder,
        private RouterInterface $router,
    ) {
    }

    // ... rest of the code is omitted
}

The definitions:

    App\OAuth\OAuthStateDecoder:
        arguments:
            $secret: "%env(string:APP_SECRET)%"
            $ttl: "%env(int:OAUTH_STATE_TTL)%"

    App\OAuth\OAuthStateDecoderInterface: '@App\OAuth\OAuthStateDecoder'

    App\Security\Authenticator\Handler\ZoomOAuthAuthenticationFailureHandler:
        arguments:
            $webAppUrl: "%env(string:WEB_APP_FRONTEND_URL)%"

Here is what I tried:

  • ✅ Remove the string env var processor
  • ❌ Remove only the $webAppUrl arguments from the constructor
  • ✅ Remove the $stateDecoder arguments from the constructor
  • ✅ Disable parallel processing

So in the end I am not sure anymore it is linked to the env var processors.

I don't find a simple way to debug that, how could I add breakpoints and use Xdebug in sub processes?

@benja-M-1
Copy link
Author

benja-M-1 commented Jan 21, 2022

And problem found!

It was due to the fact that my code was try/catching an exception without assigning the exception to a value.

    /**
     * @return array<string, mixed>
     */
    private function decodeState(?string $state): array
    {
        if (null === $state) {
            return [];
        }

        try {
            return $this->stateDecoder->decode($state, false);
        } catch (InvalidOAuthStateException) { // <- $e is missing, on purpose as it is not used afterward.
            return [];
        }
    }

I finally managed to have the stack trace of the error, but it only led me on the way and it is linked to thecodingmachine/phpstan-strict-rules#57

#0 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/FileAnalyser.php(94):
TheCodingMachine\PHPStan\Rules\Exceptions\ThrowMustBundlePreviousExceptionRule->processNode(Object(PhpParser\Node\Stmt\Catch_), Object(PHPStan\Analyser\MutatingScope))
#1 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Node/ClassStatementsGatherer.php(95):
PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Catch_), Object(PHPStan\Analyser\MutatingScope))
#2 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(445): PHPStan\Node\ClassStatementsGatherer->__invoke(Object(PhpParser\Node\Stmt\Catch_),
Object(PHPStan\Analyser\MutatingScope))
#3 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(1001):
PHPStan\Analyser\NodeScopeResolver::PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Catch_), Object(PHPStan\Analyser\MutatingScope))
#4 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\TryCatch),
Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#5 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(460):
PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\ClassMethod), Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#6 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\ClassMethod),
Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer))
#7 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(544): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Class_),
Array, Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer))
#8 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Class_),
Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#9 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(510): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Namespace_),
Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#10 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/NodeScopeResolver.php(269): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Namespace_),
Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#11 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Analyser/FileAnalyser.php(181): PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope),
Object(Closure))
#12 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Command/WorkerCommand.php(143): PHPStan\Analyser\FileAnalyser->analyseFile('/Users/benjamin...', Array,
Object(PHPStan\Rules\Registry), NULL)
#13 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}(Array)
#14 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(110): _PHPStan_1aef99d3c\Evenement\EventEmitter->emit('data', Array)
#15 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): _PHPStan_1aef99d3c\Clue\React\NDJson\Decoder->handleData(Array)
#16 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/react/stream/src/Util.php(62): _PHPStan_1aef99d3c\Evenement\EventEmitter->emit('data', Array)
#17 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):
_PHPStan_1aef99d3c\React\Stream\Util::_PHPStan_1aef99d3c\React\Stream\{closure}('{"action":"anal...')
#18 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(154): _PHPStan_1aef99d3c\Evenement\EventEmitter->emit('data', Array)
#19 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(200): _PHPStan_1aef99d3c\React\Stream\DuplexResourceStream->handleData(Resource id
#3625)
#20 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(172):
_PHPStan_1aef99d3c\React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)
#21 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/src/Command/WorkerCommand.php(106): _PHPStan_1aef99d3c\React\EventLoop\StreamSelectLoop->run()
#22 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/symfony/console/Command/Command.php(259):
PHPStan\Command\WorkerCommand->execute(Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Output\ConsoleOutput))
#23 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/symfony/console/Application.php(848):
_PHPStan_1aef99d3c\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Input\ArgvInput),
Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Output\ConsoleOutput))
#24 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/symfony/console/Application.php(259):
_PHPStan_1aef99d3c\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\WorkerCommand), Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Input\ArgvInput),
Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Output\ConsoleOutput))
#25 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/vendor/symfony/console/Application.php(157):
_PHPStan_1aef99d3c\Symfony\Component\Console\Application->doRun(Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Input\ArgvInput),
Object(_PHPStan_1aef99d3c\Symfony\Component\Console\Output\ConsoleOutput))
#26 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/bin/phpstan(94): _PHPStan_1aef99d3c\Symfony\Component\Console\Application->run()
#27 phar:///Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar/bin/phpstan(95): _PHPStan_1aef99d3c\{closure}()
#28 /Users/benjamingrandfond/Dev/phpstan-src/tmp/phpstan.phar(6): require('phar:///Users/b...')
#29 {main}

@benja-M-1
Copy link
Author

benja-M-1 commented Jan 21, 2022

Sorry for the noise here! Even if I found my problem, I think there might still be some issues here:

The output does not help to fully understand the internal error
Here the message tells that an error occurred in a file from the analyzed project. This is untrue and misleading. The error was actually thrown by an extension (or even PHPStan) when analyzing the project file.
As a first improvement, I worked on a PR to add the stack trace when -v (or above) is passed to the command line: phpstan/phpstan-src#935

It is quite cumbersome to debug what is happening in a worker
Maybe it could be documented, WDYT?
I don't know if it is the right way to do it but, in the end:

  • I forked phpstan/phpstan-src,
  • I made a change to display the stack trace of the exception thrown by the analysis,
  • I recompiled the phar

And there still some open questions

  • why does it say that there are 2 errors and displays only one?
  • why when the parallelization was disabled the error disappeared?

I'd be glad to help but I have no idea from where to look at, yet.

@ondrejmirtes
Copy link
Member

why does it say that there are 2 errors and displays only one?

No idea, probably some confusion about the internal error.

why when the parallelization was disabled the error disappeared?

Because the problem is probably related to some non-deterministic and side-effect nature of why the error sometimes appears and sometimes not. This is usually because the error doesn't happen when something is executed before the important part of the code that causes it.

With a single thread all the things always happen in the same order, but with parallelization, there are differences and the order of executed lines of code can change and that's why your error sometimes appears and sometimes not.

One of the notable examples of this is twigphp/Twig#3562. The function twig_date_format_filter exists only after Twig\Extension\CoreExtension is loaded: #5640 (comment)

So if you're lucky and PHPStan analyses code that uses twig_date_format_filter after Twig\Extension\CoreExtension has been loaded in the same thread, there's no error. But if PHPStan analyses code that calls twig_date_format_filter without loading Twig\Extension\CoreExtension prior to that, you get an error.

Your problem will probably be similar, something about lazy initialization etc...

@ondrejmirtes
Copy link
Member

Latest PHPStan version should show you the error with stack trace in CI if you run with -v. It'd tell us where the exception is coming from. Post the stack trace here and I'll reopen :)

@benja-M-1
Copy link
Author

@ondrejmirtes the stack trace is the one I here in this issue.

It helped me to fix the error I had when reporting the issue. However, there are still some issues for which I wasn't able to debug so far. Anyway, if they are not reported again then maybe it is not worth keeping the issue open. :)

@github-actions
Copy link

github-actions bot commented Mar 5, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants