...
Run Format

Source file src/go/build/deps_test.go

Documentation: go/build

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  	"io/ioutil"
    14  	"os"
    15  	"path/filepath"
    16  	"runtime"
    17  	"sort"
    18  	"strconv"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  // pkgDeps defines the expected dependencies between packages in
    24  // the Go source tree. It is a statement of policy.
    25  // Changes should not be made to this map without prior discussion.
    26  //
    27  // The map contains two kinds of entries:
    28  // 1) Lower-case keys are standard import paths and list the
    29  // allowed imports in that package.
    30  // 2) Upper-case keys define aliases for package sets, which can then
    31  // be used as dependencies by other rules.
    32  //
    33  // DO NOT CHANGE THIS DATA TO FIX BUILDS.
    34  //
    35  var pkgDeps = map[string][]string{
    36  	// L0 is the lowest level, core, nearly unavoidable packages.
    37  	"errors":                  {},
    38  	"io":                      {"errors", "sync", "sync/atomic"},
    39  	"runtime":                 {"unsafe", "runtime/internal/atomic", "runtime/internal/sys", "internal/cpu", "internal/bytealg"},
    40  	"runtime/internal/sys":    {},
    41  	"runtime/internal/atomic": {"unsafe", "runtime/internal/sys"},
    42  	"internal/race":           {"runtime", "unsafe"},
    43  	"sync":                    {"internal/race", "runtime", "sync/atomic", "unsafe"},
    44  	"sync/atomic":             {"unsafe"},
    45  	"unsafe":                  {},
    46  	"internal/cpu":            {},
    47  	"internal/bytealg":        {"unsafe", "internal/cpu"},
    48  
    49  	"L0": {
    50  		"errors",
    51  		"io",
    52  		"runtime",
    53  		"runtime/internal/atomic",
    54  		"sync",
    55  		"sync/atomic",
    56  		"unsafe",
    57  		"internal/cpu",
    58  		"internal/bytealg",
    59  	},
    60  
    61  	// L1 adds simple functions and strings processing,
    62  	// but not Unicode tables.
    63  	"math":          {"internal/cpu", "unsafe"},
    64  	"math/bits":     {},
    65  	"math/cmplx":    {"math"},
    66  	"math/rand":     {"L0", "math"},
    67  	"strconv":       {"L0", "unicode/utf8", "math", "math/bits"},
    68  	"unicode/utf16": {},
    69  	"unicode/utf8":  {},
    70  
    71  	"L1": {
    72  		"L0",
    73  		"math",
    74  		"math/bits",
    75  		"math/cmplx",
    76  		"math/rand",
    77  		"sort",
    78  		"strconv",
    79  		"unicode/utf16",
    80  		"unicode/utf8",
    81  	},
    82  
    83  	// L2 adds Unicode and strings processing.
    84  	"bufio":   {"L0", "unicode/utf8", "bytes"},
    85  	"bytes":   {"L0", "unicode", "unicode/utf8"},
    86  	"path":    {"L0", "unicode/utf8", "strings"},
    87  	"strings": {"L0", "unicode", "unicode/utf8"},
    88  	"unicode": {},
    89  
    90  	"L2": {
    91  		"L1",
    92  		"bufio",
    93  		"bytes",
    94  		"path",
    95  		"strings",
    96  		"unicode",
    97  	},
    98  
    99  	// L3 adds reflection and some basic utility packages
   100  	// and interface definitions, but nothing that makes
   101  	// system calls.
   102  	"crypto":                 {"L2", "hash"}, // interfaces
   103  	"crypto/cipher":          {"L2", "crypto/subtle", "crypto/internal/subtle"},
   104  	"crypto/internal/subtle": {"unsafe", "reflect"}, // reflect behind a appengine tag
   105  	"crypto/subtle":          {},
   106  	"encoding/base32":        {"L2"},
   107  	"encoding/base64":        {"L2", "encoding/binary"},
   108  	"encoding/binary":        {"L2", "reflect"},
   109  	"hash":                   {"L2"}, // interfaces
   110  	"hash/adler32":           {"L2", "hash"},
   111  	"hash/crc32":             {"L2", "hash"},
   112  	"hash/crc64":             {"L2", "hash"},
   113  	"hash/fnv":               {"L2", "hash"},
   114  	"image":                  {"L2", "image/color"}, // interfaces
   115  	"image/color":            {"L2"},                // interfaces
   116  	"image/color/palette":    {"L2", "image/color"},
   117  	"reflect":                {"L2"},
   118  	"sort":                   {"reflect"},
   119  
   120  	"L3": {
   121  		"L2",
   122  		"crypto",
   123  		"crypto/cipher",
   124  		"crypto/internal/subtle",
   125  		"crypto/subtle",
   126  		"encoding/base32",
   127  		"encoding/base64",
   128  		"encoding/binary",
   129  		"hash",
   130  		"hash/adler32",
   131  		"hash/crc32",
   132  		"hash/crc64",
   133  		"hash/fnv",
   134  		"image",
   135  		"image/color",
   136  		"image/color/palette",
   137  		"reflect",
   138  	},
   139  
   140  	// End of linear dependency definitions.
   141  
   142  	// Operating system access.
   143  	"syscall":                           {"L0", "internal/race", "internal/syscall/windows/sysdll", "syscall/js", "unicode/utf16"},
   144  	"syscall/js":                        {"L0"},
   145  	"internal/syscall/unix":             {"L0", "syscall"},
   146  	"internal/syscall/windows":          {"L0", "syscall", "internal/syscall/windows/sysdll"},
   147  	"internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
   148  	"time": {
   149  		// "L0" without the "io" package:
   150  		"errors",
   151  		"runtime",
   152  		"runtime/internal/atomic",
   153  		"sync",
   154  		"sync/atomic",
   155  		"unsafe",
   156  		// Other time dependencies:
   157  		"internal/syscall/windows/registry",
   158  		"syscall",
   159  	},
   160  
   161  	"internal/poll":    {"L0", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows"},
   162  	"internal/testlog": {"L0"},
   163  	"os":               {"L1", "os", "syscall", "time", "internal/poll", "internal/syscall/windows", "internal/syscall/unix", "internal/testlog"},
   164  	"path/filepath":    {"L2", "os", "syscall", "internal/syscall/windows"},
   165  	"io/ioutil":        {"L2", "os", "path/filepath", "time"},
   166  	"os/exec":          {"L2", "os", "context", "path/filepath", "syscall"},
   167  	"os/signal":        {"L2", "os", "syscall"},
   168  
   169  	// OS enables basic operating system functionality,
   170  	// but not direct use of package syscall, nor os/signal.
   171  	"OS": {
   172  		"io/ioutil",
   173  		"os",
   174  		"os/exec",
   175  		"path/filepath",
   176  		"time",
   177  	},
   178  
   179  	// Formatted I/O: few dependencies (L1) but we must add reflect.
   180  	"fmt": {"L1", "os", "reflect"},
   181  	"log": {"L1", "os", "fmt", "time"},
   182  
   183  	// Packages used by testing must be low-level (L2+fmt).
   184  	"regexp":         {"L2", "regexp/syntax"},
   185  	"regexp/syntax":  {"L2"},
   186  	"runtime/debug":  {"L2", "fmt", "io/ioutil", "os", "time"},
   187  	"runtime/pprof":  {"L2", "compress/gzip", "context", "encoding/binary", "fmt", "io/ioutil", "os", "text/tabwriter", "time"},
   188  	"runtime/trace":  {"L0", "context", "fmt"},
   189  	"text/tabwriter": {"L2"},
   190  
   191  	"testing":          {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
   192  	"testing/iotest":   {"L2", "log"},
   193  	"testing/quick":    {"L2", "flag", "fmt", "reflect", "time"},
   194  	"internal/testenv": {"L2", "OS", "flag", "testing", "syscall"},
   195  
   196  	// L4 is defined as L3+fmt+log+time, because in general once
   197  	// you're using L3 packages, use of fmt, log, or time is not a big deal.
   198  	"L4": {
   199  		"L3",
   200  		"fmt",
   201  		"log",
   202  		"time",
   203  	},
   204  
   205  	// Go parser.
   206  	"go/ast":     {"L4", "OS", "go/scanner", "go/token"},
   207  	"go/doc":     {"L4", "go/ast", "go/token", "regexp", "text/template"},
   208  	"go/parser":  {"L4", "OS", "go/ast", "go/scanner", "go/token"},
   209  	"go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
   210  	"go/scanner": {"L4", "OS", "go/token"},
   211  	"go/token":   {"L4"},
   212  
   213  	"GOPARSER": {
   214  		"go/ast",
   215  		"go/doc",
   216  		"go/parser",
   217  		"go/printer",
   218  		"go/scanner",
   219  		"go/token",
   220  	},
   221  
   222  	"go/format":       {"L4", "GOPARSER", "internal/format"},
   223  	"internal/format": {"L4", "GOPARSER"},
   224  
   225  	// Go type checking.
   226  	"go/constant":               {"L4", "go/token", "math/big"},
   227  	"go/importer":               {"L4", "go/build", "go/internal/gccgoimporter", "go/internal/gcimporter", "go/internal/srcimporter", "go/token", "go/types"},
   228  	"go/internal/gcimporter":    {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
   229  	"go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
   230  	"go/internal/srcimporter":   {"L4", "OS", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"},
   231  	"go/types":                  {"L4", "GOPARSER", "container/heap", "go/constant"},
   232  
   233  	// One of a kind.
   234  	"archive/tar":              {"L4", "OS", "syscall", "os/user"},
   235  	"archive/zip":              {"L4", "OS", "compress/flate"},
   236  	"container/heap":           {"sort"},
   237  	"compress/bzip2":           {"L4"},
   238  	"compress/flate":           {"L4"},
   239  	"compress/gzip":            {"L4", "compress/flate"},
   240  	"compress/lzw":             {"L4"},
   241  	"compress/zlib":            {"L4", "compress/flate"},
   242  	"context":                  {"errors", "fmt", "reflect", "sync", "time"},
   243  	"database/sql":             {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
   244  	"database/sql/driver":      {"L4", "context", "time", "database/sql/internal"},
   245  	"debug/dwarf":              {"L4"},
   246  	"debug/elf":                {"L4", "OS", "debug/dwarf", "compress/zlib"},
   247  	"debug/gosym":              {"L4"},
   248  	"debug/macho":              {"L4", "OS", "debug/dwarf", "compress/zlib"},
   249  	"debug/pe":                 {"L4", "OS", "debug/dwarf", "compress/zlib"},
   250  	"debug/plan9obj":           {"L4", "OS"},
   251  	"encoding":                 {"L4"},
   252  	"encoding/ascii85":         {"L4"},
   253  	"encoding/asn1":            {"L4", "math/big"},
   254  	"encoding/csv":             {"L4"},
   255  	"encoding/gob":             {"L4", "OS", "encoding"},
   256  	"encoding/hex":             {"L4"},
   257  	"encoding/json":            {"L4", "encoding"},
   258  	"encoding/pem":             {"L4"},
   259  	"encoding/xml":             {"L4", "encoding"},
   260  	"flag":                     {"L4", "OS"},
   261  	"go/build":                 {"L4", "OS", "GOPARSER"},
   262  	"html":                     {"L4"},
   263  	"image/draw":               {"L4", "image/internal/imageutil"},
   264  	"image/gif":                {"L4", "compress/lzw", "image/color/palette", "image/draw"},
   265  	"image/internal/imageutil": {"L4"},
   266  	"image/jpeg":               {"L4", "image/internal/imageutil"},
   267  	"image/png":                {"L4", "compress/zlib"},
   268  	"index/suffixarray":        {"L4", "regexp"},
   269  	"internal/singleflight":    {"sync"},
   270  	"internal/trace":           {"L4", "OS"},
   271  	"math/big":                 {"L4"},
   272  	"mime":                     {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
   273  	"mime/quotedprintable":     {"L4"},
   274  	"net/internal/socktest":    {"L4", "OS", "syscall", "internal/syscall/windows"},
   275  	"net/url":                  {"L4"},
   276  	"plugin":                   {"L0", "OS", "CGO"},
   277  	"runtime/pprof/internal/profile": {"L4", "OS", "compress/gzip", "regexp"},
   278  	"testing/internal/testdeps":      {"L4", "internal/testlog", "runtime/pprof", "regexp"},
   279  	"text/scanner":                   {"L4", "OS"},
   280  	"text/template/parse":            {"L4"},
   281  
   282  	"html/template": {
   283  		"L4", "OS", "encoding/json", "html", "text/template",
   284  		"text/template/parse",
   285  	},
   286  	"text/template": {
   287  		"L4", "OS", "net/url", "text/template/parse",
   288  	},
   289  
   290  	// Cgo.
   291  	// If you add a dependency on CGO, you must add the package to
   292  	// cgoPackages in cmd/dist/test.go.
   293  	"runtime/cgo": {"L0", "C"},
   294  	"CGO":         {"C", "runtime/cgo"},
   295  
   296  	// Fake entry to satisfy the pseudo-import "C"
   297  	// that shows up in programs that use cgo.
   298  	"C": {},
   299  
   300  	// Race detector/MSan uses cgo.
   301  	"runtime/race": {"C"},
   302  	"runtime/msan": {"C"},
   303  
   304  	// Plan 9 alone needs io/ioutil and os.
   305  	"os/user": {"L4", "CGO", "io/ioutil", "os", "syscall", "internal/syscall/windows", "internal/syscall/windows/registry"},
   306  
   307  	// Internal package used only for testing.
   308  	"os/signal/internal/pty": {"CGO", "fmt", "os", "syscall"},
   309  
   310  	// Basic networking.
   311  	// Because net must be used by any package that wants to
   312  	// do networking portably, it must have a small dependency set: just L0+basic os.
   313  	"net": {
   314  		"L0", "CGO",
   315  		"context", "math/rand", "os", "reflect", "sort", "syscall", "time",
   316  		"internal/nettrace", "internal/poll",
   317  		"internal/syscall/windows", "internal/singleflight", "internal/race",
   318  		"golang_org/x/net/dns/dnsmessage", "golang_org/x/net/lif", "golang_org/x/net/route",
   319  	},
   320  
   321  	// NET enables use of basic network-related packages.
   322  	"NET": {
   323  		"net",
   324  		"mime",
   325  		"net/textproto",
   326  		"net/url",
   327  	},
   328  
   329  	// Uses of networking.
   330  	"log/syslog":    {"L4", "OS", "net"},
   331  	"net/mail":      {"L4", "NET", "OS", "mime"},
   332  	"net/textproto": {"L4", "OS", "net"},
   333  
   334  	// Core crypto.
   335  	"crypto/aes":               {"L3"},
   336  	"crypto/des":               {"L3"},
   337  	"crypto/hmac":              {"L3"},
   338  	"crypto/internal/randutil": {"io", "sync"},
   339  	"crypto/md5":               {"L3"},
   340  	"crypto/rc4":               {"L3"},
   341  	"crypto/sha1":              {"L3"},
   342  	"crypto/sha256":            {"L3"},
   343  	"crypto/sha512":            {"L3"},
   344  
   345  	"CRYPTO": {
   346  		"crypto/aes",
   347  		"crypto/des",
   348  		"crypto/hmac",
   349  		"crypto/internal/randutil",
   350  		"crypto/md5",
   351  		"crypto/rc4",
   352  		"crypto/sha1",
   353  		"crypto/sha256",
   354  		"crypto/sha512",
   355  		"golang_org/x/crypto/chacha20poly1305",
   356  		"golang_org/x/crypto/curve25519",
   357  		"golang_org/x/crypto/poly1305",
   358  	},
   359  
   360  	// Random byte, number generation.
   361  	// This would be part of core crypto except that it imports
   362  	// math/big, which imports fmt.
   363  	"crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "syscall/js", "internal/syscall/unix"},
   364  
   365  	// Mathematical crypto: dependencies on fmt (L4) and math/big.
   366  	// We could avoid some of the fmt, but math/big imports fmt anyway.
   367  	"crypto/dsa":      {"L4", "CRYPTO", "math/big"},
   368  	"crypto/ecdsa":    {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
   369  	"crypto/elliptic": {"L4", "CRYPTO", "math/big"},
   370  	"crypto/rsa":      {"L4", "CRYPTO", "crypto/rand", "math/big"},
   371  
   372  	"CRYPTO-MATH": {
   373  		"CRYPTO",
   374  		"crypto/dsa",
   375  		"crypto/ecdsa",
   376  		"crypto/elliptic",
   377  		"crypto/rand",
   378  		"crypto/rsa",
   379  		"encoding/asn1",
   380  		"math/big",
   381  	},
   382  
   383  	// SSL/TLS.
   384  	"crypto/tls": {
   385  		"L4", "CRYPTO-MATH", "OS",
   386  		"container/list", "crypto/x509", "encoding/pem", "net", "syscall",
   387  	},
   388  	"crypto/x509": {
   389  		"L4", "CRYPTO-MATH", "OS", "CGO",
   390  		"crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "os/user", "syscall", "net/url",
   391  		"golang_org/x/crypto/cryptobyte", "golang_org/x/crypto/cryptobyte/asn1",
   392  	},
   393  	"crypto/x509/pkix": {"L4", "CRYPTO-MATH", "encoding/hex"},
   394  
   395  	// Simple net+crypto-aware packages.
   396  	"mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
   397  	"net/smtp":       {"L4", "CRYPTO", "NET", "crypto/tls"},
   398  
   399  	// HTTP, kingpin of dependencies.
   400  	"net/http": {
   401  		"L4", "NET", "OS",
   402  		"compress/gzip",
   403  		"container/list",
   404  		"context",
   405  		"crypto/rand",
   406  		"crypto/tls",
   407  		"golang_org/x/net/http/httpguts",
   408  		"golang_org/x/net/http/httpproxy",
   409  		"golang_org/x/net/http2/hpack",
   410  		"golang_org/x/net/idna",
   411  		"golang_org/x/text/unicode/norm",
   412  		"golang_org/x/text/width",
   413  		"internal/nettrace",
   414  		"mime/multipart",
   415  		"net/http/httptrace",
   416  		"net/http/internal",
   417  		"runtime/debug",
   418  		"syscall/js",
   419  	},
   420  	"net/http/internal":  {"L4"},
   421  	"net/http/httptrace": {"context", "crypto/tls", "internal/nettrace", "net", "net/textproto", "reflect", "time"},
   422  
   423  	// HTTP-using packages.
   424  	"expvar":             {"L4", "OS", "encoding/json", "net/http"},
   425  	"net/http/cgi":       {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
   426  	"net/http/cookiejar": {"L4", "NET", "net/http"},
   427  	"net/http/fcgi":      {"L4", "NET", "OS", "context", "net/http", "net/http/cgi"},
   428  	"net/http/httptest": {
   429  		"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal", "crypto/x509",
   430  		"golang_org/x/net/http/httpguts",
   431  	},
   432  	"net/http/httputil": {"L4", "NET", "OS", "context", "net/http", "net/http/internal"},
   433  	"net/http/pprof":    {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
   434  	"net/rpc":           {"L4", "NET", "encoding/gob", "html/template", "net/http"},
   435  	"net/rpc/jsonrpc":   {"L4", "NET", "encoding/json", "net/rpc"},
   436  }
   437  
   438  // isMacro reports whether p is a package dependency macro
   439  // (uppercase name).
   440  func isMacro(p string) bool {
   441  	return 'A' <= p[0] && p[0] <= 'Z'
   442  }
   443  
   444  func allowed(pkg string) map[string]bool {
   445  	m := map[string]bool{}
   446  	var allow func(string)
   447  	allow = func(p string) {
   448  		if m[p] {
   449  			return
   450  		}
   451  		m[p] = true // set even for macros, to avoid loop on cycle
   452  
   453  		// Upper-case names are macro-expanded.
   454  		if isMacro(p) {
   455  			for _, pp := range pkgDeps[p] {
   456  				allow(pp)
   457  			}
   458  		}
   459  	}
   460  	for _, pp := range pkgDeps[pkg] {
   461  		allow(pp)
   462  	}
   463  	return m
   464  }
   465  
   466  // listStdPkgs returns the same list of packages as "go list std".
   467  func listStdPkgs(goroot string) ([]string, error) {
   468  	// Based on cmd/go's matchPackages function.
   469  	var pkgs []string
   470  
   471  	src := filepath.Join(goroot, "src") + string(filepath.Separator)
   472  	walkFn := func(path string, fi os.FileInfo, err error) error {
   473  		if err != nil || !fi.IsDir() || path == src {
   474  			return nil
   475  		}
   476  
   477  		base := filepath.Base(path)
   478  		if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
   479  			return filepath.SkipDir
   480  		}
   481  
   482  		name := filepath.ToSlash(path[len(src):])
   483  		if name == "builtin" || name == "cmd" || strings.Contains(name, "golang_org") {
   484  			return filepath.SkipDir
   485  		}
   486  
   487  		pkgs = append(pkgs, name)
   488  		return nil
   489  	}
   490  	if err := filepath.Walk(src, walkFn); err != nil {
   491  		return nil, err
   492  	}
   493  	return pkgs, nil
   494  }
   495  
   496  func TestDependencies(t *testing.T) {
   497  	iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
   498  	if runtime.GOOS == "nacl" || iOS {
   499  		// Tests run in a limited file system and we do not
   500  		// provide access to every source file.
   501  		t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
   502  	}
   503  
   504  	ctxt := Default
   505  	all, err := listStdPkgs(ctxt.GOROOT)
   506  	if err != nil {
   507  		t.Fatal(err)
   508  	}
   509  	sort.Strings(all)
   510  
   511  	for _, pkg := range all {
   512  		imports, err := findImports(pkg)
   513  		if err != nil {
   514  			t.Error(err)
   515  			continue
   516  		}
   517  		ok := allowed(pkg)
   518  		var bad []string
   519  		for _, imp := range imports {
   520  			if !ok[imp] {
   521  				bad = append(bad, imp)
   522  			}
   523  		}
   524  		if bad != nil {
   525  			t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
   526  		}
   527  	}
   528  }
   529  
   530  var buildIgnore = []byte("\n// +build ignore")
   531  
   532  func findImports(pkg string) ([]string, error) {
   533  	dir := filepath.Join(Default.GOROOT, "src", pkg)
   534  	files, err := ioutil.ReadDir(dir)
   535  	if err != nil {
   536  		return nil, err
   537  	}
   538  	var imports []string
   539  	var haveImport = map[string]bool{}
   540  	for _, file := range files {
   541  		name := file.Name()
   542  		if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
   543  			continue
   544  		}
   545  		f, err := os.Open(filepath.Join(dir, name))
   546  		if err != nil {
   547  			return nil, err
   548  		}
   549  		var imp []string
   550  		data, err := readImports(f, false, &imp)
   551  		f.Close()
   552  		if err != nil {
   553  			return nil, fmt.Errorf("reading %v: %v", name, err)
   554  		}
   555  		if bytes.Contains(data, buildIgnore) {
   556  			continue
   557  		}
   558  		for _, quoted := range imp {
   559  			path, err := strconv.Unquote(quoted)
   560  			if err != nil {
   561  				continue
   562  			}
   563  			if !haveImport[path] {
   564  				haveImport[path] = true
   565  				imports = append(imports, path)
   566  			}
   567  		}
   568  	}
   569  	sort.Strings(imports)
   570  	return imports, nil
   571  }
   572  

View as plain text