1 // Copyright 2022 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/coverage, internal/coverage/rtcov,
44 internal/coverage/uleb128, internal/coverage/calloc,
45 internal/cpu, internal/goarch,
46 internal/goexperiment, internal/goos,
47 internal/goversion, internal/nettrace,
48 unicode/utf8, unicode/utf16, unicode,
51 # These packages depend only on internal/goarch and unsafe.
52 internal/goarch, unsafe
55 # RUNTIME is the core runtime group of packages, all of them very light-weight.
56 internal/abi, internal/cpu, internal/goarch,
57 internal/coverage/rtcov, internal/goexperiment, internal/goos, unsafe
60 < internal/unsafeheader
61 < runtime/internal/sys
62 < runtime/internal/syscall
63 < runtime/internal/atomic
64 < runtime/internal/math
69 < internal/reflectlite
71 < internal/oserror, math/bits
89 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
90 RUNTIME, unicode/utf8, unicode/utf16
91 < internal/syscall/windows/sysdll, syscall/js
93 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
94 < internal/syscall/execenv
97 # TIME is SYSCALL plus the core packages about time, including context.
107 # MATH is RUNTIME plus the basic math packages.
128 # STR is basic string and buffer manipulation.
129 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
136 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
137 # OS includes string routines, but those must be layered above package os.
138 # OS does not include reflection.
148 unicode, fmt !< net, os, os/signal;
154 os < internal/godebug;
156 path/filepath, internal/godebug < os/exec;
158 io/ioutil, os/exec, os/signal
164 < golang.org/x/sys/cpu;
166 # FMT is OS (which includes string routines) plus reflect and fmt.
167 # It does not include package log, which should be avoided in core packages.
180 # Misc packages needing only FMT.
185 mime/quotedprintable,
186 net/internal/socktest,
196 # core ones do not use fmt.
202 < encoding/base32, encoding/base64;
204 FMT, encoding < flag;
206 fmt !< encoding/base32, encoding/base64;
208 FMT, encoding/base32, encoding/base64, internal/saferio
209 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
210 encoding/json, encoding/pem, encoding/xml, mime;
215 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
218 FMT, encoding/binary, math/rand
222 FMT, encoding/binary, hash/adler32, hash/crc32
223 < compress/bzip2, compress/flate, compress/lzw
224 < archive/zip, compress/gzip, compress/zlib;
228 < text/template/parse;
230 net/url, text/template/parse
232 < internal/lazytemplate;
234 encoding/json, html, text/template
241 < internal/lazyregexp;
244 encoding/binary, regexp
248 FMT, encoding/binary, compress/zlib, internal/saferio
251 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
255 # go parser and friends.
260 < go/internal/typeparams
264 < go/build/constraint, go/doc/comment;
266 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
270 go/doc/comment, go/parser, internal/lazyregexp, text/template
276 container/heap, go/constant, go/parser, regexp
279 FMT, internal/goexperiment
282 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
287 < database/sql/internal
288 < database/sql/driver
292 FMT, compress/lzw, compress/zlib
294 < image, image/color/palette
295 < image/internal/imageutil
297 < image/gif, image/jpeg, image/png;
299 # cgo, delayed as long as possible.
300 # If you add a dependency on CGO, you must add the package
301 # to cgoPackages in cmd/dist/test.go as well.
306 < runtime/msan, runtime/asan;
309 NONE < runtime/race/internal/amd64v1;
310 NONE < runtime/race/internal/amd64v3;
311 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
313 # Bulk of the standard library must not use cgo.
314 # The prohibition stops at net and os/user.
315 C !< fmt, go/types, CRYPTO-MATH;
325 < internal/singleflight;
328 < golang.org/x/net/dns/dnsmessage,
329 golang.org/x/net/lif,
330 golang.org/x/net/route;
332 os, runtime, strconv, sync, unsafe,
336 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
339 # net is unavoidable when doing any networking,
340 # so large dependencies must be kept out.
341 # This is a long-looking list but most of these
342 # are small with few dependencies.
344 golang.org/x/net/dns/dnsmessage,
345 golang.org/x/net/lif,
346 golang.org/x/net/route,
350 internal/singleflight,
357 math/rand !< net; # net uses runtime instead
359 # NET is net plus net-helper packages.
363 mime, net/textproto, net/url
366 # logging - most packages should not import; http and up is allowed
370 log !< crypto/tls, database/sql, go/importer, testing;
378 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
379 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
380 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
382 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
383 # Unfortunately, stuck with reflect via encoding/binary.
384 crypto/internal/boring/sig,
385 crypto/internal/boring/syso,
387 golang.org/x/sys/cpu,
391 < crypto/internal/alias
392 < crypto/internal/randutil
393 < crypto/internal/nistec/fiat
394 < crypto/internal/nistec
395 < crypto/internal/edwards25519/field
396 < crypto/internal/edwards25519, crypto/ecdh
400 crypto/internal/boring/bcache
401 < crypto/internal/boring
403 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
404 crypto/sha1, crypto/sha256, crypto/sha512
407 CGO, fmt, net !< CRYPTO;
409 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
410 CRYPTO, FMT, math/big
411 < crypto/internal/boring/bbig
415 < golang.org/x/crypto/cryptobyte/asn1
416 < golang.org/x/crypto/cryptobyte
417 < crypto/dsa, crypto/elliptic, crypto/rsa
421 CGO, net !< CRYPTO-MATH;
423 # TLS, Prince of Dependencies.
424 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
425 < golang.org/x/crypto/internal/alias
426 < golang.org/x/crypto/internal/subtle
427 < golang.org/x/crypto/chacha20
428 < golang.org/x/crypto/internal/poly1305
429 < golang.org/x/crypto/chacha20poly1305
430 < golang.org/x/crypto/hkdf
431 < crypto/x509/internal/macos
434 crypto/internal/boring/fipstls, crypto/x509/pkix
438 # crypto-aware packages
440 DEBUG, go/build, go/types, text/scanner, crypto/md5
442 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
445 NET, crypto/rand, mime/quotedprintable
451 # HTTP, King of Dependencies.
454 < golang.org/x/net/http2/hpack
455 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
457 FMT, NET, container/list, encoding/binary, log
458 < golang.org/x/text/transform
459 < golang.org/x/text/unicode/norm
460 < golang.org/x/text/unicode/bidi
461 < golang.org/x/text/secure/bidirule
462 < golang.org/x/net/idna
463 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
466 < net/http/httptrace;
469 golang.org/x/net/http/httpguts,
470 golang.org/x/net/http/httpproxy,
471 golang.org/x/net/http2/hpack,
473 net/http/internal/ascii,
474 net/http/internal/testcert,
480 # HTTP-aware packages
482 encoding/json, net/http
485 net/http, net/http/internal/ascii
486 < net/http/cookiejar, net/http/httputil;
496 FMT, compress/gzip, encoding/binary, text/tabwriter
499 OS, compress/gzip, regexp
502 html, internal/profile, net/http, runtime/pprof, runtime/trace
506 encoding/gob, encoding/json, go/token, html/template, net/http
522 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
525 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
526 internal/godebug, math/rand, encoding/hex, crypto/sha256
529 internal/fuzz, internal/testlog, runtime/pprof, regexp
530 < testing/internal/testdeps;
532 OS, flag, testing, internal/cfg
536 < internal/obscuretestdata;
539 < os/signal/internal/pty;
541 NET, testing, math/rand
542 < golang.org/x/net/nettest;
545 < os/exec/internal/fdtest;
547 FMT, container/heap, math/rand
551 < internal/diff, internal/txtar;
554 < internal/coverage/slicewriter;
556 encoding/binary, internal/unsafeheader, unsafe
557 < internal/coverage/slicereader;
559 FMT, math, internal/coverage
560 < internal/coverage/cmerge;
562 FMT, math, internal/coverage, internal/coverage/cmerge, text/tabwriter
563 < internal/coverage/cformat;
565 FMT, io, internal/coverage/slicereader, internal/coverage/uleb128
566 < internal/coverage/stringtab;
568 FMT, encoding/binary, internal/coverage, internal/coverage/stringtab,
569 io, os, bufio, crypto/md5
570 < internal/coverage/encodemeta;
572 FMT, bufio, encoding/binary, internal/coverage,
573 internal/coverage/stringtab, internal/coverage/slicewriter, os, unsafe
574 < internal/coverage/encodecounter;
576 FMT, encoding/binary, internal/coverage, io, os,
577 internal/coverage/slicereader, internal/coverage/stringtab
578 < internal/coverage/decodecounter;
580 FMT, encoding/binary, internal/coverage, io, os,
581 crypto/md5, internal/coverage/stringtab
582 < internal/coverage/decodemeta;
584 FMT, internal/coverage, os,
585 path/filepath, regexp, sort, strconv
586 < internal/coverage/pods;
588 FMT, bufio, crypto/md5, encoding/binary, runtime/debug,
589 internal/coverage, internal/coverage/cmerge,
590 internal/coverage/cformat, internal/coverage/calloc,
591 internal/coverage/decodecounter, internal/coverage/decodemeta,
592 internal/coverage/encodecounter, internal/coverage/encodemeta,
593 internal/coverage/pods, os, path/filepath, reflect, time, unsafe
597 // listStdPkgs returns the same list of packages as "go list std".
598 func listStdPkgs(goroot string) ([]string, error) {
599 // Based on cmd/go's matchPackages function.
602 src := filepath.Join(goroot, "src") + string(filepath.Separator)
603 walkFn := func(path string, d fs.DirEntry, err error) error {
604 if err != nil || !d.IsDir() || path == src {
608 base := filepath.Base(path)
609 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
610 return filepath.SkipDir
613 name := filepath.ToSlash(path[len(src):])
614 if name == "builtin" || name == "cmd" {
615 return filepath.SkipDir
618 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
621 if err := filepath.WalkDir(src, walkFn); err != nil {
627 func TestDependencies(t *testing.T) {
628 if !testenv.HasSrc() {
629 // Tests run in a limited file system and we do not
630 // provide access to every source file.
631 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
635 all, err := listStdPkgs(ctxt.GOROOT)
641 sawImport := map[string]map[string]bool{} // from package => to package => true
642 policy := depsPolicy(t)
644 for _, pkg := range all {
645 imports, err := findImports(pkg)
650 if sawImport[pkg] == nil {
651 sawImport[pkg] = map[string]bool{}
654 for _, imp := range imports {
655 sawImport[pkg][imp] = true
656 if !policy.HasEdge(pkg, imp) {
657 bad = append(bad, imp)
661 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
666 var buildIgnore = []byte("\n//go:build ignore")
668 func findImports(pkg string) ([]string, error) {
670 if strings.HasPrefix(pkg, "golang.org") {
671 vpkg = "vendor/" + pkg
673 dir := filepath.Join(Default.GOROOT, "src", vpkg)
674 files, err := os.ReadDir(dir)
679 var haveImport = map[string]bool{}
680 if pkg == "crypto/internal/boring" {
681 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
683 fset := token.NewFileSet()
684 for _, file := range files {
686 if name == "slice_go14.go" || name == "slice_go18.go" {
687 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
690 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
694 name: filepath.Join(dir, name),
697 f, err := os.Open(info.name)
701 err = readGoInfo(f, &info)
704 return nil, fmt.Errorf("reading %v: %v", name, err)
706 if info.parsed.Name.Name == "main" {
709 if bytes.Contains(info.header, buildIgnore) {
712 for _, imp := range info.imports {
714 if !haveImport[path] {
715 haveImport[path] = true
716 imports = append(imports, path)
720 sort.Strings(imports)
724 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
725 func depsPolicy(t *testing.T) *dag.Graph {
726 g, err := dag.Parse(depsRules)
733 // TestStdlibLowercase tests that all standard library package names are
734 // lowercase. See Issue 40065.
735 func TestStdlibLowercase(t *testing.T) {
736 if !testenv.HasSrc() {
737 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
741 all, err := listStdPkgs(ctxt.GOROOT)
746 for _, pkgname := range all {
747 if strings.ToLower(pkgname) != pkgname {
748 t.Errorf("package %q should not use upper-case path", pkgname)
753 // TestFindImports tests that findImports works. See #43249.
754 func TestFindImports(t *testing.T) {
755 imports, err := findImports("go/build")
759 t.Logf("go/build imports %q", imports)
760 want := []string{"bytes", "os", "path/filepath", "strings"}
762 for _, w := range want {
763 for _, imp := range imports {
768 t.Errorf("expected to find %q in import list", w)