dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.12k stars 4.04k forks source link

Remove unused automatically-added using directives on save #66898

Open sharwell opened 1 year ago

sharwell commented 1 year ago

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.

Dean-NC commented 1 year ago

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.

CyrusNajmabadi commented 1 year ago

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

sharwell commented 1 year ago

@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

Dean-NC commented 1 year ago

@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.

sharwell commented 1 year ago

@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.

smaillet-ms commented 9 months ago

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.

sej69 commented 9 months ago

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.

CyrusNajmabadi commented 9 months ago

@smaillet-ms You can turn this off with:

image

and

image

CyrusNajmabadi commented 9 months ago

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.

RobDaytona commented 7 months ago

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 commented 7 months ago

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

CyrusNajmabadi commented 7 months ago

@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.

RobDaytona commented 7 months ago

Thanks for that @CyrusNajmabadi