Skip to content

cmd/go: 'go mod tidy -diff' doesn't accurately indicate the changes that 'go mod tidy' will make #75299

@cespare

Description

@cespare

Go version

go version go1.25.1 linux/amd64

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='/home/caleb/bin'
GOCACHE='/home/caleb/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/caleb/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build186885984=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/tmp/tidy/go.mod'
GOMODCACHE='/home/caleb/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/caleb/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/caleb/3p/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/caleb/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/caleb/3p/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I've noticed that go mod tidy sometimes does formatting cleanup on go.mod which go mod tidy -diff doesn't report. Here's a demo where go mod tidy removes an extra newline, but go mod tidy -diff doesn't know about it:

$ mkdir /tmp/tidy
$ cd /tmp/tidy
$ go mod init example.com/tidy
go: creating new go.mod: module example.com/tidy
$ echo >>go.mod
$ cat go.mod
module example.com/tidy

go 1.25.1

$ cat >hello.go
package main

func main() {}
$ go mod tidy -diff
$ echo $?
0
$ cat go.mod
module example.com/tidy

go 1.25.1

$ go mod tidy
$ cat go.mod
module example.com/tidy

go 1.25.1
$

What did you see happen?

go mod tidy -diff printed nothing.

What did you expect to see?

go mod tidy -diff should print the diff that go mod tidy will apply in all cases.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions