Skip to content

Use gopls LSP server to (slowly) find project-wide unused code

Notifications You must be signed in to change notification settings

SenorWoolyLLC/gopls-unused

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Find Project-Wide Unused Code Using Golang's LSP

For the last year or so (as of 2023) Golang has only had one active project for linting unused code, namely: unused from https://github.com/dominikh/go-tools. It works really well, but only within a package, not across packages, like within a traditional monolith. unused used to be part of another project called staticcheck, that did indeed have a flag for detecting project-wide unused code, but that is no longer supported. There are good reasons for that (see this Github discussion), mainly that it's computationally expensive.

So, as far as I know, there is no single tool to help with finding project-wide unused code, no matter how slow it might be. Hence this repo. It crudely piggybacks off of the official Golang LSP server by searching for publicly exported symbols then searching for references to those symbols. It is slow, but it works.

Usage

You will first need BASH and gopls.

To run, clone the repo and run ./gopls-unused.bash find_all path/to/golang/code.

There are some more find-grained commands:

./gopls-unused.bash find_all_in_file path/to/golang/file.go
./gopls-unused.bash find_symbol_references_in_project path/to/symbol/file.go:1:1

An ignore regex flag, eg: IGNORE_REGEX='.*gen\.go' ./gopls-unused.bash find_all path/to/golang/code.

An CWD flag, eg: CWD=path/to/golang/code ./gopls-unused.bash find_all.

And a debugging flag, eg: DEBUG=1 ./gopls-unused.bash find_all path/to/golang/code.

About

Use gopls LSP server to (slowly) find project-wide unused code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published