SanderRonde / phpstan-vscode

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

[ENHANCEMENT] Custom autoload #35

Closed lflorent closed 1 year ago

lflorent commented 1 year ago

First of all, I would like to thank you for implementing PHPStan with VSCode. Even if other extensions exist, your extension seems to be the most accomplished and best maintained.

I'm trying to migrate a PHPStorm configuration to VSCode.

In some of my projects I have to autoload classes mentioned in .neon using a custom autoload. For that it seems that the only way is to use the -a option (cf. https://github.com/phpstan/phpstan/issues/7615#issuecomment-1181563615 ).

In PHPStorm we have a setting to set a custom autoloader like: <option name="autoload" value="$PROJECT_DIR$/src/phpstan/autoload.php" /> This setting is used to append the -a option to the PHPstan command used to validate files.

With your extension I tried to add something like "phpstan.options": ["-a", "${workspaceFolder}/src/phpstan/custom-autoload.php"] in settings but this fails.

But maybe a new setting like phpstan.autoload make more sense.

I hope you'll find my request relevant enough.

SanderRonde commented 1 year ago

Hey thanks for the kind words :)

I think the phpstan.options setting should indeed allow you to add the autoloader. But what part of that it fails? Does it not run at all? Does it throw an error? Does it not do the autoloading? Can you check the PHPStan output panel in VSCode? That should show you what happened. One thing that might not work is the workspaceFolder part. I'm not actually sure if VSCode automatically replaces that. Could consider removing that and using an absolute/relative path and seeing if that works.

lflorent commented 1 year ago

Hey @SanderRonde thanks for you very fast reply ;)

You're absolutely right about workspaceFolder it is not interpolated :

[check:5] Spawning PHPStan with the following configuration:  {"binCmd":"/Users/lohen/myproject/vendor/bin/phpstan","args":["analyse","-c","/Users/lohen/myproject/phpstan.neon.dist","--error-format=raw","--no-interaction","--memory-limit=2G","--no-progress","-a","${workspaceFolder}/src/PHPStan/autoload.php","/Users/lohen/myproject/functions.php","-a","/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/autoload.php","-c","/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/config.neon"]}
[check:5] PHPStan process exited with error  filteredErr=PHP Warning:  chdir(): No such file or directory (errno 2) in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/autoload.php on line 2
Warning: chdir(): No such file or directory (errno 2) in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/autoload.php on line 2
PHP Warning:  require_once(${workspaceFolder}/src/PHPStan/autoload.php): failed to open stream: No such file or directory in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/autoload.php on line 3
Warning: require_once(${workspaceFolder}/src/PHPStan/autoload.php): failed to open stream: No such file or directory in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/autoload.php on line 3
PHP Fatal error:  require_once(): Failed opening required '${workspaceFolder}/src/PHPStan/autoload.php' (include_path='.:/opt/homebrew/Cellar/php@7.4/7.4.33/share/php@7.4/pear') in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-CltBYgyruIpI/autoload.php on line 3

Same with relative to PHPStan binary path:

check:6] Spawning PHPStan with the following configuration:  {"binCmd":"/Users/lohen/myproject/vendor/bin/phpstan","args":["analyse","-c","/Users/lohen/myproject/phpstan.neon.dist","--error-format=raw","--no-interaction","--memory-limit=2G","--no-progress","-a","../../src/PHPStan/custom-autoload.php","/Users/lohen/myproject/functions.php","-a","/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/autoload.php","-c","/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/config.neon"]}
[check:6] PHPStan process exited with error  filteredErr=PHP Warning:  chdir(): No such file or directory (errno 2) in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/autoload.php on line 2
Warning: chdir(): No such file or directory (errno 2) in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/autoload.php on line 2
PHP Warning:  require_once(../../src/PHPStan/custom-autoload.php): failed to open stream: No such file or directory in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/autoload.php on line 3
Warning: require_once(../../src/PHPStan/custom-autoload.php): failed to open stream: No such file or directory in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/autoload.php on line 3
PHP Fatal error:  require_once(): Failed opening required '../../src/PHPStan/custom-autoload.php' (include_path='.:/opt/homebrew/Cellar/php@7.4/7.4.33/share/php@7.4/pear') in /private/var/folders/zt/lp8xwm517blc9jsvwbqvpbyxz_fs3q/T/tmp-78822-jHcVBb3jQXHa/autoload.php on line 3

I'm not familiar with the details of how the extension works but the temporary autoload.php seems to required the path I add in phpstan.options.

About using an absolute path, this would be impractical in my case because I want to share its workspace settings with other developers.

SanderRonde commented 1 year ago

Ah yes that makes sense. I'll make it so the workspaceFolder variable is replaced. Can you try this .vsix file for me? I've renamed it to a .zip file because github doesn't allow .vsix files. Simply rename it to that and install it in VSCode. phpstan-vscode-2.2.23.zip

lflorent commented 1 year ago

You did it !

The settings "phpstan.options": ["-a", "${workspaceFolder}/src/PHPStan/autoload.php"] works like a charm :).

Thank you so much.

SanderRonde commented 1 year ago

Awesome, I'll publish the new version then :)