SanderRonde / phpstan-vscode

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

Yet another docker problem #84

Closed Sectimus closed 4 months ago

Sectimus commented 4 months ago

There are a few other issues describing problems setting up this extension via docker so I thought I'd throw my hat into the ring. (see: https://github.com/SanderRonde/phpstan-vscode/issues/1, https://github.com/SanderRonde/phpstan-vscode/issues/12, https://github.com/SanderRonde/phpstan-vscode/issues/53)

For a bit of context, I am running a symfony project in docker, with the phpstan executable being within the container under vendor/bin/phpstan.

I have these settings:

"phpstan.rootDir": "${workspaceFolder}",
"phpstan.configFile": "phpstan.neon,phpstan.neon.dist",
"phpstan.binPath": "/var/www/REDACTED/vendor/bin/phpstan",
"phpstan.projectTimeout": 9999999,
"phpstan.paths":{
    "${workspaceFolder}": "/var/www/REDACTED",
},
"phpstan.binCommand": ["docker exec -t REDACTED_api_php-1 /bin/sh -c /var/www/REDACTED/vendor/bin/phpstan"],

This fails with the following vague error:

[check:6] Spawning PHPStan with the following configuration: {"binStr":"docker exec -t REDACTED_api_php-1 /bin/sh -c","args":["/var/www/REDACTED/vendor/bin/phpstan","analyse","-c","/var/www/REDACTED/phpstan.neon.dist","--error-format=json","--no-interaction","--memory-limit=1G"],"tmpDir":"/tmp/phpstan"}

[status-bar] Hiding status bar, last operation result = Error

[check:6] Check completed for project, errors= {"fileSpecificErrors":{},"notFileSpecificErrors":[]}

[check:6] PHPStan process exited with error filteredErr= rawErr= data= errMsg=spawn docker exec -t REDACTED_api_php-1 /bin/sh -c ENOENT**

...However... If I open a terminal and execute what I think this is trying to do based on the trace above: docker exec -t REDACTED_api_php-1 /bin/sh -c /var/www/REDACTED/vendor/bin/phpstan analyse -c /var/www/REDACTED/phpstan.neon.dist --error-format=json --no-interaction --memory-limit=1G

Then it works just fine (Albeit it complains about a lack of specified paths to analyse, presumably this is passed at runtime based on file edits, vscode tabs open ect)

At least one path must be specified to analyse.

I would assume this docker setup would work similarly to the PHPUnit extension which appears to operate fine with the following config:

"phpunit.command": "docker exec -t REDACTED_api_php-1 /bin/sh -c",
"phpunit.php": "XDEBUG_MODE=debug XDEBUG_TRIGGER=vscode XDEBUG_SESSION=vscode php",
"phpunit.phpunit": "/var/www/REDACTED/vendor/bin/phpunit",
"phpunit.args": [
    "-c",
    "${workspaceFolder}/phpunit.xml.dist"
],
"phpunit.paths": {
    "${workspaceFolder}": "/var/www/REDACTED/",
}, 
SanderRonde commented 4 months ago

I think the issue is surprisingly simple here. The error is that docker exec -t ... ENOENT or does not exist. That's correct. docker exec -t ... is indeed not a file that exists. docker (or more accurately something like /usr/bin/docker) is a file that exists.

So the short answer is: binCommand is an array that should contain the individual parts of the command to be ran and its args, not the entire command as one arg (similar to how spawn is called in node, unix, etc). So it should be: ["docker", "exec", "-t", "REDACTED_api_php-1", "/bin/sh", "-c", "/var/www/REDACTED/vendor/bin/phpstan"]. Or maybe all after -t should be a single arg that is passed to docker, you might have to try around with that.

I'll add an example to the README.md showing this too.

Let me know if this helps

Sectimus commented 4 months ago

This actually did do the trick! I did think to separate each flag of the command to it's own array element, but not each piece individually ["--someflag somevalue", "--otherflag othervalue"]vs your suggestion of ["--someflag", "somevalue", "--otherflag", "othervalue"]

SanderRonde commented 4 months ago

Good to hear! I'll close the issue now then :)