@@ -602,7 +602,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
602
602
603
603
if len (d .image .Config .OnBuild ) > 0 {
604
604
if b , err := initOnBuildTriggers (d , d .image .Config .OnBuild , allDispatchStates ); err != nil {
605
- return nil , err
605
+ return nil , parser . SetLocation ( err , d . stage . Location )
606
606
} else if b {
607
607
newDeps = true
608
608
}
@@ -788,7 +788,7 @@ func toCommand(ic instructions.Command, allDispatchStates *dispatchStates) (comm
788
788
stn , ok = allDispatchStates .findStateByName (c .From )
789
789
if ! ok {
790
790
stn = & dispatchState {
791
- stage : instructions.Stage {BaseName : c .From , Location : ic .Location ()},
791
+ stage : instructions.Stage {BaseName : c .From , Location : c .Location ()},
792
792
deps : make (map [* dispatchState ]instructions.Command ),
793
793
paths : make (map [string ]struct {}),
794
794
unregistered : true ,
@@ -860,6 +860,7 @@ func (e *envsFromState) Keys() []string {
860
860
}
861
861
862
862
func dispatch (d * dispatchState , cmd command , opt dispatchOpt ) error {
863
+ d .cmdIsOnBuild = cmd .isOnBuild
863
864
var err error
864
865
// ARG command value could be ignored, so defer handling the expansion error
865
866
_ , isArg := cmd .Command .(* instructions.ArgCommand )
@@ -1015,6 +1016,7 @@ type dispatchState struct {
1015
1016
unregistered bool
1016
1017
stageName string
1017
1018
cmdIndex int
1019
+ cmdIsOnBuild bool
1018
1020
cmdTotal int
1019
1021
prefixPlatform bool
1020
1022
outline outlineCapture
@@ -1093,7 +1095,8 @@ func (dss *dispatchStates) lastTarget() *dispatchState {
1093
1095
1094
1096
type command struct {
1095
1097
instructions.Command
1096
- sources []* dispatchState
1098
+ sources []* dispatchState
1099
+ isOnBuild bool
1097
1100
}
1098
1101
1099
1102
// initOnBuildTriggers initializes the onbuild triggers and creates the commands and dependecies for them.
@@ -1110,6 +1113,9 @@ func initOnBuildTriggers(d *dispatchState, triggers []string, allDispatchStates
1110
1113
if len (ast .AST .Children ) != 1 {
1111
1114
return false , errors .New ("onbuild trigger should be a single expression" )
1112
1115
}
1116
+ node := ast .AST .Children [0 ]
1117
+ // reset the location to the onbuild trigger
1118
+ node .StartLine , node .EndLine = rangeStartEnd (d .stage .Location )
1113
1119
ic , err := instructions .ParseCommand (ast .AST .Children [0 ])
1114
1120
if err != nil {
1115
1121
return false , err
@@ -1118,6 +1124,7 @@ func initOnBuildTriggers(d *dispatchState, triggers []string, allDispatchStates
1118
1124
if err != nil {
1119
1125
return false , err
1120
1126
}
1127
+ cmd .isOnBuild = true
1121
1128
if len (cmd .sources ) > 0 {
1122
1129
hasNewDeps = true
1123
1130
}
@@ -1134,6 +1141,7 @@ func initOnBuildTriggers(d *dispatchState, triggers []string, allDispatchStates
1134
1141
}
1135
1142
}
1136
1143
d .commands = append (commands , d .commands ... )
1144
+ d .cmdTotal += len (commands )
1137
1145
1138
1146
return hasNewDeps , nil
1139
1147
}
@@ -2065,6 +2073,9 @@ func prefixCommand(ds *dispatchState, str string, prefixPlatform bool, platform
2065
2073
}
2066
2074
ds .cmdIndex ++
2067
2075
out += fmt .Sprintf ("%*d/%d] " , int (1 + math .Log10 (float64 (ds .cmdTotal ))), ds .cmdIndex , ds .cmdTotal )
2076
+ if ds .cmdIsOnBuild {
2077
+ out += "ONBUILD "
2078
+ }
2068
2079
return out + str
2069
2080
}
2070
2081
@@ -2509,6 +2520,23 @@ func buildMetaArgs(args *llb.EnvList, shlex *shell.Lex, argCommands []instructio
2509
2520
return args , allArgs , nil
2510
2521
}
2511
2522
2523
+ func rangeStartEnd (r []parser.Range ) (int , int ) {
2524
+ if len (r ) == 0 {
2525
+ return 0 , 0
2526
+ }
2527
+ start := math .MaxInt32
2528
+ end := 0
2529
+ for _ , rng := range r {
2530
+ if rng .Start .Line < start {
2531
+ start = rng .Start .Line
2532
+ }
2533
+ if rng .End .Line > end {
2534
+ end = rng .End .Line
2535
+ }
2536
+ }
2537
+ return start , end
2538
+ }
2539
+
2512
2540
type emptyEnvs struct {}
2513
2541
2514
2542
func (emptyEnvs ) Get (string ) (string , bool ) {
0 commit comments