withastro / language-tools

Language tools for Astro
MIT License
240 stars 45 forks source link

🐛 BUG: Language server not starting properly in non-VSCode editors if prettier or the prettier plugin are missing #838

Closed ChristianIvicevic closed 3 months ago

ChristianIvicevic commented 3 months ago

Describe the Bug

Hey everyone,

this is Christian from the WebStorm team at JetBrains and we have been working on integrating the Astro LS for our most recent version but noticed an unexpected issue close to release that we'd like to raise with you in order to discuss whether you would consider fixing it on your end before we build some unconventional workarounds.

During the startup of the language server it attempts to load prettier and the prettier plugin for the purposes of initializing the Volar services:

https://github.com/withastro/language-tools/blob/6fd9314b22b12a19ec104f19ce4ad5e3856a2ece/packages/language-server/src/languageServerPlugin.ts#L98-L107

However, if no instance of prettier was found then the upstream volar-service-prettier will throw an error and prevent the Astro LS from starting at all. While it is true that this is fixed once the user installs those two peer dependencies (prettier and the plugin) explicitly into their project, this is not necessary when using VSCode. The readme does mention this, but I had the assumption this formatting is optional and not required.

Seemingly, the VSCode Astro plugin is silently (and unconventionally) installing the LS into the node_modules together with prettier and the plugin and thus the LS is able to pick up those dependencies. I came to this conclusion after creating a fresh Astro project that caused the LS to fail in WebStorm and noticed that opening it once in VSCode and then again in WebStorm causes no more issues until I noticed that the node_modules folder has been changed.

I would like to propose to make the detection and inclusion of the prettier service more robust and optional, i.e. making the following function call be handled more gracefully or the implementation check whether the dependencies exist in the first place before attempting to initialize the Volar service. Calling the create function from volar-service-prettier with no instance causes the failure to start.

https://github.com/withastro/language-tools/blob/6fd9314b22b12a19ec104f19ce4ad5e3856a2ece/packages/language-server/src/languageServerPlugin.ts#L55

I'm open for discussions and willing to contribute the necessary changes to handle this use-case more gracefully than before if there are no objections.

Steps to Reproduce

  1. npm init astro
  2. Open the project outside of VSCode in any editor/IDE that supports the language server, but hasn't prettier or prettier-plugin-astro installed
  3. The LS is not starting at all
ChristianIvicevic commented 3 months ago

After a quick conversation with @Princesseuh we noticed that this issue has been fixed last week as part of upstream changes that were done in https://github.com/volarjs/services/pull/84 and then pulled in via https://github.com/withastro/language-tools/pull/823. The problems are no longer occurring in newer versions of the LS.