vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.51k stars 656 forks source link

psalm-language-server fails to find classes from extra files #9885

Open come-nc opened 1 year ago

come-nc commented 1 year ago

Using the psalm-language-server with Kate, I always see this kind of errors:

[Psalm] (https://psalm.dev/157) OCP\User\GetQuotaEvent depends on class or interface psr\eventdispatcher\stoppableeventinterface that does not exist

But this interface is in 3rdparty/psr/event-dispatcher/src/StoppableEventInterface.php, and psalm.xml contains:

    <extraFiles>
        <directory name="3rdparty"/>
        <directory name="apps/files_external/3rdparty"/>
    </extraFiles>

Psalm does find the class, why doesn’t the LSP server?

orklah commented 1 year ago

@tm1000 is this something you observed before?

tm1000 commented 1 year ago

I'd have to see an example repo. The language server is the same code base as psalm itself

come-nc commented 1 year ago

This is happening to me on https://github.com/nextcloud/server/ repository The kate configuration is:

        "php": {
            "command": ["vendor-bin/psalm/vendor/vimeo/psalm/psalm-language-server"],
            "rootIndicationFileNames": ["psalm.xml","composer.json"],
            "path": ["%{Project:Path}"],
            "url": "https://psalm.dev",
            "highlightingModeRegex": "^PHP.*$"
        },
tm1000 commented 1 year ago

@come-nc can you please clarify your Psalm/PHP version

come-nc commented 1 year ago
> vendor-bin/psalm/vendor/vimeo/psalm/psalm-language-server --version
Psalm 4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69
> php --version
PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies
tm1000 commented 1 year ago

Please use Psalm 5. Psalm 4 is not supported.

come-nc commented 1 year ago

Please use Psalm 5. Psalm 4 is not supported.

I updated:

vendor-bin/psalm/vendor/vimeo/psalm/psalm-language-server --version
Psalm 5.9.0@8b9ad1eb9e8b7d3101f949291da2b9f7767cd163

Still have the same problem. I had it once with psalm, and had to run psalm --clear-cache to get rid of it. But for language-server I do not know how to clear cache.

I also have the problem that it sometimes leave Kate hanging for a long time searching for autocompletion (when I start typing a method name, more than 10 seconds I think), but that is most likely unrelated to current ticket.

tm1000 commented 1 year ago

The cache is cleared on every boot of language server. I still don't have this issue so nothing much I can do at the moment sorry :(

jakobmerrild commented 3 months ago

I'm also seeing this issue. I'm using the VSCode plugin and having enabled the --verbose flag and set log level to DEBUG I notice a pattern that it seems that Psalm is unable to populate classes that it tries to locate via reflection. E.g. In the log when it logs

Using composer to locate file for GuzzleHttp\Promise\PromiseInterface

Then it will later have an entry

Have populated GuzzleHttp\Promise\PromiseInterface

However, when it logs

Using reflection to locate file for Neos\Flow\Tests\UnitTestCase

then there's no corresponding

Have populated Neos\Flow\Tests\UnitTestCase

I'm unsure how and why it tries to locate files using reflection, but the folder structure of our workspace is likely such that it is unable to do so.

jakobmerrild commented 3 months ago

Our psalm.xml configuration has the following extraFiles element

    <extraFiles>
        <directory name="Packages/Framework/Neos.Flow/Tests" />
    </extraFiles>

which is the path where the Neos\Flow\Tests\UnitTestCase class is located.

bdovaz commented 1 month ago

Same problem here...

<extraFiles>
    <directory name="vendor"/>
</extraFiles>
tm1000 commented 1 month ago

@bdovaz putting vendor in extraFiles is going to cause Psalm to quickly run out of memory as it lightly analyzes Composer vendor repos but you are telling it to fully analyze them.

Is there a specific use case we are missing where you feel like you need to add "vendor"

bdovaz commented 1 month ago

@bdovaz putting vendor in extraFiles is going to cause Psalm to quickly run out of memory as it lightly analyzes Composer vendor repos but you are telling it to fully analyze them.

Is there a specific use case we are missing where you feel like you need to add "vendor"

But isn't that the way it doesn't analyze your code as yours but makes it possible that when analyzing your code it doesn't tell you that it is not able to resolve certain classes?

Maybe I am wrong.

tm1000 commented 1 month ago

@bdovaz thats not the way it works. It will always traverse the vendor folder

bdovaz commented 1 month ago

@bdovaz thats not the way it works. It will always traverse the vendor folder

Nextcloud recommends this:

https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/continuous_integration.html#static-analysis

What should I do instead?

come-nc commented 1 month ago

That seems to be the source of the problem then, what is the correct way to have psalm load classes from a folder without analysing it if it’s not extraFiles ?