prettier / prettier-vscode

Visual Studio Code extension for Prettier
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
MIT License
5.18k stars 459 forks source link

prettier-vscode does not work with plugins since prettier v3.1.1 #3235

Closed DashwoodIce9 closed 8 months ago

DashwoodIce9 commented 11 months ago

Summary

Since prettier v3.1.1 the prettier-vscode extension does not work with prettier plugins. In the example repo linked below the Java plugin is not working. However, I have verified this to be a bug with other plugins like sh and XML as well. Formatting with prettier directly (running on command line) works (try running npm run format in the sample repo linked below)

Github Repository to Reproduce Issue

Link to a Github repo that can be used to reproduce the issue. Branch 3.1.1 has the issue. Branch 3.1.0 doesn't.

Steps To Reproduce:

  1. Install prettier 3.1.1 in an npm project alongwith a plugin. I am using prettier-plugin-java as an example.
  2. Reload VSCode and verify that the prettier-vscode extension is now using the local prettier binary (the one in node_modules).
  3. Try to format a Java file.

Expected result

It should format the file.

Actual result

Does not format. VSCode says - "Extension 'Prettier - Code formatter' is configured as formatter but it cannot format 'Java'-files"

Additional information

The extension doesn't seem to be communicating to VSCode that it is capable of formatting Java files. Although it looks like it is reading the configuration correctly. If I try to format a JSON like package.json which Prettier supports out of the box and see the logs of the extension, it shows this -

["DEBUG" - 13:12:02] Local prettier module path: '/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier/index.cjs'
["DEBUG" - 13:12:02] Using prettier version 3.1.1
["DEBUG" - 13:12:05] Local prettier module path: '/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier/index.cjs'
["INFO" - 13:12:07] Formatting file:///Users/prakhaga/Programming/prettier-bug-report/package.json
["DEBUG" - 13:12:07] Local prettier module path: '/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier/index.cjs'
["INFO" - 13:12:07] Using config file at '/Users/prakhaga/Programming/prettier-bug-report/package.json'
["DEBUG" - 13:12:07] Local prettier module path: '/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier/index.cjs'
["INFO" - 13:12:07] PrettierInstance:
{
  "modulePath": "/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier/index.cjs",
  "importResolver": {},
  "callMethodResolvers": {},
  "currentCallMethodId": 8,
  "version": "3.1.1"
}
["INFO" - 13:12:07] Using ignore file (if present) at /Users/prakhaga/Programming/prettier-bug-report/.prettierignore
["INFO" - 13:12:07] File Info:
{
  "ignored": false,
  "inferredParser": "json-stringify"
}
["INFO" - 13:12:07] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 13:12:07] Prettier Options:
{
  "filepath": "/Users/prakhaga/Programming/prettier-bug-report/package.json",
  "parser": "json-stringify",
  "printWidth": 120,
  "tabWidth": 4,
  "trailingComma": "all",
  "useTabs": false,
  "plugins": [
    "/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier-plugin-java/dist/index.js"
  ]
}
["INFO" - 13:12:07] Formatting completed in 643ms.

wherein it looks like that it knows that the Java plugin exists, but somehow fails to communicate that to VSCode.

VS Code Version:

Version: 1.85.1 Commit: 0ee08df0cf4527e40edc9aa28f4b5bd38bbff2b2 Date: 2023-12-13T09:48:06.308Z Electron: 25.9.7 ElectronBuildId: 25551756 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Darwin x64 22.6.0

Prettier Extension Version: v10.1.0

OS and version: MacOS Ventura v13.6.3

Prettier Log Output

Bad log with v3.1.1 -

