@@ -187,20 +187,17 @@ type PackageInternal struct {
187
187
Gccgoflags []string // -gccgoflags for this package
188
188
}
189
189
190
+ // A NoGoError indicates that no Go files for the package were applicable to the
191
+ // build for that package.
192
+ //
193
+ // That may be because there were no files whatsoever, or because all files were
194
+ // excluded, or because all non-excluded files were test sources.
190
195
type NoGoError struct {
191
196
Package * Package
192
197
}
193
198
194
199
func (e * NoGoError ) Error () string {
195
- // Count files beginning with _ and ., which we will pretend don't exist at all.
196
- dummy := 0
197
- for _ , name := range e .Package .IgnoredGoFiles {
198
- if strings .HasPrefix (name , "_" ) || strings .HasPrefix (name , "." ) {
199
- dummy ++
200
- }
201
- }
202
-
203
- if len (e .Package .IgnoredGoFiles ) > dummy {
200
+ if len (e .Package .constraintIgnoredGoFiles ()) > 0 {
204
201
// Go files exist, but they were ignored due to build constraints.
205
202
return "build constraints exclude all Go files in " + e .Package .Dir
206
203
}
@@ -213,6 +210,23 @@ func (e *NoGoError) Error() string {
213
210
return "no Go files in " + e .Package .Dir
214
211
}
215
212
213
+ // rewordError returns a version of err with trivial layers removed and
214
+ // (possibly-wrapped) instances of build.NoGoError replaced with load.NoGoError,
215
+ // which more clearly distinguishes sub-cases.
216
+ func (p * Package ) rewordError (err error ) error {
217
+ if mErr , ok := err .(* search.MatchError ); ok && mErr .Match .IsLiteral () {
218
+ err = mErr .Err
219
+ }
220
+ var noGo * build.NoGoError
221
+ if errors .As (err , & noGo ) {
222
+ if p .Dir == "" && noGo .Dir != "" {
223
+ p .Dir = noGo .Dir
224
+ }
225
+ err = & NoGoError {Package : p }
226
+ }
227
+ return err
228
+ }
229
+
216
230
// Resolve returns the resolved version of imports,
217
231
// which should be p.TestImports or p.XTestImports, NOT p.Imports.
218
232
// The imports in p.TestImports and p.XTestImports are not recursively
@@ -313,10 +327,7 @@ type PackageError struct {
313
327
314
328
func (p * PackageError ) Error () string {
315
329
// Import cycles deserve special treatment.
316
- if p .IsImportCycle {
317
- return fmt .Sprintf ("%s\n package %s\n " , p .Err , strings .Join (p .ImportStack , "\n \t imports " ))
318
- }
319
- if p .Pos != "" {
330
+ if p .Pos != "" && ! p .IsImportCycle {
320
331
// Omit import stack. The full path to the file where the error
321
332
// is the most important thing.
322
333
return p .Pos + ": " + p .Err .Error ()
@@ -339,6 +350,8 @@ func (p *PackageError) Error() string {
339
350
return "package " + strings .Join (stack , "\n \t imports " ) + ": " + p .Err .Error ()
340
351
}
341
352
353
+ func (p * PackageError ) Unwrap () error { return p .Err }
354
+
342
355
// PackageError implements MarshalJSON so that Err is marshaled as a string
343
356
// and non-essential fields are omitted.
344
357
func (p * PackageError ) MarshalJSON () ([]byte , error ) {
@@ -583,9 +596,10 @@ func loadImport(pre *preload, path, srcDir string, parent *Package, stk *ImportS
583
596
if ! cfg .ModulesEnabled && path != cleanImport (path ) {
584
597
p .Error = & PackageError {
585
598
ImportStack : stk .Copy (),
586
- Err : fmt . Errorf ( "non-canonical import path: %q should be %q" , path , pathpkg .Clean (path )),
599
+ Err : ImportErrorf ( path , "non-canonical import path %q: should be %q" , path , pathpkg .Clean (path )),
587
600
}
588
601
p .Incomplete = true
602
+ setErrorPos (p , importPos )
589
603
}
590
604
}
591
605
@@ -1533,12 +1547,8 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
1533
1547
}
1534
1548
1535
1549
if err != nil {
1536
- if _ , ok := err .(* build.NoGoError ); ok {
1537
- err = & NoGoError {Package : p }
1538
- }
1539
1550
p .Incomplete = true
1540
-
1541
- setError (base .ExpandScanner (err ))
1551
+ setError (base .ExpandScanner (p .rewordError (err )))
1542
1552
if _ , isScanErr := err .(scanner.ErrorList ); ! isScanErr {
1543
1553
return
1544
1554
}
@@ -1934,13 +1944,22 @@ func (p *Package) InternalXGoFiles() []string {
1934
1944
// using absolute paths. "Possibly relevant" means that files are not excluded
1935
1945
// due to build tags, but files with names beginning with . or _ are still excluded.
1936
1946
func (p * Package ) InternalAllGoFiles () []string {
1937
- var extra []string
1947
+ return p .mkAbs (str .StringList (p .constraintIgnoredGoFiles (), p .GoFiles , p .CgoFiles , p .TestGoFiles , p .XTestGoFiles ))
1948
+ }
1949
+
1950
+ // constraintIgnoredGoFiles returns the list of Go files ignored for reasons
1951
+ // other than having a name beginning with '.' or '_'.
1952
+ func (p * Package ) constraintIgnoredGoFiles () []string {
1953
+ if len (p .IgnoredGoFiles ) == 0 {
1954
+ return nil
1955
+ }
1956
+ files := make ([]string , 0 , len (p .IgnoredGoFiles ))
1938
1957
for _ , f := range p .IgnoredGoFiles {
1939
- if f != "" && f [0 ] != '.' || f [0 ] != '_' {
1940
- extra = append (extra , f )
1958
+ if f != "" && f [0 ] != '.' && f [0 ] != '_' {
1959
+ files = append (files , f )
1941
1960
}
1942
1961
}
1943
- return p . mkAbs ( str . StringList ( extra , p . GoFiles , p . CgoFiles , p . TestGoFiles , p . XTestGoFiles ))
1962
+ return files
1944
1963
}
1945
1964
1946
1965
// usesSwig reports whether the package needs to run SWIG.
@@ -2034,7 +2053,7 @@ func Packages(args []string) []*Package {
2034
2053
var pkgs []* Package
2035
2054
for _ , pkg := range PackagesAndErrors (args ) {
2036
2055
if pkg .Error != nil {
2037
- base .Errorf ("can't load package: %s " , pkg .Error )
2056
+ base .Errorf ("%v " , pkg .Error )
2038
2057
continue
2039
2058
}
2040
2059
pkgs = append (pkgs , pkg )
@@ -2092,8 +2111,24 @@ func PackagesAndErrors(patterns []string) []*Package {
2092
2111
pkgs = append (pkgs , p )
2093
2112
}
2094
2113
2095
- // TODO: if len(m.Pkgs) == 0 && len(m.Errs) > 0, should we add a *Package
2096
- // with a non-nil Error field?
2114
+ if len (m .Errs ) > 0 {
2115
+ // In addition to any packages that were actually resolved from the
2116
+ // pattern, there was some error in resolving the pattern itself.
2117
+ // Report it as a synthetic package.
2118
+ p := new (Package )
2119
+ p .ImportPath = m .Pattern ()
2120
+ p .Error = & PackageError {
2121
+ ImportStack : nil , // The error arose from a pattern, not an import.
2122
+ Err : p .rewordError (m .Errs [0 ]),
2123
+ }
2124
+ p .Incomplete = true
2125
+ p .Match = append (p .Match , m .Pattern ())
2126
+ p .Internal .CmdlinePkg = true
2127
+ if m .IsLiteral () {
2128
+ p .Internal .CmdlinePkgLiteral = true
2129
+ }
2130
+ pkgs = append (pkgs , p )
2131
+ }
2097
2132
}
2098
2133
2099
2134
// Now that CmdlinePkg is set correctly,
@@ -2129,7 +2164,7 @@ func PackagesForBuild(args []string) []*Package {
2129
2164
printed := map [* PackageError ]bool {}
2130
2165
for _ , pkg := range pkgs {
2131
2166
if pkg .Error != nil {
2132
- base .Errorf ("can't load package: %s " , pkg .Error )
2167
+ base .Errorf ("%v " , pkg .Error )
2133
2168
printed [pkg .Error ] = true
2134
2169
}
2135
2170
for _ , err := range pkg .DepsErrors {
@@ -2139,7 +2174,7 @@ func PackagesForBuild(args []string) []*Package {
2139
2174
// Only print each once.
2140
2175
if ! printed [err ] {
2141
2176
printed [err ] = true
2142
- base .Errorf ("%s " , err )
2177
+ base .Errorf ("%v " , err )
2143
2178
}
2144
2179
}
2145
2180
}
0 commit comments