roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Remove unused automatically-added using directives on save

Open sharwell opened this issue 3 years ago • 9 comments

Summary

Add a new default behavior to automatically remove any unused using directives on save which were implicitly added to the document by another feature (e.g. add usings on paste, completion for unimported types).

:link: Visual Studio 2022 keeps adding unnecessary using statements (AB#1697866) :link: C# editor adding unwanted using (AB#1747136) :link: Why7 is VS inserting using directives??? (AB#1755796) :link: Visual Studios is inserting Directives that break my program! (AB#1762096) :link: VS Innsist on using not needed namespace (AB#1792812) 🔗 Why is Visual Studios still inserting Using statements I do not want! (AB#1798265)

Background and Motivation

Sometimes users unintentionally complete items that result in using directives being added, or change course while developing such that the automatically-added using directive is no longer needed. There is no mechanism is place to automatically clean these up.

Proposed Feature

The new feature would keep track of implicitly-added using directives for an unsaved document. When the document is saved, any of these directives which are no longer in use will be automatically removed.

Manually added using directives will not be altered by this feature, even in cases where they are not used. This includes indirectly added using directives from features like the Add Using Directive code fix.

Alternative Designs

Code Cleanup on Save is intended to fulfill this need, but is too aggressive for many users and too slow for others.

sharwell avatar Feb 15 '23 17:02 sharwell

Don't forget that there's a bug where if you paste code that references a type that's not defined in the project, instead of not putting any using for that unknown type (correct), it puts a using for System.Numerics or other random one (incorrect). So in this case, instead of removing the wrong one after the fact, just don't put it in.

Dean-NC avatar Apr 11 '23 17:04 Dean-NC

@Dean-NC can you give an example (or report a new bug that demonstrates the issue)?

CyrusNajmabadi avatar Apr 11 '23 17:04 CyrusNajmabadi

@Dean-NC That's a very different bug. I'll see if I can find a reference.

Edit: It's here: https://github.com/dotnet/roslyn/issues/59499#issuecomment-1042013125

sharwell avatar Apr 11 '23 18:04 sharwell

@CyrusNajmabadi @sharwell I've been providing details about the paste bug in the following VS bug report that was closed as being a duplicate of this Roslyn issue. https://developercommunity.visualstudio.com/t/Visual-Studio-2022-keeps-adding-unnecess/10226615 I didn't create that report, but I provided most of the detail, including the animated .Gif toward the end that shows the bug I've mentioned. I didn't know when I used that bug report that there were different bugs. It was the only report about erroneous usings I could find at the time. I tried to let the support person in that report know what I found, but they went dark.

I posted here because I assumed my issue was being lumped into this one, so it wasn't off-topic. Thanks for the correct Roslyn issue link.

Dean-NC avatar Apr 12 '23 12:04 Dean-NC

@Dean-NC It's no problem at all. I only hid the comments because this issue is being watched by a bunch of people and I don't want to confuse things for them.

sharwell avatar Apr 12 '23 14:04 sharwell

It would be best if this was implemented with an option to disable ALL "implicit" auto insertion of usings. They should only be added when I explicitly want them to be. (And I can select the one I want if there are ambiguities). I don't want the IDE ever trying to guess my intent - I know it better (No matter how much AI you put in to things, you can't read my mind) I get a TON of these all the time and it is REALLY annoying to have to constantly remove things. (I just got this one " using static System.Runtime.InteropServices.JavaScript.JSType;" But my code has NOTHING at all to do with that, it's not even referenced anywhere. I have no idea what that is or where things like that keep coming from. I have as much auto insert xyz turned off as I possibly can. IntelliSense doesn't pop up unless I ask it to. So, I'm confused by claims this is a result of "completing" something unintentionally.

NOTE: removing them on save as this title suggests is WRONG. They should not be added in the first place. Trying to fix it by cleaning up the mess you made after the fact isn't the right answer. Not putting it there in the first place is the correct answer.

smaillet-ms avatar Feb 19 '24 01:02 smaillet-ms

This has become a major issue for me recently. I'm on 17.9.0 and I'm thinking it may have happened last update, but I was seeing some of this prior as well. It's not prolific for me. I don't know if it's compiling or working on the code but I'll have files that all of a sudden have references to projects that are not in use for this project. Things like "EventKitUI" or "SharedWithYouCore", along with Android popping up (when it's a disabled profile in my project file).

This may have something to do with the new .net Maui library I created in my project, too. I also only have iOS enabled for a profile on there. Oddly, I can't even create a reference from that project in my app (and a dependency does exist).

But, having all of these odd references are causing a big headache for me in that I have to keep deleting them from the source files.

Oh one more thing, these source files are not even opened in VS when the references are being added.

sej69 avatar Feb 23 '24 19:02 sej69

@smaillet-ms You can turn this off with:

image

and

image

CyrusNajmabadi avatar Feb 24 '24 06:02 CyrusNajmabadi

Oh one more thing, these source files are not even opened in VS when the references are being added.

@sej69 This is not very unlikely to be roslyn. Perhaps some other tool or extension you're using.

CyrusNajmabadi avatar Feb 24 '24 06:02 CyrusNajmabadi

Just to add my comments here, Maui is doing this in my new project aslo. I have been manaully removing these weird using statements that seem to randomly appear when compiling or after compiling or who knows when exactly.. for example this one just popped up: using Intents; and this: using static UIKit.UIGestureRecognizer; and this: using Microsoft.Maui.Controls.Compatibility.Platform.iOS;

just randomly get added to classes?

and today this one: using Java.Net; Just randomly appeared, and breaks the compile.. - project is a maui project. just trying to run it on the PC as a PC app and this happened...

RobDaytona avatar Apr 12 '24 12:04 RobDaytona

today it randomly added this to into a random class's usings: using MetalPerformanceShaders;

RobDaytona avatar Apr 17 '24 09:04 RobDaytona

@RobDaytona see my message above for how to disable these features if you do not want them.

If that does not help, then what you are experiencing is happening from some other tool, not roslyn.

CyrusNajmabadi avatar Apr 17 '24 14:04 CyrusNajmabadi

Thanks for that @CyrusNajmabadi

RobDaytona avatar Apr 18 '24 11:04 RobDaytona