Skip to content

Commit f4d6435

Browse files
committed
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 golang#44287
1 parent 70deaa3 commit f4d6435

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,14 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
18461846
stk.Push(path)
18471847
defer stk.Pop()
18481848

1849+
mainPath := p.ImportPath
1850+
if p.Internal.CmdlineFiles {
1851+
mainPath = "command-line-arguments"
1852+
}
1853+
if cfg.ModulesEnabled {
1854+
p.Module = modload.PackageModuleInfo(ctx, mainPath)
1855+
}
1856+
18491857
p.EmbedFiles, p.Internal.Embed, err = resolveEmbed(p.Dir, p.EmbedPatterns)
18501858
if err != nil {
18511859
p.Incomplete = true
@@ -1905,6 +1913,10 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
19051913
p.Internal.Imports = imports
19061914
p.collectDeps()
19071915

1916+
if cfg.ModulesEnabled && p.Error == nil && p.Name == "main" && len(p.DepsErrors) == 0 {
1917+
p.Internal.BuildInfo = modload.PackageBuildInfo(mainPath, p.Deps)
1918+
}
1919+
19081920
// unsafe is a fake package.
19091921
if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
19101922
p.Target = ""
@@ -1944,17 +1956,6 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
19441956
setError(fmt.Errorf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
19451957
return
19461958
}
1947-
1948-
if cfg.ModulesEnabled && p.Error == nil {
1949-
mainPath := p.ImportPath
1950-
if p.Internal.CmdlineFiles {
1951-
mainPath = "command-line-arguments"
1952-
}
1953-
p.Module = modload.PackageModuleInfo(ctx, mainPath)
1954-
if p.Name == "main" && len(p.DepsErrors) == 0 {
1955-
p.Internal.BuildInfo = modload.PackageBuildInfo(mainPath, p.Deps)
1956-
}
1957-
}
19581959
}
19591960

19601961
// An EmbedError indicates a problem with a go:embed directive.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The Module field should be populated even if there is an error loading the package.
2+
3+
env GO111MODULE=on
4+
5+
go list -e -f {{.Module}}
6+
stdout '^mod.com$'
7+
8+
-- go.mod --
9+
module mod.com
10+
11+
go 1.16
12+
13+
-- blah.go --
14+
package blah
15+
16+
import _ "embed"
17+
18+
//go:embed README.md
19+
var readme string

0 commit comments

Comments
 (0)