coc-prettier icon indicating copy to clipboard operation
coc-prettier copied to clipboard

Does not format ruby code with latest prettier and prettier/plugin-ruby

Open tklepzig opened this issue 2 years ago • 4 comments

Hi,

when trying to format ruby files either on save or forced with CocCommand prettier.formatFile nothing happens.

The output of the prettier coc extension is the following:

["INFO" - 1:24:09 PM] File Info:
{
  "ignored": false,
  "inferredParser": null
}
["WARN" - 1:24:09 PM] Parser not inferred, trying languageId.
["ERROR" - 1:24:09 PM] Error formatting document
["ERROR" - 1:24:09 PM] Cannot read properties of undefined (reading 'find')
TypeError: Cannot read properties of undefined (reading 'find')
    at getParserFromLanguageId (/Users/me/.config/coc/extensions/node_modules/coc-prettier/lib/index.js:4287:31)
    at PrettierEditService.format (/Users/me/.config/coc/extensions/node_modules/coc-prettier/lib/index.js:4599:16)
    at async PrettierEditService.provideEdits (/Users/me/.config/coc/extensions/node_modules/coc-prettier/lib/index.js:4482:22)
    at async PrettierEditService.forceFormatDocument (/Users/me/.config/coc/extensions/node_modules/coc-prettier/lib/index.js:4369:23)
    at async BR.fireCommand (/Users/me/.vim/vim-plug/coc.nvim/build/index.js:66:4285)
    at async Cy.runCommand (/Users/me/.vim/vim-plug/coc.nvim/build/index.js:257:24015)
    at async Gy.cocAction (/Users/me/.vim/vim-plug/coc.nvim/build/index.js:277:46084)
    at async EventEmitter.<anonymous> (/Users/me/.vim/vim-plug/coc.nvim/build/index.js:277:47703)

I've investigated it further and added some additional logging to my installed coc-prettier and prettier (from node_modules). It turned out that inferring the language from the file extension (.rb) didn't work. Prettier builds up a big array of languages (see below) and searches for ruby in it. For whatever reason, ruby is not present in this array despite having correctly defined the necessary plugin for prettier (prettier/plugin-ruby) in the prettier config.

When doing this with prettier 2.8.0 and prettier/plugin-ruby 3.2.2 everything works as expected and the language array contains ruby. My guess is that the ruby plugin is somewhat ignored and therefore the create prettier instance doesn't know anything about ruby.

I'm not deep into the knowledge how coc-prettier, prettier and plugins work together, so I'm not quite sure what the real underlying issue could be, any comments/insights are welcome.

The languages array mentioned above with latest prettier and prettier/plugin-ruby:

