Skip to content

Commit

Permalink
cmd/go: don't compute Embed fields if they're not needed
Browse files Browse the repository at this point in the history
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 golang#29666.
  • Loading branch information
fviernau committed Feb 14, 2023
1 parent 0cd309e commit dcf229b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/cmd/go/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
// See issue #52443.
SuppressDeps: !listJsonFields.needAny("Deps", "DepsErrors"),
SuppressBuildInfo: !listJsonFields.needAny("Stale", "StaleReason"),
SuppressEmbed: !listJsonFields.needAny(
"EmbedPatterns", "EmbedFiles",
"TestEmbedPatterns", "TestEmbedFiles",
"XTestEmbedPatterns", "XTestEmbedFiles",
),
}
pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
if !*listE {
Expand Down
17 changes: 11 additions & 6 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1919,12 +1919,14 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
p.Module = modload.PackageModuleInfo(ctx, pkgPath)
}

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.SuppressEmbed {
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.
Expand Down Expand Up @@ -2774,6 +2776,9 @@ 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 embedded files to be populated on the package.
SuppressEmbed bool
}

// PackagesAndErrors returns the packages named by the command line arguments
Expand Down

0 comments on commit dcf229b

Please sign in to comment.