File: hash.go

package info (click to toggle)
golang-honnef-go-tools 2024.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,968 kB
  • sloc: sh: 132; xml: 48; lisp: 31; makefile: 11; javascript: 1
file content (98 lines) | stat: -rw-r--r-- 2,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package loader

import (
	"fmt"
	"runtime"
	"sort"
	"strings"

	"honnef.co/go/tools/go/buildid"
	"honnef.co/go/tools/lintcmd/cache"
)

// computeHash computes a package's hash. The hash is based on all Go
// files that make up the package, as well as the hashes of imported
// packages.
func computeHash(c *cache.Cache, pkg *PackageSpec) (cache.ActionID, error) {
	key := c.NewHash("package " + pkg.PkgPath)
	fmt.Fprintf(key, "goos %s goarch %s\n", runtime.GOOS, runtime.GOARCH)
	fmt.Fprintf(key, "import %q\n", pkg.PkgPath)

	// Compute the hashes of all files making up the package. As an
	// optimization, we use the build ID that Go already computed for us,
	// because it is virtually identical to hashing all CompiledGoFiles. It is
	// also sensitive to the Go version declared in go.mod.
	success := false
	if pkg.ExportFile != "" {
		id, err := getBuildid(pkg.ExportFile)
		if err == nil {
			if idx := strings.IndexRune(id, '/'); idx > -1 {
				fmt.Fprintf(key, "files %s\n", id[:idx])
				success = true
			}
		}
	}
	if !success {
		for _, f := range pkg.CompiledGoFiles {
			h, err := cache.FileHash(f)
			if err != nil {
				return cache.ActionID{}, err
			}
			fmt.Fprintf(key, "file %s %x\n", f, h)
		}
		if pkg.Module != nil && pkg.Module.GoMod != "" {
			// The go.mod file specifies the language version, which affects how
			// packages are analyzed.
			h, err := cache.FileHash(pkg.Module.GoMod)
			if err != nil {
				// TODO(dh): this doesn't work for tests because the go.mod file doesn't
				// exist on disk and is instead provided via an overlay. However, we're
				// unlikely to get here in the first place, as reading the build ID from
				// the export file is likely to succeed.
				return cache.ActionID{}, fmt.Errorf("couldn't hash go.mod: %w", err)
			} else {
				fmt.Fprintf(key, "file %s %x\n", pkg.Module.GoMod, h)
			}
		}
	}

	imps := make([]*PackageSpec, 0, len(pkg.Imports))
	for _, v := range pkg.Imports {
		imps = append(imps, v)
	}
	sort.Slice(imps, func(i, j int) bool {
		return imps[i].PkgPath < imps[j].PkgPath
	})

	for _, dep := range imps {
		if dep.ExportFile == "" {
			fmt.Fprintf(key, "import %s \n", dep.PkgPath)
		} else {
			id, err := getBuildid(dep.ExportFile)
			if err == nil {
				fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, id)
			} else {
				fh, err := cache.FileHash(dep.ExportFile)
				if err != nil {
					return cache.ActionID{}, err
				}
				fmt.Fprintf(key, "import %s %x\n", dep.PkgPath, fh)
			}
		}
	}
	return key.Sum(), nil
}

var buildidCache = map[string]string{}

func getBuildid(f string) (string, error) {
	if h, ok := buildidCache[f]; ok {
		return h, nil
	}
	h, err := buildid.ReadFile(f)
	if err != nil {
		return "", err
	}
	buildidCache[f] = h
	return h, nil
}