Make nogo more robust to variety of stdlib layouts.

PiperOrigin-RevId: 331206424
This commit is contained in:
Michael Pratt
2020-09-11 13:07:30 -07:00
committed by gVisor bot
parent 9a5635eb17
commit 490e5c83bd
+31 -14
View File
@@ -202,29 +202,41 @@ func checkStdlib(config *stdlibConfig, ac map[*analysis.Analyzer]matcher) ([]str
config.Srcs[i] = path.Clean(config.Srcs[i])
}
// Calculate the root directory.
longestPrefix := path.Dir(config.Srcs[0])
for _, file := range config.Srcs[1:] {
for i := 0; i < len(file) && i < len(longestPrefix); i++ {
if file[i] != longestPrefix[i] {
// Truncate here; will stop the loop.
longestPrefix = longestPrefix[:i]
break
}
// Calculate the root source directory. This is always a directory
// named 'src', of which we simply take the first we find. This is a
// bit fragile, but works for all currently known Go source
// configurations.
//
// Note that there may be extra files outside of the root source
// directory; we simply ignore those.
rootSrcPrefix := ""
for _, file := range config.Srcs {
const src = "/src/"
i := strings.Index(file, src)
if i == -1 {
// Superfluous file.
continue
}
}
if len(longestPrefix) > 0 && longestPrefix[len(longestPrefix)-1] != '/' {
longestPrefix += "/"
// Index of first character after /src/.
i += len(src)
rootSrcPrefix = file[:i]
break
}
// Aggregate all files by directory.
packages := make(map[string]*packageConfig)
for _, file := range config.Srcs {
if !strings.HasPrefix(file, rootSrcPrefix) {
// Superflouous file.
continue
}
d := path.Dir(file)
if len(longestPrefix) >= len(d) {
if len(rootSrcPrefix) >= len(d) {
continue // Not a file.
}
pkg := path.Dir(file)[len(longestPrefix):]
pkg := d[len(rootSrcPrefix):]
// Skip cmd packages and obvious test files: see above.
if strings.HasPrefix(pkg, "cmd/") || strings.HasSuffix(file, "_test.go") {
continue
@@ -303,6 +315,11 @@ func checkStdlib(config *stdlibConfig, ac map[*analysis.Analyzer]matcher) ([]str
checkOne(pkg)
}
// Sanity check.
if len(stdlibFacts) == 0 {
return nil, nil, fmt.Errorf("no stdlib facts found: misconfiguration?")
}
// Write out all findings.
factData, err := json.Marshal(stdlibFacts)
if err != nil {