zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
46.98k stars 2.7k forks source link

"Organize Imports" code action for TypeScript does not delete unused imports #11017

Open micthiesen opened 4 months ago

micthiesen commented 4 months ago

Check for existing issues

Describe the bug / provide steps to reproduce it

The code action "source.organizeImports" is supposed to both sort imports and also remove unused imports. Compared to the "source.sortImports" code action which only sorts imports. See here.

To reproduce, create a TS file and import something that isn't used. Select "Organize Imports". The unused import is not removed.

Environment

Zed: v0.132.2 (Zed) OS: macOS 14.1.2 Memory: 32 GiB Architecture: aarch64

Also: "typescript": "^5.3.3"

If applicable, add mockups / screenshots to help explain present your vision of the feature

When chosen from the context menu, "Organize Imports" successfully sorts imports but it doesn't remove unused imports: Capture 2024-04-25 at 16 21 03

It also only sorts when used as a "code_actions_on_format" option: Capture 2024-04-25 at 16 23 36

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

No response

micthiesen commented 4 months ago

I can get around this by enabling the "source.removeUnusedImports" code action on format:

Capture 2024-04-25 at 16 41 31

But I think there's a race condition between all the code actions that causes the imports to duplicate on save:

Capture 2024-04-25 at 16 41 06

AlbertoLanaro commented 3 months ago

@micthiesen did you find any solution to the duplicated imports problem?

micthiesen commented 3 months ago

@micthiesen did you find any solution to the duplicated imports problem?

No I'm back to VSCode 😭

GustavoMelloGit commented 2 months ago

Having the exact same problem and it is making me want to go back to vscode 😭 EDIT: now it started working, but it is adding eslint disable every time it needs to format, terrible experience

{
  "theme": "Ayu Mirage",
  "ui_font_size": 13,
  "buffer_font_size": 13,
  "format_on_save": "on",
  "languages": {
    "TypeScript": {
      "code_actions_on_format": {
        "source.fixAll.eslint": true,
        "source.organizeImports": true,
        "source.removeUnusedImports": true
      }
    },
    "TSX": {
      "code_actions_on_format": {
        "source.fixAll.eslint": true,
        "source.organizeImports": true,
        "source.removeUnusedImports": true
      }
    }
  },
  "formatter": {
    "code_actions": {
      "source.fixAll.eslint": true,
      "source.organizeImports": true,
      "source.removeUnusedImports": true
    }
  }
}
thraizz commented 1 month ago

For me just this works as a local .zed/settings.json in a react/tsx project:

{
  "format_on_save": "on",
  "code_actions_on_format": {
    "source.removeUnusedImports": true,
    "source.addMissingImports": true,
    "source.fixAll.eslint": true,
    "sourceAll.organizeImports": true
  }
}

EDIT: Nevermind this adds e.g. // eslint-disable-next-line react/jsx-no-undef on Zed Preview 0.145.1

eulke commented 1 month ago

The comments added might be related to this It should be fixed in the tomorrow preview release

respectTheCode commented 2 weeks ago

Any updates on this? I am trying to make the switch to zed but this is kind of a deal breaker.