IanVS / prettier-plugin-sort-imports

An opinionated but flexible prettier plugin to sort import statements
Apache License 2.0
951 stars 21 forks source link

Imports not getting sorted #106

Closed WouterrV closed 1 year ago

WouterrV commented 1 year ago

Your Environment

Describe the bug

When saving a file, Prettier formats my tsx/jsx files just fine, but I haven't found a file yet where the imports (often 10-20 lines of completely unsorted imports) are being touched. I restarted vs code a few times. the @ianvs/prettier-plugin-sort-imports folder is present in node_modules.

How do I verify that Prettier has loaded the plugin? Or is there some kind of setup step that I'm missing?

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)


["INFO" - 1:16:14 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 1:16:14 PM] Prettier Options:
{
  "filepath": "c:\\Users\\(.redacted..)\\src\\modules\\\drawer.jsx",
  "parser": "babel",
  "singleQuote": true,
  "trailingComma": "es5",
  "printWidth": 100,
  "tabWidth": 2,
  "useTabs": false,
  "importOrderParserPlugins": [
    "typescript",
    "jsx",
    "decorators-legacy"
  ]
}
["INFO" - 1:16:15 PM] Formatting completed in 43ms.

Error log

Contribute to @ianvs/prettier-plugin-sort-imports

IanVS commented 1 year ago

I'd suggest starting off by running prettier manually on the cli, to verify whether this is a plugin issue or vscode issue. Also try restarting vscode if you haven't already. One other thing you can try is to add it explicitly to your config, though that should not be necessary:

// prettier.config.cjs
module.exports = [
    // ...existing config
    plugins: [require.resolve("@ianvs/prettier-plugin-sort-imports")]
]
WouterrV commented 1 year ago

Thanks for the fast reply!!

That solved the issue - after explicitly loading the plugin, I got this message in the Prettier output in VScode: Error: Cannot find module 'prettier/parser-babel'

Which makes sense, because I haven't installed prettier in the repo, it's installed somewhere by VScode.

After doing npm i prettier --force --legacy-peer-deps (our repo has some technical debt which I'm in the process of solving, which I gues is why this problem occurred in the first place, ignoring peer deps), the sorting is happening wonderfully.

IanVS commented 1 year ago

Ah, yes, prettier does need to be installed for the plugin to work. It's listed as a peerDependency of this package.

Glad you were able to figure it out!