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

Incorrect issues classification when using baseline #9180

Closed
mindriven opened this issue Jan 25, 2023 · 27 comments · Fixed by #8960
Closed

Incorrect issues classification when using baseline #9180

mindriven opened this issue Jan 25, 2023 · 27 comments · Fixed by #8960

Comments

@mindriven
Copy link

Hi!
It seems that this switch statement

switch ($severity) {
case Config::REPORT_INFO:
$diagnostic_severity = DiagnosticSeverity::WARNING;
break;
case Config::REPORT_ERROR:
default:
$diagnostic_severity = DiagnosticSeverity::ERROR;
break;
}

is causing a confusion when using baseline. IDE will show "infos" as errors.
Here the originally created issue in repo of vscode plugin psalm/psalm-vscode-plugin#221.
In comments of this issue there are screenshots and link to minimal sample repo that I used to visualize the problem.

FYI @tm1000, @weirdan

@psalm-github-bot
Copy link

Hey @mindriven, can you reproduce the issue on https://psalm.dev ?

@mindriven
Copy link
Author

mindriven commented Jan 25, 2023

not possible due to necessity of baseline

@weirdan
Copy link
Collaborator

weirdan commented Jan 25, 2023

So it appears baseline is not used by language server at all. Baseline processing is done in IssueBuffer::finish(), but language server is not calling that, it uses IssueBuffer::clear() instead.

@tm1000
Copy link
Contributor

tm1000 commented Jan 25, 2023

Ok so looking this over I can't use finish() in the LSP because it echos out to stdout. $issue_baseline is actually passed from somewhere else.

It looks like I can parse the baseline from https://github.com/tm1000/psalm/blob/b14ed73c0b3717513ac5ec421672a5c03699b028/src/Psalm/Internal/Cli/Psalm.php#L650-653 and perhaps ignore what I need from there.

@tm1000
Copy link
Contributor

tm1000 commented Jan 25, 2023

@weirdan @orklah assign to me

tm1000 added a commit to tm1000/psalm that referenced this issue Jan 25, 2023
@tm1000
Copy link
Contributor

tm1000 commented Jan 25, 2023

The addition for this is in my WIP PR () but you can see the changes here: tm1000@27c0caf

This adds one new cli arg of use-baseline. Used in conjunction with show-diagnostic-warnings=false makes it so the errors are ignored.

    "--use-baseline=your-baseline.xml",
    "--show-diagnostic-warnings=false"

I had to do a lot of funky psalm suppressions because it didn't like when I had to pass-by reference but I have to do that to keep the tracking the same but other than that it's fine

@mindriven
Copy link
Author

thanks for taking care of it so fast @tm1000.
Is the release still pending, or already done? Would love to give it a test-drive.

@tm1000
Copy link
Contributor

tm1000 commented Jan 25, 2023

@mindriven the release is pending because I want to write tests and writing tests for amp (which is what the language server used for non-blocking php) is not for the faint of heart.

That said you can use/test the wip branch. It's up to date with 5.6.0. The power of GIT allows me to track what the community does while I slowly work on this and because @weirdan and @orklah are kind enough to tag me in most LSP changes it makes it easy to follow

composer.json:

    "require-dev": {
        "vimeo/psalm": "dev-feature/upgrade-lsp-v5 as 5.6.0"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/tm1000/psalm"
        }
    ]

vscode settings:

    "psalm.psalmVersion": "5.6.0",
    "psalm.psalmScriptArgs": [
        "--clear-cache-on-boot",
        "--use-baseline=your-baseline.xml",
        "--on-change-debounce-ms=500",
        "--show-diagnostic-warnings=false"
    ],
    "psalm.psalmScriptPath": "app/vendor/bin/psalm-language-server",

Note that psalmScriptPath is specific to my setup. The point is you want to use psalm-language-server not psalm (even though psalm does a pass-thru to psalm-language-server)

@mindriven
Copy link
Author

mindriven commented Jan 26, 2023

Hey, I gave it a spin. I do not see the "Problems" in vscode - that's the good news. Bad news is that I have this stack-trace in my output window. Not sure what to make of that.

