]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: don't construct module version info if there are import errors
authorJay Conrod <jayconrod@google.com>
Thu, 19 Sep 2019 16:58:02 +0000 (12:58 -0400)
committerJay Conrod <jayconrod@google.com>
Thu, 19 Sep 2019 20:53:11 +0000 (20:53 +0000)
A precondition of modload.PackageBuildInfo is that its path and deps
arguments correspond to paths that have been loaded successfully with
modload.ImportPaths or one of the Load functions. load.Package.load
should not call PackageBuildInfo if there were any errors resolving
imports.

Fixes #34393

Change-Id: I107514f1c535885330ff266c85d3981b71b31c2d
Reviewed-on: https://go-review.googlesource.com/c/go/+/196520
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

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

index daaa3ab0c1adeef2afc892c7648d1cbf7241d049..b8cd36f1da27e36f4b0fe9f5c210595460e04387 100644 (file)
@@ -1674,13 +1674,13 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
                return
        }
 
-       if cfg.ModulesEnabled {
+       if cfg.ModulesEnabled && p.Error == nil {
                mainPath := p.ImportPath
                if p.Internal.CmdlineFiles {
                        mainPath = "command-line-arguments"
                }
                p.Module = ModPackageModuleInfo(mainPath)
-               if p.Name == "main" {
+               if p.Name == "main" && len(p.DepsErrors) == 0 {
                        p.Internal.BuildInfo = ModPackageBuildInfo(mainPath, p.Deps)
                }
        }
index f049a1ad94f65c75f27bb1eb1ff0b54d639baeb1..1309d1ad6317be73ce7935546d7730cc1e95c4ec 100644 (file)
@@ -183,6 +183,10 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
        return info
 }
 
+// PackageBuildInfo returns a string containing module version information
+// for modules providing packages named by path and deps. path and deps must
+// name packages that were resolved successfully with ImportPaths or one of
+// the Load functions.
 func PackageBuildInfo(path string, deps []string) string {
        if isStandardImportPath(path) || !Enabled() {
                return ""
diff --git a/src/cmd/go/testdata/script/mod_build_info_err.txt b/src/cmd/go/testdata/script/mod_build_info_err.txt
new file mode 100644 (file)
index 0000000..5ceb154
--- /dev/null
@@ -0,0 +1,22 @@
+# This test verifies that line numbers are included in module import errors.
+# Verifies golang.org/issue/34393.
+
+go list -e -deps -f '{{with .Error}}{{.Pos}}: {{.Err}}{{end}}' ./main
+stdout 'bad[/\\]bad.go:3:8: malformed module path "string": missing dot in first path element'
+
+-- go.mod --
+module m
+
+go 1.13
+
+-- main/main.go --
+package main
+
+import _ "m/bad"
+
+func main() {}
+
+-- bad/bad.go --
+package bad
+
+import _ "string"