Visual Studio Code: Unable to locate phpcs

PhpVisual Studio-Code

Php Problem Overview


I am facing this issue in Visual Studio Code. I have already tried reinstalling the phpcs extension but still facing this issue.

this is the image

> Unable to locate phpcs > > Please add phpcs to your global path or use the composer dependency manager to install it in your project locally in Visual studio Code

Php Solutions


Solution 1 - Php

I had the same issue, I did the following to fix it:

  1. Install the phpcs by using composer with composer global require squizlabs/php_codesniffer

  2. Press Command + , (Click Code -> Preferences -> Settings)

  3. Select User Settings and locate 'PHP CodeSniffer'

  4. Scroll to 'Executatble Path' and put

    /Users/your-username/.composer/vendor/bin/phpcs

This fixed the issue for me, I hope it does for you too. Cheers!

Solution 2 - Php

open the project in the terminal and run this command

composer global require squizlabs/php_codesniffer

Hope that helps!

Solution 3 - Php

I think uninstalling it does not solve the issue.

i had the same issue. So,

  1. Go to extensions and install the plugin...."phpcs".
  2. Search for the extension if you had install it.
  3. Disable the extension.

it resolved the issue in my VS code.

Solution 4 - Php

On Debian based systems run as root:

apt-get install php-codesniffer

Solution 5 - Php

From a GitHub comment.

> If someone still needs help about this "Unable to locate phpcs ..." > error message, try this: you just need to install phpcs globally > via Composer. > > 1. Install Composer from this resource if you don't have it, > 2. After Composer is installed, open a command prompt wherever you want and run this command > > composer global require squizlabs/php_codesniffer

Solution 6 - Php

My solution without the need to install php_codesniffer globally.

Just go to VSCode settings and define the local path ./vendor/squizlabs/php_codesniffer/bin/phpcs

enter image description here

Or you can set the path directly in your settings.json

"phpcs.executablePath": "./vendor/squizlabs/php_codesniffer/bin/phpcs"

Solution 7 - Php

In my case and in Windows 10, composer located at:

C:\Users\{user_name}\AppData\Roaming\Composer\vendor\bin\

and when i added this to VSCode settings at PHP CodeSniffer section this solve my problem.

Solution 8 - Php

I had the same problem and i did this to fix it in my windows:

  1. You will Download the Composer (From here)

  2. Go to Vs code and open your terminal and run this command

    composer global require squizlabs/php_codesniffer
    
  3. Go to (settings--> User Settings), then search PHP CodeSniffer

  4. Scroll to Executatble Path and type

    \Users\{user_name}\AppData\Roaming\Composer\vendor\bin\phpcs
    

    and restart VS code.

This works For me, I hope it Works for you too :D

Solution 9 - Php

paste the following in your terminal for system-wide installation

composer global require squizlabs/php_codesniffer

This stopped the error message from popping up in mine.

Solution 10 - Php

Install PHP Sniffer instead of phpcs

PHP Sniffer worked on the fly without any extra configuration

phpcs could not recognize my commands, despite a configured PATH, Composer & PHPCS installed.

The commands were recognized in the terminal, but not by the plugin (under windows 10)

Solution 11 - Php

I had the same issue on my Mac in Visual Studio Code (I used PEAR to install PHPCS). I do not know why but PHPCS couldn't find autoload.php of CodeSniffer. You can try to type on terminal command line:

$ phpcs

I got an error:

> Warning: include_once(PHP/CodeSniffer/autoload.php): failed to open > stream: No such file or directory in /Users/david/pear/bin/phpcs on > line 14 > > Warning: include_once(): Failed opening 'PHP/CodeSniffer/autoload.php' > for inclusion (include_path='.:') in /Users/david/pear/bin/phpcs on > line 14

Solution:

Open Pear Bin folder in your user folder, e.g. in my case: /Users/david/pear/bin/ in Finder,

Find phpcs file inside and edit it (e.g. in Visual Studio Code :),

You will find these lines there:

if (is_file(__DIR__.'/../autoload.php') === true) {
    include_once __DIR__.'/../autoload.php';
} else {
    include_once 'PHP/CodeSniffer/autoload.php';
}

