]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modindex: ignore non-source files for index
authorMichael Matloob <matloob@golang.org>
Wed, 5 Oct 2022 16:54:00 +0000 (12:54 -0400)
committerMichael Matloob <matloob@golang.org>
Wed, 5 Oct 2022 18:59:21 +0000 (18:59 +0000)
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 <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/cmd/go/internal/modindex/build.go
src/cmd/go/internal/modindex/index_test.go
src/cmd/go/internal/modindex/scan.go
src/cmd/go/internal/modindex/testdata/ignore_non_source/a.syso [new file with mode: 0644]
src/cmd/go/internal/modindex/testdata/ignore_non_source/b.go [new file with mode: 0644]
src/cmd/go/internal/modindex/testdata/ignore_non_source/bar.json [new file with mode: 0644]
src/cmd/go/internal/modindex/testdata/ignore_non_source/baz.log [new file with mode: 0644]
src/cmd/go/internal/modindex/testdata/ignore_non_source/c.c [new file with mode: 0644]

index 8903c156bbaadd872332258239b85e346f3412fe..e4380973e07813a644a3ad25979fca501e19811a 100644 (file)
@@ -477,6 +477,8 @@ type fileEmbed struct {
        pos     token.Position
 }
 
+var errNonSource = errors.New("non source file")
+
 // getFileInfo extracts the information needed from each go file for the module
 // index.
 //
@@ -484,6 +486,9 @@ type fileEmbed struct {
 // 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, ".") {
@@ -498,7 +503,7 @@ func getFileInfo(dir, name string, fset *token.FileSet) (*fileInfo, error) {
 
        if ext != ".go" && fileListForExt(&dummyPkg, ext) == nil {
                // skip
-               return nil, nil
+               return nil, errNonSource
        }
 
        info := &fileInfo{name: filepath.Join(dir, name), fset: fset}
index 2c072f909d3b64163b98b38d4cd617311516cb2a..1c32973d399f14fe93aa4186bcd920b7277970c7 100644 (file)
@@ -85,3 +85,20 @@ func TestIndex(t *testing.T) {
                }
        })
 }
+
+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)
+       }
+}
index 56ba9e86c865d27e39b1ce37c1ee250c84ac0d2e..7207e1e523bf084ee580c1b4cbc113b90d1bb4ba 100644 (file)
@@ -210,7 +210,10 @@ func importRaw(modroot, reldir string) *rawPackage {
                        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 {
diff --git a/src/cmd/go/internal/modindex/testdata/ignore_non_source/a.syso b/src/cmd/go/internal/modindex/testdata/ignore_non_source/a.syso
new file mode 100644 (file)
index 0000000..9527d05
--- /dev/null
@@ -0,0 +1 @@
+package ignore_non_source
diff --git a/src/cmd/go/internal/modindex/testdata/ignore_non_source/b.go b/src/cmd/go/internal/modindex/testdata/ignore_non_source/b.go
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/cmd/go/internal/modindex/testdata/ignore_non_source/bar.json b/src/cmd/go/internal/modindex/testdata/ignore_non_source/bar.json
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/cmd/go/internal/modindex/testdata/ignore_non_source/baz.log b/src/cmd/go/internal/modindex/testdata/ignore_non_source/baz.log
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/cmd/go/internal/modindex/testdata/ignore_non_source/c.c b/src/cmd/go/internal/modindex/testdata/ignore_non_source/c.c
new file mode 100644 (file)
index 0000000..e69de29