Repository navigation
Description
Environments:
- Prettier Version: 2.6.2
- Running Prettier via: Node.js API
- Runtime: 18.1.0
- Operating System: Linux (through WSL)
- Prettier plugins (if any):
prettier-plugin-tailwindcss,prettier-plugin-sort-imports(disclosure: I made this plugin, issue also happens with@trivago/prettier-plugin-sort-importsthough)
Steps to reproduce:
- Install two plugins that both register a preprocessor/parser for the same language
- One of them stops working
Expected behavior:
They both work
Actual behavior:
One of them stops working (probably dependent on alphabetical order)
The issue
I did some digging and the issue is actually pretty fundamental. It seems to be common practice to register prettier plugins that handle existing languages in this way:
module.exports = {
parsers: {
typescript: {
...require('prettier/parser-typescript'),
preprocess: (...args) => {
require('prettier/parser-typescript').parsers.typescript.preprocess(...args);
myCustomPreProcessFunction(...args);
}
}
}
}However, when two prettier plugins add their own preprocessors (or handlers or whatever) for the same language, things break. Because parsers.typescript is re-assigned to a fresh object, and since require('prettier/parser-typescipt') is not modified in-place, the new object won't have the previous plugins' handlers attached. This means that any double (or more) registration of a language just disables all plugins except for one. In the case of prettier-plugin-tailwindcss this is particularly bad because it registers 18 parsers.
This could be fixed by requiring plugin authors to modify require('prettier/parser-typescript') in-place, for example:
const parserTypescript = require('prettier/parser-typescript');
parserTypescript.parsers.typescript.preprocess = (...args) => {
parserTypescript.parsers.typescript.preprocess(...args);
myCustomPreProcessFunction(...args);
}
module.exports = { parsers: {typescript: parserTypescript.parsers.typescript } };But I'd say a plugin system that requires plugins to export a callback might be better here. That way they'll always have access to the "current" parser and don't need to import it themselves.
What are your thoughts on this? Has this been thought about before (I can't imagine nobody has ran into this issue before)? Or am I doing something wrong?
Activity
mzalevski commentedon May 23, 2022
I have the same issue
only the second one works at a time
mheob commentedon Aug 2, 2022
I have the same behavior using
pnpmwith this config:Environments:
Prettier Version: 2.7.1
Running Prettier via: Node.js 18.7.0 / PNPM 7.8.0
Operating System: MacOS
Prettier plugins (if any):
prettier-plugin-jsdoc,@trivago/prettier-plugin-sort-importslucasrabiec commentedon Sep 18, 2022
Same for me with
plugins: ['@trivago/prettier-plugin-sort-imports', 'prettier-plugin-tailwindcss']and only the second one works.markerikson commentedon Oct 20, 2022
Just ran into this myself. There's a great workaround over in a related issue in the
prettier-plugin-tailwindcssrepo. Convert your.prettierrctoprettier.config.js, manually mash the two plugins together, and pass that to Prettier in the config:tailwindlabs/prettier-plugin-tailwindcss#31 (comment)
filipesmedeiros commentedon Nov 6, 2022
@markerikson like I found out in comment tailwindlabs/prettier-plugin-tailwindcss#31 (comment), that solution breaks for pnpm for some reason. Probably module resolution?
Ali-Parandeh commentedon Nov 17, 2022
Same for me for pnpm. Only the second one works
jameswilliamknight commentedon Jan 31, 2023
I am also experiencing this issue.
Setup: Vite, React, TypeScript+SWC
I have two plugins autoloading,
@trivago/prettier-plugin-sort-importswas installed first, thenprettier-plugin-tailwindcss.As soon as I remove trivago, hit save in my file, prettier sorts my classes with the tailwindcss plugin. Reinstall trivago and my imports get sorted, but not my classes.
Autoloading appears to favour only one package based on name sorting.
EDIT: tried the workaround markerikson mentioned and was successful, had to call my config
prettier.config.cjsfor my particular setup.johanbook commentedon May 31, 2023
I am seeing the same issue when using
@trivago/prettier-plugin-sort-importsandprettier-plugin-tailwindcsswith pnpm, however, it seems the first plugin in thepluginsarray is used instead of the second one as others have observed.I am using Eslint 7.32.0.
dhextras commentedon Aug 5, 2024
If you’re still facing issues with Prettier plugins conflicting because they format the same files, here's a workaround:
Configure Prettier: In your
.prettierrc, set up the plugin you want to use for formatting on save. For example, if you want TailwindCSS to be formatted on save, configure it like this:{ "plugins": ["prettier-plugin-tailwindcss"] }Run Prettier with Both Plugins: Add a script to your
package.jsonto handle the formatting with the other plugin:This script runs Prettier twice, which might double the formatting time (though usually just milliseconds for normal projects) and won’t work on save.
Preferred Setup: Use the plugin that’s most important for on-save formatting (like TailwindCSS) in your
.prettierrc. Use the other plugin in yournpm run formatscript to ensure everything is organized before you ship your code.If you know of a better solution, please share!
Disclaimer: Paraphrased by GPT...... cause my grammar sucks LOL.
Jme797 commentedon Feb 17, 2025
I created this workaround for anyone that may still be having this problem https://www.npmjs.com/package/merge-prettier-plugins
sxxov commentedon Jun 21, 2026
hey, i wrote a plugin to run prettier multiple times (in passes):
none of the workarounds here worked universally except running prettier multiple times (as said by #12807 (comment)).
example of what can work together now:
jameswilliamknight commentedon Jun 23, 2026