cue icon indicating copy to clipboard operation
cue copied to clipboard

cue fmt: remove unused imports

Open vikstrous2 opened this issue 3 years ago • 4 comments

Is your feature request related to a problem? Please describe. Sometimes I refactor cue configs and end up with unused imports.

Describe the solution you'd like I'd like cue fmt to remove them for me.

Describe alternatives you've considered Right now I have to manually remove them after building the config fails.

Additional context I'm pretty lazy :)

vikstrous2 avatar Mar 07 '22 15:03 vikstrous2

I believe such a concern as been raised before (maybe on slack). I also find the current behavior a tad annoying.

PierreR avatar Mar 08 '22 11:03 PierreR

It's worth noting that go fmt does not remove unused imports either and that editor and 3rd party tools implemented this feature.

verdverm avatar Mar 08 '22 20:03 verdverm

That makes sense. I've always used goimports / gopls, not just go fmt.

vikstrous2 avatar Mar 18 '22 21:03 vikstrous2

Perhaps this should be provided as part of the LSP, muc like Go does, since formatting has little to do with removing unused imports or adding missing imports. cc @myitcv

mvdan avatar May 16 '24 20:05 mvdan

Interestingly this is, in theory, fairly easy for the LSP, now that it supports find-references - we can use find-references to detect where an import is used and if it is used nowhere then we can remove it. I'll move this to the LSP project for now, but I'm not ruling out that the cmd line cue fmt (or related) could grow this feature too in the future.

See also https://github.com/cue-lang/cue/issues/3573

cuematthew avatar Nov 10 '25 14:11 cuematthew

I would suggest that we focus on supporting this within the LSP first, i.e. https://github.com/cue-lang/cue/issues/3573, and once that's done we can see if users still want a feature like this outside the LSP.

My intuition is that we should focus on support within the LSP, as it already does the legwork of loading the current module and analyzing its dependencies and package imports. cue fmt does not do any of this; it is a syntax-only tool. So while it could fairly easily remove unused imports, it could not do anything else like adding missing imports, which the LSP should be able to do in most cases.

mvdan avatar Dec 02 '25 16:12 mvdan