Refactor / Remove Usings

Issue #26 resolved
Svante Seleborg
created an issue

When Sort & Remove usings is enabled, and run at document save is enabled and for example the 'rename' refactoring is used things get broken.

Apparently the cleanup is run in the middle of the refactoring operation, causing the end-result to be missing it's appropriate usings.

Comments (4)

  1. Steve Cadwallader repo owner
    • changed status to open

    I have noticed this issue as well, and there are some other circumstances under which it can occur. If the code is in an invalid state (i.e. will not compile) then Visual Studio may remove using statements that are needed. Also if there are preprocessor directives defined but not active, and the associated using statements are not also bracketed in the same preprocessor directives, those using statements may get removed. These same issues will occur if you invoke the command directly or if CodeMaid invokes it for you.

    I have looked into detecting the renaming scenario but there is no information published from the API that it is occurring that I could find. Unfortunately that leaves me with little I can do to work around the native issues within Visual Studio's remove unused using statements command.

    If you are seeing this issue often and it is causing problems, you can either disabe the auto-save or the remove-usings option as a trade-off for losing that functionality. I wish I had a better alternative, but for now I'm just hoping for an improvement to the command in the next version of Visual Studio. I believe it is already reported on Microsoft Connect, but I can't recall for certain.

    Hope it helps, -Steve Cadwallader

  2. Svante Seleborg reporter

    I was afraid it was something like that. How about not doing the 'remove' part of the using handling when doing save, since it can actually break things?

    It would work as normal when doing 'clean up'. That would facilitate usage, reduce the risk of breaking code at the expense of a slightly more complex behavior. It could be mitigated by adding an explanatory text to the 'remove usings' options, i.e: 'Remove Usings (Only at Manual CleanUp, not during Save)' or something like that.



  3. Steve Cadwallader repo owner

    (Reply via

    Hi Svante -

    That's a very good suggestion, thanks for recommending it. I'll plan on doing that and probably extending it by providing an opt-in (defaulted off) option to remove usings during auto-save with an additional warning message.

    Thanks for the help :) -Steve Cadwallader

  4. Steve Cadwallader repo owner

    Hi Svante -

    The new v0.3.8 ALPHA release includes this configurable option defaulted to off with a warning message if users enable the behavior that it can cause issues under some circumstances.

    Thanks again for your input, -Steve Cadwallader

  5. Log in to comment