# 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}}'


# This module selects the same versions in Go 1.16 and 1.17 for all modules
# that provide packages (or test dependencies of packages) imported by the
# main module. However, in Go 1.16 it selects a higher version of a
# transitive module dependency that is not otherwise relevant to the main module.
# As a result, Go 1.16 needs an additional checksum for the go.mod file of
# that irrelevant dependency.
#
# 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

cp go.mod go.mod.orig
go mod tidy
cmp go.mod go.mod.orig

go list -deps -test -f $MODFMT all
cp stdout out-117.txt

go mod edit -go=1.16
go list -deps -test -f $MODFMT all
cmp stdout out-117.txt


# If we explicitly drop compatibility with 1.16, we retain fewer checksums,
# which gives a cleaner go.sum file but causes 1.16 to fail in readonly mode.

cp go.mod.orig go.mod
go mod tidy -compat=1.17
cmp go.mod go.mod.orig

go list -deps -test -f $MODFMT all
cmp stdout out-117.txt

go mod edit -go=1.16
! go list -deps -test -f $MODFMT all
stderr -count=1 '^go: example.net/lazy@v0.1.0 requires\n\texample.com/retract/incompatible@v1.0.0: missing go.sum entry for go.mod file; to add it:\n\tgo mod download example.com/retract/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
-- m.go --
package m

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/unimported/unimported.go --
package unimported

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

go 1.15

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