@@ -153,11 +153,15 @@ func (b *Builder) Do(ctx context.Context, root *Action) {
153
153
defer b .exec .Unlock ()
154
154
155
155
if err != nil {
156
- if b .AllowErrors {
156
+ if b .AllowErrors && a . Package != nil {
157
157
if a .Package .Error == nil {
158
158
a .Package .Error = & load.PackageError {Err : err }
159
159
}
160
160
} else {
161
+ var ipe load.ImportPathError
162
+ if a .Package != nil && (! errors .As (err , & ipe ) || ipe .ImportPath () != a .Package .ImportPath ) {
163
+ err = fmt .Errorf ("%s: %v" , a .Package .ImportPath , err )
164
+ }
161
165
base .Errorf ("%s" , err )
162
166
}
163
167
a .Failed = true
@@ -495,9 +499,6 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
495
499
}
496
500
497
501
defer func () {
498
- if err != nil {
499
- err = fmt .Errorf ("go build %s: %v" , p .ImportPath , err )
500
- }
501
502
if err != nil && b .IsCmdList && b .NeedError && p .Error == nil {
502
503
p .Error = & load.PackageError {Err : err }
503
504
}
@@ -846,8 +847,7 @@ OverlayLoop:
846
847
}
847
848
848
849
if err != nil {
849
- prefix , suffix := formatOutput (b .WorkDir , p .Dir , p .Desc (), output )
850
- return errors .New (prefix + suffix )
850
+ return formatOutput (b .WorkDir , p .Dir , p .ImportPath , p .Desc (), output )
851
851
} else {
852
852
b .showOutput (a , p .Dir , p .Desc (), output )
853
853
}
@@ -1530,8 +1530,8 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
1530
1530
var out []byte
1531
1531
out , err = b .runOut (nil , p .Dir , nil , b .PkgconfigCmd (), "--cflags" , pcflags , "--" , pkgs )
1532
1532
if err != nil {
1533
- prefix , suffix : = formatOutput (b .WorkDir , p .Dir , b .PkgconfigCmd ()+ " --cflags " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ))
1534
- return nil , nil , errors . New ( fmt . Sprint ( prefix , suffix + err . Error ()))
1533
+ err = formatOutput (b .WorkDir , p .Dir , p . ImportPath , b .PkgconfigCmd ()+ " --cflags " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ) + err . Error ( ))
1534
+ return nil , nil , err
1535
1535
}
1536
1536
if len (out ) > 0 {
1537
1537
cflags , err = splitPkgConfigOutput (out )
@@ -1544,8 +1544,8 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
1544
1544
}
1545
1545
out , err = b .runOut (nil , p .Dir , nil , b .PkgconfigCmd (), "--libs" , pcflags , "--" , pkgs )
1546
1546
if err != nil {
1547
- prefix , suffix : = formatOutput (b .WorkDir , p .Dir , b .PkgconfigCmd ()+ " --libs " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ))
1548
- return nil , nil , errors . New ( fmt . Sprint ( prefix , suffix + err . Error ()))
1547
+ err = formatOutput (b .WorkDir , p .Dir , p . ImportPath , b .PkgconfigCmd ()+ " --libs " + strings .Join (pcflags , " " )+ " -- " + strings .Join (pkgs , " " ), string (out ) + err . Error ( ))
1548
+ return nil , nil , err
1549
1549
}
1550
1550
if len (out ) > 0 {
1551
1551
// NOTE: we don't attempt to parse quotes and unescapes here. pkg-config
@@ -2093,16 +2093,37 @@ func (b *Builder) Showcmd(dir string, format string, args ...any) {
2093
2093
// If a is not nil and a.output is not nil, showOutput appends to that slice instead of
2094
2094
// printing to b.Print.
2095
2095
func (b * Builder ) showOutput (a * Action , dir , desc , out string ) {
2096
- prefix , suffix := formatOutput (b .WorkDir , dir , desc , out )
2096
+ importPath := ""
2097
+ if a != nil && a .Package != nil {
2098
+ importPath = a .Package .ImportPath
2099
+ }
2100
+ psErr := formatOutput (b .WorkDir , dir , importPath , desc , out )
2097
2101
if a != nil && a .output != nil {
2098
- a .output = append (a .output , prefix ... )
2099
- a .output = append (a .output , suffix ... )
2102
+ a .output = append (a .output , psErr . prefix ... )
2103
+ a .output = append (a .output , psErr . suffix ... )
2100
2104
return
2101
2105
}
2102
2106
2103
2107
b .output .Lock ()
2104
2108
defer b .output .Unlock ()
2105
- b .Print (prefix , suffix )
2109
+ b .Print (psErr .prefix , psErr .suffix )
2110
+ }
2111
+
2112
+ // A prefixSuffixError is an error formatted by formatOutput.
2113
+ type prefixSuffixError struct {
2114
+ importPath string
2115
+ prefix , suffix string
2116
+ }
2117
+
2118
+ func (e * prefixSuffixError ) Error () string {
2119
+ if e .importPath != "" && ! strings .HasPrefix (strings .TrimPrefix (e .prefix , "# " ), e .importPath ) {
2120
+ return fmt .Sprintf ("go build %s:\n %s%s" , e .importPath , e .prefix , e .suffix )
2121
+ }
2122
+ return e .prefix + e .suffix
2123
+ }
2124
+
2125
+ func (e * prefixSuffixError ) ImportPath () string {
2126
+ return e .importPath
2106
2127
}
2107
2128
2108
2129
// formatOutput prints "# desc" followed by the given output.
@@ -2128,17 +2149,17 @@ func (b *Builder) showOutput(a *Action, dir, desc, out string) {
2128
2149
// formatOutput also replaces references to the work directory with $WORK.
2129
2150
// formatOutput returns the output in a prefix with the description and a
2130
2151
// suffix with the actual output.
2131
- func formatOutput (workDir , dir , desc , out string ) ( prefix , suffix string ) {
2132
- prefix = "# " + desc
2133
- suffix = "\n " + out
2152
+ func formatOutput (workDir , dir , importPath , desc , out string ) * prefixSuffixError {
2153
+ prefix : = "# " + desc
2154
+ suffix : = "\n " + out
2134
2155
if reldir := base .ShortPath (dir ); reldir != dir {
2135
2156
suffix = strings .ReplaceAll (suffix , " " + dir , " " + reldir )
2136
2157
suffix = strings .ReplaceAll (suffix , "\n " + dir , "\n " + reldir )
2137
2158
suffix = strings .ReplaceAll (suffix , "\n \t " + dir , "\n \t " + reldir )
2138
2159
}
2139
2160
suffix = strings .ReplaceAll (suffix , " " + workDir , " $WORK" )
2140
2161
2141
- return prefix , suffix
2162
+ return & prefixSuffixError { importPath : importPath , prefix : prefix , suffix : suffix }
2142
2163
}
2143
2164
2144
2165
var cgoLine = lazyregexp .New (`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]` )
@@ -2154,8 +2175,7 @@ func (b *Builder) run(a *Action, dir string, desc string, env []string, cmdargs
2154
2175
desc = b .fmtcmd (dir , "%s" , strings .Join (str .StringList (cmdargs ... ), " " ))
2155
2176
}
2156
2177
if err != nil {
2157
- prefix , suffix := formatOutput (b .WorkDir , dir , desc , b .processOutput (out ))
2158
- err = errors .New (prefix + suffix )
2178
+ err = formatOutput (b .WorkDir , dir , a .Package .ImportPath , desc , b .processOutput (out ))
2159
2179
} else {
2160
2180
b .showOutput (a , dir , desc , b .processOutput (out ))
2161
2181
}
@@ -2441,7 +2461,6 @@ func (b *Builder) gfortran(a *Action, p *load.Package, workdir, out string, flag
2441
2461
// ccompile runs the given C or C++ compiler and creates an object from a single source file.
2442
2462
func (b * Builder ) ccompile (a * Action , p * load.Package , outfile string , flags []string , file string , compiler []string ) error {
2443
2463
file = mkAbs (p .Dir , file )
2444
- desc := p .ImportPath
2445
2464
outfile = mkAbs (p .Dir , outfile )
2446
2465
2447
2466
// Elide source directory paths if -trimpath or GOROOT_FINAL is set.
@@ -2502,10 +2521,9 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
2502
2521
}
2503
2522
2504
2523
if err != nil || os .Getenv ("GO_BUILDER_NAME" ) != "" {
2505
- prefix , suffix := formatOutput (b .WorkDir , p .Dir , desc , b .processOutput (output ))
2506
- err = errors .New (prefix + suffix )
2524
+ err = formatOutput (b .WorkDir , p .Dir , p .ImportPath , p .Desc (), b .processOutput (output ))
2507
2525
} else {
2508
- b .showOutput (a , p .Dir , desc , b .processOutput (output ))
2526
+ b .showOutput (a , p .Dir , p . Desc () , b .processOutput (output ))
2509
2527
}
2510
2528
}
2511
2529
return err
@@ -3079,8 +3097,6 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
3079
3097
cgoflags = append (cgoflags , "-exportheader=" + objdir + "_cgo_install.h" )
3080
3098
}
3081
3099
3082
- execdir := p .Dir
3083
-
3084
3100
// Rewrite overlaid paths in cgo files.
3085
3101
// cgo adds //line and #line pragmas in generated files with these paths.
3086
3102
var trimpath []string
@@ -3095,7 +3111,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
3095
3111
cgoflags = append (cgoflags , "-trimpath" , strings .Join (trimpath , ";" ))
3096
3112
}
3097
3113
3098
- if err := b .run (a , execdir , p .ImportPath , cgoenv , cfg .BuildToolexec , cgoExe , "-objdir" , objdir , "-importpath" , p .ImportPath , cgoflags , "--" , cgoCPPFLAGS , cgoCFLAGS , cgofiles ); err != nil {
3114
+ if err := b .run (a , p . Dir , p .ImportPath , cgoenv , cfg .BuildToolexec , cgoExe , "-objdir" , objdir , "-importpath" , p .ImportPath , cgoflags , "--" , cgoCPPFLAGS , cgoCFLAGS , cgofiles ); err != nil {
3099
3115
return nil , nil , err
3100
3116
}
3101
3117
outGo = append (outGo , gofiles ... )
@@ -3553,8 +3569,7 @@ func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFL
3553
3569
return "" , "" , errors .New ("must have SWIG version >= 3.0.6" )
3554
3570
}
3555
3571
// swig error
3556
- prefix , suffix := formatOutput (b .WorkDir , p .Dir , p .Desc (), b .processOutput (out ))
3557
- return "" , "" , errors .New (prefix + suffix )
3572
+ err = formatOutput (b .WorkDir , p .Dir , p .ImportPath , p .Desc (), b .processOutput (out ))
3558
3573
}
3559
3574
return "" , "" , err
3560
3575
}
0 commit comments