[Error - 7:20:55 AM] RuntimeException: PHP Error: Undefined array key 1 in /home/kamil/vscode-psalm-testing/vendor/felixfbecker/advanced-json-rpc/lib/Dispatcher.php:140 for command with CLI args "/home/kamil/vscode-psalm-testing/vendor/bin/psalm-language-server --use-extended-diagnostic-codes -c /home/kamil/vscode-psalm-testing/psalm.xml -r /home/kamil/vscode-psalm-testing --clear-cache-on-boot --use-baseline=baseline.xml --on-change-debounce-ms=500 --show-diagnostic-warnings=false" in /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/ErrorHandler.php:75
Stack trace:
#0 /home/kamil/vscode-psalm-testing/vendor/felixfbecker/advanced-json-rpc/lib/Dispatcher.php(140): Psalm\Internal\ErrorHandler::Psalm\Internal\{closure}()
#1 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/LanguageServer/LanguageServer.php(191): AdvancedJsonRpc\Dispatcher->dispatch()
#2 [internal function]: Psalm\Internal\LanguageServer\LanguageServer->Psalm\Internal\LanguageServer\{closure}()
#3 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Coroutine.php(67): Generator->current()
#4 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/functions.php(96): Amp\Coroutine->__construct()
#5 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/functions.php(61): Amp\call()
#6 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/LanguageServer/EmitterTrait.php(85): Amp\{closure}()
#7 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/LanguageServer/ProtocolStreamReader.php(116): Psalm\Internal\LanguageServer\ProtocolStreamReader->emit()
#8 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/LanguageServer/ProtocolStreamReader.php(63): Psalm\Internal\LanguageServer\ProtocolStreamReader->readMessages()
#9 [internal function]: Psalm\Internal\LanguageServer\ProtocolStreamReader->Psalm\Internal\LanguageServer\{closure}()
#10 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Coroutine.php(118): Generator->send()
#11 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Internal/Placeholder.php(149): Amp\Coroutine->Amp\{closure}()
#12 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Deferred.php(53): Amp\Promise@anonymous->resolve()
#13 /home/kamil/vscode-psalm-testing/vendor/amphp/byte-stream/lib/ResourceInputStream.php(101): Amp\Deferred->resolve()
#14 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Loop/NativeDriver.php(327): Amp\ByteStream\ResourceInputStream::Amp\ByteStream\{closure}()
#15 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Loop/NativeDriver.php(127): Amp\Loop\NativeDriver->selectStreams()
#16 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Loop/Driver.php(138): Amp\Loop\NativeDriver->dispatch()
#17 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Loop/Driver.php(72): Amp\Loop\Driver->tick()
#18 /home/kamil/vscode-psalm-testing/vendor/amphp/amp/lib/Loop.php(95): Amp\Loop\Driver->run()
#19 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/LanguageServer/LanguageServer.php(393): Amp\Loop::run()
#20 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/src/Psalm/Internal/Cli/LanguageServer.php(384): Psalm\Internal\LanguageServer\LanguageServer::run()
#21 /home/kamil/vscode-psalm-testing/vendor/vimeo/psalm/psalm-language-server(9): Psalm\Internal\Cli\LanguageServer::run()
#22 /home/kamil/vscode-psalm-testing/vendor/bin/psalm-language-server(120): include('...')
#23 {main}

@tm1000
Copy link
Contributor

tm1000 commented Jan 26, 2023

Is the server crashing or is that just a message in the output window. A lot of those messages are caught and the server still functions as intended

@mindriven
Copy link
Author

Seems like it's still working despite the error message in logs (so far tested only in sample repository here https://github.com/mindriven/vscode-psalm-testing\).

Can I please ask what's your best estimation, when will it be officially released?

