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.
129 # STR is basic string and buffer manipulation.
130 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
137 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
138 # OS includes string routines, but those must be layered above package os.
139 # OS does not include reflection.
149 unicode, fmt !< net, os, os/signal;
155 os < internal/godebug;
157 path/filepath, internal/godebug < os/exec;
159 io/ioutil, os/exec, os/signal
165 < golang.org/x/sys/cpu;
167 # FMT is OS (which includes string routines) plus reflect and fmt.
168 # It does not include package log, which should be avoided in core packages.
181 # Misc packages needing only FMT.
186 mime/quotedprintable,
187 net/internal/socktest,
194 # core ones do not use fmt.
200 < encoding/base32, encoding/base64;
202 FMT, encoding < flag;
204 fmt !< encoding/base32, encoding/base64;
206 FMT, encoding/base32, encoding/base64
207 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
208 encoding/json, encoding/pem, encoding/xml, mime;
213 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
216 FMT, encoding/binary, math/rand
220 FMT, encoding/binary, hash/adler32, hash/crc32
221 < compress/bzip2, compress/flate, compress/lzw
222 < archive/zip, compress/gzip, compress/zlib;
226 < text/template/parse;
228 net/url, text/template/parse
230 < internal/lazytemplate;
232 encoding/json, html, text/template
239 < internal/lazyregexp;
242 encoding/binary, regexp
246 FMT, encoding/binary, compress/zlib, internal/saferio
249 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
253 # go parser and friends.
258 < go/internal/typeparams
262 < go/build/constraint, go/doc/comment;
264 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
268 go/doc/comment, go/parser, internal/lazyregexp, text/template
274 container/heap, go/constant, go/parser, regexp
277 FMT, internal/goexperiment
280 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
285 < database/sql/internal
286 < database/sql/driver
290 FMT, compress/lzw, compress/zlib
292 < image, image/color/palette
293 < image/internal/imageutil
295 < image/gif, image/jpeg, image/png;
297 # cgo, delayed as long as possible.
298 # If you add a dependency on CGO, you must add the package
299 # to cgoPackages in cmd/dist/test.go as well.
304 < runtime/race, runtime/msan, runtime/asan;
306 # Bulk of the standard library must not use cgo.
307 # The prohibition stops at net and os/user.
308 C !< fmt, go/types, CRYPTO-MATH;
318 < internal/singleflight;
321 < golang.org/x/net/dns/dnsmessage,
322 golang.org/x/net/lif,
323 golang.org/x/net/route;
325 os, runtime, strconv, sync, unsafe,
329 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
332 # net is unavoidable when doing any networking,
333 # so large dependencies must be kept out.
334 # This is a long-looking list but most of these
335 # are small with few dependencies.
337 golang.org/x/net/dns/dnsmessage,
338 golang.org/x/net/lif,
339 golang.org/x/net/route,
343 internal/singleflight,
350 math/rand !< net; # net uses runtime instead
352 # NET is net plus net-helper packages.
356 mime, net/textproto, net/url
359 # logging - most packages should not import; http and up is allowed
363 log !< crypto/tls, database/sql, go/importer, testing;
371 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
372 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
373 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
375 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
376 # Unfortunately, stuck with reflect via encoding/binary.
377 crypto/internal/boring/sig,
378 crypto/internal/boring/syso,
380 golang.org/x/sys/cpu,
384 < crypto/internal/subtle
385 < crypto/internal/randutil
386 < crypto/internal/nistec/fiat
387 < crypto/internal/nistec
388 < crypto/internal/edwards25519/field
389 < crypto/internal/edwards25519, crypto/ecdh
393 crypto/internal/boring/bcache
394 < crypto/internal/boring
396 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
397 crypto/sha1, crypto/sha256, crypto/sha512
400 CGO, fmt, net !< CRYPTO;
402 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
403 CRYPTO, FMT, math/big
404 < crypto/internal/boring/bbig
408 < golang.org/x/crypto/cryptobyte/asn1
409 < golang.org/x/crypto/cryptobyte
410 < crypto/dsa, crypto/elliptic, crypto/rsa
414 CGO, net !< CRYPTO-MATH;
416 # TLS, Prince of Dependencies.
417 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
418 < golang.org/x/crypto/internal/subtle
419 < golang.org/x/crypto/chacha20
420 < golang.org/x/crypto/internal/poly1305
421 < golang.org/x/crypto/chacha20poly1305
422 < golang.org/x/crypto/hkdf
423 < crypto/x509/internal/macos
426 crypto/internal/boring/fipstls, crypto/x509/pkix
430 # crypto-aware packages
432 DEBUG, go/build, go/types, text/scanner, crypto/md5
434 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
437 NET, crypto/rand, mime/quotedprintable
443 # HTTP, King of Dependencies.
446 < golang.org/x/net/http2/hpack
447 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
449 FMT, NET, container/list, encoding/binary, log
450 < golang.org/x/text/transform
451 < golang.org/x/text/unicode/norm
452 < golang.org/x/text/unicode/bidi
453 < golang.org/x/text/secure/bidirule
454 < golang.org/x/net/idna
455 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
458 < net/http/httptrace;
461 golang.org/x/net/http/httpguts,
462 golang.org/x/net/http/httpproxy,
463 golang.org/x/net/http2/hpack,
465 net/http/internal/ascii,
466 net/http/internal/testcert,
472 # HTTP-aware packages
474 encoding/json, net/http
477 net/http, net/http/internal/ascii
478 < net/http/cookiejar, net/http/httputil;
488 FMT, compress/gzip, encoding/binary, text/tabwriter
491 OS, compress/gzip, regexp
494 html, internal/profile, net/http, runtime/pprof, runtime/trace
498 encoding/gob, encoding/json, go/token, html/template, net/http
514 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
517 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
518 internal/godebug, math/rand, encoding/hex, crypto/sha256
521 internal/fuzz, internal/testlog, runtime/pprof, regexp
522 < testing/internal/testdeps;
524 OS, flag, testing, internal/cfg
528 < internal/obscuretestdata;
531 < os/signal/internal/pty;
533 NET, testing, math/rand
534 < golang.org/x/net/nettest;
537 < os/exec/internal/fdtest;
539 FMT, container/heap, math/rand
543 < internal/diff, internal/txtar;
546 // listStdPkgs returns the same list of packages as "go list std".
547 func listStdPkgs(goroot string) ([]string, error) {
548 // Based on cmd/go's matchPackages function.
551 src := filepath.Join(goroot, "src") + string(filepath.Separator)
552 walkFn := func(path string, d fs.DirEntry, err error) error {
553 if err != nil || !d.IsDir() || path == src {
557 base := filepath.Base(path)
558 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
559 return filepath.SkipDir
562 name := filepath.ToSlash(path[len(src):])
563 if name == "builtin" || name == "cmd" {
564 return filepath.SkipDir
567 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
570 if err := filepath.WalkDir(src, walkFn); err != nil {
576 func TestDependencies(t *testing.T) {
577 if !testenv.HasSrc() {
578 // Tests run in a limited file system and we do not
579 // provide access to every source file.
580 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
584 all, err := listStdPkgs(ctxt.GOROOT)
590 sawImport := map[string]map[string]bool{} // from package => to package => true
591 policy := depsPolicy(t)
593 for _, pkg := range all {
594 imports, err := findImports(pkg)
599 if sawImport[pkg] == nil {
600 sawImport[pkg] = map[string]bool{}
603 for _, imp := range imports {
604 sawImport[pkg][imp] = true
605 if !policy.HasEdge(pkg, imp) {
606 bad = append(bad, imp)
610 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
615 var buildIgnore = []byte("\n//go:build ignore")
617 func findImports(pkg string) ([]string, error) {
619 if strings.HasPrefix(pkg, "golang.org") {
620 vpkg = "vendor/" + pkg
622 dir := filepath.Join(Default.GOROOT, "src", vpkg)
623 files, err := os.ReadDir(dir)
628 var haveImport = map[string]bool{}
629 if pkg == "crypto/internal/boring" {
630 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
632 fset := token.NewFileSet()
633 for _, file := range files {
635 if name == "slice_go14.go" || name == "slice_go18.go" {
636 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
639 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
643 name: filepath.Join(dir, name),
646 f, err := os.Open(info.name)
650 err = readGoInfo(f, &info)
653 return nil, fmt.Errorf("reading %v: %v", name, err)
655 if info.parsed.Name.Name == "main" {
658 if bytes.Contains(info.header, buildIgnore) {
661 for _, imp := range info.imports {
663 if !haveImport[path] {
664 haveImport[path] = true
665 imports = append(imports, path)
669 sort.Strings(imports)
673 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
674 func depsPolicy(t *testing.T) *dag.Graph {
675 g, err := dag.Parse(depsRules)
682 // TestStdlibLowercase tests that all standard library package names are
683 // lowercase. See Issue 40065.
684 func TestStdlibLowercase(t *testing.T) {
685 if !testenv.HasSrc() {
686 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
690 all, err := listStdPkgs(ctxt.GOROOT)
695 for _, pkgname := range all {
696 if strings.ToLower(pkgname) != pkgname {
697 t.Errorf("package %q should not use upper-case path", pkgname)
702 // TestFindImports tests that findImports works. See #43249.
703 func TestFindImports(t *testing.T) {
704 imports, err := findImports("go/build")
708 t.Logf("go/build imports %q", imports)
709 want := []string{"bytes", "os", "path/filepath", "strings"}
711 for _, w := range want {
712 for _, imp := range imports {
717 t.Errorf("expected to find %q in import list", w)