Php_codesniffer: PHPStorm integration hangs in 3.2.0 / 3.2.1

Created on 18 Dec 2017  Â·  17Comments  Â·  Source: squizlabs/PHP_CodeSniffer

I read that 3.2.1 was supposed to fix the issue with PHPStorm integration hanging and not producing any results. However, even after updating to the latest version, I still see this issue. When the PHPCS integration is enabled in PHPStorm, all I get are errors (from idea.log):

Note: I'm developing in Windows 7 using PHPStorm 2017.3.1

2017-12-18 11:35:24,758 [ 553921]   INFO - lity.QualityToolProcessCreator - No response from C:\Users\Riimu\AppData\Roaming\Composer\vendor\bin\phpcs.bat after 10000 ms 
2017-12-18 11:35:24,759 [ 553922]   WARN - s.quality.QualityToolAnnotator - phpcs: Can not correctly run the tool with parameters:
  C:/Users/Riimu/AppData/Local/Temp/phpcs_temp.tmp/src/ConfigureCommand.php
  --standard=PSR2
  --encoding=utf-8
  --report=xml
Possible tool process hangup after 10 sec. for 'D:/Projects/php-configure/src/ConfigureCommand.php' on local 

As with previous 3.2.0 version, doing this via command line works fine:

> phpcs src/ConfigureCommand.php --standard=PSR2 --encoding=utf-8 --report=xml
<?xml version="1.0" encoding="UTF-8"?>
<phpcs version="3.2.1">
</phpcs>

PHPCS version:

