pos token.Position
}
+var errNonSource = errors.New("non source file")
+
// getFileInfo extracts the information needed from each go file for the module
// index.
//
// Imports and returns that section of the file in the FileInfo's Header field,
// even though it only considers text until the first non-comment
// for +build lines.
+//
+// getFileInfo will return errNonSource if the file is not a source or object
+// file and shouldn't even be added to IgnoredFiles.
func getFileInfo(dir, name string, fset *token.FileSet) (*fileInfo, error) {
if strings.HasPrefix(name, "_") ||
strings.HasPrefix(name, ".") {
if ext != ".go" && fileListForExt(&dummyPkg, ext) == nil {
// skip
- return nil, nil
+ return nil, errNonSource
}
info := &fileInfo{name: filepath.Join(dir, name), fset: fset}
}
})
}
+
+func TestImportRaw_IgnoreNonGo(t *testing.T) {
+ path := filepath.Join("testdata", "ignore_non_source")
+ p := importRaw(path, ".")
+
+ wantFiles := []string{"a.syso", "b.go", "c.c"}
+
+ var gotFiles []string
+ for i := range p.sourceFiles {
+ gotFiles = append(gotFiles, p.sourceFiles[i].name)
+ }
+
+ if !reflect.DeepEqual(gotFiles, wantFiles) {
+ t.Errorf("names of files in importRaw(testdata/ignore_non_source): got %v; want %v",
+ gotFiles, wantFiles)
+ }
+}
continue
}
info, err := getFileInfo(absdir, name, fset)
- if err != nil {
+ if err == errNonSource {
+ // not a source or object file. completely ignore in the index
+ continue
+ } else if err != nil {
p.sourceFiles = append(p.sourceFiles, &rawFile{name: name, error: err.Error()})
continue
} else if info == nil {