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