cp go.mod go.mod.orig


# If a dependency cannot be resolved, 'go mod tidy' fails with an error message
# explaining the problem and does not update the go.mod file.
# TODO(bcmills): Ideally, with less redundancy than these error messages!

! go mod tidy

stderr '^example.com/untidy imports\n\texample.net/directnotfound: cannot find module providing package example.net/directnotfound: module example.net/directnotfound: reading http://.*: 404 Not Found$'

stderr '^example.com/untidy imports\n\texample.net/m imports\n\texample.net/indirectnotfound: cannot find module providing package example.net/indirectnotfound: module example.net/indirectnotfound: reading http://.*: 404 Not Found$'

stderr '^example.com/untidy tested by\n\texample.com/untidy.test imports\n\texample.net/directtestnotfound: cannot find module providing package example.net/directtestnotfound: module example.net/directtestnotfound: reading http://.*: 404 Not Found$'

stderr '^example.com/untidy imports\n\texample.net/m tested by\n\texample.net/m.test imports\n\texample.net/indirecttestnotfound: cannot find module providing package example.net/indirecttestnotfound: module example.net/indirecttestnotfound: reading http://.*: 404 Not Found$'

cmp go.mod.orig go.mod


# If a dependency cannot be resolved, 'go mod vendor' fails with an error message
# explaining the problem, does not update the go.mod file, and does not create
# the vendor directory.

! go mod vendor

stderr '^example.com/untidy imports\n\texample.net/directnotfound: no required module provides package example.net/directnotfound; to add it:\n\tgo get example.net/directnotfound$'

stderr '^example.com/untidy imports\n\texample.net/m: module example.net/m provides package example.net/m and is replaced but not required; to add it:\n\tgo get example.net/m@v0.1.0$'

stderr '^example.com/untidy tested by\n\texample.com/untidy.test imports\n\texample.net/directtestnotfound: no required module provides package example.net/directtestnotfound; to add it:\n\tgo get example.net/directtestnotfound$'

! stderr 'indirecttestnotfound'  # Vendor prunes test dependencies.

cmp go.mod.orig go.mod
! exists vendor


# 'go mod tidy' still logs the errors, but succeeds and updates go.mod.

go mod tidy -e
stderr -count=4 'cannot find module providing package'
cmp go.mod.final go.mod


# 'go mod vendor -e' still logs the errors, but creates a vendor directory
# and exits with status 0.
# 'go mod vendor -e' does not update go.mod and will not vendor packages that
# would require changing go.mod, for example, by adding a requirement.
cp go.mod.orig go.mod
go mod vendor -e
stderr -count=2 'no required module provides package'
stderr '^example.com/untidy imports\n\texample.net/m: module example.net/m provides package example.net/m and is replaced but not required; to add it:\n\tgo get example.net/m@v0.1.0$'
exists vendor/modules.txt
! exists vendor/example.net

go mod edit -require example.net/m@v0.1.0
go mod vendor -e
stderr -count=3 'no required module provides package'
exists vendor/modules.txt
exists vendor/example.net/m/m.go

-- go.mod --
module example.com/untidy
go 1.16
replace example.net/m v0.1.0 => ./m
-- go.mod.final --
module example.com/untidy

go 1.16

replace example.net/m v0.1.0 => ./m

require example.net/m v0.1.0
-- untidy.go --
package untidy

import (
	_ "example.net/m"
	_ "example.net/directnotfound"
)
-- untidy_test.go --
package untidy_test

import _ "example.net/directtestnotfound"
-- m/go.mod --
module example.net/m
go 1.16
-- m/m.go --
package m

import _ "example.net/indirectnotfound"
-- m/m_test.go --
package m_test

import _ "example.net/indirecttestnotfound"
