# For this module, Go 1.17 prunes out a (transitive and otherwise-irrelevant)
# requirement on a retracted higher version of a dependency.
# However, when Go 1.16 reads the same requirements from the go.mod file,
# it does not prune out that requirement, and selects the retracted version.
#
# The Go 1.16 module graph looks like:
#
# m ---- lazy v0.1.0 ---- requireincompatible v0.1.0 ---- incompatible v2.0.0+incompatible
# |        |
# + -------+------------- incompatible v1.0.0
#
# 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).

cp go.mod go.mod.orig

go mod graph
cp stdout graph-1.17.txt
stdout '^example\.com/m example\.com/retract/incompatible@v1\.0\.0$'
stdout '^example\.net/lazy@v0\.1\.0 example\.com/retract/incompatible@v1\.0\.0$'
! stdout 'example\.com/retract/incompatible@v2\.0\.0\+incompatible'

go mod graph -go=1.17
cmp stdout graph-1.17.txt

cmp go.mod go.mod.orig


# Setting -go=1.16 should report the graph as viewed by Go 1.16,
# but should not edit the go.mod file.

go mod graph -go=1.16
cp stdout graph-1.16.txt
stdout '^example\.com/m example\.com/retract/incompatible@v1\.0\.0$'
stdout '^example\.net/lazy@v0\.1\.0 example.com/retract/incompatible@v1\.0\.0$'
stdout '^example.net/requireincompatible@v0.1.0 example.com/retract/incompatible@v2\.0\.0\+incompatible$'

cmp go.mod go.mod.orig


# If we actually update the go.mod file to the requested go version,
# we should get the same selected versions, but the roots of the graph
# may be updated.
#
# TODO(#45551): The roots should not be updated.

go mod edit -go=1.16
go mod graph
! stdout '^example\.com/m example\.com/retract/incompatible@v1\.0\.0$'
stdout '^example\.net/lazy@v0.1.0 example.com/retract/incompatible@v1\.0\.0$'
stdout '^example.net/requireincompatible@v0.1.0 example.com/retract/incompatible@v2\.0\.0\+incompatible$'
	# TODO(#45551): cmp stdout graph-1.16.txt


# Unsupported go versions should be rejected, since we don't know
# what versions they would report.
! go mod graph -go=1.99999999999
stderr '^invalid value "1\.99999999999" for flag -go: maximum supported Go version is '$goversion'\nusage: go mod graph \[-go=version\]\nRun ''go help mod graph'' for details.$'


-- go.mod --
// Module m indirectly imports a package from
// example.com/retract/incompatible. Its selected version of
// that module is lower under Go 1.17 semantics than under Go 1.16.
module example.com/m

go 1.17

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

require (
	example.com/retract/incompatible v1.0.0 // indirect
	example.net/lazy v0.1.0
)
-- 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
)
-- requireincompatible/go.mod --
module example.net/requireincompatible

go 1.15

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