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.
25 // depsRules defines the expected dependencies between packages in
26 // the Go source tree. It is a statement of policy.
28 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
29 // Existing packages should not have their constraints relaxed
30 // without prior discussion.
31 // Negative assertions should almost never be removed.
33 // "a < b" means package b can import package a.
35 // See `go doc internal/dag' for the full syntax.
37 // All-caps names are pseudo-names for specific points
38 // in the dependency lattice.
40 # No dependencies allowed for any of these packages.
42 < constraints, container/list, container/ring,
43 internal/cfg, internal/cpu, internal/goarch,
44 internal/goexperiment, internal/goos,
45 internal/goversion, internal/nettrace,
46 unicode/utf8, unicode/utf16, unicode,
49 # These packages depend only on internal/goarch and unsafe.
50 internal/goarch, unsafe
53 # RUNTIME is the core runtime group of packages, all of them very light-weight.
54 internal/abi, internal/cpu, internal/goarch,
55 internal/goexperiment, internal/goos, unsafe
58 < internal/unsafeheader
59 < runtime/internal/sys
60 < runtime/internal/syscall
61 < runtime/internal/atomic
62 < runtime/internal/math
67 < internal/reflectlite
69 < internal/oserror, math/bits
87 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
88 RUNTIME, unicode/utf8, unicode/utf16
89 < internal/syscall/windows/sysdll, syscall/js
91 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
92 < internal/syscall/execenv
95 # TIME is SYSCALL plus the core packages about time, including context.
105 # MATH is RUNTIME plus the basic math packages.
126 # STR is basic string and buffer manipulation.
127 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
134 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
135 # OS includes string routines, but those must be layered above package os.
136 # OS does not include reflection.
146 unicode, fmt !< net, os, os/signal;
152 os < internal/godebug;
154 path/filepath, internal/godebug < os/exec;
156 io/ioutil, os/exec, os/signal
162 < golang.org/x/sys/cpu;
164 # FMT is OS (which includes string routines) plus reflect and fmt.
165 # It does not include package log, which should be avoided in core packages.
178 # Misc packages needing only FMT.
183 mime/quotedprintable,
184 net/internal/socktest,
191 # core ones do not use fmt.
197 < encoding/base32, encoding/base64;
199 FMT, encoding < flag;
201 fmt !< encoding/base32, encoding/base64;
203 FMT, encoding/base32, encoding/base64
204 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
205 encoding/json, encoding/pem, encoding/xml, mime;
210 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
213 FMT, encoding/binary, math/rand
217 FMT, encoding/binary, hash/adler32, hash/crc32
218 < compress/bzip2, compress/flate, compress/lzw
219 < archive/zip, compress/gzip, compress/zlib;
223 < text/template/parse;
225 net/url, text/template/parse
227 < internal/lazytemplate;
229 encoding/json, html, text/template
236 < internal/lazyregexp;
239 encoding/binary, regexp
243 FMT, encoding/binary, compress/zlib
246 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
250 # go parser and friends.
255 < go/internal/typeparams
259 < go/build/constraint, go/doc/comment;
261 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
265 go/doc/comment, go/parser, internal/lazyregexp, text/template
271 container/heap, go/constant, go/parser, regexp
274 FMT, internal/goexperiment
277 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
282 < database/sql/internal
283 < database/sql/driver
287 FMT, compress/lzw, compress/zlib
289 < image, image/color/palette
290 < image/internal/imageutil
292 < image/gif, image/jpeg, image/png;
294 # cgo, delayed as long as possible.
295 # If you add a dependency on CGO, you must add the package
296 # to cgoPackages in cmd/dist/test.go as well.
301 < runtime/race, runtime/msan, runtime/asan;
303 # Bulk of the standard library must not use cgo.
304 # The prohibition stops at net and os/user.
305 C !< fmt, go/types, CRYPTO-MATH;
315 < internal/singleflight;
318 < golang.org/x/net/dns/dnsmessage,
319 golang.org/x/net/lif,
320 golang.org/x/net/route;
322 os, runtime, strconv, sync, unsafe,
326 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
329 # net is unavoidable when doing any networking,
330 # so large dependencies must be kept out.
331 # This is a long-looking list but most of these
332 # are small with few dependencies.
334 golang.org/x/net/dns/dnsmessage,
335 golang.org/x/net/lif,
336 golang.org/x/net/route,
340 internal/singleflight,
347 math/rand !< net; # net uses runtime instead
349 # NET is net plus net-helper packages.
353 mime, net/textproto, net/url
356 # logging - most packages should not import; http and up is allowed
360 log !< crypto/tls, database/sql, go/importer, testing;
368 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
369 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
370 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
372 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
373 # Unfortunately, stuck with reflect via encoding/binary.
374 crypto/internal/boring/sig,
375 crypto/internal/boring/syso,
377 golang.org/x/sys/cpu,
381 < crypto/internal/subtle
382 < crypto/internal/nistec/fiat
383 < crypto/internal/nistec
384 < crypto/internal/edwards25519/field, golang.org/x/crypto/curve25519/internal/field
385 < crypto/internal/edwards25519
389 crypto/internal/boring/bcache
390 < crypto/internal/boring
392 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
393 crypto/sha1, crypto/sha256, crypto/sha512
396 CGO, fmt, net !< CRYPTO;
398 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
399 CRYPTO, FMT, math/big, embed
400 < crypto/internal/boring/bbig
401 < crypto/internal/randutil
405 < golang.org/x/crypto/cryptobyte/asn1
406 < golang.org/x/crypto/cryptobyte
407 < golang.org/x/crypto/curve25519
408 < crypto/dsa, crypto/elliptic, crypto/rsa
412 CGO, net !< CRYPTO-MATH;
414 # TLS, Prince of Dependencies.
415 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
416 < golang.org/x/crypto/internal/subtle
417 < golang.org/x/crypto/chacha20
418 < golang.org/x/crypto/internal/poly1305
419 < golang.org/x/crypto/chacha20poly1305
420 < golang.org/x/crypto/hkdf
421 < crypto/x509/internal/macos
424 crypto/internal/boring/fipstls, crypto/x509/pkix
428 # crypto-aware packages
430 DEBUG, go/build, go/types, text/scanner, crypto/md5
432 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
435 NET, crypto/rand, mime/quotedprintable
441 # HTTP, King of Dependencies.
444 < golang.org/x/net/http2/hpack
445 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
447 FMT, NET, container/list, encoding/binary, log
448 < golang.org/x/text/transform
449 < golang.org/x/text/unicode/norm
450 < golang.org/x/text/unicode/bidi
451 < golang.org/x/text/secure/bidirule
452 < golang.org/x/net/idna
453 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
456 < net/http/httptrace;
459 golang.org/x/net/http/httpguts,
460 golang.org/x/net/http/httpproxy,
461 golang.org/x/net/http2/hpack,
463 net/http/internal/ascii,
464 net/http/internal/testcert,
470 # HTTP-aware packages
472 encoding/json, net/http
475 net/http, net/http/internal/ascii
476 < net/http/cookiejar, net/http/httputil;
486 FMT, compress/gzip, encoding/binary, text/tabwriter
489 OS, compress/gzip, regexp
492 html, internal/profile, net/http, runtime/pprof, runtime/trace
496 encoding/gob, encoding/json, go/token, html/template, net/http
512 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
515 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
516 internal/godebug, math/rand, encoding/hex, crypto/sha256
519 internal/fuzz, internal/testlog, runtime/pprof, regexp
520 < testing/internal/testdeps;
522 OS, flag, testing, internal/cfg
526 < internal/obscuretestdata;
529 < os/signal/internal/pty;
531 NET, testing, math/rand
532 < golang.org/x/net/nettest;
535 < os/exec/internal/fdtest;
537 FMT, container/heap, math/rand
541 < internal/diff, internal/txtar;
544 // listStdPkgs returns the same list of packages as "go list std".
545 func listStdPkgs(goroot string) ([]string, error) {
546 // Based on cmd/go's matchPackages function.
549 src := filepath.Join(goroot, "src") + string(filepath.Separator)
550 walkFn := func(path string, d fs.DirEntry, err error) error {
551 if err != nil || !d.IsDir() || path == src {
555 base := filepath.Base(path)
556 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
557 return filepath.SkipDir
560 name := filepath.ToSlash(path[len(src):])
561 if name == "builtin" || name == "cmd" {
562 return filepath.SkipDir
565 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
568 if err := filepath.WalkDir(src, walkFn); err != nil {
574 func TestDependencies(t *testing.T) {
575 if !testenv.HasSrc() {
576 // Tests run in a limited file system and we do not
577 // provide access to every source file.
578 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
582 all, err := listStdPkgs(ctxt.GOROOT)
588 sawImport := map[string]map[string]bool{} // from package => to package => true
589 policy := depsPolicy(t)
591 for _, pkg := range all {
592 imports, err := findImports(pkg)
597 if sawImport[pkg] == nil {
598 sawImport[pkg] = map[string]bool{}
601 for _, imp := range imports {
602 sawImport[pkg][imp] = true
603 if !policy.HasEdge(pkg, imp) {
604 bad = append(bad, imp)
608 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
613 var buildIgnore = []byte("\n//go:build ignore")
615 func findImports(pkg string) ([]string, error) {
617 if strings.HasPrefix(pkg, "golang.org") {
618 vpkg = "vendor/" + pkg
620 dir := filepath.Join(Default.GOROOT, "src", vpkg)
621 files, err := os.ReadDir(dir)
626 var haveImport = map[string]bool{}
627 if pkg == "crypto/internal/boring" {
628 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
630 fset := token.NewFileSet()
631 for _, file := range files {
633 if name == "slice_go14.go" || name == "slice_go18.go" {
634 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
637 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
641 name: filepath.Join(dir, name),
644 f, err := os.Open(info.name)
648 err = readGoInfo(f, &info)
651 return nil, fmt.Errorf("reading %v: %v", name, err)
653 if info.parsed.Name.Name == "main" {
656 if bytes.Contains(info.header, buildIgnore) {
659 for _, imp := range info.imports {
661 if !haveImport[path] {
662 haveImport[path] = true
663 imports = append(imports, path)
667 sort.Strings(imports)
671 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
672 func depsPolicy(t *testing.T) *dag.Graph {
673 g, err := dag.Parse(depsRules)
680 // TestStdlibLowercase tests that all standard library package names are
681 // lowercase. See Issue 40065.
682 func TestStdlibLowercase(t *testing.T) {
683 if !testenv.HasSrc() {
684 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
688 all, err := listStdPkgs(ctxt.GOROOT)
693 for _, pkgname := range all {
694 if strings.ToLower(pkgname) != pkgname {
695 t.Errorf("package %q should not use upper-case path", pkgname)
700 // TestFindImports tests that findImports works. See #43249.
701 func TestFindImports(t *testing.T) {
702 imports, err := findImports("go/build")
706 t.Logf("go/build imports %q", imports)
707 want := []string{"bytes", "os", "path/filepath", "strings"}
709 for _, w := range want {
710 for _, imp := range imports {
715 t.Errorf("expected to find %q in import list", w)