-
Notifications
You must be signed in to change notification settings - Fork 4
/
known_imports.go
48 lines (42 loc) · 1013 Bytes
/
known_imports.go
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
package knownimports
import (
_ "embed"
"go/build"
"os"
"path/filepath"
"strings"
)
//go:embed go_root_packages
var goRootPkgs string
// Optimisation to avoid going to disk, but also includes packages like unsafe and C that don't actually appear in
// the SDK. We still check in case a new Go version includes a new package.
var goRootImports = map[string]struct{}{
"unsafe": {},
"C": {},
}
func init() {
for _, pkg := range strings.Split(goRootPkgs, "\n") {
pkg := strings.TrimSpace(pkg)
if pkg == "" {
continue
}
goRootImports[pkg] = struct{}{}
}
}
func IsInGoRoot(i string) bool {
if strings.HasPrefix(i, "crypto/") {
return true
}
_, ok := goRootImports[i]
if ok {
return true
}
// Attempt to look this up in the GOROOT incase the users is on a newer version of Go to us
b := build.Default
path := filepath.Join(b.GOROOT, "pkg", b.GOOS+"_"+b.GOARCH, i+".a")
if _, err := os.Lstat(path); err == nil {
goRootImports[i] = struct{}{}
return true
}
return false
}