ikappas / vscode-phpcs

PHP CodeSniffer for Visual Studio Code
MIT License
128 stars 56 forks source link

How to use phpcs from docker-compose? #134

Open alexd73 opened 5 years ago

alexd73 commented 5 years ago

I use wodby/drupal-php (docker4drupal), which have inside a phpcs docker-compose exec php phpcs --version

PHP_CodeSniffer version 3.3.2 (stable) by Squiz (http://www.squiz.net)

Can I use this in vscode?

I trying create exec file phpcs-vscobe

#!/bin/sh

docker-compose exec php phpcs "$@"

and have an error from phpcs-vscode:

the input device is not a TTY

I added to file export COMPOSE_INTERACTIVE_NO_CLI=1

and error changed to

phpcs: Unable to locate phpcs. Command failed: "/home/alexey/phpcs-vscode" --version Traceback (most recent call last): File "/usr/lib/python3.7/site-packages/dockerpty/pty.py", line 334, in start self._hijack_tty(pumps) File "/usr/lib/python3.7/site-packages/dockerpty/pty.py", line 367, in _hijack_tty read_ready, write_ready = io.select(read_pumps, write_streams, timeout=60) File "/usr/lib/python3.7/site-packages/dockerpty/io.py", line 59, in select timeout, File "/usr/lib/python3.7/site-packages/dockerpty/io.py", line 351, in fileno return self.from_stream.fileno() File "/usr/lib/python3.7/site-packages/dockerpty/io.py", line 103, in fileno return self.fd.fileno() File "/usr/lib/python3.7/socket.py", line 638, in fileno self._checkClosed() ValueError: I/O operation on closed file. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/bin/docker-compose", line 11, in load_entry_point('docker-compose==1.23.2', 'console_scripts', 'docker-compose')() File "/usr/lib/python3.7/site-packages/compose/cli/main.py", line 71, in main command() File "/usr/lib/python3.7/site-packages/compose/cli/main.py", line 127, in perform_command handler(command, command_options) File "/usr/lib/python3.7/site-packages/compose/cli/main.py", line 535, in exec_command pty.start() File "/usr/lib/python3.7/site-packages/dockerpty/pty.py", line 338, in start io.set_blocking(pump, flag) File "/usr/lib/python3.7/site-packages/dockerpty/io.py", line 32, in set_blocking old_flag = fcntl.fcntl(fd, fcntl.F_GETFL) File "/usr/lib/python3.7/site-packages/dockerpty/io.py", line 351, in fileno return self.from_stream.fileno() File "/usr/lib/python3.7/site-packages/dockerpty/io.py", line 103, in fileno return self.fd.fileno() File "/usr/lib/python3.7/socket.py", line 638, in fileno self._checkClosed() ValueError: I/O operation on closed file.

I don`t known that I can try else. I whuld be happy if we could solve this situation. :)

Idealien commented 5 years ago

I tried same setup and the current result is a less verbose error, "phpcs: Cannot read property 'toString' of null"

jgonyea commented 5 years ago

The closest I could get was to create an executable bash wrapper at /usr/local/bin/phpcs with the following:

#!/bin/sh

 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

 docker run --rm --user $(id -u):$(id -g) --volume $(pwd):/project herloct/phpcs --standard=PSR2 $@

Then I'm able to run phpcs from the VSCode terminal. I still can't get the GUI to recognize issues, however. I see the spinning loading icon that phpcs is attempting to parse the current PHP file as I edit it.

OS: Ubuntu 19.04 Docker version 19.03.0-beta5, build 4a18bf4

jgonyea commented 5 years ago

I do the same thing with phpcbf (create a executable bash wrapper at /usr/local/bin/phpcbf with the following content):

#!/bin/sh

 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

 docker run --rm --user $(id -u):$(id -g) --volume $(pwd):/project herloct/phpcbf --no-patch $@
ErwanTremiot commented 4 years ago

I almost managed to make it work. I managed to get my phpcs binary recognized by disabling pseudo-tty allocation on docker-compose exec with -T flag. My wrapper looks like this:

#!/bin/sh

docker-compose exec -T php phpcs "$@"

But then the only output I got was a warning that the file doesn't contain php code. Seems like the extension builds the following command to lint a file: bin/phpcs --report=json -q --encoding=UTF-8 --standard=PSR12 --error-severity=5 --warning-severity=5 --stdin-path=your-php-file - Where your-php-file is the absolute path to your file. Thus passing the absolute path to --stdin-path cannot work work since this path does not exists in the container.

I don't know if this can be fixed.

benharris-conversica commented 3 years ago

At first I was having the same problem as jgonyea where phpcs just spins infinitely, but I finally got it working. The key was adding the -i flag. Here's the script that works for me:

#!/bin/bash

# note: pwd will be set to the directory that you opened vscode from
docker run -i --rm -v $(pwd):$(pwd) cytopia/phpcs $*
vipex commented 2 years ago

I also get "phpcs: Cannot read property 'toString' of null" using @benharris-conversica approach Also from the extension's trace I can't figure out what's the problem =/

ultimike commented 1 year ago

I have a similar issue, where I'd like to use a version of phpcs that is located inside a container that is normally running for my project.

I have created a bash wrapper:

#!/bin/sh

docker exec -it -w '/var/www/html/' [CONTAINER ID] bash -c 'phpcs'

Running this script directly provides the response I expect:

$ ./phpcs.bash
ERROR: You must supply at least one file or directory to process.

Run "phpcs --help" for usage information

When I try to set the executable path to this in VSCode, I get the error: "phpcs: Unable to locate phpcs. Command failed: "[/path/to]/phpcs.bash" --version the input device is not a TTY"

"phpcs.executablePath": "[/path/to]/phpcs.bash",

Is there any way to get this to work?

-mike

hkirsman commented 1 month ago

Not sure how your script worked, benharris-conversica , but I adjusted the internal mount point to /data as on that particular docker image page and that works nicely for me.

#!/bin/bash

# note: pwd will be set to the directory that you opened vscode from
docker run -i --rm -v $(pwd):/data cytopia/phpcs $*
benharris-conversica commented 1 month ago

My current version of the script looks similar, I must have switched the mount to /data at some point as well:

#!/bin/bash
# see: https://github.com/cytopia/docker-phpcs and https://hub.docker.com/r/cytopia/phpcs
PHPCS_IMAGE=cytopia/phpcs:latest-php7.3
PHPCS_STANDARD=./phpcs.xml
PHPCS_IGNORE_DIRS=tests,vendor

docker run --rm \
    --volume $(pwd):/data \
    $PHPCS_IMAGE \
        -p -s \
        --standard=$PHPCS_STANDARD \
        --ignore=$PHPCS_IGNORE_DIRS \
        --runtime-set ignore_warnings_on_exit 1 \
        --extensions=php \
        $*