> phpcs --version
PHP_CodeSniffer version 3.2.1 (stable) by Squiz (http://www.squiz.net)

PHP Version:

> php --version
PHP 7.2.0 (cli) (built: Nov 28 2017 23:48:49) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

Will have to revert to 3.1.1 as that seems to work without issues.

Bug

All 17 comments

Yes, this release should have fixed that. It did in my testing and it did for a couple of other people testing before release as well. I'm not sure why you'd still be getting the error as there is now a timeout on STDIN that should be detected instead of waiting forever.

Is there any chance you could find src/Config.php and confirm this timeout code is in there: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Config.php#L380

If you can play around in there, adding a break at the start of that while statement should simulate the timeout and get it going. If that doesn't work, comment out the whole while statement and see what happens. Any debugging you can do in there would be really helpful.

I have the same problem
PhpStorm: 2017.3.1
CodeSniffer: 3.2.1
PHP: 7.1.9

@gsherwood , when I had a break at start of the while, it is working now !

untitled 1

Tried to investigate this a bit more deeply to find a root cause of this issue. This is most likely a Windows specific issue, since STDIN behaves a little differently on Windows.

Essentially, reading from STDIN is always blocking on windows and there doesn't seem to be any way around it. More information can be seen in this bug report:

https://bugs.php.net/bug.php?id=34972

It seems that the Config class has been changed to always attempt reading from the STDIN, if PHPCS isn't being run in an interactive console. However, as PhpStorm does not provide STDIN for the script, trying to read from it blocks (despite steam_select() claiming activity on it). From what I've read, there doesn't seem to be any way in PHP to detect if any STDIN has actually been provided. Therefore, trying to read from STDIN without explicitly being instructed to seems to be asking for trouble.

Console and command line interaction in PHP has always been troublesome in Windows as can be seen from the issue reported back in 2005 and still remaining unaddressed.

The previous version 3.1.1 does not seem to have this issue, because it tried to set the STDIN to unblocking mode via if (stream_set_blocking($handle, false) === true) {, which would always fail.

Essentially, reading from STDIN is always blocking on windows and there doesn't seem to be any way around it.

The change in 3.2.0 is actually to keep STDIN blocking at all times. The original issue fixed in 3.2.1 was to add a timeout (using stream_select) so that a blocked STDIN where no content was actually coming would timeout after 200ms. But I guess stream_select timeouts aren't working in windows.

A possible solution would be to implement my own timeout. I'll see if I can get a windows/phpstorm test setup going to try it out.

Edit: ignore me - fgets is the blocking call

I had to disable STDIN detection on Windows because I just don't think there is any way to solve the problem. This basically reverts windows back to skipping all STDIN processing like it did in 3.1.1 but retains the new support for other environments.

Note that using - to tell PHPCS to wait for STDIN still works.

Is anyone able to confirm the fix for me by grabbing the latest master?

Works fine for me
(Windows 10)

phpcs works again in PhpStorm with "squizlabs/php_codesniffer": "dev-master as 3.2.2" (- Installing squizlabs/php_codesniffer (dev-master 35b81c2): Cloning 35b81c289a from cache)

Can confirm that the latest dev-master works again on Windows.

I had to disable STDIN detection on Windows because I just don't think there is any way to solve the problem.

This was my conclusion as well, and has been my assumption for many years. The only way I've gotten decent cross platform implementations work using STDIN for any purpose has been to add an explicit flag to indicate reading from STDIN.

PHP desperately needs a better cross platform console IO implementation (and has needed for years).

Fun fact: symfony/console actually bundles a hiddeninput.exe binary which it calls for hidden input questions, due to PHP's poor IO handling in Windows. (See Symfony\Component\Console\Helper\QuestionHelper::getHiddenResponse())

Thanks @quienti @tomasfejfar @Riimu for testing that for me. I'll package up a release.

@gsherwood
I have same error like this in PHPStorm 2017.3.2
squizlabs/php_codesniffer version 3.2.2

I have followed this instruction https://confluence.jetbrains.com/display/PhpStorm/PHP+Code+Sniffer+in+PhpStorm

My Coding standard is PSR2

image

@ToujouAya I haven't had any reports about PHPStorm integration not working since this fix. Are you sure PHPStorm is using the right version of PHPCS?

Edit: And have you tried running PHPCS on that file manually? Maybe this is a different issue making PHPCS hang (or run slowly) for this file.

@gsherwood

Are you sure PHPStorm is using the right version of PHPCS?

Yep I think so.

Composer global show

composer/semver               1.4.2   Semver library that offers utilities, version constraint parsing and valid...
doctrine/annotations          v1.4.0  Docblock Annotations Parser
doctrine/lexer                v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent P...
friendsofphp/php-cs-fixer     v2.10.0 A tool to automatically fix PHP code style
gecko-packages/gecko-php-unit v2.2    Additional PHPUnit asserts and constraints.
paragonie/random_compat       v2.0.10 PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
php-cs-fixer/diff             v1.2.0  sebastian/diff v2 backport support for PHP5.6
psr/log                       1.0.2   Common interface for logging libraries
sebastian/diff                1.4.3   Diff implementation
squizlabs/php_codesniffer     3.2.2   PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violat...
symfony/console               v3.3.8  Symfony Console Component
symfony/debug                 v3.3.8  Symfony Debug Component
symfony/event-dispatcher      v3.3.8  Symfony EventDispatcher Component
symfony/filesystem            v3.3.8  Symfony Filesystem Component
symfony/finder                v3.3.8  Symfony Finder Component
symfony/options-resolver      v3.3.8  Symfony OptionsResolver Component
symfony/polyfill-mbstring     v1.5.0  Symfony polyfill for the Mbstring extension
symfony/polyfill-php70        v1.5.0  Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-php72        v1.5.0  Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/process               v3.3.8  Symfony Process Component
symfony/stopwatch             v3.3.8  Symfony Stopwatch Component

Edit: And have you tried running PHPCS on that file manually?

I tried it and it ok

image

@ToujouAya I'm sorry, I don't have any idea why it wouldn't work with version 3.2.2. Is anyone else subscribed to this issue having the same problem?

@ToujouAya it might be because you use global phpcs instead of local. Double check your settings in Settings → Lang&Fw → PHP → Code Sniffer → Configuration → Local → [...] → Code Sniffer (phpcs) path and make sure you're using local vendor folder and not anything else.

I actually have this same issue. Began last week, I'll change some settings, clear logs and it will start working. If I close PhpStorm and relaunch it, I get the same error message.

Since I changed to a local copy of Phpcs, I only get the error message for the currently open file (whatever Php file it is) when PhpStorm relaunches. If I clear the message and open another file, everything works great.

And yes, Phpcs works fine with Visual Studio Code with no errors.

My setup
Windows 10,
squizlabs/php_codesniffer 3.3.0 (global and local)
PhpStorm 2018.1.6

Was this page helpful?
0 / 5 - 0 ratings