From 65152baa3f7a55b7a57030d7eef1f94640bb9afa Mon Sep 17 00:00:00 2001 From: Remco Smits Date: Thu, 21 Mar 2024 08:23:15 +0100 Subject: [PATCH] Fix `prettier-plugin-organize-imports` plugin removes used imports (#9598) ### Issue So this pull request fixes an issue that was driven me crazy. The issue was that when you use the `prettier-plugin-organize-imports` It would remove some imports that should not be removed before they were used inside the module itself. You can reproduce it with the following `prettierrc.json` config and source code. When you **save** the file, it would remove the `import clsx from "clsx";` import from the file. **Prettier config** ```json { "semi": true, "tabWidth": 4, "trailingComma": "es5", "useTabs": true, "plugins": [ "prettier-plugin-tailwindcss", "prettier-plugin-organize-imports" ] } ``` **Source code** ```typescript import clsx from "clsx"; export default function Home() { return (
{clsx("asdjklasdjlkasd", "asdjlkasjdjlk")}
); } ``` ### Findings After a deep dive with @mrnugget, I was debugging deep down the prettier plugin system and found the issue that was causing this issue. When I was looking inside the `node_modules/prettier-plugin-organize-imports/lib/organize.js`. I saw the following code that looked strange to me because it falls back to `file.ts` if `filepath` is not passed through inside the prettier config options. Screenshot 2024-03-20 at 21 31 46 So the issue was small, if you look at the following code, the `path` key should be `filepath` inside the `crates/prettier/src/prettier_server.js:205` ![Screenshot 2024-03-20 at 21 35 25](https://github.com/zed-industries/zed/assets/62463826/1eea0a88-c886-4632-9c69-9f3095126971) Release Notes: - Fixed prettier integration not using the correct filepath when invoking prettier, which could lead to some prettier plugins failing to format correctly. ([#9496](https://github.com/zed-industries/zed/issues/9496)). --- crates/prettier/src/prettier_server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/prettier/src/prettier_server.js b/crates/prettier/src/prettier_server.js index bbec46dfca..7e4ced773e 100644 --- a/crates/prettier/src/prettier_server.js +++ b/crates/prettier/src/prettier_server.js @@ -202,7 +202,7 @@ async function handleMessage(message, prettier) { ...resolvedConfig, plugins, parser: params.options.parser, - path: params.options.filepath, + filepath: params.options.filepath, }; process.stderr.write( `Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${