SanderRonde / phpstan-vscode

PHPStan plugin for VSCode
https://marketplace.visualstudio.com/items?itemName=SanderRonde.phpstan-vscode
MIT License
37 stars 7 forks source link

Exit gracefully when no files found to analyse in single file mode #75

Closed Grldk closed 2 weeks ago

Grldk commented 2 months ago

We have a couple of folders that are ignored by phpstan (through the excludePaths config in phpstan.neon.dist). If I run this extension in single file mode and scan a file in one of these folders phpstan exits with an error (copy/pasted from the output log):

[WARNING] This will cause a non-zero exit code in PHPStan 2.0.  rawErr=
 ! [NOTE] No files found to analyse.                                                                                    

 [WARNING] This will cause a non-zero exit code in PHPStan 2.0.                                                         

  data=

This causes the plugin to error out as well.

Previously I had a regex in phpstan.ignoreErrors to ignore this (only way I found to ignore this error...), but this doesn't work anymore in v3. (see https://github.com/SanderRonde/phpstan-vscode/issues/70).

So what I'd like is either to use a regex in ignoreErrors again, or for the plugin to exit without an error in this scenario...

SanderRonde commented 2 months ago

Did my changes in that issue not fix the problem? I thought I had fixed it. If not I'll look into it again.

Grldk commented 2 months ago

Ah sorry, maybe should have reacted in that issue. Looks like I'm still getting the same error:

TypeError: e.replace is not a function
    at Dt (/var/www/.vscode-server/extensions/sanderronde.phpstan-vscode-3.1.4/out/server.js:74:2725)
    at /var/www/.vscode-server/extensions/sanderronde.phpstan-vscode-3.1.4/out/server.js:74:2689
    at Array.map (<anonymous>)
    at le (/var/www/.vscode-server/extensions/sanderronde.phpstan-vscode-3.1.4/out/server.js:74:2635)
    at async gP (/var/www/.vscode-server/extensions/sanderronde.phpstan-vscode-3.1.4/out/server.js:79:11839)
SanderRonde commented 2 months ago

Ah I've figured it out. The issue is that the setting you posted is not valid JSON. Regex in JSON isn't supported (you'll probably notice how github and VSCode mark it as red). What VSCode does is that it tries to parse it to the best of its ability, and when it sees the [ it turns it into an empty array. So effectively your setting is phpstan.ignoreErrors: [[], []], which then leads to an issue when my extension expects a string or RegExp.

It should work again if you just replace it with "phpstan.ignoreErrors": [ "! [NOTE] No files found to analyse. [WARNING] This will cause a non-zero exit code in PHPStan 2.0. " ],

My extension still does parse the string as regex, so things like .* will still work. Also since it's treated as a regex you can just do "phpstan.ignoreErrors": ["No files found to analyse"] and it should also work since the error contains the to-ignore value you specified (it doesn't need to be an exact match).

Grldk commented 2 months ago

I can't seem to be able to ignore that error in any way. I tried your suggestions but they still lead to the extension erroring out.

SanderRonde commented 2 months ago

My bad, since it's actually two separate errors (one NOTE and one WARNING), both need to be ignored. You'll notice that err= will no longer contain the NOTE but will still contain the WARNING. To fix this, ignore both, so: "phpstan.ignoreErrors": ["No files found to analyse", "This will cause a non-zero exit code in"],

Grldk commented 2 months ago

Hmm. I should have noticed that there were two errors... Trying that suggestion returns another error though:

[check:1] PHPStan process exited succesfully
undefined:1

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at ChildProcess.<anonymous> (/var/www/.vscode-server/extensions/sanderronde.phpstan-vscode-3.1.4/out/server.js:76:1595)

Node.js v18.18.2
[Info  - 2:45:21 PM] Connection to server got closed. Server will restart.
true
SanderRonde commented 2 months ago

Ahh what's happening is that I'm giving the output-mode=json to PHPStan and because of the error it's not outputting anything, which makes the JSON parse fail. I'll make it so that, if any errors were ignored, this error does not cause a check to fail. But it does seem iffy to just ignore PHPStan exiting with an actual error (not just a warning).

Can you check whether the below version fixes the issue?

phpstan-vscode-3.1.4.zip

Grldk commented 2 months ago

if any errors were ignored, this error does not cause a check to fail. But it does seem iffy to just ignore PHPStan exiting with an actual error (not just a warning).

Yeah that's part of why I actually made a separate issue. I was wondering if it's possible to handle this error separately so you don't ignore unexpected errors..

Can you check whether the below version fixes the issue?

Will try later today or tomorrow.

Thanks!

SanderRonde commented 2 months ago

Yeah good point. I actually had hoped that the move to full-project scans would fix this issue since that way PHPStan only includes the relevant files. But I think you're actually right that this should be handled separately, especially since the extension is (unless manually triggered) the one triggering the checking. I'll do that instead. (you can skip checking the package I posted before, I'll throw away that fix and go for an automatic ignore).

SanderRonde commented 2 months ago

I've fixed the issue in https://github.com/SanderRonde/phpstan-vscode/commit/65064610b99af5fa45f1428f071c3a19f886cc05. That version is live now, can you let me know if it works?

github-actions[bot] commented 2 weeks ago

Issues go stale after too much time without activity. If inactive for another 7 days this issue will be closed.

SanderRonde commented 2 weeks ago

Hey I'll just mark this as fixed since I did ship the fix. Feel free to re-open if it wasn't fixed.