diff options
author | Michael Pratt <mpratt@google.com> | 2020-09-11 13:00:30 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-09-11 13:07:30 -0700 |
commit | 490e5c83bd647639e36a9f7e871af33fa64b9bb1 (patch) | |
tree | 34e317af24499d4221b4db02bb417a7473cf1fcc | |
parent | 9a5635eb179f3a21c51e912aa87277025d7950e9 (diff) |
Make nogo more robust to variety of stdlib layouts.
PiperOrigin-RevId: 331206424
-rw-r--r-- | tools/nogo/nogo.go | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/tools/nogo/nogo.go b/tools/nogo/nogo.go index 40e48540d..120fdcff5 100644 --- a/tools/nogo/nogo.go +++ b/tools/nogo/nogo.go @@ -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 { |