Skip to content

Commit 881a165

Browse files
committed
cmd/go/internal/modindex: ignore non-source files for index
We were saving non-go file information in the module index files, leading in an unnecessary increase in memory usage in modules containing many non-go files. This was a bug because this information is never used. Don't save that information. For #54226 Change-Id: I0644064f83f96e3a9f43b7e66ca94d69d9603376 Reviewed-on: https://go-review.googlesource.com/c/go/+/439118 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 3a37b11 commit 881a165

File tree

8 files changed

+28
-2
lines changed

8 files changed

+28
-2
lines changed

src/cmd/go/internal/modindex/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,18 @@ type fileEmbed struct {
477477
pos token.Position
478478
}
479479

480+
var errNonSource = errors.New("non source file")
481+
480482
// getFileInfo extracts the information needed from each go file for the module
481483
// index.
482484
//
483485
// If Name denotes a Go program, matchFile reads until the end of the
484486
// Imports and returns that section of the file in the FileInfo's Header field,
485487
// even though it only considers text until the first non-comment
486488
// for +build lines.
489+
//
490+
// getFileInfo will return errNonSource if the file is not a source or object
491+
// file and shouldn't even be added to IgnoredFiles.
487492
func getFileInfo(dir, name string, fset *token.FileSet) (*fileInfo, error) {
488493
if strings.HasPrefix(name, "_") ||
489494
strings.HasPrefix(name, ".") {
@@ -498,7 +503,7 @@ func getFileInfo(dir, name string, fset *token.FileSet) (*fileInfo, error) {
498503

499504
if ext != ".go" && fileListForExt(&dummyPkg, ext) == nil {
500505
// skip
501-
return nil, nil
506+
return nil, errNonSource
502507
}
503508

504509
info := &fileInfo{name: filepath.Join(dir, name), fset: fset}

src/cmd/go/internal/modindex/index_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,20 @@ func TestIndex(t *testing.T) {
8585
}
8686
})
8787
}
88+
89+
func TestImportRaw_IgnoreNonGo(t *testing.T) {
90+
path := filepath.Join("testdata", "ignore_non_source")
91+
p := importRaw(path, ".")
92+
93+
wantFiles := []string{"a.syso", "b.go", "c.c"}
94+
95+
var gotFiles []string
96+
for i := range p.sourceFiles {
97+
gotFiles = append(gotFiles, p.sourceFiles[i].name)
98+
}
99+
100+
if !reflect.DeepEqual(gotFiles, wantFiles) {
101+
t.Errorf("names of files in importRaw(testdata/ignore_non_source): got %v; want %v",
102+
gotFiles, wantFiles)
103+
}
104+
}

src/cmd/go/internal/modindex/scan.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ func importRaw(modroot, reldir string) *rawPackage {
210210
continue
211211
}
212212
info, err := getFileInfo(absdir, name, fset)
213-
if err != nil {
213+
if err == errNonSource {
214+
// not a source or object file. completely ignore in the index
215+
continue
216+
} else if err != nil {
214217
p.sourceFiles = append(p.sourceFiles, &rawFile{name: name, error: err.Error()})
215218
continue
216219
} else if info == nil {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package ignore_non_source

src/cmd/go/internal/modindex/testdata/ignore_non_source/b.go

Whitespace-only changes.

src/cmd/go/internal/modindex/testdata/ignore_non_source/bar.json

Whitespace-only changes.

src/cmd/go/internal/modindex/testdata/ignore_non_source/baz.log

Whitespace-only changes.

src/cmd/go/internal/modindex/testdata/ignore_non_source/c.c

Whitespace-only changes.

0 commit comments

Comments
 (0)