@@ -149,11 +149,15 @@ func (b *Builder) Do(ctx context.Context, root *Action) {
149
149
defer b .exec .Unlock ()
150
150
151
151
if err != nil {
152
- if b .AllowErrors {
152
+ if b .AllowErrors && a . Package != nil {
153
153
if a .Package .Error == nil {
154
154
a .Package .Error = & load.PackageError {Err : err }
155
155
}
156
156
} else {
157
+ var ipe load.ImportPathError
158
+ if a .Package != nil && (! errors .As (err , & ipe ) || ipe .ImportPath () != a .Package .ImportPath ) {
159
+ err = fmt .Errorf ("%s: %v" , a .Package .ImportPath , err )
160
+ }
157
161
base .Errorf ("%s" , err )
158
162
}
159
163
a .Failed = true
@@ -510,9 +514,6 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
510
514
}
511
515
512
516
defer func () {
513
- if err != nil {
514
- err = fmt .Errorf ("go build %s: %v" , p .ImportPath , err )
515
- }
516
517
if err != nil && b .IsCmdList && b .NeedError && p .Error == nil {
517
518
p .Error = & load.PackageError {Err : err }
518
519
}
@@ -825,7 +826,7 @@ OverlayLoop:
825
826
}
826
827
827
828
if err != nil {
828
- return errors . New ( fmt . Sprint ( formatOutput (b .WorkDir , p .Dir , p .Desc (), output )) )
829
+ return formatOutput (b .WorkDir , p .Dir , p .ImportPath , p . Desc (), output )
829
830
} else {
830
831
b .showOutput (a , p .Dir , p .Desc (), output )
831
832
}
@@ -1506,8 +1507,8 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
1506
1507
var out []byte
1507
1508
out , err = b .runOut (nil , p .Dir , nil , b .PkgconfigCmd (), "--cflags" , pcflags , "--" , pkgs )
1508
1509
if err != nil {
1509
- prefix , suffix : = formatOutput (b .WorkDir , p .Dir , b .PkgconfigCmd ()+ " --cflags " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ))
1510
- return nil , nil , errors . New ( fmt . Sprint ( prefix , suffix + err . Error ()))
1510
+ err = formatOutput (b .WorkDir , p .Dir , p . ImportPath , b .PkgconfigCmd ()+ " --cflags " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ) + err . Error ( ))
1511
+ return nil , nil , err
1511
1512
}
1512
1513
if len (out ) > 0 {
1513
1514
cflags , err = splitPkgConfigOutput (out )
@@ -1520,8 +1521,8 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
1520
1521
}
1521
1522
out , err = b .runOut (nil , p .Dir , nil , b .PkgconfigCmd (), "--libs" , pcflags , "--" , pkgs )
1522
1523
if err != nil {
1523
- prefix , suffix : = formatOutput (b .WorkDir , p .Dir , b .PkgconfigCmd ()+ " --libs " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ))
1524
- return nil , nil , errors . New ( fmt . Sprint ( prefix , suffix + err . Error ()))
1524
+ err = formatOutput (b .WorkDir , p .Dir , p . ImportPath , b .PkgconfigCmd ()+ " --libs " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ) + err . Error ( ))
1525
+ return nil , nil , err
1525
1526
}
1526
1527
if len (out ) > 0 {
1527
1528
// NOTE: we don't attempt to parse quotes and unescapes here. pkg-config
@@ -2017,16 +2018,37 @@ func (b *Builder) Showcmd(dir string, format string, args ...any) {
2017
2018
// If a is not nil and a.output is not nil, showOutput appends to that slice instead of
2018
2019
// printing to b.Print.
2019
2020
func (b * Builder ) showOutput (a * Action , dir , desc , out string ) {
2020
- prefix , suffix := formatOutput (b .WorkDir , dir , desc , out )
2021
+ importPath := ""
2022
+ if a != nil && a .Package != nil {
2023
+ importPath = a .Package .ImportPath
2024
+ }
2025
+ psErr := formatOutput (b .WorkDir , dir , importPath , desc , out )
2021
2026
if a != nil && a .output != nil {
2022
- a .output = append (a .output , prefix ... )
2023
- a .output = append (a .output , suffix ... )
2027
+ a .output = append (a .output , psErr . prefix ... )
2028
+ a .output = append (a .output , psErr . suffix ... )
2024
2029
return
2025
2030
}
2026
2031
2027
2032
b .output .Lock ()
2028
2033
defer b .output .Unlock ()
2029
- b .Print (prefix , suffix )
2034
+ b .Print (psErr .prefix , psErr .suffix )
2035
+ }
2036
+
2037
+ // A prefixSuffixError is an error formatted by formatOutput.
2038
+ type prefixSuffixError struct {
2039
+ importPath string
2040
+ prefix , suffix string
2041
+ }
2042
+
2043
+ func (e * prefixSuffixError ) Error () string {
2044
+ if e .importPath != "" && ! strings .HasPrefix (strings .TrimPrefix (e .prefix , "# " ), e .importPath ) {
2045
+ return fmt .Sprintf ("go build %s:\n %s%s" , e .importPath , e .prefix , e .suffix )
2046
+ }
2047
+ return e .prefix + e .suffix
2048
+ }
2049
+
2050
+ func (e * prefixSuffixError ) ImportPath () string {
2051
+ return e .importPath
2030
2052
}
2031
2053
2032
2054
// formatOutput prints "# desc" followed by the given output.
@@ -2052,17 +2074,17 @@ func (b *Builder) showOutput(a *Action, dir, desc, out string) {
2052
2074
// formatOutput also replaces references to the work directory with $WORK.
2053
2075
// formatOutput returns the output in a prefix with the description and a
2054
2076
// suffix with the actual output.
2055
- func formatOutput (workDir , dir , desc , out string ) ( prefix , suffix string ) {
2056
- prefix = "# " + desc
2057
- suffix = "\n " + out
2077
+ func formatOutput (workDir , dir , importPath , desc , out string ) * prefixSuffixError {
2078
+ prefix : = "# " + desc
2079
+ suffix : = "\n " + out
2058
2080
if reldir := base .ShortPath (dir ); reldir != dir {
2059
2081
suffix = strings .ReplaceAll (suffix , " " + dir , " " + reldir )
2060
2082
suffix = strings .ReplaceAll (suffix , "\n " + dir , "\n " + reldir )
2061
2083
suffix = strings .ReplaceAll (suffix , "\n \t " + dir , "\n \t " + reldir )
2062
2084
}
2063
2085
suffix = strings .ReplaceAll (suffix , " " + workDir , " $WORK" )
2064
2086
2065
- return prefix , suffix
2087
+ return & prefixSuffixError { importPath : importPath , prefix : prefix , suffix : suffix }
2066
2088
}
2067
2089
2068
2090
var cgoLine = lazyregexp .New (`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]` )
@@ -2078,7 +2100,7 @@ func (b *Builder) run(a *Action, dir string, desc string, env []string, cmdargs
2078
2100
desc = b .fmtcmd (dir , "%s" , strings .Join (str .StringList (cmdargs ... ), " " ))
2079
2101
}
2080
2102
if err != nil {
2081
- err = errors . New ( fmt . Sprint ( formatOutput (b .WorkDir , dir , desc , b .processOutput (out )) ))
2103
+ err = formatOutput (b .WorkDir , dir , a . Package . ImportPath , desc , b .processOutput (out ))
2082
2104
} else {
2083
2105
b .showOutput (a , dir , desc , b .processOutput (out ))
2084
2106
}
@@ -2364,7 +2386,6 @@ func (b *Builder) gfortran(a *Action, p *load.Package, workdir, out string, flag
2364
2386
// ccompile runs the given C or C++ compiler and creates an object from a single source file.
2365
2387
func (b * Builder ) ccompile (a * Action , p * load.Package , outfile string , flags []string , file string , compiler []string ) error {
2366
2388
file = mkAbs (p .Dir , file )
2367
- desc := p .ImportPath
2368
2389
outfile = mkAbs (p .Dir , outfile )
2369
2390
2370
2391
// Elide source directory paths if -trimpath or GOROOT_FINAL is set.
@@ -2425,9 +2446,9 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
2425
2446
}
2426
2447
2427
2448
if err != nil || os .Getenv ("GO_BUILDER_NAME" ) != "" {
2428
- err = errors . New ( fmt . Sprintf ( formatOutput (b .WorkDir , p .Dir , desc , b .processOutput (output )) ))
2449
+ err = formatOutput (b .WorkDir , p .Dir , p . ImportPath , p . Desc (), b .processOutput (output ))
2429
2450
} else {
2430
- b .showOutput (a , p .Dir , desc , b .processOutput (output ))
2451
+ b .showOutput (a , p .Dir , p . Desc () , b .processOutput (output ))
2431
2452
}
2432
2453
}
2433
2454
return err
@@ -2890,8 +2911,6 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
2890
2911
cgoflags = append (cgoflags , "-exportheader=" + objdir + "_cgo_install.h" )
2891
2912
}
2892
2913
2893
- execdir := p .Dir
2894
-
2895
2914
// Rewrite overlaid paths in cgo files.
2896
2915
// cgo adds //line and #line pragmas in generated files with these paths.
2897
2916
var trimpath []string
@@ -2906,7 +2925,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
2906
2925
cgoflags = append (cgoflags , "-trimpath" , strings .Join (trimpath , ";" ))
2907
2926
}
2908
2927
2909
- if err := b .run (a , execdir , p .ImportPath , cgoenv , cfg .BuildToolexec , cgoExe , "-objdir" , objdir , "-importpath" , p .ImportPath , cgoflags , "--" , cgoCPPFLAGS , cgoCFLAGS , cgofiles ); err != nil {
2928
+ if err := b .run (a , p . Dir , p .ImportPath , cgoenv , cfg .BuildToolexec , cgoExe , "-objdir" , objdir , "-importpath" , p .ImportPath , cgoflags , "--" , cgoCPPFLAGS , cgoCFLAGS , cgofiles ); err != nil {
2910
2929
return nil , nil , err
2911
2930
}
2912
2931
outGo = append (outGo , gofiles ... )
@@ -3382,7 +3401,7 @@ func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFL
3382
3401
return "" , "" , errors .New ("must have SWIG version >= 3.0.6" )
3383
3402
}
3384
3403
// swig error
3385
- return "" , "" , errors . New ( fmt . Sprint ( formatOutput (b .WorkDir , p .Dir , p .Desc (), b .processOutput (out )) ))
3404
+ err = formatOutput (b .WorkDir , p .Dir , p .ImportPath , p . Desc (), b .processOutput (out ))
3386
3405
}
3387
3406
return "" , "" , err
3388
3407
}
0 commit comments