]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
a686bb78502e6a2278c651db99756fe8e5e68e72
[gostls13.git] / src / go / build / deps_test.go
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.
4
5 // This file exercises the import parser but also checks that
6 // some low-level packages do not have new dependencies added.
7
8 package build
9
10 import (
11         "bytes"
12         "fmt"
13         "go/token"
14         "internal/dag"
15         "internal/testenv"
16         "io/fs"
17         "os"
18         "path/filepath"
19         "runtime"
20         "sort"
21         "strings"
22         "testing"
23 )
24
25 // depsRules defines the expected dependencies between packages in
26 // the Go source tree. It is a statement of policy.
27 //
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.
32 //
33 // "a < b" means package b can import package a.
34 //
35 // See `go doc internal/dag' for the full syntax.
36 //
37 // All-caps names are pseudo-names for specific points
38 // in the dependency lattice.
39 var depsRules = `
40         # No dependencies allowed for any of these packages.
41         NONE
42         < cmp, container/list, container/ring,
43           internal/cfg, internal/coverage, internal/coverage/rtcov,
44           internal/coverage/uleb128, internal/coverage/calloc,
45           internal/cpu, internal/goarch, internal/godebugs,
46           internal/goexperiment, internal/goos,
47           internal/goversion, internal/nettrace, internal/platform,
48           log/internal,
49           unicode/utf8, unicode/utf16, unicode,
50           unsafe;
51
52         # These packages depend only on internal/goarch and unsafe.
53         internal/goarch, unsafe
54         < internal/abi;
55
56         unsafe < maps;
57
58         # RUNTIME is the core runtime group of packages, all of them very light-weight.
59         internal/abi, internal/cpu, internal/goarch,
60         internal/coverage/rtcov, internal/godebugs, internal/goexperiment,
61         internal/goos, unsafe
62         < internal/bytealg
63         < internal/itoa
64         < internal/unsafeheader
65         < runtime/internal/sys
66         < runtime/internal/syscall
67         < runtime/internal/atomic
68         < runtime/internal/math
69         < runtime
70         < sync/atomic
71         < internal/race
72         < sync
73         < internal/bisect
74         < internal/godebug
75         < internal/reflectlite
76         < errors
77         < internal/oserror, math/bits
78         < RUNTIME;
79
80         # slices depends on unsafe for overlapping check, cmp for comparison
81         # semantics, and math/bits for # calculating bitlength of numbers.
82         unsafe, cmp, math/bits
83         < slices;
84
85         RUNTIME, slices
86         < sort;
87
88         sort
89         < container/heap;
90
91         RUNTIME
92         < io;
93
94         RUNTIME
95         < arena;
96
97         syscall !< io;
98         reflect !< sort;
99
100         RUNTIME, unicode/utf8
101         < path;
102
103         unicode !< path;
104
105         # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
106         RUNTIME, unicode/utf8, unicode/utf16
107         < internal/syscall/windows/sysdll, syscall/js
108         < syscall
109         < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
110         < internal/syscall/execenv
111         < SYSCALL;
112
113         # TIME is SYSCALL plus the core packages about time, including context.
114         SYSCALL
115         < time/tzdata
116         < time
117         < context
118         < TIME;
119
120         TIME, io, path, sort
121         < io/fs;
122
123         # MATH is RUNTIME plus the basic math packages.
124         RUNTIME
125         < math
126         < MATH;
127
128         unicode !< math;
129
130         MATH
131         < math/cmplx;
132
133         MATH
134         < math/rand, math/rand/v2;
135
136         MATH
137         < runtime/metrics;
138
139         MATH, unicode/utf8
140         < strconv;
141
142         unicode !< strconv;
143
144         # STR is basic string and buffer manipulation.
145         RUNTIME, io, unicode/utf8, unicode/utf16, unicode
146         < bytes, strings
147         < bufio;
148
149         bufio, path, strconv
150         < STR;
151
152         # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
153         # OS includes string routines, but those must be layered above package os.
154         # OS does not include reflection.
155         io/fs
156         < internal/testlog
157         < internal/poll
158         < internal/safefilepath
159         < os
160         < os/signal;
161
162         io/fs
163         < embed;
164
165         unicode, fmt !< net, os, os/signal;
166
167         os/signal, STR
168         < path/filepath
169         < io/ioutil;
170
171         path/filepath, internal/godebug < os/exec;
172
173         io/ioutil, os/exec, os/signal
174         < OS;
175
176         reflect !< OS;
177
178         OS
179         < golang.org/x/sys/cpu;
180
181         # FMT is OS (which includes string routines) plus reflect and fmt.
182         # It does not include package log, which should be avoided in core packages.
183         arena, strconv, unicode
184         < reflect;
185
186         os, reflect
187         < internal/fmtsort
188         < fmt;
189
190         OS, fmt
191         < FMT;
192
193         log !< FMT;
194
195         # Misc packages needing only FMT.
196         FMT
197         < html,
198           internal/dag,
199           internal/goroot,
200           internal/types/errors,
201           mime/quotedprintable,
202           net/internal/socktest,
203           net/url,
204           runtime/trace,
205           text/scanner,
206           text/tabwriter;
207
208         io, reflect
209         < internal/saferio;
210
211         # encodings
212         # core ones do not use fmt.
213         io, strconv, slices
214         < encoding;
215
216         encoding, reflect
217         < encoding/binary
218         < encoding/base32, encoding/base64;
219
220         FMT, encoding < flag;
221
222         fmt !< encoding/base32, encoding/base64;
223
224         FMT, encoding/base32, encoding/base64, internal/saferio
225         < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
226           encoding/json, encoding/pem, encoding/xml, mime;
227
228         # hashes
229         io
230         < hash
231         < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
232
233         # math/big
234         FMT, encoding/binary, math/rand
235         < math/big;
236
237         # compression
238         FMT, encoding/binary, hash/adler32, hash/crc32
239         < compress/bzip2, compress/flate, compress/lzw, internal/zstd
240         < archive/zip, compress/gzip, compress/zlib;
241
242         # templates
243         FMT
244         < text/template/parse;
245
246         net/url, text/template/parse
247         < text/template
248         < internal/lazytemplate;
249
250         # regexp
251         FMT
252         < regexp/syntax
253         < regexp
254         < internal/lazyregexp;
255
256         encoding/json, html, text/template, regexp
257         < html/template;
258
259         # suffix array
260         encoding/binary, regexp
261         < index/suffixarray;
262
263         # executable parsing
264         FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
265         < runtime/debug
266         < debug/dwarf
267         < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
268         < debug/buildinfo
269         < DEBUG;
270
271         # go parser and friends.
272         FMT
273         < go/token
274         < go/scanner
275         < go/ast
276         < go/internal/typeparams;
277
278         FMT
279         < go/build/constraint, go/doc/comment;
280
281         go/internal/typeparams, go/build/constraint
282         < go/parser;
283
284         go/doc/comment, go/parser, text/tabwriter
285         < go/printer
286         < go/format;
287
288         math/big, go/token
289         < go/constant;
290
291         FMT, internal/goexperiment
292         < internal/buildcfg;
293
294         container/heap, go/constant, go/parser, internal/buildcfg, internal/goversion, internal/types/errors
295         < go/types;
296
297         # The vast majority of standard library packages should not be resorting to regexp.
298         # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
299         # that is low-enough level to be used by go/types.
300         regexp !< go/types;
301
302         go/doc/comment, go/parser, internal/lazyregexp, text/template
303         < go/doc;
304
305         go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
306         < go/build;
307
308         # databases
309         FMT
310         < database/sql/internal
311         < database/sql/driver
312         < database/sql;
313
314         # images
315         FMT, compress/lzw, compress/zlib
316         < image/color
317         < image, image/color/palette
318         < image/internal/imageutil
319         < image/draw
320         < image/gif, image/jpeg, image/png;
321
322         # cgo, delayed as long as possible.
323         # If you add a dependency on CGO, you must add the package
324         # to cgoPackages in cmd/dist/test.go as well.
325         RUNTIME
326         < C
327         < runtime/cgo
328         < CGO
329         < runtime/msan, runtime/asan;
330
331         # runtime/race
332         NONE < runtime/race/internal/amd64v1;
333         NONE < runtime/race/internal/amd64v3;
334         CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
335
336         # Bulk of the standard library must not use cgo.
337         # The prohibition stops at net and os/user.
338         C !< fmt, go/types, CRYPTO-MATH, log/slog;
339
340         CGO, OS
341         < plugin;
342
343         CGO, FMT
344         < os/user
345         < archive/tar;
346
347         sync
348         < internal/singleflight;
349
350         os
351         < golang.org/x/net/dns/dnsmessage,
352           golang.org/x/net/lif,
353           golang.org/x/net/route;
354
355         os, runtime, strconv, sync, unsafe,
356         internal/godebug
357         < internal/intern;
358
359         internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
360         < net/netip;
361
362         # net is unavoidable when doing any networking,
363         # so large dependencies must be kept out.
364         # This is a long-looking list but most of these
365         # are small with few dependencies.
366         CGO,
367         golang.org/x/net/dns/dnsmessage,
368         golang.org/x/net/lif,
369         golang.org/x/net/route,
370         internal/godebug,
371         internal/nettrace,
372         internal/poll,
373         internal/singleflight,
374         internal/race,
375         net/netip,
376         os
377         < net;
378
379         fmt, unicode !< net;
380         math/rand !< net; # net uses runtime instead
381
382         # NET is net plus net-helper packages.
383         FMT, net
384         < net/textproto;
385
386         mime, net/textproto, net/url
387         < NET;
388
389         # logging - most packages should not import; http and up is allowed
390         FMT, log/internal
391         < log;
392
393         log, log/slog !< crypto/tls, database/sql, go/importer, testing;
394
395         FMT, log, net
396         < log/syslog;
397
398         RUNTIME
399         < log/slog/internal, log/slog/internal/buffer;
400
401         FMT,
402         encoding, encoding/json,
403         log, log/internal,
404         log/slog/internal, log/slog/internal/buffer,
405         slices
406         < log/slog
407         < log/slog/internal/slogtest, log/slog/internal/benchmarks;
408
409         NET, log
410         < net/mail;
411
412         NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
413         sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
414         crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
415
416         # CRYPTO is core crypto algorithms - no cgo, fmt, net.
417         # Unfortunately, stuck with reflect via encoding/binary.
418         crypto/internal/boring/sig,
419         crypto/internal/boring/syso,
420         encoding/binary,
421         golang.org/x/sys/cpu,
422         hash, embed
423         < crypto
424         < crypto/subtle
425         < crypto/internal/alias
426         < crypto/cipher;
427
428         crypto/cipher,
429         crypto/internal/boring/bcache
430         < crypto/internal/boring
431         < crypto/boring;
432
433         crypto/internal/alias
434         < crypto/internal/randutil
435         < crypto/internal/nistec/fiat
436         < crypto/internal/nistec
437         < crypto/internal/edwards25519/field
438         < crypto/internal/edwards25519;
439
440         crypto/boring
441         < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
442           crypto/sha1, crypto/sha256, crypto/sha512;
443
444         crypto/boring, crypto/internal/edwards25519/field
445         < crypto/ecdh;
446
447         crypto/aes,
448         crypto/des,
449         crypto/ecdh,
450         crypto/hmac,
451         crypto/internal/edwards25519,
452         crypto/md5,
453         crypto/rc4,
454         crypto/sha1,
455         crypto/sha256,
456         crypto/sha512
457         < CRYPTO;
458
459         CGO, fmt, net !< CRYPTO;
460
461         # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
462         CRYPTO, FMT, math/big
463         < crypto/internal/boring/bbig
464         < crypto/rand
465         < crypto/ed25519
466         < encoding/asn1
467         < golang.org/x/crypto/cryptobyte/asn1
468         < golang.org/x/crypto/cryptobyte
469         < crypto/internal/bigmod
470         < crypto/dsa, crypto/elliptic, crypto/rsa
471         < crypto/ecdsa
472         < CRYPTO-MATH;
473
474         CGO, net !< CRYPTO-MATH;
475
476         # TLS, Prince of Dependencies.
477         CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
478         < golang.org/x/crypto/internal/alias
479         < golang.org/x/crypto/internal/subtle
480         < golang.org/x/crypto/chacha20
481         < golang.org/x/crypto/internal/poly1305
482         < golang.org/x/crypto/chacha20poly1305
483         < golang.org/x/crypto/hkdf
484         < crypto/x509/internal/macos
485         < crypto/x509/pkix;
486
487         crypto/internal/boring/fipstls, crypto/x509/pkix
488         < crypto/x509
489         < crypto/tls;
490
491         # crypto-aware packages
492
493         DEBUG, go/build, go/types, text/scanner, crypto/md5
494         < internal/pkgbits
495         < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
496         < go/importer;
497
498         NET, crypto/rand, mime/quotedprintable
499         < mime/multipart;
500
501         crypto/tls
502         < net/smtp;
503
504         crypto/rand
505         < hash/maphash; # for purego implementation
506
507         # HTTP, King of Dependencies.
508
509         FMT
510         < golang.org/x/net/http2/hpack
511         < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
512
513         FMT, NET, container/list, encoding/binary, log
514         < golang.org/x/text/transform
515         < golang.org/x/text/unicode/norm
516         < golang.org/x/text/unicode/bidi
517         < golang.org/x/text/secure/bidirule
518         < golang.org/x/net/idna
519         < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
520
521         NET, crypto/tls
522         < net/http/httptrace;
523
524         compress/gzip,
525         golang.org/x/net/http/httpguts,
526         golang.org/x/net/http/httpproxy,
527         golang.org/x/net/http2/hpack,
528         net/http/internal,
529         net/http/internal/ascii,
530         net/http/internal/testcert,
531         net/http/httptrace,
532         mime/multipart,
533         log
534         < net/http;
535
536         # HTTP-aware packages
537
538         encoding/json, net/http
539         < expvar;
540
541         net/http, net/http/internal/ascii
542         < net/http/cookiejar, net/http/httputil;
543
544         net/http, flag
545         < net/http/httptest;
546
547         net/http, regexp
548         < net/http/cgi
549         < net/http/fcgi;
550
551         # Profiling
552         FMT, compress/gzip, encoding/binary, text/tabwriter
553         < runtime/pprof;
554
555         OS, compress/gzip, internal/lazyregexp
556         < internal/profile;
557
558         html, internal/profile, net/http, runtime/pprof, runtime/trace
559         < net/http/pprof;
560
561         # RPC
562         encoding/gob, encoding/json, go/token, html/template, net/http
563         < net/rpc
564         < net/rpc/jsonrpc;
565
566         # System Information
567         bufio, bytes, internal/cpu, io, os, strings, sync
568         < internal/sysinfo;
569
570         # Test-only
571         log
572         < testing/iotest
573         < testing/fstest;
574
575         FMT, flag, math/rand
576         < testing/quick;
577
578         FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
579         < testing;
580
581         log/slog, testing
582         < testing/slogtest;
583
584         FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
585         internal/godebug, math/rand, encoding/hex, crypto/sha256
586         < internal/fuzz;
587
588         internal/fuzz, internal/testlog, runtime/pprof, regexp
589         < testing/internal/testdeps;
590
591         OS, flag, testing, internal/cfg, internal/platform, internal/goroot
592         < internal/testenv;
593
594         OS, encoding/base64
595         < internal/obscuretestdata;
596
597         CGO, OS, fmt
598         < internal/testpty;
599
600         NET, testing, math/rand
601         < golang.org/x/net/nettest;
602
603         syscall
604         < os/exec/internal/fdtest;
605
606         FMT, container/heap, math/rand
607         < internal/trace;
608
609         FMT
610         < internal/diff, internal/txtar;
611
612         FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
613         internal/coverage, internal/coverage/uleb128
614         < internal/coverage/cmerge,
615           internal/coverage/pods,
616           internal/coverage/slicereader,
617           internal/coverage/slicewriter;
618
619         internal/coverage/slicereader, internal/coverage/slicewriter
620         < internal/coverage/stringtab
621         < internal/coverage/decodecounter, internal/coverage/decodemeta,
622           internal/coverage/encodecounter, internal/coverage/encodemeta;
623
624         internal/coverage/cmerge
625         < internal/coverage/cformat;
626
627         internal/coverage, crypto/sha256, FMT
628         < cmd/internal/cov/covcmd;
629
630     encoding/json,
631         runtime/debug,
632         internal/coverage/calloc,
633         internal/coverage/cformat,
634         internal/coverage/decodecounter, internal/coverage/decodemeta,
635         internal/coverage/encodecounter, internal/coverage/encodemeta,
636         internal/coverage/pods
637         < runtime/coverage;
638 `
639
640 // listStdPkgs returns the same list of packages as "go list std".
641 func listStdPkgs(goroot string) ([]string, error) {
642         // Based on cmd/go's matchPackages function.
643         var pkgs []string
644
645         src := filepath.Join(goroot, "src") + string(filepath.Separator)
646         walkFn := func(path string, d fs.DirEntry, err error) error {
647                 if err != nil || !d.IsDir() || path == src {
648                         return nil
649                 }
650
651                 base := filepath.Base(path)
652                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
653                         return filepath.SkipDir
654                 }
655
656                 name := filepath.ToSlash(path[len(src):])
657                 if name == "builtin" || name == "cmd" {
658                         return filepath.SkipDir
659                 }
660
661                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
662                 return nil
663         }
664         if err := filepath.WalkDir(src, walkFn); err != nil {
665                 return nil, err
666         }
667         return pkgs, nil
668 }
669
670 func TestDependencies(t *testing.T) {
671         if !testenv.HasSrc() {
672                 // Tests run in a limited file system and we do not
673                 // provide access to every source file.
674                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
675         }
676
677         ctxt := Default
678         all, err := listStdPkgs(ctxt.GOROOT)
679         if err != nil {
680                 t.Fatal(err)
681         }
682         sort.Strings(all)
683
684         sawImport := map[string]map[string]bool{} // from package => to package => true
685         policy := depsPolicy(t)
686
687         for _, pkg := range all {
688                 imports, err := findImports(pkg)
689                 if err != nil {
690                         t.Error(err)
691                         continue
692                 }
693                 if sawImport[pkg] == nil {
694                         sawImport[pkg] = map[string]bool{}
695                 }
696                 var bad []string
697                 for _, imp := range imports {
698                         sawImport[pkg][imp] = true
699                         if !policy.HasEdge(pkg, imp) {
700                                 bad = append(bad, imp)
701                         }
702                 }
703                 if bad != nil {
704                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
705                 }
706         }
707 }
708
709 var buildIgnore = []byte("\n//go:build ignore")
710
711 func findImports(pkg string) ([]string, error) {
712         vpkg := pkg
713         if strings.HasPrefix(pkg, "golang.org") {
714                 vpkg = "vendor/" + pkg
715         }
716         dir := filepath.Join(Default.GOROOT, "src", vpkg)
717         files, err := os.ReadDir(dir)
718         if err != nil {
719                 return nil, err
720         }
721         var imports []string
722         var haveImport = map[string]bool{}
723         if pkg == "crypto/internal/boring" {
724                 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
725         }
726         fset := token.NewFileSet()
727         for _, file := range files {
728                 name := file.Name()
729                 if name == "slice_go14.go" || name == "slice_go18.go" {
730                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
731                         continue
732                 }
733                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
734                         continue
735                 }
736                 info := fileInfo{
737                         name: filepath.Join(dir, name),
738                         fset: fset,
739                 }
740                 f, err := os.Open(info.name)
741                 if err != nil {
742                         return nil, err
743                 }
744                 err = readGoInfo(f, &info)
745                 f.Close()
746                 if err != nil {
747                         return nil, fmt.Errorf("reading %v: %v", name, err)
748                 }
749                 if info.parsed.Name.Name == "main" {
750                         continue
751                 }
752                 if bytes.Contains(info.header, buildIgnore) {
753                         continue
754                 }
755                 for _, imp := range info.imports {
756                         path := imp.path
757                         if !haveImport[path] {
758                                 haveImport[path] = true
759                                 imports = append(imports, path)
760                         }
761                 }
762         }
763         sort.Strings(imports)
764         return imports, nil
765 }
766
767 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
768 func depsPolicy(t *testing.T) *dag.Graph {
769         g, err := dag.Parse(depsRules)
770         if err != nil {
771                 t.Fatal(err)
772         }
773         return g
774 }
775
776 // TestStdlibLowercase tests that all standard library package names are
777 // lowercase. See Issue 40065.
778 func TestStdlibLowercase(t *testing.T) {
779         if !testenv.HasSrc() {
780                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
781         }
782
783         ctxt := Default
784         all, err := listStdPkgs(ctxt.GOROOT)
785         if err != nil {
786                 t.Fatal(err)
787         }
788
789         for _, pkgname := range all {
790                 if strings.ToLower(pkgname) != pkgname {
791                         t.Errorf("package %q should not use upper-case path", pkgname)
792                 }
793         }
794 }
795
796 // TestFindImports tests that findImports works.  See #43249.
797 func TestFindImports(t *testing.T) {
798         imports, err := findImports("go/build")
799         if err != nil {
800                 t.Fatal(err)
801         }
802         t.Logf("go/build imports %q", imports)
803         want := []string{"bytes", "os", "path/filepath", "strings"}
804 wantLoop:
805         for _, w := range want {
806                 for _, imp := range imports {
807                         if imp == w {
808                                 continue wantLoop
809                         }
810                 }
811                 t.Errorf("expected to find %q in import list", w)
812         }
813 }