findRoots sets member RemoteGOROOT, RemoteGOPATHs and LocalGomods. This causes disk I/O as it checks for file presence. Returns the number of missing files.
()
| 1038 | // |
| 1039 | // Returns the number of missing files. |
| 1040 | func (s *Snapshot) findRoots() int { |
| 1041 | // TODO(maruel): Reduce memory allocations in this function. |
| 1042 | s.RemoteGOPATHs = map[string]string{} |
| 1043 | s.LocalGomods = map[string]string{} |
| 1044 | missing := 0 |
| 1045 | gmc := gomodCache{} |
| 1046 | for _, f := range getFiles(s.Goroutines) { |
| 1047 | // TODO(maruel): Could a stack dump have mixed cases? I think it's |
| 1048 | // possible, need to confirm and handle. |
| 1049 | //log.Printf(" Analyzing %s", f) |
| 1050 | |
| 1051 | // First checks skip file I/O. |
| 1052 | if s.RemoteGOROOT != "" && strings.HasPrefix(f, s.RemoteGOROOT+"/src/") { |
| 1053 | // stdlib. |
| 1054 | continue |
| 1055 | } |
| 1056 | if hasSrcPrefix(f, s.RemoteGOPATHs) { |
| 1057 | // $GOPATH/src or go.mod dependency in $GOPATH/pkg/mod. |
| 1058 | continue |
| 1059 | } |
| 1060 | if hasPrefix(f, s.LocalGomods) { |
| 1061 | continue |
| 1062 | } |
| 1063 | |
| 1064 | // At this point, disk will be looked up. |
| 1065 | parts := splitPath(f) |
| 1066 | // Initializes RemoteGOROOT. |
| 1067 | const src = "/src" |
| 1068 | if s.RemoteGOROOT == "" { |
| 1069 | if r := isRootedIn(s.LocalGOROOT+src, parts); r != "" { |
| 1070 | s.RemoteGOROOT = r[:len(r)-len(src)] |
| 1071 | //log.Printf("Found RemoteGOROOT=%s", s.RemoteGOROOT) |
| 1072 | continue |
| 1073 | } |
| 1074 | } |
| 1075 | // Initializes RemoteGOPATHs. |
| 1076 | found := false |
| 1077 | for _, l := range s.LocalGOPATHs { |
| 1078 | if r := isRootedIn(l+src, parts); r != "" { |
| 1079 | //log.Printf("Found RemoteGOPATHs[%s] = %s", r[:len(r)-len(src)], l) |
| 1080 | s.RemoteGOPATHs[r[:len(r)-len(src)]] = l |
| 1081 | found = true |
| 1082 | break |
| 1083 | } |
| 1084 | const pkgmod = "/pkg/mod" |
| 1085 | if r := isRootedIn(l+pkgmod, parts); r != "" { |
| 1086 | //log.Printf("Found RemoteGOPATHs[%s] = %s", r[:len(r)-len(pkgmod)], l) |
| 1087 | s.RemoteGOPATHs[r[:len(r)-len(pkgmod)]] = l |
| 1088 | found = true |
| 1089 | break |
| 1090 | } |
| 1091 | } |
| 1092 | if found { |
| 1093 | continue |
| 1094 | } |
| 1095 | // Initializes localGomods. |
| 1096 | if len(parts) > 1 { |
| 1097 | // Search upward looking for a go.mod. |
no test coverage detected