soderlind / vscode-phpcbf

PHP Code Beautifier and Fixer for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=persoderlind.vscode-phpcbf
GNU General Public License v3.0
28 stars 10 forks source link

Error: executablePath not found when PhpCodeSniffer installed via Composer #1

Closed kevinwhoffman closed 6 years ago

kevinwhoffman commented 6 years ago

Current Behavior

On save, I see the following error:

Error: PHPCBF: spawn ./vendor/bin/phpcbf ENOENT. executablePath not found.

Expected Behavior

Code is beautified on save.

Project Details

VS Code Preferences

"phpcbf.executablePath": "./vendor/bin/phpcbf",
"phpcbf.onsave": true

Directory Structure

image

composer.json

{
    "name": "wordimpress/wp-business-reviews",
    "description": "",
    "homepage": "https://wpbusinessreviews.com",
    "type": "wordpress-plugin",
    "license": "GPL-2.0+",
    "require": {
        "composer/installers": "^1.4"
    },
    "require-dev": {
        "squizlabs/php_codesniffer": "*",
        "wp-coding-standards/wpcs": "^0.13.1"
    },
    "scripts": {
        "post-install-cmd": [
            "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
        ],
        "post-update-cmd": [
            "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
        ]
    }
}

Do you see anything that would cause that error?

soderlind commented 6 years ago

Please try to add the full path to phpcbfand see if it helps

soderlind commented 6 years ago

or add your vendor bin to your path: PATH="./vendor/bin:$PATH" If you ad to your path, you should be able to use the default setting "phpcbf.executablePath": "phpcbf",

soderlind commented 6 years ago

I've tested, and can confirm that adding the full path to phpcbf works find.

I will update the extension later today with support for relative path ie relative to project root

soderlind commented 6 years ago

When you install in workspace root, use: phpcbf.executablePath": "${workspaceRoot}/vendor/bin/phpcbf"

WraithKenny commented 6 years ago

${workspaceRoot} is deprecated. ${workspaceFolder} is the new multi-root friendly version, which prompts the question of how this extension should be configured in a multi-root project.

The fallback in multi-root for the workspaceRoot variable appears to simply be the first folder in the multi-root settings, meaning, if the correct folder isn't listed in the 0 index of the folders array, it'll fail.

I'm not really suggesting a change, just making a note for future bug reporters.

I'm not familiar enough with the new multi-root relevant apis, but I am pretty sure that there is a declaration of some sort to define a setting as being globally related (folder/file independent) or resource dependent (dealing with files and folders), which is the default this extension is using, is the globally related one (meaning that in mult-root projects, the .vscode/settings.json in each folder is ignored).

soderlind commented 6 years ago

Thank you. It's my first extension so I misunderstood the use of ${workspaceRoot} (didn't know of ${workspaceFolder} ) . I'll read me up on its use and update the extension.

In a multi-root environment, and only if you use a relative path starting with ${workspaceRoot} or ./, I'll iterate through the root folders looking for the file you added https://github.com/soderlind/vscode-phpcbf/blob/master/extension.js#L164-L193

WraithKenny commented 6 years ago

https://github.com/Microsoft/vscode/wiki/Extension-Authoring:-Adopting-Multi-Root-Workspace-APIs#settings-configuration

Yeah, it's very new, and (to me at least) pretty confusing. There are User Settings, but also Workspace Settings and Folder Settings now. I think when you register the settings, you have to choose between

So perhaps you should register the settings twice, for both types. (EDIT: Nevermind this. When registering a scope resource, it lets you set it in both places...)

I think that the extension should check for Folder based options ( "scope": "resource"), in case phpcs is composer installed in each folder, and also because each $workspaceFolder might want a different phpcbf.standard (and phpcbf.enable, phpcbf.onsave etc).

I think it might make sense to also register a Workspace File project-wide setting, to fallback to when the Folder settings aren't set (some people might not need per-folder settings).

Of course, if you do this, the most important thing would be making the documentation clear ;-). To be honest, none of the plugins I use have gotten this correct yet, and you'd be the first!

gaambo commented 6 years ago

i'm still having this issue on windows. i'm using the global phpcbf - which is in the PATH env variable. running phpcbf directly from cmd works. i tried adding the absolute path (in windows style with \\) and only the command phpcbf - always giving me the above error message. has this something to do with how paths are handled?

gaambo commented 6 years ago

for anyone coming here: what solved my issue was changing the executablePath in the user-/workspace-config to "phpcbf.bat" on windows. "phpcbf" (without .bat) works in cmd, but not in child_process.spawn (don't know why).
what i also noticed: the default on windows (extension.js#26) is php-cbf.bat - the dash should be removed. but the default setting was never called for me - even after removing the executablepath in the user- & workspace settings. maybe this should be filed as a seperate issue? though i had no time to test this in other environments.

xfudox commented 3 years ago

Hi, i still have this issue on Windows 10: extension is not working, while running the executable from cmd it works perfectly.
I can call just the executable name, i.e. phpcbf/phpcbf.bat (it works with both) without the entire path to the file.

Moreover, if i change my executablePath from phpcbf to phpcbf.bat i get no errors but neither my code is fixed; enabling debug with phpcbf.debug: true show the output of the .bat executable that is run on the temporary file but not applied then on the original project file.