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