[{"linguistLanguageId":50,"name":"CSS","type":"markup","tmScope":"source.css","aceMode":"css","codemirrorMode":"css","codemirrorMimeType":"text/css","color":"#563d7c","extensions":[".css",".wxss"],"parsers":["css"],"vscodeLanguageIds":["css"]},{"linguistLanguageId":262764437,"name":"PostCSS","type":"markup","color":"#dc3a0c","tmScope":"source.postcss","group":"CSS","extensions":[".pcss",".postcss"],"aceMode":"text","parsers":["css"],"vscodeLanguageIds":["postcss"]},{"linguistLanguageId":198,"name":"Less","type":"markup","color":"#1d365d","aliases":["less-css"],"extensions":[".less"],"tmScope":"source.css.less","aceMode":"less","codemirrorMode":"css","codemirrorMimeType":"text/css","parsers":["less"],"vscodeLanguageIds":["less"]},{"linguistLanguageId":329,"name":"SCSS","type":"markup","color":"#c6538c","tmScope":"source.css.scss","aceMode":"scss","codemirrorMode":"css","codemirrorMimeType":"text/x-scss","extensions":[".scss"],"parsers":["scss"],"vscodeLanguageIds":["scss"]},{"linguistLanguageId":139,"name":"GraphQL","type":"data","color":"#e10098","extensions":[".graphql",".gql",".graphqls"],"tmScope":"source.graphql","aceMode":"text","parsers":["graphql"],"vscodeLanguageIds":["graphql"]},{"linguistLanguageId":155,"name":"Handlebars","type":"markup","color":"#f7931e","aliases":["hbs","htmlbars"],"extensions":[".handlebars",".hbs"],"tmScope":"text.html.handlebars","aceMode":"handlebars","parsers":["glimmer"],"vscodeLanguageIds":["handlebars"]},{"linguistLanguageId":146,"name":"Angular","type":"markup","tmScope":"text.html.basic","aceMode":"html","codemirrorMode":"htmlmixed","codemirrorMimeType":"text/html","color":"#e34c26","aliases":["xhtml"],"extensions":[".component.html"],"parsers":["angular"],"vscodeLanguageIds":["html"],"filenames":[]},{"linguistLanguageId":146,"name":"HTML","type":"markup","tmScope":"text.html.basic","aceMode":"html","codemirrorMode":"htmlmixed","codemirrorMimeType":"text/html","color":"#e34c26","aliases":["xhtml"],"extensions":[".html",".hta",".htm",".html.hl",".inc",".xht",".xhtml",".mjml"],"parsers":["html"],"vscodeLanguageIds":["html"]},{"linguistLanguageId":146,"name":"Lightning Web Components","type":"markup","tmScope":"text.html.basic","aceMode":"html","codemirrorMode":"htmlmixed","codemirrorMimeType":"text/html","color":"#e34c26","aliases":["xhtml"],"extensions":[],"parsers":["lwc"],"vscodeLanguageIds":["html"],"filenames":[]},{"linguistLanguageId":391,"name":"Vue","type":"markup","color":"#41b883","extensions":[".vue"],"tmScope":"text.html.vue","aceMode":"html","parsers":["vue"],"vscodeLanguageIds":["vue"]},{"linguistLanguageId":183,"name":"JavaScript","type":"programming","tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"text/javascript","color":"#f1e05a","aliases":["js","node"],"extensions":[".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"],"filenames":["Jakefile"],"interpreters":["chakra","d8","gjs","js","node","nodejs","qjs","rhino","v8","v8-shell","zx"],"parsers":["babel","acorn","espree","meriyah","babel-flow","babel-ts","flow","typescript"],"vscodeLanguageIds":["javascript","mongo"]},{"linguistLanguageId":183,"name":"Flow","type":"programming","tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"text/javascript","color":"#f1e05a","aliases":[],"extensions":[".js.flow"],"filenames":[],"interpreters":["chakra","d8","gjs","js","node","nodejs","qjs","rhino","v8","v8-shell"],"parsers":["flow","babel-flow"],"vscodeLanguageIds":["javascript"]},{"linguistLanguageId":183,"name":"JSX","type":"programming","tmScope":"source.js.jsx","aceMode":"javascript","codemirrorMode":"jsx","codemirrorMimeType":"text/jsx","extensions":[".jsx"],"parsers":["babel","babel-flow","babel-ts","flow","typescript","espree","meriyah"],"vscodeLanguageIds":["javascriptreact"],"group":"JavaScript"},{"linguistLanguageId":378,"name":"TypeScript","type":"programming","color":"#3178c6","aliases":["ts"],"interpreters":["deno","ts-node"],"extensions":[".ts",".cts",".mts"],"tmScope":"source.ts","aceMode":"typescript","codemirrorMode":"javascript","codemirrorMimeType":"application/typescript","parsers":["typescript","babel-ts"],"vscodeLanguageIds":["typescript"]},{"linguistLanguageId":94901924,"name":"TSX","type":"programming","color":"#3178c6","group":"TypeScript","extensions":[".tsx"],"tmScope":"source.tsx","aceMode":"javascript","codemirrorMode":"jsx","codemirrorMimeType":"text/jsx","parsers":["typescript","babel-ts"],"vscodeLanguageIds":["typescriptreact"]},{"linguistLanguageId":174,"name":"JSON.stringify","type":"data","color":"#292929","tmScope":"source.json","aceMode":"json","codemirrorMode":"javascript","codemirrorMimeType":"application/json","aliases":["geojson","jsonl","topojson"],"extensions":[".importmap"],"filenames":["package.json","package-lock.json","composer.json"],"parsers":["json-stringify"],"vscodeLanguageIds":["json"]},{"linguistLanguageId":174,"name":"JSON","type":"data","color":"#292929","tmScope":"source.json","aceMode":"json","codemirrorMode":"javascript","codemirrorMimeType":"application/json","aliases":["geojson","jsonl","topojson"],"extensions":[".json",".4DForm",".4DProject",".avsc",".geojson",".gltf",".har",".ice",".JSON-tmLanguage",".mcmeta",".tfstate",".tfstate.backup",".topojson",".webapp",".webmanifest",".yy",".yyp"],"filenames":[".all-contributorsrc",".arcconfig",".auto-changelog",".c8rc",".htmlhintrc",".imgbotconfig",".nycrc",".tern-config",".tern-project",".watchmanconfig","Pipfile.lock","composer.lock","flake.lock","mcmod.info"],"parsers":["json"],"vscodeLanguageIds":["json"]},{"linguistLanguageId":423,"name":"JSON with Comments","type":"data","color":"#292929","group":"JSON","tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"text/javascript","aliases":["jsonc"],"extensions":[".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"],"filenames":[".babelrc",".devcontainer.json",".eslintrc.json",".jscsrc",".jshintrc",".jslintrc",".swcrc","api-extractor.json","devcontainer.json","jsconfig.json","language-configuration.json","tsconfig.json","tslint.json",".eslintrc"],"parsers":["json"],"vscodeLanguageIds":["jsonc"]},{"linguistLanguageId":175,"name":"JSON5","type":"data","color":"#267CB9","extensions":[".json5"],"tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"application/json","parsers":["json5"],"vscodeLanguageIds":["json5"]},{"linguistLanguageId":222,"name":"Markdown","type":"prose","color":"#083fa1","aliases":["md","pandoc"],"aceMode":"markdown","codemirrorMode":"gfm","codemirrorMimeType":"text/x-gfm","wrap":true,"extensions":[".md",".livemd",".markdown",".mdown",".mdwn",".mkd",".mkdn",".mkdown",".ronn",".scd",".workbook"],"filenames":["contents.lr","README"],"tmScope":"text.md","parsers":["markdown"],"vscodeLanguageIds":["markdown"]},{"linguistLanguageId":222,"name":"MDX","type":"prose","color":"#083fa1","aliases":["md","pandoc"],"aceMode":"markdown","codemirrorMode":"gfm","codemirrorMimeType":"text/x-gfm","wrap":true,"extensions":[".mdx"],"filenames":[],"tmScope":"text.md","parsers":["mdx"],"vscodeLanguageIds":["mdx"]},{"linguistLanguageId":407,"name":"YAML","type":"data","color":"#cb171e","tmScope":"source.yaml","aliases":["yml"],"extensions":[".yml",".mir",".reek",".rviz",".sublime-syntax",".syntax",".yaml",".yaml-tmlanguage",".yaml.sed",".yml.mysql"],"filenames":[".clang-format",".clang-tidy",".gemrc","CITATION.cff","glide.lock",".prettierrc",".stylelintrc",".lintstagedrc"],"aceMode":"yaml","codemirrorMode":"yaml","codemirrorMimeType":"text/x-yaml","parsers":["yaml"],"vscodeLanguageIds":["yaml","ansible","home-assistant"]}]

For comparison reasons, the languages array with old version (prettier 2.8.0 and prettier/plugin-ruby 3.2.2)

[{"linguistLanguageId":183,"name":"JavaScript","type":"programming","tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"text/javascript","color":"#f1e05a","aliases":["js","node"],"extensions":[".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"],"filenames":["Jakefile"],"interpreters":["chakra","d8","gjs","js","node","nodejs","qjs","rhino","v8","v8-shell","zx"],"since":"0.0.0","parsers":["babel","acorn","espree","meriyah","babel-flow","babel-ts","flow","typescript"],"vscodeLanguageIds":["javascript","mongo"]},{"linguistLanguageId":183,"name":"Flow","type":"programming","tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"text/javascript","color":"#f1e05a","aliases":[],"extensions":[".js.flow"],"filenames":[],"interpreters":["chakra","d8","gjs","js","node","nodejs","qjs","rhino","v8","v8-shell"],"since":"0.0.0","parsers":["flow","babel-flow"],"vscodeLanguageIds":["javascript"]},{"linguistLanguageId":183,"name":"JSX","type":"programming","tmScope":"source.js.jsx","aceMode":"javascript","codemirrorMode":"jsx","codemirrorMimeType":"text/jsx","extensions":[".jsx"],"since":"0.0.0","parsers":["babel","babel-flow","babel-ts","flow","typescript","espree","meriyah"],"vscodeLanguageIds":["javascriptreact"],"group":"JavaScript"},{"linguistLanguageId":378,"name":"TypeScript","type":"programming","color":"#3178c6","aliases":["ts"],"interpreters":["deno","ts-node"],"extensions":[".ts",".cts",".mts"],"tmScope":"source.ts","aceMode":"typescript","codemirrorMode":"javascript","codemirrorMimeType":"application/typescript","since":"1.4.0","parsers":["typescript","babel-ts"],"vscodeLanguageIds":["typescript"]},{"linguistLanguageId":94901924,"name":"TSX","type":"programming","color":"#3178c6","group":"TypeScript","extensions":[".tsx"],"tmScope":"source.tsx","aceMode":"javascript","codemirrorMode":"jsx","codemirrorMimeType":"text/jsx","since":"1.4.0","parsers":["typescript","babel-ts"],"vscodeLanguageIds":["typescriptreact"]},{"linguistLanguageId":174,"name":"JSON.stringify","type":"data","color":"#292929","tmScope":"source.json","aceMode":"json","codemirrorMode":"javascript","codemirrorMimeType":"application/json","aliases":["geojson","jsonl","topojson"],"extensions":[".importmap"],"filenames":["package.json","package-lock.json","composer.json"],"since":"1.13.0","parsers":["json-stringify"],"vscodeLanguageIds":["json"]},{"linguistLanguageId":174,"name":"JSON","type":"data","color":"#292929","tmScope":"source.json","aceMode":"json","codemirrorMode":"javascript","codemirrorMimeType":"application/json","aliases":["geojson","jsonl","topojson"],"extensions":[".json",".4DForm",".4DProject",".avsc",".geojson",".gltf",".har",".ice",".JSON-tmLanguage",".mcmeta",".tfstate",".tfstate.backup",".topojson",".webapp",".webmanifest",".yy",".yyp"],"filenames":[".arcconfig",".auto-changelog",".c8rc",".htmlhintrc",".imgbotconfig",".nycrc",".tern-config",".tern-project",".watchmanconfig","Pipfile.lock","composer.lock","mcmod.info"],"since":"1.5.0","parsers":["json"],"vscodeLanguageIds":["json"]},{"linguistLanguageId":423,"name":"JSON with Comments","type":"data","color":"#292929","group":"JSON","tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"text/javascript","aliases":["jsonc"],"extensions":[".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"],"filenames":[".babelrc",".devcontainer.json",".eslintrc.json",".jscsrc",".jshintrc",".jslintrc","api-extractor.json","devcontainer.json","jsconfig.json","language-configuration.json","tsconfig.json","tslint.json",".eslintrc",".swcrc"],"since":"1.5.0","parsers":["json"],"vscodeLanguageIds":["jsonc"]},{"linguistLanguageId":175,"name":"JSON5","type":"data","color":"#267CB9","extensions":[".json5"],"tmScope":"source.js","aceMode":"javascript","codemirrorMode":"javascript","codemirrorMimeType":"application/json","since":"1.13.0","parsers":["json5"],"vscodeLanguageIds":["json5"]},{"linguistLanguageId":50,"name":"CSS","type":"markup","tmScope":"source.css","aceMode":"css","codemirrorMode":"css","codemirrorMimeType":"text/css","color":"#563d7c","extensions":[".css",".wxss"],"since":"1.4.0","parsers":["css"],"vscodeLanguageIds":["css"]},{"linguistLanguageId":262764437,"name":"PostCSS","type":"markup","color":"#dc3a0c","tmScope":"source.postcss","group":"CSS","extensions":[".pcss",".postcss"],"aceMode":"text","since":"1.4.0","parsers":["css"],"vscodeLanguageIds":["postcss"]},{"linguistLanguageId":198,"name":"Less","type":"markup","color":"#1d365d","aliases":["less-css"],"extensions":[".less"],"tmScope":"source.css.less","aceMode":"less","codemirrorMode":"css","codemirrorMimeType":"text/css","since":"1.4.0","parsers":["less"],"vscodeLanguageIds":["less"]},{"linguistLanguageId":329,"name":"SCSS","type":"markup","color":"#c6538c","tmScope":"source.css.scss","aceMode":"scss","codemirrorMode":"css","codemirrorMimeType":"text/x-scss","extensions":[".scss"],"since":"1.4.0","parsers":["scss"],"vscodeLanguageIds":["scss"]},{"linguistLanguageId":155,"name":"Handlebars","type":"markup","color":"#f7931e","aliases":["hbs","htmlbars"],"extensions":[".handlebars",".hbs"],"tmScope":"text.html.handlebars","aceMode":"handlebars","since":"2.3.0","parsers":["glimmer"],"vscodeLanguageIds":["handlebars"]},{"linguistLanguageId":139,"name":"GraphQL","type":"data","color":"#e10098","extensions":[".graphql",".gql",".graphqls"],"tmScope":"source.graphql","aceMode":"text","since":"1.5.0","parsers":["graphql"],"vscodeLanguageIds":["graphql"]},{"linguistLanguageId":222,"name":"Markdown","type":"prose","color":"#083fa1","aliases":["pandoc"],"aceMode":"markdown","codemirrorMode":"gfm","codemirrorMimeType":"text/x-gfm","wrap":true,"extensions":[".md",".livemd",".markdown",".mdown",".mdwn",".mkd",".mkdn",".mkdown",".ronn",".scd",".workbook"],"filenames":["contents.lr","README"],"tmScope":"source.gfm","since":"1.8.0","parsers":["markdown"],"vscodeLanguageIds":["markdown"]},{"linguistLanguageId":222,"name":"MDX","type":"prose","color":"#083fa1","aliases":["pandoc"],"aceMode":"markdown","codemirrorMode":"gfm","codemirrorMimeType":"text/x-gfm","wrap":true,"extensions":[".mdx"],"filenames":[],"tmScope":"source.gfm","since":"1.15.0","parsers":["mdx"],"vscodeLanguageIds":["mdx"]},{"linguistLanguageId":146,"name":"Angular","type":"markup","tmScope":"text.html.basic","aceMode":"html","codemirrorMode":"htmlmixed","codemirrorMimeType":"text/html","color":"#e34c26","aliases":["xhtml"],"extensions":[".component.html"],"since":"1.15.0","parsers":["angular"],"vscodeLanguageIds":["html"],"filenames":[]},{"linguistLanguageId":146,"name":"HTML","type":"markup","tmScope":"text.html.basic","aceMode":"html","codemirrorMode":"htmlmixed","codemirrorMimeType":"text/html","color":"#e34c26","aliases":["xhtml"],"extensions":[".html",".hta",".htm",".html.hl",".inc",".xht",".xhtml",".mjml"],"since":"1.15.0","parsers":["html"],"vscodeLanguageIds":["html"]},{"linguistLanguageId":146,"name":"Lightning Web Components","type":"markup","tmScope":"text.html.basic","aceMode":"html","codemirrorMode":"htmlmixed","codemirrorMimeType":"text/html","color":"#e34c26","aliases":["xhtml"],"extensions":[],"since":"1.17.0","parsers":["lwc"],"vscodeLanguageIds":["html"],"filenames":[]},{"linguistLanguageId":391,"name":"Vue","type":"markup","color":"#41b883","extensions":[".vue"],"tmScope":"text.html.vue","aceMode":"html","since":"1.10.0","parsers":["vue"],"vscodeLanguageIds":["vue"]},{"linguistLanguageId":407,"name":"YAML","type":"data","color":"#cb171e","tmScope":"source.yaml","aliases":["yml"],"extensions":[".yml",".mir",".reek",".rviz",".sublime-syntax",".syntax",".yaml",".yaml-tmlanguage",".yaml.sed",".yml.mysql"],"filenames":[".clang-format",".clang-tidy",".gemrc","CITATION.cff","glide.lock",".prettierrc",".stylelintrc",".lintstagedrc"],"aceMode":"yaml","codemirrorMode":"yaml","codemirrorMimeType":"text/x-yaml","since":"1.14.0","parsers":["yaml"],"vscodeLanguageIds":["yaml","ansible","home-assistant"]},{"name":"Ruby","parsers":["ruby"],"extensions":[".arb",".axlsx",".builder",".eye",".fcgi",".gemfile",".gemspec",".god",".jb",".jbuilder",".mspec",".opal",".pluginspec",".podspec",".rabl",".rake",".rb",".rbi",".rbuild",".rbw",".rbx",".ru",".ruby",".thor",".watchr"],"filenames":[".irbrc",".pryrc",".simplecov","Appraisals","Berksfile","Brewfile","Buildfile","Capfile","Cheffile","Dangerfile","Deliverfile","Fastfile","Gemfile","Guardfile","Jarfile","Mavenfile","Podfile","Puppetfile","Rakefile","Snapfile","Thorfile","Vagabondfile","Vagrantfile","buildfile"],"interpreters":["jruby","macruby","rake","rbx","ruby"],"linguistLanguageId":326,"vscodeLanguageIds":["ruby"]},{"name":"RBS","parsers":["rbs"],"extensions":[".rbs"]},{"name":"HAML","parsers":["haml"],"extensions":[".haml"],"vscodeLanguageIds":["haml"]}]

tklepzig avatar Sep 12 '23 11:09 tklepzig

Seems bug of prettier or we may have to use another method to get languageIds that contributed by prettier plugins.

chemzqm avatar Sep 13 '23 05:09 chemzqm

@tklepzig you should specify the file parser in prettier config such .prettierrc:

{
  ...
  "overrides": [
    {
      "files": "*.ruby",
      "options": {
        "parser": "ruby"
      }
    }
  ],
  ...
}

xusiyuan841028 avatar Dec 12 '23 17:12 xusiyuan841028

@xusiyuan841028's workaround fixes CocCommand prettier.formatFile for me but format on save still doesn't work—any ideas? The log says:

[handler-format] - Format provider not found for file

ekmartin avatar Jan 03 '24 22:01 ekmartin