Skip to content

cmd/go: go mod tidy does not remove useless tools #71819

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

Closed
dozer1412131 opened this issue Feb 18, 2025 · 8 comments
Closed

cmd/go: go mod tidy does not remove useless tools #71819

dozer1412131 opened this issue Feb 18, 2025 · 8 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dozer1412131
Copy link

dozer1412131 commented Feb 18, 2025

Go version

go1.24

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/dozer/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/dozer/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build318528478=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/dozer/go/pkg/mod'
GOOS='linux'
GOPATH='/home/dozer/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/snap/go/10853'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/dozer/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/snap/go/10853/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

Introduction

go1.24 has a great feature - go tool

this looks so cool. Add a library as tool and reuse it.

but what if i add a library as tool, then use it, and then change this library on another library

so, the library 1 is not used anymore in project


In case of simple go.mod dependency - this is not a problem. Just run go mod tidy and all useless libraries should remove from it

but, if i have useless tool - go mod tidy does not react.

What did you do?

install library

go install -tool github.com/dmarkham/enumer

then use it in file

//go:generate go tool github.com/dmarkham/enumer -type=Pill
package gotool

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

then remove go:generate line, so the project does not use enumer now

then go mod tidy

What did you see happen?

tool is still in project, with all its dependencies

What did you expect to see?

if tool is usable through the project - it locates in go.mod and go mod tidy does not remove it

if tool is not usable - go mod tidy must remove it like as other simple dependencies

@mknyszek
Copy link
Contributor

CC @matloob @samthanawalla

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 18, 2025
@mknyszek mknyszek added this to the Backlog milestone Feb 18, 2025
@mknyszek mknyszek changed the title go mod tidy does not remove useless tools cmd/go: go mod tidy does not remove useless tools Feb 18, 2025
@seankhliao
Copy link
Member

I believe this is working as intended: tools may be invoked manually without any other references to it in code. We do not want to remove tools that are used.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2025
@dozer1412131
Copy link
Author

We do not want to remove tools that are used.

tools that are used - yes


but how to manage not used ones?

@seankhliao
Copy link
Member

you remove them from go.mod directly, or remove them via go commands like any other dependency: go get -tool my.tool@none. https://go.dev/ref/mod#go-get

@dozer1412131
Copy link
Author

yes, correct, but this look like not a go way


For example: in php dependencies can be installed and removed ONLY MANUALLY

it does not have same useful command like go mod tidy

so, php has a big problem with useless libraries in projects. And there is a fix solutions around it https://github.com/composer-unused/composer-unused


golang run its own really simple way with dependencies

why does it works with dependencies, but does not work with tool?

@dozer1412131
Copy link
Author

why golang try to simplify developer experience and life as much as possible

but in this case - "no. Suffer!"

it is strange.


go mod tidy allow me to not manage dependencies. It guarantee that I will use only what I need in the project

on the other side - if I forgot to remove tools - they still be in project. No more simple dependency managements. Just need to manually remove.

But why?) If you already show that there is a simple way

@matloob
Copy link
Contributor

matloob commented Feb 19, 2025

As @seankhliao mentioned, the tool directive is not meant to only be used for //go:generate lines, so tools that are used may not appear in any go source files at all. The tool directive is the way users tell the go command that the tool is needed.

Think of the tool directive as adding an import on a library. go mod tidy doesn't remove imports in your files, it removes direct requirements if there are no imports. Similarly, go mod tidy won't remove tool directives.

@dozer1412131
Copy link
Author

got u


actually, why I ask this?

because, for example - I'm working with hundreds of microservices

some of them use tools, other - dont

it looks very useful to add tools in services where they need.

but - programmer is always lazy. I not expect that they always keep tracking use they tool in service or not

so, human can add it once, and forget to remove

and some instruments that help him remove this mistake seems like a pleasure


easier to remove - easier to use


but okay(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants