]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: don't compute Embed fields if they're not needed
authorFrank Viernau <frank_viernau@epam.com>
Mon, 20 Feb 2023 09:08:00 +0000 (09:08 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 23 Feb 2023 15:24:16 +0000 (15:24 +0000)
If the user provides the -json flag to explicitly specify fields, but doesn't specify any *Embed* field, skip computing the embed fields.

This enhances the initial implementation of #29666.

Change-Id: I60e86fb25a445689aecbcc7f3f3f88e0f37a0fc5
GitHub-Last-Rev: 2795c195bf995f798a45e928becebc253c89b9d6
GitHub-Pull-Request: golang/go#58522
Reviewed-on: https://go-review.googlesource.com/c/go/+/468075
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>

src/cmd/go/internal/list/list.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_json_fields.txt

index fc2e087dba2cb2ddf844ccec1ad2d230608661ba..ec23024ac880f566af21df2eed714a8e142c30f8 100644 (file)
@@ -603,8 +603,9 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                // for test variants of packages and users who have been providing format strings
                // might not expect those errors to stop showing up.
                // See issue #52443.
-               SuppressDeps:      !listJsonFields.needAny("Deps", "DepsErrors"),
-               SuppressBuildInfo: !listJsonFields.needAny("Stale", "StaleReason"),
+               SuppressDeps:       !listJsonFields.needAny("Deps", "DepsErrors"),
+               SuppressBuildInfo:  !listJsonFields.needAny("Stale", "StaleReason"),
+               SuppressEmbedFiles: !listJsonFields.needAny("EmbedFiles", "TestEmbedFiles", "XTestEmbedFiles"),
        }
        pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
        if !*listE {
index 2d479561ac6c1b50cffea270734cf499696b3be3..799f7de85e468c305e8953cfa6998e08e4575bf2 100644 (file)
@@ -1922,12 +1922,14 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
        }
        p.DefaultGODEBUG = defaultGODEBUG(p, nil, nil, nil)
 
-       p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns)
-       if err != nil {
-               p.Incomplete = true
-               setError(err)
-               embedErr := err.(*EmbedError)
-               p.Error.setPos(p.Internal.Build.EmbedPatternPos[embedErr.Pattern])
+       if !opts.SuppressEmbedFiles {
+               p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns)
+               if err != nil {
+                       p.Incomplete = true
+                       setError(err)
+                       embedErr := err.(*EmbedError)
+                       p.Error.setPos(p.Internal.Build.EmbedPatternPos[embedErr.Pattern])
+               }
        }
 
        // Check for case-insensitive collision of input files.
@@ -2780,6 +2782,10 @@ type PackageOpts struct {
        // SuppressBuildInfo is true if the caller does not need p.Stale, p.StaleReason, or p.Internal.BuildInfo
        // to be populated on the package.
        SuppressBuildInfo bool
+
+       // SuppressEmbedFiles is true if the caller does not need any embed files to be populated on the
+       // package.
+       SuppressEmbedFiles bool
 }
 
 // PackagesAndErrors returns the packages named by the command line arguments
index 54d22201100670377f9d28390ee4e4ac11b612fa..7e008eaabf6a003b69430e62bef52f0e6dc7ecfe 100644 (file)
@@ -26,6 +26,21 @@ go list -json=Deps
 stdout '"Deps": \['
 stdout '"errors",'
 
+# Test -json=<field> with *EmbedPatterns outputs embed patterns.
+cd embed
+go list -json=EmbedPatterns,TestEmbedPatterns,XTestEmbedPatterns
+stdout '"EmbedPatterns": \['
+stdout '"TestEmbedPatterns": \['
+stdout '"XTestEmbedPatterns": \['
+# Test -json=<field> with *EmbedFiles fails due to broken file reference.
+! go list -json=EmbedFiles
+stderr 'no matching files found'
+! go list -json=TestEmbedFiles
+stderr 'no matching files found'
+! go list -json=XTestEmbedFiles
+stderr 'no matching files found'
+cd ..
+
 [!git] skip
 
 # Test -json=<field> without Stale skips computing buildinfo
@@ -73,3 +88,26 @@ module example.com/repo
 package main
 
 func main() {}
+-- embed/go.mod --
+module example.com/embed
+-- embed/embed.go --
+package embed
+
+import _ "embed"
+
+//go:embed non-existing-file.txt
+var s string
+-- embed/embed_test.go --
+package embed
+
+import _ "embed"
+
+//go:embed non-existing-file.txt
+var s string
+-- embed/embed_xtest_test.go --
+package embed_test
+
+import _ "embed"
+
+//go:embed non-existing-file.txt
+var s string