SanderRonde / phpstan-vscode

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

Can't find config file #8

Closed shaneparsons closed 2 years ago

shaneparsons commented 2 years ago

I have a similar-ish set up to https://github.com/SanderRonde/phpstan-vscode/issues/1 in that I'm using phpstan through docker:

Setup details ```json "phpstan.binCommand": ["php-dme", "vendor/bin/phpstan"], ``` `php-dme` (i.e. `/usr/local/bin/php-dme`) is a command for: ``` path=$(printf '%s\n' "${PWD##*/}") command="docker-compose run --rm app "$@"" $command ``` And `dme-app` is my docker container running php.

The problem I'm having is w/ the phpstan.configFile setting... My config file in this case is in /vendor/phpstan/phpstan-strict-rules/rules.neon. I've tried different variations of that path (e.g. vendor, /vendor), but it always ends with the following error:

Project config file at path *vendor/phpstan/phpstan-strict-rules/rules.neon does not exist.

I think the problem is my phpstan.rootDir, but I'm not sure what it should be in this case? This config file is accessible both locally and through the php container, so I'm not sure which path I should be using? My docker container is set up like so:

More setup details ```yaml dme-app: build: context: ./ dockerfile: ./Dockerfile volumes: - ./:/var/www/html/ ``` So what's `/vendor/phpstan/phpstan-strict-rules/rules.neon` locally, is technically `/var/www/html/vendor/phpstan/phpstan-strict-rules/rules.neon` in the container. Running `php-dme ./vendor/bin/phpstan analyse src/` manually works as intended, and finds my config automatically because of `phpstan/extension-installer`.

I guess the ultimate question is how can I get this to work? If the problem is because my setup is a unique one that hasn't been accounted for, is there any chance that maybe the phpstan.configFile can be ignored / fall back to grabbing it through the composer.json? Maybe I can use the phpstan.paths option somehow? Maybe I'm just overlooking something else?

Any help would be greatly appreciated.

SanderRonde commented 2 years ago

You should be able to get this working using the phpstan.paths option indeed. So if your local path is ./vendor/phpstan/phpstan-strict-rules/rules.neon and the remote path is /var/www/html/vendor/phpstan/phpstan-strict-rules/rules.neon, you should be able to get it working by setting the settings to:

{
    "phpstan.configFile" :"./vendor/phpstan/phpstan-strict-rules/rules.neon",
    "phpstan.paths": { "./": "/var/www/html/"
}

Not sure if that's the exact config, the paths might need to be absolute but I'm not sure about that. With some fumbling around you should be able to get it to work with the paths option.

shaneparsons commented 2 years ago

I actually tried a few different variations of that in between opening the ticket and your response (and again now)

"phpstan.configFile": "./vendor/phpstan/phpstan-strict-rules/rules.neon",
"phpstan.binCommand": ["php-dme", "vendor/bin/phpstan"],
"phpstan.paths": { "./": "/var/www/html/" },

It seems like starting phpstan.configFile with anything other than / always translates to the default ${workspaceFolder} (i.e. /home/sparsons/repo/dme)... Is there anything else I can try here?

SanderRonde commented 2 years ago

Is that not correct then? Anything path not starting with a / indicates a relative path (relative to the workspace folder). Where is the neon file supposed to be? Is it at /vendor/phpstan/phpstan-strict-rules/rules.neon (so at this abolute path) or is it at ./vendor/phpstan/phpstan-strict-rules/rules.neon (relative to the project folder)

shaneparsons commented 2 years ago

Looks like I was able to get past that part by changing it to "phpstan.paths": { "/home/sparsons/repo/dme": "/var/www/html/" },

But now I'm getting the following:

PHPStan process exited successfully but with error message, error= This file is included multiple times:
- /var/www/html/vendor/phpstan/phpstan-strict-rules/rules.neon

It can lead to unexpected results. If you're using phpstan/extension-installer, make sure you have removed corresponding neon files from your project config file.

In my case I am using phpstan/extension-installer, which is what's attaching the config to the phpstan call automatically. I'm thinking the only way around this would be for this plugin to accept a null value for phpstan.configFile thereby skipping the adding of that to the command.

Technically I guess this issue can be renamed "Add support for phpstan/extension-installer configs"...

Does this make sense / is this something doable? I'm not sure how the plugin logic has been constructed.

shaneparsons commented 2 years ago

To clarify my config setup, I'm using phpstan/phpstan-strict-rules for the config, which is using phpstan/extension-installer to automatically apply that config. You can read more about it here.

EDIT: I also just tried using phpstan/phpstan-strict-rules manually, without phpstan/extension-installer, which leads to the following:

PHPStan process exited successfully but with error message, error= 
No rules detected

You have the following choices:

* while running the analyse option, use the --level option to adjust your rule level - the higher the stricter

* create your own custom ruleset by selecting which rules you want to check by copying the service definitions from the built-in config level files in phar:///var/www/html/vendor/phpstan/phpstan/phpstan.phar/conf.
  * in this case, don't forget to define parameter customRulesetUsed in your config file.

Still debugging this part.

SanderRonde commented 2 years ago

Hmm that wouldn't be too hard to do. Can you try out this demo build? I've removed the -c arg completely here. Let me know if this works. If it does, it's a pretty easy step to just have the neon file be nullable.

I can't upload .vsix files because github doesn't allow them. Instead I uploaded a zip file. Just rename it to .vsix and install it like normal. phpstan-vscode-1.3.2.zip

shaneparsons commented 2 years ago

Alright, I got the "manual" / phpstan/phpstan-strict-rules part figured out and it's now working! Had to make the following change to my phpstan.neon:

parameters:
    customRulesetUsed: true

includes:
    - vendor/phpstan/phpstan-strict-rules/rules.neon

So the only thing left here is whether or not you wanted to add support for phpstan/extension-installer, which the removal of the -c arg should theoretically achieve. I'll test your build now.

shaneparsons commented 2 years ago

Awesome, that worked! Still need to include a phpstan.neon with:

parameters:
    customRulesetUsed: true

But I think that's probably a limitation of phpstan / phpstan-strict-rules / extension-installer rather than being specific to anything going on here.

Thanks for the quick work on this! Looking forward having this plugin used in my day to day workflow.

SanderRonde commented 2 years ago

Ah good to hear. I'll add support for it since it's not too hard.

shaneparsons commented 2 years ago

I'll re-open the issue and let you close it once you've implemented it.

SanderRonde commented 2 years ago

Done :)