withastro / prettier-plugin-astro

Prettier plugin for Astro
Other
475 stars 33 forks source link

🐛 BUG: Prettier not formatting on save in VSCODE using pnpm #381

Closed virketrang closed 10 months ago

virketrang commented 10 months ago

Describe the Bug

I have been trying for a couple of hours to get prettier to format my .astro files on save. I have tried multiple online solutions in vain.

I have the following setup in my .vscode/settings.json file: { "prettier.useEditorConfig": false, "prettier.configPath": ".prettierrc", "editor.formatOnSave": true, "editor.defaultFormatter": "esbenp.prettier-vscode", "prettier.documentSelectors": ["**/*.astro"], "[astro]": { "editor.defaultFormatter": "astro-build.astro-vscode", "editor.formatOnSave": true } }

I've also tried having the editor.defaultFormatter under [astro] as "esbenp.prettier-vscode", but to no avail.

I have the following .prettierrc: { "singleQuote": true, "printWidth": 120, "tabWidth": 4, "arrowParens": "always", "bracketSpacing": true, "quoteProps": "consistent", "bracketSameLine": false, "jsxSingleQuote": false, "semi": false, "trailingComma": "none", "astroAllowShorthand": true, "overrides": [ { "files": "*.astro", "options": { "parser": "astro" } } ], "pluginSearchDirs": ["."], "plugins": ["prettier-plugin-astro"] }

I've tried having my prettier config in the settings.json file inside .vscode and inside my package.json. I've also tried using a prettier.config.cjs.

I have installed prettier and the prettier-plugin-astro package as a devDependency. I have the astro VSCode extention installed.

I'm in a pnpm monorepo. I suspect it has something to do with pnpm.

I'll try to single out the problem.

Cheers to anyone, whos willing to help :)

Steps to Reproduce

  1. Create an astro project using pnpm
  2. Configure .prettierrc and settings.json as shown above
  3. Install prettier, prettier-plugin-astro and the Astro VSCode extension.
  4. Prettier shows no error message, it simply states Using ignore file (if present) at .prettierrc
virketrang commented 10 months ago

Just to clarify. I've also tried using the setup showed in the documentation, however it gives the following error message: { "ignored": false, "inferredParser": null } ["WARN" - 18.18.01] Parser not inferred, trying VS Code language. ["ERROR" - 18.18.01] Failed to resolve a parser, skipping file. If you registered a custom file extension, be sure to configure the parser.

virketrang commented 10 months ago

I have found a working solution:

.prettierrc.js: /** @type {import("prettier").Config} */ module.exports = { plugins: ['prettier-plugin-astro'], singleQuote: true, printWidth: 120, tabWidth: 4, arrowParens: 'always', bracketSpacing: true, quoteProps: 'consistent', bracketSameLine: false, jsxSingleQuote: false, semi: false, trailingComma: 'none', overrides: [ { files: '*.astro', options: { parser: 'astro' } } ] }

"editor.formatOnSave": true, "prettier.configPath": "./.prettierrc.js", "editor.defaultFormatter": "esbenp.prettier-vscode", "prettier.documentSelectors": ["**/*.astro"], "[astro]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }

I'm still in a pnpm workspace btw.

I have the prettier-plugin-astro installed at the root of the workspace using pnpm i -D prettier-plugin-astro -w

Hope it saves someone from spending a couple of hours on the issues as I have :)

I'm going to close the issue for now.

Odonno commented 7 months ago

Just to clarify. I've also tried using the setup showed in the documentation, however it gives the following error message: { "ignored": false, "inferredParser": null } ["WARN" - 18.18.01] Parser not inferred, trying VS Code language. ["ERROR" - 18.18.01] Failed to resolve a parser, skipping file. If you registered a custom file extension, be sure to configure the parser.

Currently have this issue with Bun. I suppose this an issue related to the package manager because I set everything exactly as expected...