["INFO" - 12:54:48] Extension Name: esbenp.prettier-vscode.
["INFO" - 12:54:48] Extension Version: 10.1.0.
["DEBUG" - 12:54:48] Enabling Prettier globally
{
  "languageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "graphql"
    }
  ]
}
["DEBUG" - 12:54:48] Local prettier module path: '/Users/prakhaga/Programming/bug_report/node_modules/prettier/index.cjs'
["DEBUG" - 12:54:48] Using prettier version 3.1.1
["DEBUG" - 12:54:48] Enabling Prettier for Workspace /Users/prakhaga/Programming/bug_report
{
  "languageSelector": [
    {
      "pattern": "/Users/prakhaga/Programming/bug_report/**/*.{js,_js,bones,cjs,es,es6,frag,gs,jake,javascript,jsb,jscad,jsfl,jslib,jsm,jspre,jss,mjs,njs,pac,sjs,ssjs,xsjs,xsjslib,wxs,js.flow,jsx,ts,cts,mts,tsx,importmap,json,4DForm,4DProject,avsc,geojson,gltf,har,ice,JSON-tmLanguage,mcmeta,tfstate,tfstate.backup,topojson,webapp,webmanifest,yy,yyp,jsonc,code-snippets,sublime-build,sublime-commands,sublime-completions,sublime-keymap,sublime-macro,sublime-menu,sublime-mousemap,sublime-project,sublime-settings,sublime-theme,sublime-workspace,sublime_metrics,sublime_session,json5,css,wxss,pcss,postcss,less,scss,handlebars,hbs,graphql,gql,graphqls,md,livemd,markdown,mdown,mdwn,mkd,mkdn,mkdown,ronn,scd,workbook,mdx,component.html,html,hta,htm,html.hl,inc,xht,xhtml,mjml,vue,yml,mir,reek,rviz,sublime-syntax,syntax,yaml,yaml-tmlanguage,yaml.sed,yml.mysql,code-workspace}",
      "scheme": "file"
    },
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "graphql"
    }
  ]
}

Good log with v3.1.0 -

["INFO" - 13:04:29] Extension Name: esbenp.prettier-vscode.
["INFO" - 13:04:29] Extension Version: 10.1.0.
["DEBUG" - 13:04:29] Enabling Prettier globally
{
  "languageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "graphql"
    }
  ]
}
["DEBUG" - 13:04:29] Local prettier module path: '/Users/prakhaga/Programming/prettier-bug-report/node_modules/prettier/index.cjs'
["DEBUG" - 13:04:29] Using prettier version 3.1.0
["INFO" - 13:04:29] Using config file at '/Users/prakhaga/Programming/prettier-bug-report/package.json'
["DEBUG" - 13:04:30] Enabling Prettier for Workspace /Users/prakhaga/Programming/prettier-bug-report
{
  "languageSelector": [
    {
      "pattern": "/Users/prakhaga/Programming/prettier-bug-report/**/*.{js,_js,bones,cjs,es,es6,frag,gs,jake,javascript,jsb,jscad,jsfl,jslib,jsm,jspre,jss,mjs,njs,pac,sjs,ssjs,xsjs,xsjslib,wxs,js.flow,jsx,ts,cts,mts,tsx,importmap,json,4DForm,4DProject,avsc,geojson,gltf,har,ice,JSON-tmLanguage,mcmeta,tfstate,tfstate.backup,topojson,webapp,webmanifest,yy,yyp,jsonc,code-snippets,sublime-build,sublime-commands,sublime-completions,sublime-keymap,sublime-macro,sublime-menu,sublime-mousemap,sublime-project,sublime-settings,sublime-theme,sublime-workspace,sublime_metrics,sublime_session,json5,css,wxss,pcss,postcss,less,scss,handlebars,hbs,graphql,gql,graphqls,md,livemd,markdown,mdown,mdwn,mkd,mkdn,mkdown,ronn,scd,workbook,mdx,component.html,html,hta,htm,html.hl,inc,xht,xhtml,mjml,vue,yml,mir,reek,rviz,sublime-syntax,syntax,yaml,yaml-tmlanguage,yaml.sed,yml.mysql,code-workspace,java}",
      "scheme": "file"
    },
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "java"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "graphql"
    }
  ]
}
achou11 commented 11 months ago

Noticed this issue too as I had Prettier v3.1.1 and https://github.com/prettier/plugin-xml v3.2.2 installed. The workaround of using Prettier v3.1.0 solved my issue as well

lts20050703 commented 10 months ago

I'm pretty sure this is related to #3104

Try adding "prettier.configPath": "./.prettierrc" to .vscode/settings.json file (replace .prettierrc with the file name that you use)

DashwoodIce9 commented 10 months ago

@lts20050703 This worked, thanks!

Prettier team, is this expected behavior?

sosukesuzuki commented 8 months ago

@fisker https://github.com/prettier/prettier/pull/15363 breaks prettier-vscode config resolution..?

fisker commented 8 months ago

Use file path as we agreed https://github.com/prettier/prettier/issues/15879#issuecomment-1885212033

github-actions[bot] commented 5 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.