...
1# Regression test for https://go.dev/issue/51125:
2# Relative import paths (a holdover from GOPATH) were accidentally allowed in module mode.
3
4cd $WORK
5
6# Relative imports should not be allowed with a go.mod file.
7
8! go run driver.go
9stderr '^driver.go:3:8: "./mypkg" is relative, but relative import paths are not supported in module mode$'
10
11go list -e -f '{{with .Error}}{{.}}{{end}}' -deps driver.go
12stdout '^driver.go:3:8: "./mypkg" is relative, but relative import paths are not supported in module mode$'
13! stderr .
14
15
16# Relative imports should not be allowed in module mode even without a go.mod file.
17rm go.mod
18
19! go run driver.go
20stderr '^driver.go:3:8: "./mypkg" is relative, but relative import paths are not supported in module mode$'
21
22go list -e -f '{{with .Error}}{{.}}{{end}}' -deps driver.go
23stdout '^driver.go:3:8: "./mypkg" is relative, but relative import paths are not supported in module mode$'
24! stderr .
25
26
27# In GOPATH mode, they're still allowed (but only outside of GOPATH/src).
28env GO111MODULE=off
29
30[!short] go run driver.go
31
32go list -deps driver.go
33
34
35-- $WORK/go.mod --
36module example
37
38go 1.17
39-- $WORK/driver.go --
40package main
41
42import "./mypkg"
43
44func main() {
45 mypkg.MyFunc()
46}
47-- $WORK/mypkg/code.go --
48package mypkg
49
50import "fmt"
51
52func MyFunc() {
53 fmt.Println("Hello, world!")
54}
View as plain text