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/buildinternal,
44 internal/cfg, internal/coverage, internal/coverage/rtcov,
45 internal/coverage/uleb128, internal/coverage/calloc,
46 internal/cpu, internal/goarch,
47 internal/goexperiment, internal/goos,
48 internal/goversion, internal/nettrace, internal/platform,
49 unicode/utf8, unicode/utf16, unicode,
52 # These packages depend only on internal/goarch and unsafe.
53 internal/goarch, unsafe
59 # RUNTIME is the core runtime group of packages, all of them very light-weight.
60 internal/abi, internal/cpu, internal/goarch,
61 internal/coverage/rtcov, internal/goexperiment,
62 internal/goos, internal/godebug, unsafe
65 < internal/unsafeheader
66 < runtime/internal/sys
67 < runtime/internal/syscall
68 < runtime/internal/atomic
69 < runtime/internal/math
74 < internal/reflectlite
76 < internal/oserror, math/bits
97 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
98 RUNTIME, unicode/utf8, unicode/utf16
99 < internal/syscall/windows/sysdll, syscall/js
101 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
102 < internal/syscall/execenv
105 # TIME is SYSCALL plus the core packages about time, including context.
115 # MATH is RUNTIME plus the basic math packages.
136 # STR is basic string and buffer manipulation.
137 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
144 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
145 # OS includes string routines, but those must be layered above package os.
146 # OS does not include reflection.
156 unicode, fmt !< net, os, os/signal;
162 path/filepath, internal/godebug < os/exec;
164 io/ioutil, os/exec, os/signal
170 < golang.org/x/sys/cpu;
172 # FMT is OS (which includes string routines) plus reflect and fmt.
173 # It does not include package log, which should be avoided in core packages.
174 arena, strconv, unicode
186 # Misc packages needing only FMT.
191 internal/types/errors,
192 mime/quotedprintable,
193 net/internal/socktest,
203 # core ones do not use fmt.
209 < encoding/base32, encoding/base64;
211 FMT, encoding < flag;
213 fmt !< encoding/base32, encoding/base64;
215 FMT, encoding/base32, encoding/base64, internal/saferio
216 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
217 encoding/json, encoding/pem, encoding/xml, mime;
222 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
225 FMT, encoding/binary, math/rand
229 FMT, encoding/binary, hash/adler32, hash/crc32
230 < compress/bzip2, compress/flate, compress/lzw
231 < archive/zip, compress/gzip, compress/zlib;
235 < text/template/parse;
237 net/url, text/template/parse
239 < internal/lazytemplate;
241 encoding/json, html, text/template
248 < internal/lazyregexp;
251 encoding/binary, regexp
255 FMT, encoding/binary, compress/zlib, internal/saferio
258 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
262 # go parser and friends.
267 < go/internal/typeparams
271 < go/build/constraint, go/doc/comment;
273 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
277 go/doc/comment, go/parser, internal/lazyregexp, text/template
283 container/heap, go/constant, go/parser, internal/types/errors, regexp
286 FMT, internal/goexperiment
289 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/buildinternal
294 < database/sql/internal
295 < database/sql/driver
299 FMT, compress/lzw, compress/zlib
301 < image, image/color/palette
302 < image/internal/imageutil
304 < image/gif, image/jpeg, image/png;
306 # cgo, delayed as long as possible.
307 # If you add a dependency on CGO, you must add the package
308 # to cgoPackages in cmd/dist/test.go as well.
313 < runtime/msan, runtime/asan;
316 NONE < runtime/race/internal/amd64v1;
317 NONE < runtime/race/internal/amd64v3;
318 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
320 # Bulk of the standard library must not use cgo.
321 # The prohibition stops at net and os/user.
322 C !< fmt, go/types, CRYPTO-MATH;
332 < internal/singleflight;
335 < golang.org/x/net/dns/dnsmessage,
336 golang.org/x/net/lif,
337 golang.org/x/net/route;
339 os, runtime, strconv, sync, unsafe,
343 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
346 # net is unavoidable when doing any networking,
347 # so large dependencies must be kept out.
348 # This is a long-looking list but most of these
349 # are small with few dependencies.
351 golang.org/x/net/dns/dnsmessage,
352 golang.org/x/net/lif,
353 golang.org/x/net/route,
357 internal/singleflight,
364 math/rand !< net; # net uses runtime instead
366 # NET is net plus net-helper packages.
370 mime, net/textproto, net/url
373 # logging - most packages should not import; http and up is allowed
377 log !< crypto/tls, database/sql, go/importer, testing;
385 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
386 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
387 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
389 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
390 # Unfortunately, stuck with reflect via encoding/binary.
391 crypto/internal/boring/sig,
392 crypto/internal/boring/syso,
394 golang.org/x/sys/cpu,
398 < crypto/internal/alias
399 < crypto/internal/randutil
400 < crypto/internal/nistec/fiat
401 < crypto/internal/nistec
402 < crypto/internal/edwards25519/field
403 < crypto/internal/edwards25519, crypto/ecdh
407 crypto/internal/boring/bcache
408 < crypto/internal/boring
410 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
411 crypto/sha1, crypto/sha256, crypto/sha512
414 CGO, fmt, net !< CRYPTO;
416 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
417 CRYPTO, FMT, math/big
418 < crypto/internal/boring/bbig
422 < golang.org/x/crypto/cryptobyte/asn1
423 < golang.org/x/crypto/cryptobyte
424 < crypto/dsa, crypto/elliptic, crypto/rsa
428 CGO, net !< CRYPTO-MATH;
430 # TLS, Prince of Dependencies.
431 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
432 < golang.org/x/crypto/internal/alias
433 < golang.org/x/crypto/internal/subtle
434 < golang.org/x/crypto/chacha20
435 < golang.org/x/crypto/internal/poly1305
436 < golang.org/x/crypto/chacha20poly1305
437 < golang.org/x/crypto/hkdf
438 < crypto/x509/internal/macos
441 crypto/internal/boring/fipstls, crypto/x509/pkix
445 # crypto-aware packages
447 DEBUG, go/build, go/types, text/scanner, crypto/md5
449 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
452 NET, crypto/rand, mime/quotedprintable
458 # HTTP, King of Dependencies.
461 < golang.org/x/net/http2/hpack
462 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
464 FMT, NET, container/list, encoding/binary, log
465 < golang.org/x/text/transform
466 < golang.org/x/text/unicode/norm
467 < golang.org/x/text/unicode/bidi
468 < golang.org/x/text/secure/bidirule
469 < golang.org/x/net/idna
470 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
473 < net/http/httptrace;
476 golang.org/x/net/http/httpguts,
477 golang.org/x/net/http/httpproxy,
478 golang.org/x/net/http2/hpack,
480 net/http/internal/ascii,
481 net/http/internal/testcert,
487 # HTTP-aware packages
489 encoding/json, net/http
492 net/http, net/http/internal/ascii
493 < net/http/cookiejar, net/http/httputil;
503 FMT, compress/gzip, encoding/binary, text/tabwriter
506 OS, compress/gzip, regexp
509 html, internal/profile, net/http, runtime/pprof, runtime/trace
513 encoding/gob, encoding/json, go/token, html/template, net/http
529 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
532 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
533 internal/godebug, math/rand, encoding/hex, crypto/sha256
536 internal/fuzz, internal/testlog, runtime/pprof, regexp
537 < testing/internal/testdeps;
539 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
543 < internal/obscuretestdata;
546 < os/signal/internal/pty;
548 NET, testing, math/rand
549 < golang.org/x/net/nettest;
552 < os/exec/internal/fdtest;
554 FMT, container/heap, math/rand
558 < internal/diff, internal/txtar;
560 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
561 internal/coverage, internal/coverage/uleb128
562 < internal/coverage/cmerge,
563 internal/coverage/pods,
564 internal/coverage/slicereader,
565 internal/coverage/slicewriter;
567 internal/coverage/slicereader, internal/coverage/slicewriter
568 < internal/coverage/stringtab
569 < internal/coverage/decodecounter, internal/coverage/decodemeta,
570 internal/coverage/encodecounter, internal/coverage/encodemeta;
572 internal/coverage/cmerge
573 < internal/coverage/cformat;
576 internal/coverage/calloc,
577 internal/coverage/cformat,
578 internal/coverage/decodecounter, internal/coverage/decodemeta,
579 internal/coverage/encodecounter, internal/coverage/encodemeta,
580 internal/coverage/pods
584 // listStdPkgs returns the same list of packages as "go list std".
585 func listStdPkgs(goroot string) ([]string, error) {
586 // Based on cmd/go's matchPackages function.
589 src := filepath.Join(goroot, "src") + string(filepath.Separator)
590 walkFn := func(path string, d fs.DirEntry, err error) error {
591 if err != nil || !d.IsDir() || path == src {
595 base := filepath.Base(path)
596 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
597 return filepath.SkipDir
600 name := filepath.ToSlash(path[len(src):])
601 if name == "builtin" || name == "cmd" {
602 return filepath.SkipDir
605 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
608 if err := filepath.WalkDir(src, walkFn); err != nil {
614 func TestDependencies(t *testing.T) {
615 if !testenv.HasSrc() {
616 // Tests run in a limited file system and we do not
617 // provide access to every source file.
618 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
622 all, err := listStdPkgs(ctxt.GOROOT)
628 sawImport := map[string]map[string]bool{} // from package => to package => true
629 policy := depsPolicy(t)
631 for _, pkg := range all {
632 imports, err := findImports(pkg)
637 if sawImport[pkg] == nil {
638 sawImport[pkg] = map[string]bool{}
641 for _, imp := range imports {
642 sawImport[pkg][imp] = true
643 if !policy.HasEdge(pkg, imp) {
644 bad = append(bad, imp)
648 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
653 var buildIgnore = []byte("\n//go:build ignore")
655 func findImports(pkg string) ([]string, error) {
657 if strings.HasPrefix(pkg, "golang.org") {
658 vpkg = "vendor/" + pkg
660 dir := filepath.Join(Default.GOROOT, "src", vpkg)
661 files, err := os.ReadDir(dir)
666 var haveImport = map[string]bool{}
667 if pkg == "crypto/internal/boring" {
668 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
670 fset := token.NewFileSet()
671 for _, file := range files {
673 if name == "slice_go14.go" || name == "slice_go18.go" {
674 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
677 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
681 name: filepath.Join(dir, name),
684 f, err := os.Open(info.name)
688 err = readGoInfo(f, &info)
691 return nil, fmt.Errorf("reading %v: %v", name, err)
693 if info.parsed.Name.Name == "main" {
696 if bytes.Contains(info.header, buildIgnore) {
699 for _, imp := range info.imports {
701 if !haveImport[path] {
702 haveImport[path] = true
703 imports = append(imports, path)
707 sort.Strings(imports)
711 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
712 func depsPolicy(t *testing.T) *dag.Graph {
713 g, err := dag.Parse(depsRules)
720 // TestStdlibLowercase tests that all standard library package names are
721 // lowercase. See Issue 40065.
722 func TestStdlibLowercase(t *testing.T) {
723 if !testenv.HasSrc() {
724 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
728 all, err := listStdPkgs(ctxt.GOROOT)
733 for _, pkgname := range all {
734 if strings.ToLower(pkgname) != pkgname {
735 t.Errorf("package %q should not use upper-case path", pkgname)
740 // TestFindImports tests that findImports works. See #43249.
741 func TestFindImports(t *testing.T) {
742 imports, err := findImports("go/build")
746 t.Logf("go/build imports %q", imports)
747 want := []string{"bytes", "os", "path/filepath", "strings"}
749 for _, w := range want {
750 for _, imp := range imports {
755 t.Errorf("expected to find %q in import list", w)