]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
a733441d5b63869a00da711974845eaffc0fb6d1
[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, internal/safefilepath, 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         < internal/gover
274         < go/version
275         < go/token
276         < go/scanner
277         < go/ast
278         < go/internal/typeparams;
279
280         FMT
281         < go/build/constraint, go/doc/comment;
282
283         go/internal/typeparams, go/build/constraint
284         < go/parser;
285
286         go/doc/comment, go/parser, text/tabwriter
287         < go/printer
288         < go/format;
289
290         math/big, go/token
291         < go/constant;
292
293         FMT, internal/goexperiment
294         < internal/buildcfg;
295
296         container/heap, go/constant, go/parser, internal/buildcfg, internal/goversion, internal/types/errors
297         < go/types;
298
299         # The vast majority of standard library packages should not be resorting to regexp.
300         # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
301         # that is low-enough level to be used by go/types.
302         regexp !< go/types;
303
304         go/doc/comment, go/parser, internal/lazyregexp, text/template
305         < go/doc;
306
307         go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
308         < go/build;
309
310         # databases
311         FMT
312         < database/sql/internal
313         < database/sql/driver
314         < database/sql;
315
316         # images
317         FMT, compress/lzw, compress/zlib
318         < image/color
319         < image, image/color/palette
320         < image/internal/imageutil
321         < image/draw
322         < image/gif, image/jpeg, image/png;
323
324         # cgo, delayed as long as possible.
325         # If you add a dependency on CGO, you must add the package
326         # to cgoPackages in cmd/dist/test.go as well.
327         RUNTIME
328         < C
329         < runtime/cgo
330         < CGO
331         < runtime/msan, runtime/asan;
332
333         # runtime/race
334         NONE < runtime/race/internal/amd64v1;
335         NONE < runtime/race/internal/amd64v3;
336         CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
337
338         # Bulk of the standard library must not use cgo.
339         # The prohibition stops at net and os/user.
340         C !< fmt, go/types, CRYPTO-MATH, log/slog;
341
342         CGO, OS
343         < plugin;
344
345         CGO, FMT
346         < os/user
347         < archive/tar;
348
349         sync
350         < internal/singleflight;
351
352         os
353         < golang.org/x/net/dns/dnsmessage,
354           golang.org/x/net/lif,
355           golang.org/x/net/route;
356
357         os, runtime, strconv, sync, unsafe,
358         internal/godebug
359         < internal/intern;
360
361         internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
362         < net/netip;
363
364         # net is unavoidable when doing any networking,
365         # so large dependencies must be kept out.
366         # This is a long-looking list but most of these
367         # are small with few dependencies.
368         CGO,
369         golang.org/x/net/dns/dnsmessage,
370         golang.org/x/net/lif,
371         golang.org/x/net/route,
372         internal/godebug,
373         internal/nettrace,
374         internal/poll,
375         internal/singleflight,
376         internal/race,
377         net/netip,
378         os
379         < net;
380
381         fmt, unicode !< net;
382         math/rand !< net; # net uses runtime instead
383
384         # NET is net plus net-helper packages.
385         FMT, net
386         < net/textproto;
387
388         mime, net/textproto, net/url
389         < NET;
390
391         # logging - most packages should not import; http and up is allowed
392         FMT, log/internal
393         < log;
394
395         log, log/slog !< crypto/tls, database/sql, go/importer, testing;
396
397         FMT, log, net
398         < log/syslog;
399
400         RUNTIME
401         < log/slog/internal, log/slog/internal/buffer;
402
403         FMT,
404         encoding, encoding/json,
405         log, log/internal,
406         log/slog/internal, log/slog/internal/buffer,
407         slices
408         < log/slog
409         < log/slog/internal/slogtest, log/slog/internal/benchmarks;
410
411         NET, log
412         < net/mail;
413
414         NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
415         sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
416         crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
417
418         # CRYPTO is core crypto algorithms - no cgo, fmt, net.
419         # Unfortunately, stuck with reflect via encoding/binary.
420         crypto/internal/boring/sig,
421         crypto/internal/boring/syso,
422         encoding/binary,
423         golang.org/x/sys/cpu,
424         hash, embed
425         < crypto
426         < crypto/subtle
427         < crypto/internal/alias
428         < crypto/cipher;
429
430         crypto/cipher,
431         crypto/internal/boring/bcache
432         < crypto/internal/boring
433         < crypto/boring;
434
435         crypto/internal/alias
436         < crypto/internal/randutil
437         < crypto/internal/nistec/fiat
438         < crypto/internal/nistec
439         < crypto/internal/edwards25519/field
440         < crypto/internal/edwards25519;
441
442         crypto/boring
443         < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
444           crypto/sha1, crypto/sha256, crypto/sha512;
445
446         crypto/boring, crypto/internal/edwards25519/field
447         < crypto/ecdh;
448
449         crypto/aes,
450         crypto/des,
451         crypto/ecdh,
452         crypto/hmac,
453         crypto/internal/edwards25519,
454         crypto/md5,
455         crypto/rc4,
456         crypto/sha1,
457         crypto/sha256,
458         crypto/sha512
459         < CRYPTO;
460
461         CGO, fmt, net !< CRYPTO;
462
463         # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
464         CRYPTO, FMT, math/big
465         < crypto/internal/boring/bbig
466         < crypto/rand
467         < crypto/ed25519
468         < encoding/asn1
469         < golang.org/x/crypto/cryptobyte/asn1
470         < golang.org/x/crypto/cryptobyte
471         < crypto/internal/bigmod
472         < crypto/dsa, crypto/elliptic, crypto/rsa
473         < crypto/ecdsa
474         < CRYPTO-MATH;
475
476         CGO, net !< CRYPTO-MATH;
477
478         # TLS, Prince of Dependencies.
479         CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
480         < golang.org/x/crypto/internal/alias
481         < golang.org/x/crypto/internal/subtle
482         < golang.org/x/crypto/chacha20
483         < golang.org/x/crypto/internal/poly1305
484         < golang.org/x/crypto/chacha20poly1305
485         < golang.org/x/crypto/hkdf
486         < crypto/x509/internal/macos
487         < crypto/x509/pkix;
488
489         crypto/internal/boring/fipstls, crypto/x509/pkix
490         < crypto/x509
491         < crypto/tls;
492
493         # crypto-aware packages
494
495         DEBUG, go/build, go/types, text/scanner, crypto/md5
496         < internal/pkgbits
497         < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
498         < go/importer;
499
500         NET, crypto/rand, mime/quotedprintable
501         < mime/multipart;
502
503         crypto/tls
504         < net/smtp;
505
506         crypto/rand
507         < hash/maphash; # for purego implementation
508
509         # HTTP, King of Dependencies.
510
511         FMT
512         < golang.org/x/net/http2/hpack
513         < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
514
515         FMT, NET, container/list, encoding/binary, log
516         < golang.org/x/text/transform
517         < golang.org/x/text/unicode/norm
518         < golang.org/x/text/unicode/bidi
519         < golang.org/x/text/secure/bidirule
520         < golang.org/x/net/idna
521         < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
522
523         NET, crypto/tls
524         < net/http/httptrace;
525
526         compress/gzip,
527         golang.org/x/net/http/httpguts,
528         golang.org/x/net/http/httpproxy,
529         golang.org/x/net/http2/hpack,
530         net/http/internal,
531         net/http/internal/ascii,
532         net/http/internal/testcert,
533         net/http/httptrace,
534         mime/multipart,
535         log
536         < net/http;
537
538         # HTTP-aware packages
539
540         encoding/json, net/http
541         < expvar;
542
543         net/http, net/http/internal/ascii
544         < net/http/cookiejar, net/http/httputil;
545
546         net/http, flag
547         < net/http/httptest;
548
549         net/http, regexp
550         < net/http/cgi
551         < net/http/fcgi;
552
553         # Profiling
554         FMT, compress/gzip, encoding/binary, text/tabwriter
555         < runtime/pprof;
556
557         OS, compress/gzip, internal/lazyregexp
558         < internal/profile;
559
560         html, internal/profile, net/http, runtime/pprof, runtime/trace
561         < net/http/pprof;
562
563         # RPC
564         encoding/gob, encoding/json, go/token, html/template, net/http
565         < net/rpc
566         < net/rpc/jsonrpc;
567
568         # System Information
569         bufio, bytes, internal/cpu, io, os, strings, sync
570         < internal/sysinfo;
571
572         # Test-only
573         log
574         < testing/iotest
575         < testing/fstest;
576
577         FMT, flag, math/rand
578         < testing/quick;
579
580         FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
581         < testing;
582
583         log/slog, testing
584         < testing/slogtest;
585
586         FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
587         internal/godebug, math/rand, encoding/hex, crypto/sha256
588         < internal/fuzz;
589
590         internal/fuzz, internal/testlog, runtime/pprof, regexp
591         < testing/internal/testdeps;
592
593         OS, flag, testing, internal/cfg, internal/platform, internal/goroot
594         < internal/testenv;
595
596         OS, encoding/base64
597         < internal/obscuretestdata;
598
599         CGO, OS, fmt
600         < internal/testpty;
601
602         NET, testing, math/rand
603         < golang.org/x/net/nettest;
604
605         syscall
606         < os/exec/internal/fdtest;
607
608         FMT, container/heap, math/rand
609         < internal/trace;
610
611         FMT
612         < internal/diff, internal/txtar;
613
614         FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
615         internal/coverage, internal/coverage/uleb128
616         < internal/coverage/cmerge,
617           internal/coverage/pods,
618           internal/coverage/slicereader,
619           internal/coverage/slicewriter;
620
621         internal/coverage/slicereader, internal/coverage/slicewriter
622         < internal/coverage/stringtab
623         < internal/coverage/decodecounter, internal/coverage/decodemeta,
624           internal/coverage/encodecounter, internal/coverage/encodemeta;
625
626         internal/coverage/cmerge
627         < internal/coverage/cformat;
628
629         internal/coverage, crypto/sha256, FMT
630         < cmd/internal/cov/covcmd;
631
632     encoding/json,
633         runtime/debug,
634         internal/coverage/calloc,
635         internal/coverage/cformat,
636         internal/coverage/decodecounter, internal/coverage/decodemeta,
637         internal/coverage/encodecounter, internal/coverage/encodemeta,
638         internal/coverage/pods
639         < runtime/coverage;
640 `
641
642 // listStdPkgs returns the same list of packages as "go list std".
643 func listStdPkgs(goroot string) ([]string, error) {
644         // Based on cmd/go's matchPackages function.
645         var pkgs []string
646
647         src := filepath.Join(goroot, "src") + string(filepath.Separator)
648         walkFn := func(path string, d fs.DirEntry, err error) error {
649                 if err != nil || !d.IsDir() || path == src {
650                         return nil
651                 }
652
653                 base := filepath.Base(path)
654                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
655                         return filepath.SkipDir
656                 }
657
658                 name := filepath.ToSlash(path[len(src):])
659                 if name == "builtin" || name == "cmd" {
660                         return filepath.SkipDir
661                 }
662
663                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
664                 return nil
665         }
666         if err := filepath.WalkDir(src, walkFn); err != nil {
667                 return nil, err
668         }
669         return pkgs, nil
670 }
671
672 func TestDependencies(t *testing.T) {
673         if !testenv.HasSrc() {
674                 // Tests run in a limited file system and we do not
675                 // provide access to every source file.
676                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
677         }
678
679         ctxt := Default
680         all, err := listStdPkgs(ctxt.GOROOT)
681         if err != nil {
682                 t.Fatal(err)
683         }
684         sort.Strings(all)
685
686         sawImport := map[string]map[string]bool{} // from package => to package => true
687         policy := depsPolicy(t)
688
689         for _, pkg := range all {
690                 imports, err := findImports(pkg)
691                 if err != nil {
692                         t.Error(err)
693                         continue
694                 }
695                 if sawImport[pkg] == nil {
696                         sawImport[pkg] = map[string]bool{}
697                 }
698                 var bad []string
699                 for _, imp := range imports {
700                         sawImport[pkg][imp] = true
701                         if !policy.HasEdge(pkg, imp) {
702                                 bad = append(bad, imp)
703                         }
704                 }
705                 if bad != nil {
706                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
707                 }
708         }
709 }
710
711 var buildIgnore = []byte("\n//go:build ignore")
712
713 func findImports(pkg string) ([]string, error) {
714         vpkg := pkg
715         if strings.HasPrefix(pkg, "golang.org") {
716                 vpkg = "vendor/" + pkg
717         }
718         dir := filepath.Join(Default.GOROOT, "src", vpkg)
719         files, err := os.ReadDir(dir)
720         if err != nil {
721                 return nil, err
722         }
723         var imports []string
724         var haveImport = map[string]bool{}
725         if pkg == "crypto/internal/boring" {
726                 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
727         }
728         fset := token.NewFileSet()
729         for _, file := range files {
730                 name := file.Name()
731                 if name == "slice_go14.go" || name == "slice_go18.go" {
732                         // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
733                         continue
734                 }
735                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
736                         continue
737                 }
738                 info := fileInfo{
739                         name: filepath.Join(dir, name),
740                         fset: fset,
741                 }
742                 f, err := os.Open(info.name)
743                 if err != nil {
744                         return nil, err
745                 }
746                 err = readGoInfo(f, &info)
747                 f.Close()
748                 if err != nil {
749                         return nil, fmt.Errorf("reading %v: %v", name, err)
750                 }
751                 if info.parsed.Name.Name == "main" {
752                         continue
753                 }
754                 if bytes.Contains(info.header, buildIgnore) {
755                         continue
756                 }
757                 for _, imp := range info.imports {
758                         path := imp.path
759                         if !haveImport[path] {
760                                 haveImport[path] = true
761                                 imports = append(imports, path)
762                         }
763                 }
764         }
765         sort.Strings(imports)
766         return imports, nil
767 }
768
769 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
770 func depsPolicy(t *testing.T) *dag.Graph {
771         g, err := dag.Parse(depsRules)
772         if err != nil {
773                 t.Fatal(err)
774         }
775         return g
776 }
777
778 // TestStdlibLowercase tests that all standard library package names are
779 // lowercase. See Issue 40065.
780 func TestStdlibLowercase(t *testing.T) {
781         if !testenv.HasSrc() {
782                 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
783         }
784
785         ctxt := Default
786         all, err := listStdPkgs(ctxt.GOROOT)
787         if err != nil {
788                 t.Fatal(err)
789         }
790
791         for _, pkgname := range all {
792                 if strings.ToLower(pkgname) != pkgname {
793                         t.Errorf("package %q should not use upper-case path", pkgname)
794                 }
795         }
796 }
797
798 // TestFindImports tests that findImports works.  See #43249.
799 func TestFindImports(t *testing.T) {
800         imports, err := findImports("go/build")
801         if err != nil {
802                 t.Fatal(err)
803         }
804         t.Logf("go/build imports %q", imports)
805         want := []string{"bytes", "os", "path/filepath", "strings"}
806 wantLoop:
807         for _, w := range want {
808                 for _, imp := range imports {
809                         if imp == w {
810                                 continue wantLoop
811                         }
812                 }
813                 t.Errorf("expected to find %q in import list", w)
814         }
815 }