BTW, while testing it I have found another bug (#9187)

@tm1000
Copy link
Contributor

tm1000 commented Jan 26, 2023

I have resolved the bug mentioned above, "Undefined array key 1". composer update will pull it in.

Can I please ask what's your best estimation, when will it be officially released?

I just have to get time to work on tests.

@mindriven
Copy link
Author

Ok, found some more. Now it works with baseline and I can confirm stacktrace is gone, but for this behavior to be consistent inline suppression should have the same effect as using a baseline.
Here I see that psalm emits Infos when issues are suppressed
https://psalm.dev/r/4973cb067b
and in VSCode it looks like this
image

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/4973cb067b
<?php

/**
 * @psalm-suppress MixedAssignment
 */
$a = $something_that_does_not_exist;

/**
 * @psalm-suppress all
 */
$b = "asas";
Psalm output (using commit aec0edc):

ERROR: UndefinedGlobalVariable - 6:6 - Cannot find referenced variable $something_that_does_not_exist in global scope

INFO: UnusedVariable - 6:1 - $a is never referenced or the value is not used

INFO: UnusedVariable - 11:1 - $b is never referenced or the value is not used

@tm1000
Copy link
Contributor

tm1000 commented Jan 26, 2023

You need to separate the suppression with a comma. It works fine here

Also level 1 will always emit even if suppressed. If your intention is to use suppression don't use level 1

I think we are getting into the nitty gritty of how psalm works and we've deviated from this post. I merely work on the LSP. The LSP shares all code bases and paths with the standard client.

I use suppression daily (a lot in fact) and I know it works. It is ignored if you set Psalms error level to 1

@mindriven
Copy link
Author

mindriven commented Jan 27, 2023

I wanted to test it in a bigger repository, but I must still be doing something wrong... just there might be another issue at play.

I generated a baseline with

psalm --set-baseline=psalm-baseline.xml

file was created, config updated. But not all classes that are in the folders configured for analysis were included in the baseline file. For example, I have 2 classes in same folder Customer.php and Order.php.
There is an entry for Customer.php in the baseline file, but no entry for Order.php.

Good news is that VSCode behaves as I would expect in the Customer.php, that is violations that have entries in baseline file are not reported, and when I add new code, they get reported.

But when I open Order.php I'm flooded with psalm errors (and rightfully so, as they are not saved in the baseline file).

I have to get this working for all the repository. Any ideas how can I debug this?

@mindriven
Copy link
Author

I managed to nail it down. It possibly is another issue at play here. #9197

@mindriven
Copy link
Author

After working around the issue mentioned in my previous comment, I feel I am significantly closer to getting the user experience I'm looking for.
However I am still getting some errors in vscode, informing me about dead code. I have checked in the baseline and those errors are NOT included there (hence vscode behavior makes sense to me).
image
Is there any reason why they are not included in the baseline, when it is being generated? Or a way to include them? Found nothing in the documentation...

@weirdan
Copy link
Collaborator

weirdan commented Feb 12, 2023

Dead code detection is an optional feature. If it's not enabled when you generate the baseline, the errors it produces won't get included in the baseline. Make sure you generate your baseline with --find-dead-code switch.

@mindriven
Copy link
Author

yep, that works, thanks @weirdan.
@tm1000 how is it going with the tests/inclusion of your changes in the official release?

@Itatsi
Copy link

Itatsi commented Feb 20, 2023

I would also like to see this functionality released officially

@achanizbekov
Copy link

Same Problem. Waiting for official release.

@weirdan
Copy link
Collaborator

weirdan commented Feb 20, 2023

@Itatsi @achanizbekov there were several related but distinct things discussed here. Please clarify what you're referring to.

@achanizbekov
Copy link

@weirdan I'm referring to the initially reported issue: Violations recorded in the baseline should not be reported by language server, to enable smooth IDE integration(reported by @mindriven).

@mindriven
Copy link
Author

Hey @weirdan I see you've been releasing new versions is a lightning speed, that's really impressive!
What can I do to help this one get into 5.8?

@weirdan
Copy link
Collaborator

weirdan commented Mar 1, 2023

What can I do to help this one get into 5.8?

Propose a pull request fixing this issue 😃

I see you've been releasing new versions is a lightning speed

That many patch releases in this small amount of time are mostly an unfortunate consequence of us taking a risky decision to enable JIT by default. It's not something we usually do. Most of them are fixing crashes that affected many people, thus the urgency.

@mindriven
Copy link
Author

... or just wait 2 weeks :-) Cool, looking forward to giving next release a spin!

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

Successfully merging a pull request may close this issue.

5 participants