1 [short] skip
2
3 # go/build's Import should find modules by invoking the go command
4
5 go build -o $WORK ./testimport ./testfindonly
6
7 # GO111MODULE=off
8 env GO111MODULE=off
9 ! exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
10
11 # GO111MODULE=auto in GOPATH/src
12 env GO111MODULE=auto
13 exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
14
15 # GO111MODULE=auto outside GOPATH/src
16 cd $GOPATH/other
17 env GO111MODULE=auto
18 exec $WORK/testimport$GOEXE other/x/y/z/w .
19 stdout w2.go
20
21 ! exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
22 stderr 'no required module provides package gobuild.example.com/x/y/z/w; to add it:\n\tgo get gobuild.example.com/x/y/z/w'
23
24 cd z
25 exec $WORK/testimport$GOEXE other/x/y/z/w .
26 stdout w2.go
27
28 # GO111MODULE=on outside GOPATH/src
29 env GO111MODULE=
30 exec $WORK/testimport$GOEXE other/x/y/z/w .
31 stdout w2.go
32 env GO111MODULE=on
33 exec $WORK/testimport$GOEXE other/x/y/z/w .
34 stdout w2.go
35
36 # GO111MODULE=on in GOPATH/src
37 cd $GOPATH/src
38 env GO111MODULE=
39 exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
40 stdout w1.go
41 env GO111MODULE=on
42 exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
43 stdout w1.go
44 cd w
45 exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w ..
46 stdout w1.go
47
48 # go/build's Import in FindOnly mode should find directories by invoking the go command
49 #
50 # Calling build.Import in build.FindOnly mode on an import path of a Go package
51 # that produces errors when loading (e.g., due to build constraints not matching
52 # the current build context) should return the package directory and nil error.
53
54 # Issue 31603: Import with non-empty srcDir should work.
55 env GO111MODULE=on
56 exec $WORK/testfindonly$GOEXE gobuild.example.com/x/y/z/i $WORK
57 ! stdout 'build constraints'
58 stdout '^dir='$WORK'.+i err=<nil>$'
59
60 # Issue 37153: Import with empty srcDir should work.
61 env GO111MODULE=on
62 exec $WORK/testfindonly$GOEXE gobuild.example.com/x/y/z/i ''
63 ! stdout 'build constraints'
64 stdout '^dir='$WORK'.+i err=<nil>$'
65
66 -- go.mod --
67 module gobuild.example.com/x/y/z
68
69 -- z.go --
70 package z
71
72 -- w/w1.go --
73 package w
74
75 -- i/i.go --
76 // +build i
77
78 package i
79
80 -- testimport/x.go --
81 package main
82
83 import (
84 "fmt"
85 "go/build"
86 "log"
87 "os"
88 "path/filepath"
89 "strings"
90 )
91
92 func main() {
93 // build.Import should support relative and absolute source dir paths.
94 path := os.Args[1]
95 srcDir := os.Args[2]
96 p1, err := build.Import(path, srcDir, 0)
97 if err != nil {
98 log.Fatal(err)
99 }
100 absSrcDir, err := filepath.Abs(srcDir)
101 if err != nil {
102 log.Fatal(err)
103 }
104 p2, err := build.Import(path, absSrcDir, 0)
105 if err != nil {
106 log.Fatal(err)
107 }
108 if p1.Dir != p2.Dir {
109 log.Fatalf("different packages loaded with relative and absolute paths:\n\t%s\n\t%s", p1.Dir, p2.Dir)
110 }
111
112 fmt.Printf("%s\n%s\n", p1.Dir, strings.Join(p1.GoFiles, " "))
113 }
114
115 -- testfindonly/x.go --
116 package main
117
118 import (
119 "fmt"
120 "go/build"
121 "os"
122 )
123
124 func main() {
125 p, err := build.Import(os.Args[1], os.Args[2], build.FindOnly)
126 fmt.Printf("dir=%s err=%v\n", p.Dir, err)
127 }
128
129 -- $GOPATH/other/go.mod --
130 module other/x/y
131
132 -- $GOPATH/other/z/w/w2.go --
133 package w
134
View as plain text