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

x/tools/gopls: consider adding a setting to control removal of unused imports #54362

Open
findleyr opened this issue Aug 10, 2022 · 4 comments
Open
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@findleyr
Copy link
Contributor

findleyr commented Aug 10, 2022

We see feedback in survey results that users don't want unused imports to be removed by the organizeImports code action. I myself sometimes get annoyed by this behavior: it can be frustrating to manually type out an import path, save, and have the import disappear.

Surprisingly, I don't think we have an open (or closed) request for this feature, unless my search failed me.

I think we should consider a configuration option removeUnusedImports to control the removal of unused imports, and the default should probably be to not to remove unused imports EDIT: I take that back, the default behavior should not change.

@findleyr findleyr added this to the gopls/v0.10.0 milestone Aug 10, 2022
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Aug 10, 2022
@muirdm
Copy link

muirdm commented Aug 10, 2022

When do you need to manually type an import?

This would mean after removing the final use of a package and saving, the import wouldn’t be removed automatically by default? When would the unused imports be cleaned up?

@findleyr findleyr added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 10, 2022
@findleyr findleyr changed the title x/tools/gopls: add a settting to control removal of unused imports x/tools/gopls: consider adding a settting to control removal of unused imports Aug 10, 2022
@findleyr findleyr modified the milestones: gopls/v0.10.0, gopls/later Aug 10, 2022
@findleyr
Copy link
Contributor Author

findleyr commented Aug 10, 2022

When do you need to manually type an import?

I sometimes do this to improve completion results, actually, which may suggest an alternative solution.

This would mean after removing the final use of a package and saving, the import wouldn’t be removed automatically by default? When would the unused imports be cleaned up?

Err yes these are good points. I filed this issue (rather nearsightedly) based on consistent feedback we see that this is a point of confusion / frustration. But of course the alternative has significant downsides, which you point out, thanks. I think we need more research into whether we can mitigate this pain point by other means, such as improving unimported completion. I've removed the conviction in the original post, and removed this from the milestone.

@skelouse
Copy link

skelouse commented Aug 10, 2022

@findleyr When trying to improve autocomplete rather than typing it into the imports type it into go get -u github.com/..... This will add it to the auto-import mechanism.

@adonovan adonovan changed the title x/tools/gopls: consider adding a settting to control removal of unused imports x/tools/gopls: consider adding a setting to control removal of unused imports Aug 17, 2022
@adonovan
Copy link
Member

adonovan commented Aug 17, 2022

An unused import generates a compile error, so we shouldn't leave them in as a convenience. If better completions is the motivation for this feature, we should improve the completion algorithm so that it generates candidates from unimported packages.

The only other case I can think of for wanting this is when temporarily commenting out the last use of an imported package, but usually goimports correctly restores the import when the code us uncommented. In any case, if that's not sufficient, I think the fix should be in goimports, not in suppressing the removal in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants