docs icon indicating copy to clipboard operation
docs copied to clipboard

Astro Language Server and Prettier

Open matadaniel opened this issue 10 months ago β€’ 4 comments

πŸ“š Subject area/topic

Astro language server

πŸ“‹ Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/editor-setup/

πŸ“‹ Description of content that is out-of-date or incorrect

Prettier is a popular formatter for JavaScript, HTML, CSS, and more. If you’re using the Astro VS Code Extension or the Astro language server within another editor, code formatting with Prettier is included.

This contradicts https://github.com/withastro/language-tools/tree/df90fe5f79978b567387cc1b0cedcc23a43bd156/packages/language-server#formatting-does-not-work.

I was not able to format Astro files in Neovim until I installed prettier and prettier-plugin-astro as dependencies.

πŸ–₯️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

matadaniel avatar Mar 27 '25 21:03 matadaniel

I think this depends on how you set up Neovim. For example, if you're using LazyVim, I believe if you install prettier extra, it would ensure that prettier is installed via mason. That said, I always feel it's best to explicitly include dev dependencies such as prettier and biome (both of which can format .astro files), so you can be sure to use the exact version for that specific project.

gsong avatar Apr 01 '25 23:04 gsong

I agree that it is best to explicitly add them as dependencies. The page content makes it sound like formatting could be provided by the language server before prettier is installed. I think it worked that way in VS Code.

matadaniel avatar Apr 15 '25 01:04 matadaniel

Related, I just came across this article: https://cosmicthemes.com/blog/astro-eslint-prettier-setup/

I would love for our advice to be better, but we do have to make sure that we account for everyone's setup in whatever advice we give (or at least don't make something sound like it should work for everyone, when it won't work in certain situations).

I really appreciate everyone sharing here, and would like to know what people think is the right advice to give in docs!

sarah11918 avatar Apr 16 '25 11:04 sarah11918

would like to know what people think is the right advice to give in docs!

I personally think it's best to explicitly include prettier and plugin friends in package.json, this way for everyone working on the project they'll be using the same version of dependencies: https://github.com/gsong/personal-site/blob/main/package.json#L54-L56

Coupled with an explicit prettier config in whatever flavor you choose: https://github.com/gsong/personal-site/blob/main/.prettierrc.mjs

gsong avatar Apr 16 '25 14:04 gsong