11

On a certain solution the "Remove and Sort Usings" option does not work. It sorts the Usings correctly, but does not remove any unnecessary ones. The IDE is flagging the unused ones correctly, but it can't seem to remove them. No errors or messages are displayed anywhere, it sorts and behaves like it has completed successfully, but the unnecessary Usings remain and are still flagged by Intellisense as such.

I have verified that in a different solution it does work, so it is not my VS2019 install. And I don't have any plugins installed (like Resharper) that could conflict. Have also tried clean and rebuild in case it needed an error-free compilation to work with.

Any ideas what could be different/special about a certain solution that would prevent the remove functionality?

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
KrustyGString
  • 903
  • 2
  • 13
  • 32

4 Answers4

9

I have found another thing which causes this issue.

For me, I discovered it was only happening in 1 particular project in a solution.

After comparing the non-working csproj to a working one, I determined that the difference was the Warning Level.

The non-working one was set to 1.

Resetting this to the default 4 allows the Remove and Sort Usings function to work as expected.

Shevek
  • 3,869
  • 5
  • 43
  • 63
  • In my case, I had the non-working project with the default value 5. Once I changed it to 4, the remove and sort usings started working, so I immediately changed the value back to the default (5) and tested that the remove and sort usings function continued to work. It did. Strange. – jarmanso7 Jul 30 '21 at 00:26
6

I found the cause, and it's an annoying one!

There is a custom ruleset specified for static analysis of the projects, and that had both CS8019 and IDE0065 (Unnecessary using directive) unselected.

On selecting these the remove unused usings command worked again.

Thanks to other answers for suggestions.

KrustyGString
  • 903
  • 2
  • 13
  • 32
  • 1
    Glad to know that your issue is solved by yourself, since you have the workaround, I suggest you could mark your answer so that it will help other community members search and handle similar issues. Anyway, have a nice day! – LoLance Mar 31 '20 at 09:13
  • Where are those settings located? In my case, the answer from Shevek was the one that worked for me. – CesarD May 27 '20 at 18:38
  • Thanks for sharing this. Small correction, however. IDE0065 pertains to the placement of usings inside or outside the namespace. I did not find it to be relevant to this problem (IDE0005 is the rule for unnecessary usings). CS8019 was the issue for me. I had originally set the reporting severity for this to "None" when overhauling my analyzer use for editorconfig, which is what broke the usings removal. I set it to none to eliminate reports against generated files (migration and designer). Trying "Silent" was the answer. It fixed using removal, and stopped the complaints on generated files. – osoviejo May 04 '21 at 21:30
  • 1
    This helped me, thanks! – classicSchmosby98 Dec 21 '21 at 23:57
2

Any ideas what could be different/special about a certain solution that would prevent the remove functionality?

This is quite a strange behavior. You could try these suggestions:

1) close VS Instance, delete .vs hidden folder, bin, obj folder.

2) clean vs component caches under C:\Users\xxx\AppData\Local\Microsoft\VisualStudio\16.0_xxxx(every this folder)\ComponentModelCache

3) use devenv /safemode to start VS IDE, open your project and then test again.

4) If your project is an old project which means that the project structure is a bit different from VS2019, please try to create a new vs2019 project and then migrate its content into the new project.

In addition, if these do not work, you can try this link's function to run Code Cleanup command with remove unused usings.

jarmanso7
  • 117
  • 1
  • 1
  • 8
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thanks Lance. Had already tried deleting .vs folder, will add that to question. Now also tried clearing all bin/obj folders, deleting ComponentModelCache, and SafeMode. Still no joy. What code cleanup command do you mean, think link is missing. I have run the "Analyze and Code Cleanup" -> "Run Code Cleanup" from right click on project. It sorts usings but doesn't remove unused. I'm trying to manually recreate project now, will be a slog as solution has 95 of them, but if it works at least there is a workaround. – KrustyGString Mar 30 '20 at 11:57
2

I had the same problem and figured out what was wrong. In your Tools|Options|Text Editor|C#|Advanced look for theses settings:

enter image description here

HTH.

Victor Ian
  • 1,034
  • 13
  • 26