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.
23 // pkgDeps defines the expected dependencies between packages in
24 // the Go source tree. It is a statement of policy.
25 // Changes should not be made to this map without prior discussion.
27 // The map contains two kinds of entries:
28 // 1) Lower-case keys are standard import paths and list the
29 // allowed imports in that package.
30 // 2) Upper-case keys define aliases for package sets, which can then
31 // be used as dependencies by other rules.
33 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
35 var pkgDeps
= map[string][]string{
36 // L0 is the lowest level, core, nearly unavoidable packages.
38 "io": {"errors", "sync"},
39 "runtime": {"unsafe", "runtime/internal/atomic", "runtime/internal/sys"},
40 "runtime/internal/sys": {},
41 "runtime/internal/atomic": {"unsafe", "runtime/internal/sys"},
42 "internal/race": {"runtime", "unsafe"},
43 "sync": {"internal/race", "runtime", "sync/atomic", "unsafe"},
44 "sync/atomic": {"unsafe"},
51 "runtime/internal/atomic",
57 // L1 adds simple functions and strings processing,
58 // but not Unicode tables.
60 "math/cmplx": {"math"},
61 "math/rand": {"L0", "math"},
63 "strconv": {"L0", "unicode/utf8", "math"},
78 // L2 adds Unicode and strings processing.
79 "bufio": {"L0", "unicode/utf8", "bytes"},
80 "bytes": {"L0", "unicode", "unicode/utf8"},
81 "path": {"L0", "unicode/utf8", "strings"},
82 "strings": {"L0", "unicode", "unicode/utf8"},
94 // L3 adds reflection and some basic utility packages
95 // and interface definitions, but nothing that makes
97 "crypto": {"L2", "hash"}, // interfaces
98 "crypto/cipher": {"L2", "crypto/subtle"}, // interfaces
100 "encoding/base32": {"L2"},
101 "encoding/base64": {"L2"},
102 "encoding/binary": {"L2", "reflect"},
103 "hash": {"L2"}, // interfaces
104 "hash/adler32": {"L2", "hash"},
105 "hash/crc32": {"L2", "hash"},
106 "hash/crc64": {"L2", "hash"},
107 "hash/fnv": {"L2", "hash"},
108 "image": {"L2", "image/color"}, // interfaces
109 "image/color": {"L2"}, // interfaces
110 "image/color/palette": {"L2", "image/color"},
128 "image/color/palette",
132 // End of linear dependency definitions.
134 // Operating system access.
135 "syscall": {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"},
136 "internal/syscall/unix": {"L0", "syscall"},
137 "internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll"},
138 "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
139 "time": {"L0", "syscall", "internal/syscall/windows/registry"},
140 "os": {"L1", "os", "syscall", "time", "internal/syscall/windows"},
141 "path/filepath": {"L2", "os", "syscall"},
142 "io/ioutil": {"L2", "os", "path/filepath", "time"},
143 "os/exec": {"L2", "os", "path/filepath", "syscall"},
144 "os/signal": {"L2", "os", "syscall"},
146 // OS enables basic operating system functionality,
147 // but not direct use of package syscall, nor os/signal.
156 // Formatted I/O: few dependencies (L1) but we must add reflect.
157 "fmt": {"L1", "os", "reflect"},
158 "log": {"L1", "os", "fmt", "time"},
160 // Packages used by testing must be low-level (L2+fmt).
161 "regexp": {"L2", "regexp/syntax"},
162 "regexp/syntax": {"L2"},
163 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
164 "runtime/pprof": {"L2", "fmt", "text/tabwriter"},
165 "runtime/trace": {"L0"},
166 "text/tabwriter": {"L2"},
168 "testing": {"L2", "flag", "fmt", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
169 "testing/iotest": {"L2", "log"},
170 "testing/quick": {"L2", "flag", "fmt", "reflect"},
171 "internal/testenv": {"L2", "os", "testing"},
173 // L4 is defined as L3+fmt+log+time, because in general once
174 // you're using L3 packages, use of fmt, log, or time is not a big deal.
183 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
184 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"},
185 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
186 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
187 "go/scanner": {"L4", "OS", "go/token"},
199 "go/format": {"L4", "GOPARSER", "internal/format"},
200 "internal/format": {"L4", "GOPARSER"},
203 "go/constant": {"L4", "go/token", "math/big"},
204 "go/importer": {"L4", "go/internal/gcimporter", "go/internal/gccgoimporter", "go/types"},
205 "go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
206 "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
207 "go/types": {"L4", "GOPARSER", "container/heap", "go/constant"},
210 "archive/tar": {"L4", "OS", "syscall"},
211 "archive/zip": {"L4", "OS", "compress/flate"},
212 "container/heap": {"sort"},
213 "compress/bzip2": {"L4"},
214 "compress/flate": {"L4"},
215 "compress/gzip": {"L4", "compress/flate"},
216 "compress/lzw": {"L4"},
217 "compress/zlib": {"L4", "compress/flate"},
218 "database/sql": {"L4", "container/list", "database/sql/driver"},
219 "database/sql/driver": {"L4", "time"},
220 "debug/dwarf": {"L4"},
221 "debug/elf": {"L4", "OS", "debug/dwarf", "compress/zlib"},
222 "debug/gosym": {"L4"},
223 "debug/macho": {"L4", "OS", "debug/dwarf"},
224 "debug/pe": {"L4", "OS", "debug/dwarf"},
225 "debug/plan9obj": {"L4", "OS"},
227 "encoding/ascii85": {"L4"},
228 "encoding/asn1": {"L4", "math/big"},
229 "encoding/csv": {"L4"},
230 "encoding/gob": {"L4", "OS", "encoding"},
231 "encoding/hex": {"L4"},
232 "encoding/json": {"L4", "encoding"},
233 "encoding/pem": {"L4"},
234 "encoding/xml": {"L4", "encoding"},
235 "flag": {"L4", "OS"},
236 "go/build": {"L4", "OS", "GOPARSER"},
238 "image/draw": {"L4", "image/internal/imageutil"},
239 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
240 "image/internal/imageutil": {"L4"},
241 "image/jpeg": {"L4", "image/internal/imageutil"},
242 "image/png": {"L4", "compress/zlib"},
243 "index/suffixarray": {"L4", "regexp"},
244 "internal/singleflight": {"sync"},
245 "internal/trace": {"L4", "OS"},
247 "mime": {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
248 "mime/quotedprintable": {"L4"},
249 "net/internal/socktest": {"L4", "OS", "syscall"},
251 "text/scanner": {"L4", "OS"},
252 "text/template/parse": {"L4"},
255 "L4", "OS", "encoding/json", "html", "text/template",
256 "text/template/parse",
259 "L4", "OS", "net/url", "text/template/parse",
263 // If you add a dependency on CGO, you must add the package to
264 // cgoPackages in cmd/dist/test.go.
265 "runtime/cgo": {"L0", "C"},
266 "CGO": {"C", "runtime/cgo"},
268 // Fake entry to satisfy the pseudo-import "C"
269 // that shows up in programs that use cgo.
272 // Race detector/MSan uses cgo.
273 "runtime/race": {"C"},
274 "runtime/msan": {"C"},
276 // Plan 9 alone needs io/ioutil and os.
277 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
280 // Because net must be used by any package that wants to
281 // do networking portably, it must have a small dependency set: just L0+basic os.
282 "net": {"L0", "CGO", "math/rand", "os", "sort", "syscall", "time", "internal/syscall/windows", "internal/singleflight", "internal/race"},
284 // NET enables use of basic network-related packages.
292 // Uses of networking.
293 "log/syslog": {"L4", "OS", "net"},
294 "net/mail": {"L4", "NET", "OS", "mime"},
295 "net/textproto": {"L4", "OS", "net"},
298 "crypto/aes": {"L3"},
299 "crypto/des": {"L3"},
300 "crypto/hmac": {"L3"},
301 "crypto/md5": {"L3"},
302 "crypto/rc4": {"L3"},
303 "crypto/sha1": {"L3"},
304 "crypto/sha256": {"L3"},
305 "crypto/sha512": {"L3"},
318 // Random byte, number generation.
319 // This would be part of core crypto except that it imports
320 // math/big, which imports fmt.
321 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"},
323 // Mathematical crypto: dependencies on fmt (L4) and math/big.
324 // We could avoid some of the fmt, but math/big imports fmt anyway.
325 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
326 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
327 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
328 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
343 "L4", "CRYPTO-MATH", "OS",
344 "container/list", "crypto/x509", "encoding/pem", "net", "syscall",
347 "L4", "CRYPTO-MATH", "OS", "CGO",
348 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall",
350 "crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
352 // Simple net+crypto-aware packages.
353 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
354 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
356 // HTTP, kingpin of dependencies.
359 "compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug",
361 "internal/golang.org/x/net/http2/hpack",
363 "net/http/internal": {"L4"},
365 // HTTP-using packages.
366 "expvar": {"L4", "OS", "encoding/json", "net/http"},
367 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
368 "net/http/cookiejar": {"L4", "NET", "net/http"},
369 "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"},
370 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal"},
371 "net/http/httputil": {"L4", "NET", "OS", "net/http", "net/http/internal"},
372 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
373 "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"},
374 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
377 // isMacro reports whether p is a package dependency macro
379 func isMacro(p
string) bool {
380 return 'A' <= p
[0] && p
[0] <= 'Z'
383 func allowed(pkg
string) map[string]bool {
384 m
:= map[string]bool{}
385 var allow
func(string)
386 allow
= func(p
string) {
390 m
[p
] = true // set even for macros, to avoid loop on cycle
392 // Upper-case names are macro-expanded.
394 for _
, pp
:= range pkgDeps
[p
] {
399 for _
, pp
:= range pkgDeps
[pkg
] {
405 var bools
= []bool{false, true}
406 var geese
= []string{"android", "darwin", "dragonfly", "freebsd", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows"}
407 var goarches
= []string{"386", "amd64", "arm", "arm64"}
413 // allowedErrors are the operating systems and packages known to contain errors
414 // (currently just "no Go source files")
415 var allowedErrors
= map[osPkg
]bool{
416 osPkg
{"windows", "log/syslog"}: true,
417 osPkg
{"plan9", "log/syslog"}: true,
420 // listStdPkgs returns the same list of packages as "go list std".
421 func listStdPkgs(goroot
string) ([]string, error
) {
422 // Based on cmd/go's matchPackages function.
425 src
:= filepath
.Join(goroot
, "src") + string(filepath
.Separator
)
426 walkFn
:= func(path
string, fi os
.FileInfo
, err error
) error
{
427 if err
!= nil ||
!fi
.IsDir() || path
== src
{
431 base
:= filepath
.Base(path
)
432 if strings
.HasPrefix(base
, ".") || strings
.HasPrefix(base
, "_") || base
== "testdata" {
433 return filepath
.SkipDir
436 name
:= filepath
.ToSlash(path
[len(src
):])
437 if name
== "builtin" || name
== "cmd" || strings
.Contains(name
, ".") {
438 return filepath
.SkipDir
441 pkgs
= append(pkgs
, name
)
444 if err
:= filepath
.Walk(src
, walkFn
); err
!= nil {
450 func TestDependencies(t
*testing
.T
) {
451 iOS
:= runtime
.GOOS
== "darwin" && (runtime
.GOARCH
== "arm" || runtime
.GOARCH
== "arm64")
452 if runtime
.GOOS
== "nacl" || iOS
{
453 // Tests run in a limited file system and we do not
454 // provide access to every source file.
455 t
.Skipf("skipping on %s/%s, missing full GOROOT", runtime
.GOOS
, runtime
.GOARCH
)
459 all
, err
:= listStdPkgs(ctxt
.GOROOT
)
465 for _
, pkg
:= range all
{
466 imports
, err
:= findImports(pkg
)
473 for _
, imp
:= range imports
{
475 bad
= append(bad
, imp
)
479 t
.Errorf("unexpected dependency: %s imports %v", pkg
, bad
)
484 var buildIgnore
= []byte("\n// +build ignore")
486 func findImports(pkg
string) ([]string, error
) {
487 dir
:= filepath
.Join(Default
.GOROOT
, "src", pkg
)
488 files
, err
:= ioutil
.ReadDir(dir
)
493 var haveImport
= map[string]bool{}
494 for _
, file
:= range files
{
496 if !strings
.HasSuffix(name
, ".go") || strings
.HasSuffix(name
, "_test.go") {
499 f
, err
:= os
.Open(filepath
.Join(dir
, name
))
504 data
, err
:= readImports(f
, false, &imp
)
507 return nil, fmt
.Errorf("reading %v: %v", name
, err
)
509 if bytes
.Contains(data
, buildIgnore
) {
512 for _
, quoted
:= range imp
{
513 path
, err
:= strconv
.Unquote(quoted
)
517 if !haveImport
[path
] {
518 haveImport
[path
] = true
519 imports
= append(imports
, path
)
523 sort
.Strings(imports
)