@@ -269,18 +269,14 @@ func ListTools() (result []types.Tool) {
269269
270270 sort .Strings (keys )
271271 for _ , key := range keys {
272- t , _ := Builtin (key )
272+ t , _ := DefaultModel (key , "" )
273273 result = append (result , t )
274274 }
275275
276276 return
277277}
278278
279- func Builtin (name string ) (types.Tool , bool ) {
280- return BuiltinWithDefaultModel (name , "" )
281- }
282-
283- func BuiltinWithDefaultModel (name , defaultModel string ) (types.Tool , bool ) {
279+ func DefaultModel (name , defaultModel string ) (types.Tool , bool ) {
284280 // Legacy syntax not used anymore
285281 name = strings .TrimSuffix (name , "?" )
286282 t , ok := tools [name ]
@@ -332,7 +328,7 @@ func SysFind(_ context.Context, _ []string, input string, _ chan<- string) (stri
332328 return strings .Join (result , "\n " ), nil
333329}
334330
335- func SysExec (_ context.Context , env []string , input string , progress chan <- string ) (string , error ) {
331+ func SysExec (ctx context.Context , env []string , input string , progress chan <- string ) (string , error ) {
336332 var params struct {
337333 Command string `json:"command,omitempty"`
338334 Directory string `json:"directory,omitempty"`
@@ -345,14 +341,20 @@ func SysExec(_ context.Context, env []string, input string, progress chan<- stri
345341 params .Directory = "."
346342 }
347343
344+ commandCtx , _ := engine .FromContext (ctx )
345+
346+ ctx , cancel := context .WithCancel (ctx )
347+ defer cancel ()
348+
349+ commandCtx .OnUserCancel (ctx , cancel )
350+
348351 log .Debugf ("Running %s in %s" , params .Command , params .Directory )
349352
350353 var cmd * exec.Cmd
351-
352354 if runtime .GOOS == "windows" {
353- cmd = exec .Command ( "cmd.exe" , "/c" , params .Command )
355+ cmd = exec .CommandContext ( ctx , "cmd.exe" , "/c" , params .Command )
354356 } else {
355- cmd = exec .Command ( "/bin/sh" , "-c" , params .Command )
357+ cmd = exec .CommandContext ( ctx , "/bin/sh" , "-c" , params .Command )
356358 }
357359
358360 var (
@@ -371,7 +373,8 @@ func SysExec(_ context.Context, env []string, input string, progress chan<- stri
371373 cmd .Dir = params .Directory
372374 cmd .Stdout = combined
373375 cmd .Stderr = combined
374- if err := cmd .Run (); err != nil {
376+ if err := cmd .Run (); err != nil && (ctx .Err () == nil || commandCtx .Ctx .Err () != nil ) {
377+ // If the command failed and the context hasn't been canceled, then return the error.
375378 return fmt .Sprintf ("ERROR: %s\n OUTPUT:\n %s" , err , & out ), nil
376379 }
377380 return out .String (), nil
@@ -420,7 +423,6 @@ func getWorkspaceEnvFileContents(envs []string) ([]string, error) {
420423 }
421424
422425 return envContents , nil
423-
424426}
425427
426428func getWorkspaceDir (envs []string ) (string , error ) {
@@ -665,6 +667,7 @@ func DiscardProgress() (progress chan<- string, closeFunc func()) {
665667 ch := make (chan string )
666668 go func () {
667669 for range ch {
670+ continue
668671 }
669672 }()
670673 return ch , func () {
0 commit comments