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