mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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: 28e9bf85e8
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>
This commit is contained in:
parent
11052d77a3
commit
5c69cb2a5b
2 changed files with 31 additions and 11 deletions
|
|
@ -1846,6 +1846,14 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
|
||||||
stk.Push(path)
|
stk.Push(path)
|
||||||
defer stk.Pop()
|
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)
|
p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Incomplete = true
|
p.Incomplete = true
|
||||||
|
|
@ -1905,6 +1913,10 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
|
||||||
p.Internal.Imports = imports
|
p.Internal.Imports = imports
|
||||||
p.collectDeps()
|
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.
|
// unsafe is a fake package.
|
||||||
if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
|
if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
|
||||||
p.Target = ""
|
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, " ")))
|
setError(fmt.Errorf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
|
||||||
return
|
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.
|
// An EmbedError indicates a problem with a go:embed directive.
|
||||||
|
|
|
||||||
19
src/cmd/go/testdata/script/list_module_when_error.txt
vendored
Normal file
19
src/cmd/go/testdata/script/list_module_when_error.txt
vendored
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue