aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/load/pkg.go
diff options
context:
space:
mode:
authorDmitri Shuralyov <dmitshur@golang.org>2020-05-06 00:20:47 -0400
committerDmitri Shuralyov <dmitshur@golang.org>2020-05-07 18:24:58 -0400
commita9d2e3abf772ee2c49394430545df1fa83699f04 (patch)
treea274d976b131829762304aef7c5f38b8f732fa71 /src/cmd/go/internal/load/pkg.go
parentc19c0a047b849cc1d63745b2e5e8d467cb4e815b (diff)
parentc9d5f60eaa4450ccf1ce878d55b4c6a12843f2f3 (diff)
downloadgo-a9d2e3abf772ee2c49394430545df1fa83699f04.tar.gz
go-a9d2e3abf772ee2c49394430545df1fa83699f04.zip
[dev.boringcrypto] all: merge master into dev.boringcrypto
Change-Id: Idd59c37d2fd759b0f73d2ee01b30f72ef4e9aee8
Diffstat (limited to 'src/cmd/go/internal/load/pkg.go')
-rw-r--r--src/cmd/go/internal/load/pkg.go91
1 files changed, 58 insertions, 33 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 4bfc291fe0..61818380c7 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -210,21 +210,63 @@ func (e *NoGoError) Error() string {
return "no Go files in " + e.Package.Dir
}
-// rewordError returns a version of err with trivial layers removed and
-// (possibly-wrapped) instances of build.NoGoError replaced with load.NoGoError,
-// which more clearly distinguishes sub-cases.
-func (p *Package) rewordError(err error) error {
- if mErr, ok := err.(*search.MatchError); ok && mErr.Match.IsLiteral() {
- err = mErr.Err
- }
- var noGo *build.NoGoError
- if errors.As(err, &noGo) {
- if p.Dir == "" && noGo.Dir != "" {
- p.Dir = noGo.Dir
+// setLoadPackageDataError presents an error found when loading package data
+// as a *PackageError. It has special cases for some common errors to improve
+// messages shown to users and reduce redundancy.
+//
+// setLoadPackageDataError returns true if it's safe to load information about
+// imported packages, for example, if there was a parse error loading imports
+// in one file, but other files are okay.
+func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportStack) {
+ // Include the path on the import stack unless the error includes it already.
+ errHasPath := false
+ if impErr, ok := err.(ImportPathError); ok && impErr.ImportPath() == path {
+ errHasPath = true
+ } else if matchErr, ok := err.(*search.MatchError); ok && matchErr.Match.Pattern() == path {
+ errHasPath = true
+ if matchErr.Match.IsLiteral() {
+ // The error has a pattern has a pattern similar to the import path.
+ // It may be slightly different (./foo matching example.com/foo),
+ // but close enough to seem redundant.
+ // Unwrap the error so we don't show the pattern.
+ err = matchErr.Err
+ }
+ }
+ var errStk []string
+ if errHasPath {
+ errStk = stk.Copy()
+ } else {
+ stk.Push(path)
+ errStk = stk.Copy()
+ stk.Pop()
+ }
+
+ // Replace (possibly wrapped) *build.NoGoError with *load.NoGoError.
+ // The latter is more specific about the cause.
+ var nogoErr *build.NoGoError
+ if errors.As(err, &nogoErr) {
+ if p.Dir == "" && nogoErr.Dir != "" {
+ p.Dir = nogoErr.Dir
}
err = &NoGoError{Package: p}
}
- return err
+
+ // Take only the first error from a scanner.ErrorList. PackageError only
+ // has room for one position, so we report the first error with a position
+ // instead of all of the errors without a position.
+ var pos string
+ if scanErr, ok := err.(scanner.ErrorList); ok && len(scanErr) > 0 {
+ scanPos := scanErr[0].Pos
+ scanPos.Filename = base.ShortPath(scanPos.Filename)
+ pos = scanPos.String()
+ err = errors.New(scanErr[0].Msg)
+ }
+
+ p.Error = &PackageError{
+ ImportStack: errStk,
+ Pos: pos,
+ Err: err,
+ }
}
// Resolve returns the resolved version of imports,
@@ -1560,21 +1602,7 @@ func (p *Package) load(path string, stk *ImportStack, bp *build.Package, err err
if err != nil {
p.Incomplete = true
- // Report path in error stack unless err is an ImportPathError with path already set.
- pushed := false
- if e, ok := err.(ImportPathError); !ok || e.ImportPath() != path {
- stk.Push(path)
- pushed = true // Remember to pop after setError.
- }
- setError(base.ExpandScanner(p.rewordError(err)))
- if pushed {
- stk.Pop()
- }
- if _, isScanErr := err.(scanner.ErrorList); !isScanErr {
- return
- }
- // Fall through if there was an error parsing a file. 'go list -e' should
- // still report imports and other metadata.
+ p.setLoadPackageDataError(err, path, stk)
}
useBindir := p.Name == "main"
@@ -1910,8 +1938,7 @@ func externalLinkingForced(p *Package) bool {
return true
}
case "darwin":
- switch cfg.BuildContext.GOARCH {
- case "arm", "arm64":
+ if cfg.BuildContext.GOARCH == "arm64" {
return true
}
}
@@ -2142,10 +2169,8 @@ func PackagesAndErrors(patterns []string) []*Package {
// Report it as a synthetic package.
p := new(Package)
p.ImportPath = m.Pattern()
- p.Error = &PackageError{
- ImportStack: nil, // The error arose from a pattern, not an import.
- Err: p.rewordError(m.Errs[0]),
- }
+ var stk ImportStack // empty stack, since the error arose from a pattern, not an import
+ p.setLoadPackageDataError(m.Errs[0], m.Pattern(), &stk)
p.Incomplete = true
p.Match = append(p.Match, m.Pattern())
p.Internal.CmdlinePkg = true