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>
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)
}
}
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 ""
--- /dev/null
+# 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"