Skip to content

Commit

Permalink
cmd/go: don't construct module version info if there are import errors
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jay Conrod committed Sep 19, 2019
1 parent 1c50fcf commit e2cbb7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/go/internal/modload/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
22 changes: 22 additions & 0 deletions src/cmd/go/testdata/script/mod_build_info_err.txt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e2cbb7f

Please sign in to comment.