[short] skip
[!git] skip

env GOPROXY=direct
env GOSUMDB=off
env GOFLAGS=-mod=mod

# Regression test for golang.org/issue/27173: if the user (or go.mod file)
# requests a pseudo-version that does not match both the module path and commit
# metadata, reject it with a helpful error message.

# An incomplete commit hash is not a valid semantic version,
# but can appear in the main go.mod file anyway and should be resolved.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@1732873
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 \(replaced by \./\.\.\): parsing ..[/\\]go.mod: '$WORK'[/\\]gopath[/\\]src[/\\]go.mod:5: require vcs-test.golang.org/go/invalid-version: version "1732873" invalid: must be of the form v1.2.3'
cd ..
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v0.1.1-0.20170915032832-1732873102df'
grep 'vcs-test.golang.org/go/invalid-version v0.1.1-0.20170915032832-1732873102df' go.mod

# A module path below the repo root that does not contain a go.mod file is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version/unicode@v0.0.0-20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version/unicode@v0.0.0-20170915032832-1732873102df: invalid version: missing vcs-test.golang.org/go/invalid-version/unicode/go.mod at revision 1732873102df'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version/unicode@v0.0.0-20170915032832-1732873102df: invalid version: missing vcs-test.golang.org/go/invalid-version/unicode/go.mod at revision 1732873102df'

# However, arguments to 'go get' can name packages above the root.
cp go.mod.orig go.mod
go get vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-90ac26bf7f41
go list -m vcs-test.golang.org/go/invalid-version/...
stdout 'vcs-test.golang.org/go/invalid-version v0.1.1-0.20170915032832-90ac26bf7f41'
! stdout 'vcs-test.golang.org/go/invalid-version/unicode'

# A major version that does not match the module path is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v2.1.1-0.20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 \(replaced by \./\.\.\): parsing ..[/\\]go.mod: '$WORK'[/\\]gopath[/\\]src[/\\]go.mod:5: require vcs-test.golang.org/go/invalid-version: version "v2.1.1-0.20170915032832-1732873102df" invalid: should be v0 or v1, not v2'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr '^go.mod:5: require vcs-test.golang.org/go/invalid-version: version "v2.1.1-0.20170915032832-1732873102df" invalid: should be v0 or v1, not v2'

# A pseudo-version with fewer than 12 digits of SHA-1 prefix is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102d
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102d: invalid pseudo-version: revision is shorter than canonical \(expected 1732873102df\)'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102d: invalid pseudo-version: revision is shorter than canonical \(expected 1732873102df\)'

# A pseudo-version with more than 12 digits of SHA-1 prefix is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102dfcda2ecfbb94082da3cd4b21bc715
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102dfcda2ecfbb94082da3cd4b21bc715: invalid pseudo-version: revision is longer than canonical \(expected 1732873102df\)'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102dfcda2ecfbb94082da3cd4b21bc715: invalid pseudo-version: revision is longer than canonical \(expected 1732873102df\)'

# A pseudo-version that does not match the commit timestamp is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.1.1-0.20190915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.1.1-0.20190915032832-1732873102df: invalid pseudo-version: does not match version-control timestamp \(expected 20170915032832\)'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.1.1-0.20190915032832-1732873102df: invalid pseudo-version: does not match version-control timestamp \(expected 20170915032832\)'

# A 'replace' directive in the main module can replace an invalid timestamp
# with a valid one.
go mod edit -replace vcs-test.golang.org/go/invalid-version@v0.1.1-0.20190915032832-1732873102df=vcs-test.golang.org/go/invalid-version@1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.1.1-0.20190915032832-1732873102df: invalid pseudo-version: does not match version-control timestamp \(expected 20170915032832\)'
cd ..
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v0.1.1-0.20190915032832-1732873102df => vcs-test.golang.org/go/invalid-version v0.1.1-0.20170915032832-1732873102df'

# A pseudo-version that is not derived from a tag is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v1.999.999-0.20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v1.999.999-0.20170915032832-1732873102df: invalid pseudo-version: preceding tag \(v1.999.998\) not found'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v1.999.999-0.20170915032832-1732873102df: invalid pseudo-version: preceding tag \(v1.999.998\) not found'

# A v1.0.0- pseudo-version that is not derived from a tag is invalid:
# v1.0.0- implies no tag, but the correct no-tag prefix for a module path
# without a major-version suffix is v0.0.0-.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v1.0.0-20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v1.0.0-20170915032832-1732873102df: invalid pseudo-version: major version without preceding tag must be v0, not v1'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v1.0.0-20170915032832-1732873102df: invalid pseudo-version: major version without preceding tag must be v0, not v1'

# A pseudo-version vX.Y.Z+1 cannot have Z+1 == 0, since that would
# imply a base tag with a negative patch field.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.0.0-0.20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.0.0-0.20170915032832-1732873102df: invalid pseudo-version: version before v0.0.0 would have negative patch number'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.0.0-0.20170915032832-1732873102df: invalid pseudo-version: version before v0.0.0 would have negative patch number'

# A 'replace' directive in the main module can replace an
# invalid pseudo-version base with a valid one.
go mod edit -replace vcs-test.golang.org/go/invalid-version@v0.0.0-0.20170915032832-1732873102df=vcs-test.golang.org/go/invalid-version@v0.0.0-20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.0.0-0.20170915032832-1732873102df: invalid pseudo-version: version before v0.0.0 would have negative patch number'
cd ..
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v0.0.0-0.20170915032832-1732873102df => vcs-test.golang.org/go/invalid-version v0.0.0-20170915032832-1732873102df'

