# Derive module path from import comment.
cd $WORK/x
exists x.go
go mod init
stderr 'module x'

# Import comment works even with CRLF line endings.
rm go.mod
addcrlf x.go
go mod init
stderr 'module x'

# go mod should die in GOPATH if modules are not enabled for GOPATH
cd $GOPATH/src/example.com/x/y
! go mod init
stderr 'go: modules disabled inside GOPATH/src by GO111MODULE=auto; see ''go help modules'''

env GO111MODULE=on

# Derive module path from location inside GOPATH.
cd $GOPATH/src/example.com/x/y
go mod init
stderr 'module example.com/x/y$'
rm go.mod

# Module path from Godeps/Godeps.json overrides GOPATH.
cd $GOPATH/src/example.com/x/y/z
go mod init
stderr 'unexpected.com/z'
rm go.mod

# Empty directory outside GOPATH fails.
mkdir $WORK/empty
cd $WORK/empty
! go mod init
stderr 'cannot determine module path for source directory'
rm go.mod

# Empty directory inside GOPATH/src uses location inside GOPATH.
mkdir $GOPATH/src/empty
cd $GOPATH/src/empty
go mod init
stderr 'empty'
rm go.mod

[!symlink] stop

# gplink1/src/empty where gopathlink -> GOPATH
symlink $WORK/gopathlink -> gopath
cd $WORK/gopathlink/src/empty
go mod init
rm go.mod

# GOPATH/src/link where link -> out of GOPATH
symlink $GOPATH/src/link -> $WORK/empty
cd $WORK/empty
! go mod init
cd $GOPATH/src/link
go mod init
stderr link
rm go.mod

# GOPATH/src/empty where GOPATH itself is a symlink
env GOPATH=$WORK/gopathlink
cd $GOPATH/src/empty
go mod init
rm go.mod
cd $WORK/gopath/src/empty
go mod init
rm go.mod

# GOPATH/src/link where GOPATH and link are both symlinks
cd $GOPATH/src/link
go mod init
stderr link
rm go.mod

# Too hard: doesn't match unevaluated nor completely evaluated. (Only partially evaluated.)
# Whether this works depends on which OS we are running on.
# cd $WORK/gopath/src/link
# ! go mod init

-- $WORK/x/x.go --
package x // import "x"

-- $GOPATH/src/example.com/x/y/y.go --
package y
-- $GOPATH/src/example.com/x/y/z/z.go --
package z
-- $GOPATH/src/example.com/x/y/z/Godeps/Godeps.json --
{"ImportPath": "unexpected.com/z"}
