Skip to content

Formatting doesn't work in VScode #1730

Description

@davidlindercodes

My setup looks like this:
VS Code (latest version - no updates available)
Prettier code formatter (6.4.0) - Installed via VS Code extensions
Prettier/plugin-php - installed globally using:
npm install --global prettier @prettier/plugin-php

My VS code setting.json file looks like this:

    {
    "editor.codeActionsOnSave": null,
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true
    }

Yes when I try to format a .php file, using either format on save or smd-shift-p nothing happens, it has zero functionality. What am I doing wrong here? Is there an installation set that I missed?

Prettier is currently working as expected for .js files, it is only plug-php that I am having issues with.

Activity

  1. czosel commented on May 20, 2021

    @czosel
    Collaborator

    Did you already try installing the plugin locally in your project? (npm install prettier @prettier/plugin-php) It seems that making the prettier-vscode plugin work with global installations of prettier and plugins requires some configuration: https://github.com/prettier/prettier-vscode#prettier-resolution

    Also see #1721 (comment) for step-by-step instructions.

  2. changed the title [-]No functionality [/-] [+]Formatting doesn't work in VScode[/+] on May 20, 2021
  3. davidlindercodes commented on May 21, 2021

    @davidlindercodes
    Author

    Hi Czosel,

    Yes, I tried installing the plugin locally, but the same issue still exists.

      "dependencies": {
        "@prettier/plugin-php": "^0.16.3",
        "gulp-cache": "^1.1.3",
        "node-sass": "^5.0.0",
        "prettier": "^2.3.0"
      }
    
  4. D1360-64RC14 commented on Jan 24, 2022

    @D1360-64RC14

    Same problem.

    On terminal Prettier formats without problems, but VSCode's extension doesn't recognize the plugin. Even specifying the global or local node_modules in pluginSearchDirs. Prettier is locally and globally installed.

    image

    My local .prettierrc:

    {
      "phpVersion": "7.0",
      "tabWidth": 4,
      "useTabs": false,
      "singleQuote": true,
      "trailingCommaPHP": true,
      "pluginSearchDirs": [
        "/HDExterno/Langs/PHP/plugin/html/wp-content/themes/api/node_modules",
        "./node_modules",
        "node_modules"
      ],
      "plugins": [
        "@prettier/plugin-php"
      ],
      "parser": "php"
    }

    My settings.json:

    {
        // ...
        "editor.defaultFormatter": "esbenp.prettier-vscode",
        "[php]": {
            "editor.formatOnSave": true
        },
        // ...
    }
  5. tm1000 commented on Feb 3, 2022

    @tm1000

    @D1360-64RC14 setting pluginSearchDirs, plugins and parser is not necessary if prettierrc is in the same path as your node_modules folder.

  6. kjoedion commented on Dec 4, 2022

    @kjoedion

    I was also having trouble with the global installation.

    Adding this to my vscode user settings.json made it work!:

    "prettier.resolveGlobalModules": true
    
  7. tm1000 commented on Dec 4, 2022

    @tm1000

    @kjoedion makes sense. For me I install prettier php for every project individually

  8. WazzaJB commented on Jun 13, 2023

    @WazzaJB
    Contributor

    The solutions I found across the various tickets here didn't work for me unfortunately, but appreciate everyones efforts to come up with a solution.

    I had to extend the prettier document selectors within VS Code to get it to run on PHP files:

    .vscode/settings.json

    "prettier.documentSelectors": [
        "**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
     ]
    

    I also added parser to the prettier config and ended up with this:
    .prettierrc

    {
     "parser": "php",
     "plugins": ["@prettier/plugin-php"],
     "phpVersion": "8.2"
    }
    

    All works fine in VS Code for me now. Hopefully this helps someone else. Thanks for this plugin, big fan of prettier and so I'm really grateful I can bring some of that experience over to the small amount of PHP work I am doing.

    Related tickets are: #1721, #1640, prettier/prettier-vscode#2564

  9. czosel commented on Jun 13, 2023

    @czosel
    Collaborator

    @WazzaJB thanks for the write-up! If this is reproducible in a fresh VScode installation, I'd be happy to add this to our documentation 👍

  10. WazzaJB commented on Jun 13, 2023

    @WazzaJB
    Contributor

    In that case I shall raise a PR and try to prepare a codespace you can trial this in :)

  11. claudiusayadi commented on Aug 28, 2023

    @claudiusayadi

    .prettierrc

    Thanks a lot, @WazzaJB. Your solution works perfectly. Thanks for sharing.

  12. radosek commented on Dec 13, 2023

    @radosek

    Thanks, @WazzaJB !

    prettier.documentSelectors did the work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions