]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: populate module info even if an error occurs in loading package
authorunbyte <i@shangyes.net>
Wed, 28 Apr 2021 04:32:15 +0000 (04:32 +0000)
committerBryan C. Mills <bcmills@google.com>
Wed, 28 Apr 2021 15:10:38 +0000 (15:10 +0000)
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 <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Michael Matloob <matloob@golang.org>

src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_module_when_error.txt [new file with mode: 0644]

index c1e3eaa0f3896520824d24aec6af92ad4f7dbe4d..2d91d1058366f351212a2372ae670b8b98baf371 100644 (file)
@@ -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 (file)
index 0000000..844164c
--- /dev/null
@@ -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