Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused imports on save #68556

Open
arunchndr opened this issue Jun 12, 2023 · 0 comments
Open

Remove unused imports on save #68556

arunchndr opened this issue Jun 12, 2023 · 0 comments
Assignees
Labels
Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings VSCode
Milestone

Comments

@arunchndr
Copy link
Member

Describe the feature you'd like
I'd like to propose adding an option to remove unused imports/usings in open files automatically on save. I would even advocate for making it part of the default behavior for the source.organizeImports code action, as it is for many of the other major language extensions/LSPs - JavaScript/TypeScript, Java, Go, to name a few that I'm sure of - but since this has never been part of the standard source.organizeImports1 behavior of the C# extension to my knowledge, even just an off-by-default option in settings.json, e.g. "Remove unused imports when organizing imports", would suffice.

  1. Apparently the C# extension's import organization fix has never actually been hooked up to the source.organizeImports code action as I assumed it was, but rather the Omnisharp-specific omnisharp.organizeImportsOnFormat setting. In any case, it seems that with the transition away from OmniSharp, this option is no longer recognized and source.organizeImports isn't being recognized in its stead, so I can only conclude that the latest version of the C# extension provides no way to have imports organized at all - with or without removing unnecessary ones. I can open a separate issue for this if it would be helpful.

Alternatives considered
While the previous OmniSharp version of the extension did provide a manual way to remove unused imports via a command palette-activated action called "OmniSharp: Fix all occurrences of a code issue" -> "CS8019: Unnecessary removing directive", it has never been possible to have this fix run automatically on save without also enabling a whole host of other on-save code fixes that you wouldn't necessarily want - e.g., generating stub implementations for unimplemented interface members, removing any unreferenced local variables (particularly annoying because this would often wipe out code you were in the middle of writing if you happened to save before using all the variables!) - which impeded the editing experience to the point where it was ultimately less trouble to just leave the setting disabled. (That is, to leave source.fixAll out of your editor.codeActionsOnSave.)

One user in dotnet/vscode-csharp#5261 was apparently able to work around this by finessing his Directory.Build.props and .editorconfig files into just the right configuration so that only the "remove unnecessary imports" fix would get applied by the formatter and none of the other fixes that weren't specified, but I've not been able to reproduce his success despite repeated attempts, and in any case, this approach requires changing your source files to enable documentation comments (seemingly due to an #41640), which isn't always possible or desired.

I threw together an extension several weeks ago which hackily implements this request by circumventing the OmniSharp extension and sending CS8019 fix requests directly to the server in response to the source.organizeImports action, but now that a different language server is in use, that extension no longer works.

Environment Information
No response

@arunchndr arunchndr added Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings VSCode labels Jun 12, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Jun 12, 2023
@arunchndr arunchndr added this to the GA milestone Jul 6, 2023
@arunchndr arunchndr modified the milestones: GA, Post GA Jul 6, 2023
@arunchndr arunchndr removed the untriaged Issues and PRs which have not yet been triaged by a lead label Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings VSCode
Projects
None yet
Development

No branches or pull requests

2 participants