Reputation: 1003
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?
Upvotes: 13
Views: 4705
Reputation: 1003
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 IDE0005 (Unnecessary using directive) unselected.
On selecting these the remove unused usings command worked again.
Thanks to other answers for suggestions.
Upvotes: 10
Reputation: 28176
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
.
Upvotes: 2
Reputation: 1070
I had the same problem and figured out what was wrong. In your Tools|Options|Text Editor|C#|Advanced
look for theses settings:
HTH.
Upvotes: 2
Reputation: 4063
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.
Upvotes: 11