prettier-plugin-tailwindcss: vscode prettier formatter doesn't match `npx prettier`

What version of prettier-plugin-tailwindcss are you using?

v0.5.14

What version of Tailwind CSS are you using?

v3.3.0

What version of Node.js are you using?

v20.12

What package manager are you using?

yarn

What operating system are you using?

Mac OS

Describe your issue

I use the classes below:

className="data-[side=top]:animate-test flex"

and in my tailwind.config.ts

const config = {
  theme: {
    extend: {
      animation: {
        test: 'FadeIn 400ms cubic-bezier(0.16, 1, 0.3, 1)',
         }
      }
   }
}

and my .prettierrc.js:

module.exports = {
  plugins: ['prettier-plugin-tailwindcss'],
  tailwindFunctions: ['cn'],
  tailwindConfig: './tailwind.config.ts',
  semi: true,
  trailingComma: 'all',
  singleQuote: true,
  printWidth: 120,
  tabWidth: 2,
};

When i run npx prettier --write it sorts the classes in a different order then when i use the prettier formatter in vscode (option + shift + f)

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 6
  • Comments: 17 (2 by maintainers)

Most upvoted comments

The problem is that the Tailwind CSS config is not found correctly. To fix it use an absolute path:

Example of Prettier config that works

// prettier.config.mjs
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));

export default {
    plugins: ['prettier-plugin-tailwindcss'],
    tailwindConfig: resolve(__dirname, './tailwind/tailwind.config.js'),
    tailwindFunctions: ['clsx', 'tw'],
};

Could this be an issue with OS?

I noted that at my workplace, people with macOS don’t have classes being sorted while others with Windows, it does;

Happens to me on both, macOS & windows

@thecrypticace Here is a minimal reproduction + screen recording using Tailwind v4 alpha:

https://github.com/aaronadamsCA/prettier-plugin-tailwindcss-issue-270

My best guess at what’s happening:

  • CLI formatting detects the Tailwind config at my-app/src/tailwind.css, so it uses the Tailwind v4 sort order.
  • IDE formatting doesn’t detect the Tailwind config, so it falls back to the plugin’s built-in sort order, which is different.

One workaround is cp .prettierrc.json my-app/, but obviously we’d rather not copy/link our Prettier config into every package.

I realize this is v4 and most other reports are v3, but I have a feeling the root cause is ultimately the same—incompatible config file resolution.

I think this has already been fixed in Tailwind 3.4.2 (https://github.com/tailwindlabs/prettier-plugin-tailwindcss/issues/246)

I can reproduce this issue on Tailwind 3.4.3