# A 'replace' directive can replace an invalid 'latest' version, and
# should suppress errors for that version in 'go get -u'
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v1.999999.0
go mod edit -replace vcs-test.golang.org/go/invalid-version@v1.999999.0=vcs-test.golang.org/go/invalid-version@v0.0.0-20170915032832-1732873102df
cd outside
! go get vcs-test.golang.org/go/invalid-version@upgrade
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v1.999999.0: reading vcs-test.golang.org/go/invalid-version/go.mod at revision v1.999999.0: unknown revision v1.999999.0'
cd ..
go get vcs-test.golang.org/go/invalid-version@upgrade
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v1.999999.0 => vcs-test.golang.org/go/invalid-version v0.0.0-20170915032832-1732873102df'


# A pseudo-version derived from a non-ancestor tag is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.2.1-0.20170915032832-1732873102df
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.2.1-0.20170915032832-1732873102df: invalid pseudo-version: revision 1732873102df is not a descendent of preceding tag \(v0.2.0\)'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.2.1-0.20170915032832-1732873102df: invalid pseudo-version: revision 1732873102df is not a descendent of preceding tag \(v0.2.0\)'

# A pseudo-version derived from a canonical tag on the same revision is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.2.1-0.20171213102548-a72be5ead5a6
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.2.1-0.20171213102548-a72be5ead5a6: invalid pseudo-version: tag \(v0.2.0\) found on revision a72be5ead5a6 is already canonical, so should not be replaced with a pseudo-version derived from that tag'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.2.1-0.20171213102548-a72be5ead5a6: invalid pseudo-version: tag \(v0.2.0\) found on revision a72be5ead5a6 is already canonical, so should not be replaced with a pseudo-version derived from that tag'

# A +incompatible suffix is not allowed on a version that is actually compatible.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102df+incompatible
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102df\+incompatible: invalid version: \+incompatible suffix not allowed: major version v0 is compatible'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr 'vcs-test.golang.org/go/invalid-version@v0.1.1-0.20170915032832-1732873102df\+incompatible: invalid version: \+incompatible suffix not allowed: major version v0 is compatible'

# The pseudo-version for a commit after a tag with a non-matching major version
# should instead be based on the last matching tag (which is v4.0.0 in this case).
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@5213b9f8842f
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v0.2.1-0.20171213102548-5213b9f8842f'
cd outside
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v0.2.1-0.20171213102548-5213b9f8842f'
cd ..

# A +incompatible pseudo-version for a module that has an explicit go.mod file is invalid.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v4.0.1-0.20171213102548-5213b9f8842f+incompatible
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr '^go: example.com@v0.0.0 requires\n\tvcs-test.golang.org/go/invalid-version@v4.0.1-0.20171213102548-5213b9f8842f\+incompatible: invalid version: module contains a go.mod file, so module path must match major version \("vcs-test.golang.org/go/invalid-version/v4"\)$'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr '^go: vcs-test.golang.org/go/invalid-version@v4.0.1-0.20171213102548-5213b9f8842f\+incompatible: invalid version: module contains a go.mod file, so module path must match major version \("vcs-test.golang.org/go/invalid-version/v4"\)$'

# A +incompatible pseudo-version is valid for a revision of the module
# that lacks a go.mod file.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v4.0.1-0.20171213102548-dbb861dd9ef0+incompatible
cd outside
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v4.0.1-0.20171213102548-dbb861dd9ef0\+incompatible'
cd ..
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v4.0.1-0.20171213102548-dbb861dd9ef0\+incompatible'

# 'go get' for a mismatched major version without a go.mod file should resolve
# to the equivalent +incompatible version, not a pseudo-version with a different
# major version.
cp go.mod.orig go.mod
go get vcs-test.golang.org/go/invalid-version@v5.0.0
go list -m vcs-test.golang.org/go/invalid-version
stdout 'vcs-test.golang.org/go/invalid-version v5.0.0\+incompatible'

# 'go get' for a mismatched major version with a go.mod file should error out,
# not resolve to a pseudo-version with a different major version.
cp go.mod.orig go.mod
! go get vcs-test.golang.org/go/invalid-version@v4.0.0
stderr 'go: vcs-test.golang.org/go/invalid-version@v4.0.0: invalid version: module contains a go.mod file, so module path must match major version \("vcs-test.golang.org/go/invalid-version/v4"\)$'

# An invalid +incompatible suffix for a canonical version should error out,
# not resolve to a pseudo-version.
#
# TODO(bcmills): The "outside" view for this failure mode is missing its import stack.
# Figure out why and fix it.
cp go.mod.orig go.mod
go mod edit -require vcs-test.golang.org/go/invalid-version@v4.0.0+incompatible
cd outside
! go list -m vcs-test.golang.org/go/invalid-version
stderr '^go: vcs-test.golang.org/go/invalid-version@v4.0.0\+incompatible: invalid version: module contains a go.mod file, so module path must match major version \("vcs-test.golang.org/go/invalid-version/v4"\)$'
cd ..
! go list -m vcs-test.golang.org/go/invalid-version
stderr '^go: vcs-test.golang.org/go/invalid-version@v4.0.0\+incompatible: invalid version: module contains a go.mod file, so module path must match major version \("vcs-test.golang.org/go/invalid-version/v4"\)$'

-- go.mod.orig --
module example.com

go 1.13
-- outside/go.mod --
module example.com/outside

go 1.13

require example.com v0.0.0
replace example.com v0.0.0 => ./..