Add two new lines there with the proper location of CodeSniffer/autoload.php file (place it before } else { line and modify the path according to your needs - replace "david" with your user name...):

} else if (is_file('/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php')) {
    include_once '/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php';

So the final code of the condition is:

if (is_file(__DIR__.'/../autoload.php') === true) {
    include_once __DIR__.'/../autoload.php';
} else if (is_file('/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php')) {
    include_once '/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php';
} else {
    include_once 'PHP/CodeSniffer/autoload.php';
}

Then save it and restart Visual Studio Code. That's it.

Solution 12 - Php

I had the same problem with Drupal, the solutions is specify coding standards. So create phpcs.xml and enable the next options.

enter image description here

Solution 13 - Php

"phpcs.standard": "PSR2",
"phpcs.enable": true,
"phpcs.executablePath": "/home/<username>/.config/composer/vendor/squizlabs/php_codesniffer/bin/phpcs",
"phpcs.autoConfigSearch": true

Solution 14 - Php

I had this same error and I noticed that phpcs was working from the command line but not working in vscode. I had to read the linter.js file (of the vscode extension) to see where this error is coming from. This fix is more of an info.

So, to fix this: First, run the command phpcs --version and check if the command does not print any warning or error before outputting a line similar to PHP_CodeSniffer version x.x.x (stable) by Squiz (HTTP://www.squiz.net).

If any error or warning (usually unrelated to phpcs) is thrown before the line showing the phpcs version, please go fix that warning or error first. According to linter.js, it expects that the first output (when phpcs.bat is executed) must start with the PHP_CodeSniffer version string. Thus, If anything should print before the PHP_CodeSniffer version ...., then it throws the error.

So, You MUST fix any warning or error printed before the phpcs version if you want phpcs to work in VSCode. I hope this fix works for you. cheers!

Solution 15 - Php

I struggled with all these answers and I think it may be confusing to someone who is new to composer on Windows 10

This gives you better understanding of paths and Windows Environment Variables settings. However, it also includes installing php7 and integration with cakephp which you might want to skip and focus on (Composer and phpcs)

I found the fix at https://www.bravo-kernel.com/2017/03/installing-php7-composer-and-codesniffer-on-windows10-for-vscode/

And it had everything I needed to get phpcs working perfectly.

PROCEEDURE:

  1. Download Composer for Windows 10 Here

  2. Install Composer on Windows 10

  3. Open Command Prompt(cmd.exe) and type "composer" and hit the enter key (Just search the windows start for "cmd")

     Microsoft Windows [Version 10.0.19042.1165]
     (c) Microsoft Corporation. All rights reserved.
    
     C:\Users\systemuser>composer
    

If composer is properly installed you will get a response with something like this

       ______
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                /_/
    Composer version 2.1.5 2021-07-23 10:35:47

    Usage:
    command [options] [arguments]

    Options:
      -h, --help                     Display this help message
      -q, --quiet                    Do not output any message
      -V, --version                  Display this application version
      --ansi                     Force ANSI output
  --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
  --profile                  Display timing and memory usage information
  --no-plugins               Whether to disable plugins.
   -d, --working-dir=WORKING-DIR  If specified, use the given directory as 
    working directory.
    --no-cache                 Prevent use of the cache
   -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for 
    normal output, 2 for more verbose output and 3 for debug

    Available commands:
    (It continues)

4. If you do not intend to change the default directory you can now install phpcs otherwise you can use instructions from the link above to set a new executable path. The default composer directory is

    C:/Users/systemuser/AppData/Roaming/Composer

Now in Command prompt add the following command (Global installation):

    composer global require squizlabs/php_codesniffer

Here is what it will look like:

    C:\Users\systemuser>composer global require squizlabs/php_codesniffer
    Changed current directory to C:/Users/systemuser/AppData/Roaming/Composer
    Using version ^3.6 for squizlabs/php_codesniffer
    ./composer.json has been updated
    Running composer update squizlabs/php_codesniffer
    Loading composer repositories with package information
    Updating dependencies
    Nothing to modify in lock file
    Writing lock file
    Installing dependencies from lock file (including require-dev)
    Nothing to install, update or remove
    Generating autoload files

    C:\Users\systemuser>

Note: Nothing installed because I already installed phpcs. Also you can see that composer switched to the default directory, if this doesn't happen you need to navigate to the appropriate directory with "cd newpath" before installing phpcs:

    C:\Users\systemuser>cd AppData\Roaming\Composer

The result is:

    C:\Users\systemuser\AppData\Roaming\Composer>

You have completed the installation so verify the app path by navigating the path

C:\Users\systemuser\AppData\Roaming\Composer\vendor\bin

Now you can copy the link to the phpcs file and paste in VSCode (If you changed your composer bin path then you need to copy the appropriate directory). It should look like this

    C:\Users\sytemuser\AppData\Roaming\Composer\vendor\bin\phpcs

C:\Users\systemuser\AppData\Roaming\Composer\vendor\bin\phpcs

Now phpcs should work fine after installing the extension!

Solution 16 - Php

According to the best answer 

In my case(in Windows 10), the composer located at:

C:\Users\user-name\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\bin

Solution 17 - Php

You stated that you already uninstalled it, so i'm assuming your trying to get rid of the extension and error.

How to fix:

  1. Uninstall extension.
  2. Delete the entire phpcs folder from the VS Code folder.

From the VS Code Docs:

Q: Where are extensions installed?

A: Depending on your platform, the global location is in the following folders:

Windows %USERPROFILE%\.vscode\extensions

Mac ~/.vscode/extensions

Linux ~/.vscode/extensions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMonika RaniView Question on Stackoverflow
Solution 1 - PhpJekayodeView Answer on Stackoverflow
Solution 2 - PhpCengkuru MichaelView Answer on Stackoverflow
Solution 3 - PhpKevin MbuguaView Answer on Stackoverflow
Solution 4 - PhpMoazzem HossenView Answer on Stackoverflow
Solution 5 - PhpKarem ShawkyView Answer on Stackoverflow
Solution 6 - PhpSandroMarquesView Answer on Stackoverflow
Solution 7 - PhpalirezaView Answer on Stackoverflow
Solution 8 - PhpThe_Big_DealView Answer on Stackoverflow
Solution 9 - PhpFusion BoltView Answer on Stackoverflow
Solution 10 - PhpmigliView Answer on Stackoverflow
Solution 11 - PhpDavid NajmanView Answer on Stackoverflow
Solution 12 - PhpmaccevedorView Answer on Stackoverflow
Solution 13 - PhpbudionoView Answer on Stackoverflow
Solution 14 - PhpMichael89View Answer on Stackoverflow
Solution 15 - PhpVee JoeView Answer on Stackoverflow
Solution 16 - Phpmohammad barzegarView Answer on Stackoverflow
Solution 17 - PhpM HView Answer on Stackoverflow