From 5c69cb2a5bc33ae2c8af598a53f4337bd1e77508 Mon Sep 17 00:00:00 2001 From: unbyte Date: Wed, 28 Apr 2021 04:32:15 +0000 Subject: [PATCH] cmd/go: populate module info even if an error occurs in loading package The existing implementation ignores module info if there is any error loading the package. Fixes #44287 Change-Id: I24142e4c7256517292fc654e29d759871b80bc09 GitHub-Last-Rev: 28e9bf85e8c119f3b805c38c79aef60322fcc551 GitHub-Pull-Request: golang/go#45777 Reviewed-on: https://go-review.googlesource.com/c/go/+/313549 Reviewed-by: Bryan C. Mills Reviewed-by: Michael Matloob Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot Trust: Michael Matloob --- src/cmd/go/internal/load/pkg.go | 23 ++++++++++--------- .../script/list_module_when_error.txt | 19 +++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/cmd/go/testdata/script/list_module_when_error.txt diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index c1e3eaa0f3..2d91d10583 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -1846,6 +1846,14 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk * stk.Push(path) defer stk.Pop() + pkgPath := p.ImportPath + if p.Internal.CmdlineFiles { + pkgPath = "command-line-arguments" + } + if cfg.ModulesEnabled { + p.Module = modload.PackageModuleInfo(ctx, pkgPath) + } + p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns) if err != nil { p.Incomplete = true @@ -1905,6 +1913,10 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk * p.Internal.Imports = imports p.collectDeps() + if cfg.ModulesEnabled && p.Error == nil && p.Name == "main" && len(p.DepsErrors) == 0 { + p.Internal.BuildInfo = modload.PackageBuildInfo(pkgPath, p.Deps) + } + // unsafe is a fake package. if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") { p.Target = "" @@ -1944,17 +1956,6 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk * setError(fmt.Errorf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " "))) return } - - if cfg.ModulesEnabled && p.Error == nil { - mainPath := p.ImportPath - if p.Internal.CmdlineFiles { - mainPath = "command-line-arguments" - } - p.Module = modload.PackageModuleInfo(ctx, mainPath) - if p.Name == "main" && len(p.DepsErrors) == 0 { - p.Internal.BuildInfo = modload.PackageBuildInfo(mainPath, p.Deps) - } - } } // An EmbedError indicates a problem with a go:embed directive. diff --git a/src/cmd/go/testdata/script/list_module_when_error.txt b/src/cmd/go/testdata/script/list_module_when_error.txt new file mode 100644 index 0000000000..844164cd6a --- /dev/null +++ b/src/cmd/go/testdata/script/list_module_when_error.txt @@ -0,0 +1,19 @@ +# The Module field should be populated even if there is an error loading the package. + +env GO111MODULE=on + +go list -e -f {{.Module}} +stdout '^mod.com$' + +-- go.mod -- +module mod.com + +go 1.16 + +-- blah.go -- +package blah + +import _ "embed" + +//go:embed README.md +var readme string