# https://golang.org/issue/46141: 'go mod tidy' for a Go 1.17 module should by
# default preserve enough checksums for the module to be used by Go 1.16.
#
# We don't have a copy of Go 1.16 handy, but we can simulate it by editing the
# 'go' version in the go.mod file to 1.16, without actually updating the
# requirements to match.

[short] skip

env MODFMT='{{with .Module}}{{.Path}} {{.Version}}{{end}}'


# For this module, Go 1.16 selects the same versions of all explicit dependencies
# as Go 1.17 does. However, Go 1.16 selects a higher version of an *implicit*
# dependency, imported by a test of one of the (external) imported packages.
# As a result, Go 1.16 also needs checksums for the module sources for that higher
# version.
#
# The Go 1.16 module graph looks like:
#
# m ---- lazy v0.1.0 ---- incompatible v1.0.0
#         |
#         + ------------- requireincompatible v0.1.0 ---- incompatible v2.0.0+incompatible
#
# The Go 1.17 module graph is the same except that the dependencies of
# requireincompatible are pruned out (because the module that requires
# it — lazy v0.1.0 — specifies 'go 1.17', and it is not otherwise relevant to
# the main module).

# 'go mod tidy' should by default diagnose the difference in dependencies as an
# error, with useful suggestions about how to resolve it.

cp go.mod go.mod.orig
! go mod tidy
stderr '^example\.com/m imports\n\texample\.net/lazy tested by\n\texample\.net/lazy.test imports\n\texample\.com/retract/incompatible loaded from example\.com/retract/incompatible@v1\.0\.0,\n\tbut go 1\.16 would select v2\.0\.0\+incompatible\n\n'
stderr '\n\nTo upgrade to the versions selected by go 1\.16:\n\tgo mod tidy -go=1\.16 && go mod tidy -go=1\.17\nIf reproducibility with go 1.16 is not needed:\n\tgo mod tidy -compat=1.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'

cmp go.mod go.mod.orig

# The suggested '-compat' flag to ignore differences should silence the error
# and leave go.mod unchanged, resulting in checksum errors when Go 1.16 tries
# to load a module pruned out by Go 1.17.

go mod tidy -compat=1.17
! stderr .
cmp go.mod go.mod.orig

go list -deps -test -f $MODFMT all
stdout '^example\.com/retract/incompatible v1\.0\.0$'

go mod edit -go=1.16
! go list -deps -test -f $MODFMT all

	# TODO(#46160): -count=1 instead of -count=2.
stderr -count=2 '^go: example\.net/lazy@v0\.1\.0 requires\n\texample\.com/retract/incompatible@v1\.0\.0: missing go\.sum entry; to add it:\n\tgo mod download example\.com/retract/incompatible$'


# If we combine a Go 1.16 go.sum file...
go mod tidy -go=1.16

# ...with a Go 1.17 go.mod file...
cp go.mod.orig go.mod

# ...then Go 1.17 no longer works. 😞
! go list -deps -test -f $MODFMT all
stderr -count=1 '^can''t load test package: lazy[/\\]lazy_test.go:3:8: missing go\.sum entry for module providing package example\.com/retract/incompatible \(imported by example\.net/lazy\); to add:\n\tgo get -t example.net/lazy@v0\.1\.0$'


# However, if we take the union of the go.sum files...
go list -mod=mod -deps -test all
cmp go.mod go.mod.orig

# ...then Go 1.17 continues to work...
go list -deps -test -f $MODFMT all
stdout '^example\.com/retract/incompatible v1\.0\.0$'

# ...and 1.16 also works(‽), but selects a different version for the
# external-test dependency.
go mod edit -go=1.16
go list -deps -test -f $MODFMT all
stdout '^example\.com/retract/incompatible v2\.0\.0\+incompatible$'


-- go.mod --
// Module m imports packages from the same versions under Go 1.17
// as under Go 1.16, but under 1.16 its (implicit) external test dependencies
// are higher.
module example.com/m

go 1.17

replace (
	example.net/lazy v0.1.0 => ./lazy
	example.net/requireincompatible v0.1.0 => ./requireincompatible
)

require example.net/lazy v0.1.0
-- implicit.go --
package implicit

import _ "example.net/lazy"
-- lazy/go.mod --
// Module lazy requires example.com/retract/incompatible v1.0.0.
//
// When viewed from the outside it also has a transitive dependency
// on v2.0.0+incompatible, but in lazy mode that transitive dependency
// is pruned out.
module example.net/lazy

go 1.17

exclude example.com/retract/incompatible v2.0.0+incompatible

require (
	example.com/retract/incompatible v1.0.0
	example.net/requireincompatible v0.1.0
)
-- lazy/lazy.go --
package lazy
-- lazy/lazy_test.go --
package lazy_test

import _ "example.com/retract/incompatible"
-- requireincompatible/go.mod --
module example.net/requireincompatible

go 1.15

require example.com/retract/incompatible v2.0.0+incompatible
