1 ! go work init doesnotexist
2 stderr 'go: creating workspace file: no go.mod file exists in directory doesnotexist'
3 go env GOWORK
4 ! stdout .
5
6 go work init ./a ./b
7 cmpenv go.work go.work.want
8 go env GOWORK
9 stdout '^'$WORK'(\\|/)gopath(\\|/)src(\\|/)go.work$'
10
11 ! go run example.com/b
12 stderr 'a(\\|/)a.go:4:8: no required module provides package rsc.io/quote; to add it:\n\tcd '$WORK(\\|/)gopath(\\|/)src(\\|/)a'\n\tgo get rsc.io/quote'
13 cd a
14 go get rsc.io/quote
15 go env GOMOD # go env GOMOD reports the module in a single module context
16 stdout $GOPATH(\\|/)src(\\|/)a(\\|/)go.mod
17 cd ..
18 go run example.com/b
19 stdout 'Hello, world.'
20
21 # And try from a different directory
22 cd c
23 go run example.com/b
24 stdout 'Hello, world.'
25 cd $GOPATH/src
26
27 go list all # all includes both modules
28 stdout 'example.com/a'
29 stdout 'example.com/b'
30
31 # -mod can only be set to readonly in workspace mode
32 go list -mod=readonly all
33 ! go list -mod=mod all
34 stderr '^go: -mod may only be set to readonly when in workspace mode'
35 env GOWORK=off
36 go list -mod=mod all
37 env GOWORK=
38
39 # Test that duplicates in the use list return an error
40 cp go.work go.work.backup
41 cp go.work.dup go.work
42 ! go run example.com/b
43 stderr 'reading go.work: path .* appears multiple times in workspace'
44 cp go.work.backup go.work
45
46 cp go.work.d go.work
47 go run example.com/d
48
49 # Test that we don't run into "newRequirements called with unsorted roots"
50 # panic with unsorted main modules.
51 cp go.work.backwards go.work
52 go run example.com/d
53
54 # Test that command-line-arguments work inside and outside modules.
55 # This exercises the code that determines which module command-line-arguments
56 # belongs to.
57 go list ./b/main.go
58 env GOWORK=off
59 go build -n -o foo foo.go
60 env GOWORK=
61 go build -n -o foo foo.go
62
63 -- go.work.dup --
64 go 1.18
65
66 use (
67 a
68 b
69 ../src/a
70 )
71 -- go.work.want --
72 go $goversion
73
74 use (
75 ./a
76 ./b
77 )
78 -- go.work.d --
79 go 1.18
80
81 use (
82 a
83 b
84 d
85 )
86 -- a/go.mod --
87
88 module example.com/a
89
90 -- a/a.go --
91 package a
92
93 import "fmt"
94 import "rsc.io/quote"
95
96 func HelloFromA() {
97 fmt.Println(quote.Hello())
98 }
99
100 -- b/go.mod --
101
102 module example.com/b
103
104 -- b/main.go --
105 package main
106
107 import "example.com/a"
108
109 func main() {
110 a.HelloFromA()
111 }
112 -- b/lib/hello.go --
113 package lib
114
115 import "example.com/a"
116
117 func Hello() {
118 a.HelloFromA()
119 }
120
121 -- c/README --
122 Create this directory so we can cd to
123 it and make sure paths are interpreted
124 relative to the go.work, not the cwd.
125 -- d/go.mod --
126 module example.com/d
127
128 -- d/main.go --
129 package main
130
131 import "example.com/b/lib"
132
133 func main() {
134 lib.Hello()
135 }
136
137 -- go.work.backwards --
138 go 1.18
139
140 use (
141 d
142 b
143 a
144 )
145
146 -- foo.go --
147 package main
148 import "fmt"
149 func main() {
150 fmt.Println("Hello, World")
151 }
152
View as plain text