• Edited

The "Organize imports" feature generally works well, but I have an annoying issue with it:
When I temporarily comment out some code with usage of some classes - it removes some imports on the file save event, because they become unused.
And when I uncomment this code back - the imports become missing, so I have to manually recover them, that's no good.

VS Code has a "Format On Save" option, that usually helps resolve this problem, but the extension still removes unused imports on save.

So, to resolve this issue, could you please disable removing imports if the "Format On Save" option is disabled?

    Murz This sounds like you have enabled the "codeActionsOnSave":

        "editor.codeActionsOnSave": {
            "source.organizeImports": "always"
        },

      I have:

      "editor.codeActionsOnSave": {
          "source.organizeImports": "explicit"
        },

      and removing this disables the feature in all modes.
      So, what is the right value to make it work only when formatting is enabled?

        5 months later

        Any news about this issue? Very annoying to lose all imports during temporary commenting some code blocks.

          Murz Hi, and sorry I've missed this one.

          There is a similar discussion with TypeScript: https://github.com/microsoft/TypeScript/issues/36085

          In short, we should introduce two other code actions: "source.sortImports", and "source.sortAndRemoveImports". When using "source.organizeImports", the behavior is defined very vaguely (but I agree that by default we should not remove unused uses).

          I'll prepare some improvements and let you know.

            5 days later

            Won't be removing uses in the next update!

              Write a Reply...