9 "honnef.co/go/tools/internal/cache"
12 // computeHash computes a package's hash. The hash is based on all Go
13 // files that make up the package, as well as the hashes of imported
15 func computeHash(pkg *PackageSpec) (cache.ActionID, error) {
16 key := cache.NewHash("package " + pkg.PkgPath)
17 fmt.Fprintf(key, "goos %s goarch %s\n", runtime.GOOS, runtime.GOARCH)
18 fmt.Fprintf(key, "import %q\n", pkg.PkgPath)
20 // Compute the hashes of all files making up the package. As an
21 // optimization, we use the build ID that Go already computed for
22 // us, because it is virtually identical to hashed all
25 if pkg.ExportFile != "" {
26 id, err := getBuildid(pkg.ExportFile)
28 if idx := strings.IndexRune(id, '/'); idx > -1 {
29 fmt.Fprintf(key, "files %s\n", id[:idx])
35 for _, f := range pkg.CompiledGoFiles {
36 h, err := cache.FileHash(f)
38 return cache.ActionID{}, err
40 fmt.Fprintf(key, "file %s %x\n", f, h)
44 imps := make([]*PackageSpec, 0, len(pkg.Imports))
45 for _, v := range pkg.Imports {
46 imps = append(imps, v)
48 sort.Slice(imps, func(i, j int) bool {
49 return imps[i].PkgPath < imps[j].PkgPath
52 for _, dep := range imps {
53 if dep.ExportFile == "" {
54 fmt.Fprintf(key, "import %s \n", dep.PkgPath)
56 id, err := getBuildid(dep.ExportFile)
58 fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, id)
60 fh, err := cache.FileHash(dep.ExportFile)
62 return cache.ActionID{}, err
64 fmt.Fprintf(key, "import %s %x\n", dep.PkgPath, fh)
71 var buildidCache = map[string]string{}
73 func getBuildid(f string) (string, error) {
74 if h, ok := buildidCache[f]; ok {