@@ -151,11 +151,14 @@ type PackageOpts struct {
151
151
// declare 'go 1.16' or higher.
152
152
UseVendorAll bool
153
153
154
- // AllowErrors indicates that LoadPackages should not log errors in resolving
155
- // patterns or imports, and should not terminate the process if such an error
156
- // occurs.
154
+ // AllowErrors indicates that LoadPackages should not terminate the process if
155
+ // an error occurs.
157
156
AllowErrors bool
158
157
158
+ // SilenceErrors indicates that LoadPackages should not print errors
159
+ // that occur while loading packages. SilenceErrors implies AllowErrors.
160
+ SilenceErrors bool
161
+
159
162
// SilenceUnmatchedWarnings suppresses the warnings normally emitted for
160
163
// patterns that did not match any packages.
161
164
SilenceUnmatchedWarnings bool
@@ -263,23 +266,31 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
263
266
264
267
// One last pass to finalize wildcards.
265
268
updateMatches (loaded )
266
- checkMultiplePaths ()
267
- WriteGoMod ()
268
269
270
+ // Report errors, if any.
271
+ checkMultiplePaths ()
269
272
for _ , pkg := range loaded .pkgs {
270
- if pkg .err != nil && ! opts .AllowErrors {
271
- base .Errorf ("%s: %v" , pkg .stackText (), pkg .err )
273
+ if pkg .err != nil && ! opts .SilenceErrors {
274
+ if opts .AllowErrors {
275
+ fmt .Fprintf (os .Stderr , "%s: %v\n " , pkg .stackText (), pkg .err )
276
+ } else {
277
+ base .Errorf ("%s: %v" , pkg .stackText (), pkg .err )
278
+ }
272
279
}
273
280
if ! pkg .isTest () {
274
281
loadedPackages = append (loadedPackages , pkg .path )
275
282
}
276
283
}
277
- if ! opts .AllowErrors {
284
+ if ! opts .SilenceErrors {
278
285
// Also list errors in matching patterns (such as directory permission
279
286
// errors for wildcard patterns).
280
287
for _ , match := range matches {
281
288
for _ , err := range match .Errs {
282
- base .Errorf ("%v" , err )
289
+ if opts .AllowErrors {
290
+ fmt .Fprintf (os .Stderr , "%v\n " , err )
291
+ } else {
292
+ base .Errorf ("%v" , err )
293
+ }
283
294
}
284
295
}
285
296
}
@@ -289,6 +300,8 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
289
300
search .WarnUnmatched (matches )
290
301
}
291
302
303
+ // Success! Update go.mod (if needed) and return the results.
304
+ WriteGoMod ()
292
305
sort .Strings (loadedPackages )
293
306
return matches , loadedPackages
294
307
}
0 commit comments