...
1env GO111MODULE=on
2
3# Regression test for https://golang.org/issue/34769.
4# Missing standard-library imports should refer to GOROOT rather than
5# complaining about a malformed module path.
6# This is especially important when GOROOT is set incorrectly,
7# since such an error will occur for every package in std.
8
9# Building a nonexistent std package directly should fail usefully.
10
11! go build -mod=readonly nonexist
12! stderr 'import lookup disabled'
13! stderr 'missing dot'
14stderr '^package nonexist is not in std \('$GOROOT'[/\\]src[/\\]nonexist\)$'
15
16! go build nonexist
17! stderr 'import lookup disabled'
18! stderr 'missing dot'
19stderr '^package nonexist is not in std \('$GOROOT'[/\\]src[/\\]nonexist\)$'
20
21# Building a nonexistent std package indirectly should also fail usefully.
22
23! go build -mod=readonly ./importnonexist
24! stderr 'import lookup disabled'
25! stderr 'missing dot'
26stderr '^importnonexist[/\\]x.go:2:8: package nonexist is not in std \('$GOROOT'[/\\]src[/\\]nonexist\)$'
27
28! go build ./importnonexist
29! stderr 'import lookup disabled'
30! stderr 'missing dot'
31stderr '^importnonexist[/\\]x.go:2:8: package nonexist is not in std \('$GOROOT'[/\\]src[/\\]nonexist\)$'
32
33# Building an *actual* std package should fail if GOROOT is set to something bogus.
34
35[!short] go build ./importjson # Prove that it works when GOROOT is valid.
36
37env GOROOT=$WORK/not-a-valid-goroot
38! go build ./importjson
39! stderr 'import lookup disabled'
40! stderr 'missing dot'
41stderr 'importjson[/\\]x.go:2:8: package encoding/json is not in std \('$WORK'[/\\]not-a-valid-goroot[/\\]src[/\\]encoding[/\\]json\)$'
42
43-- go.mod --
44module example.com
45go 1.14
46-- importnonexist/x.go --
47package importnonexist
48import _ "nonexist"
49-- importjson/x.go --
50package importjson
51import _ "encoding/json"
52-- $WORK/not-a-valid-goroot/README --
53This directory is not a valid GOROOT.
View as plain text