1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file exercises the import parser but also checks that
6 // some low-level packages do not have new dependencies added.
15 // pkgDeps defines the expected dependencies between packages in
16 // the Go source tree. It is a statement of policy.
17 // Changes should not be made to this map without prior discussion.
19 // The map contains two kinds of entries:
20 // 1) Lower-case keys are standard import paths and list the
21 // allowed imports in that package.
22 // 2) Upper-case keys define aliases for package sets, which can then
23 // be used as dependencies by other rules.
25 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
27 var pkgDeps
= map[string][]string{
28 // L0 is the lowest level, core, nearly unavoidable packages.
30 "io": {"errors", "sync"},
31 "runtime": {"unsafe"},
32 "sync": {"sync/atomic", "unsafe"},
33 "sync/atomic": {"unsafe"},
45 // L1 adds simple functions and strings processing,
46 // but not Unicode tables.
48 "math/cmplx": {"math"},
49 "math/rand": {"L0", "math"},
51 "strconv": {"L0", "unicode/utf8", "math"},
66 // L2 adds Unicode and strings processing.
67 "bufio": {"L0", "unicode/utf8", "bytes"},
68 "bytes": {"L0", "unicode", "unicode/utf8"},
69 "path": {"L0", "unicode/utf8", "strings"},
70 "strings": {"L0", "unicode", "unicode/utf8"},
82 // L3 adds reflection and some basic utility packages
83 // and interface definitions, but nothing that makes
85 "crypto": {"L2", "hash"}, // interfaces
86 "crypto/cipher": {"L2", "crypto/subtle"}, // interfaces
88 "encoding/base32": {"L2"},
89 "encoding/base64": {"L2"},
90 "encoding/binary": {"L2", "reflect"},
91 "hash": {"L2"}, // interfaces
92 "hash/adler32": {"L2", "hash"},
93 "hash/crc32": {"L2", "hash"},
94 "hash/crc64": {"L2", "hash"},
95 "hash/fnv": {"L2", "hash"},
96 "image": {"L2", "image/color"}, // interfaces
97 "image/color": {"L2"}, // interfaces
98 "image/color/palette": {"L2", "image/color"},
116 "image/color/palette",
120 // End of linear dependency definitions.
122 // Operating system access.
123 "syscall": {"L0", "unicode/utf16"},
124 "time": {"L0", "syscall"},
125 "os": {"L1", "os", "syscall", "time"},
126 "path/filepath": {"L2", "os", "syscall"},
127 "io/ioutil": {"L2", "os", "path/filepath", "time"},
128 "os/exec": {"L2", "os", "syscall"},
129 "os/signal": {"L2", "os", "syscall"},
131 // OS enables basic operating system functionality,
132 // but not direct use of package syscall, nor os/signal.
141 // Formatted I/O: few dependencies (L1) but we must add reflect.
142 "fmt": {"L1", "os", "reflect"},
143 "log": {"L1", "os", "fmt", "time"},
145 // Packages used by testing must be low-level (L2+fmt).
146 "regexp": {"L2", "regexp/syntax"},
147 "regexp/syntax": {"L2"},
148 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
149 "runtime/pprof": {"L2", "fmt", "text/tabwriter"},
150 "text/tabwriter": {"L2"},
152 "testing": {"L2", "flag", "fmt", "os", "runtime/pprof", "time"},
153 "testing/iotest": {"L2", "log"},
154 "testing/quick": {"L2", "flag", "fmt", "reflect"},
156 // L4 is defined as L3+fmt+log+time, because in general once
157 // you're using L3 packages, use of fmt, log, or time is not a big deal.
166 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
167 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"},
168 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
169 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
170 "go/scanner": {"L4", "OS", "go/token"},
183 "archive/tar": {"L4", "OS", "syscall"},
184 "archive/zip": {"L4", "OS", "compress/flate"},
185 "compress/bzip2": {"L4"},
186 "compress/flate": {"L4"},
187 "compress/gzip": {"L4", "compress/flate"},
188 "compress/lzw": {"L4"},
189 "compress/zlib": {"L4", "compress/flate"},
190 "database/sql": {"L4", "container/list", "database/sql/driver"},
191 "database/sql/driver": {"L4", "time"},
192 "debug/dwarf": {"L4"},
193 "debug/elf": {"L4", "OS", "debug/dwarf"},
194 "debug/gosym": {"L4"},
195 "debug/macho": {"L4", "OS", "debug/dwarf"},
196 "debug/pe": {"L4", "OS", "debug/dwarf"},
198 "encoding/ascii85": {"L4"},
199 "encoding/asn1": {"L4", "math/big"},
200 "encoding/csv": {"L4"},
201 "encoding/gob": {"L4", "OS", "encoding"},
202 "encoding/hex": {"L4"},
203 "encoding/json": {"L4", "encoding"},
204 "encoding/pem": {"L4"},
205 "encoding/xml": {"L4", "encoding"},
206 "flag": {"L4", "OS"},
207 "go/build": {"L4", "OS", "GOPARSER"},
209 "image/draw": {"L4"},
210 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
211 "image/jpeg": {"L4"},
212 "image/png": {"L4", "compress/zlib"},
213 "index/suffixarray": {"L4", "regexp"},
215 "mime": {"L4", "OS", "syscall"},
217 "text/scanner": {"L4", "OS"},
218 "text/template/parse": {"L4"},
221 "L4", "OS", "encoding/json", "html", "text/template",
222 "text/template/parse",
225 "L4", "OS", "net/url", "text/template/parse",
229 "runtime/cgo": {"L0", "C"},
230 "CGO": {"C", "runtime/cgo"},
232 // Fake entry to satisfy the pseudo-import "C"
233 // that shows up in programs that use cgo.
236 // Plan 9 alone needs io/ioutil and os.
237 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
240 // Because net must be used by any package that wants to
241 // do networking portably, it must have a small dependency set: just L1+basic os.
242 "net": {"L1", "CGO", "os", "syscall", "time"},
244 // NET enables use of basic network-related packages.
252 // Uses of networking.
253 "log/syslog": {"L4", "OS", "net"},
254 "net/mail": {"L4", "NET", "OS"},
255 "net/textproto": {"L4", "OS", "net"},
258 "crypto/aes": {"L3"},
259 "crypto/des": {"L3"},
260 "crypto/hmac": {"L3"},
261 "crypto/md5": {"L3"},
262 "crypto/rc4": {"L3"},
263 "crypto/sha1": {"L3"},
264 "crypto/sha256": {"L3"},
265 "crypto/sha512": {"L3"},
278 // Random byte, number generation.
279 // This would be part of core crypto except that it imports
280 // math/big, which imports fmt.
281 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall"},
283 // Mathematical crypto: dependencies on fmt (L4) and math/big.
284 // We could avoid some of the fmt, but math/big imports fmt anyway.
285 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
286 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big"},
287 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
288 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
303 "L4", "CRYPTO-MATH", "CGO", "OS",
304 "crypto/x509", "encoding/pem", "net", "syscall",
307 "L4", "CRYPTO-MATH", "OS", "CGO",
308 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall",
310 "crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
312 // Simple net+crypto-aware packages.
313 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto"},
314 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
316 // HTTP, kingpin of dependencies.
319 "compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug",
322 // HTTP-using packages.
323 "expvar": {"L4", "OS", "encoding/json", "net/http"},
324 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
325 "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"},
326 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http"},
327 "net/http/httputil": {"L4", "NET", "OS", "net/http"},
328 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof"},
329 "net/rpc": {"L4", "NET", "encoding/gob", "net/http", "text/template"},
330 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
333 // isMacro reports whether p is a package dependency macro
335 func isMacro(p
string) bool {
336 return 'A' <= p
[0] && p
[0] <= 'Z'
339 func allowed(pkg
string) map[string]bool {
340 m
:= map[string]bool{}
341 var allow
func(string)
342 allow
= func(p
string) {
346 m
[p
] = true // set even for macros, to avoid loop on cycle
348 // Upper-case names are macro-expanded.
350 for _
, pp
:= range pkgDeps
[p
] {
355 for _
, pp
:= range pkgDeps
[pkg
] {
361 var bools
= []bool{false, true}
362 var geese
= []string{"darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "plan9", "solaris", "windows"}
363 var goarches
= []string{"386", "amd64", "arm", "arm64"}
369 // allowedErrors are the operating systems and packages known to contain errors
370 // (currently just "no Go source files")
371 var allowedErrors
= map[osPkg
]bool{
372 osPkg
{"windows", "log/syslog"}: true,
373 osPkg
{"plan9", "log/syslog"}: true,
376 func TestDependencies(t
*testing
.T
) {
379 for k
:= range pkgDeps
{
385 test
:= func(mustImport
bool) {
386 for _
, pkg
:= range all
{
390 p
, err
:= ctxt
.Import(pkg
, "", 0)
392 if allowedErrors
[osPkg
{ctxt
.GOOS
, pkg
}] {
395 if !ctxt
.CgoEnabled
&& pkg
== "runtime/cgo" {
398 // Some of the combinations we try might not
399 // be reasonable (like arm,plan9,cgo), so ignore
400 // errors for the auto-generated combinations.
404 t
.Errorf("%s/%s/cgo=%v %v", ctxt
.GOOS
, ctxt
.GOARCH
, ctxt
.CgoEnabled
, err
)
409 for _
, imp
:= range p
.Imports
{
411 bad
= append(bad
, imp
)
415 t
.Errorf("%s/%s/cgo=%v unexpected dependency: %s imports %v", ctxt
.GOOS
, ctxt
.GOARCH
, ctxt
.CgoEnabled
, pkg
, bad
)
422 t
.Logf("skipping other systems")
426 for _
, ctxt
.GOOS
= range geese
{
427 for _
, ctxt
.GOARCH
= range goarches
{
428 for _
, ctxt
.CgoEnabled
= range bools
{