From c68261e9799996d427417fadf68ba54d06d9ff2b Mon Sep 17 00:00:00 2001 From: zimbatm Date: Fri, 14 Jun 2024 14:09:04 +0200 Subject: [PATCH 1/2] fix: stop hard-killing formatters Give the processes some chance to clean after themselves. Fixes https://github.com/numtide/treefmt/issues/316#issuecomment-2160628891 --- format/formatter.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/format/formatter.go b/format/formatter.go index a01646e..0cb8983 100644 --- a/format/formatter.go +++ b/format/formatter.go @@ -66,6 +66,10 @@ func (f *Formatter) Apply(ctx context.Context, tasks []*Task) error { // execute the command cmd := exec.CommandContext(ctx, f.executable, args...) + // replace the default Cancel handler installed by CommandContext because it sends SIGKILL (-9). + cmd.Cancel = func() error { + return cmd.Process.Signal(os.Interrupt) + } cmd.Dir = f.workingDir // log out the command being executed From e29107d3ee11255dbe2ee7824bc4805a10c43134 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sat, 15 Jun 2024 11:57:55 +0200 Subject: [PATCH 2/2] fix: only cancel formatters on background errors If any of the formatting action returns an error, keep going to surface as many errors as possible. If the user hits Ctrl-C then terminate everything. --- cli/format.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cli/format.go b/cli/format.go index 4aabba5..492a4f3 100644 --- a/cli/format.go +++ b/cli/format.go @@ -325,9 +325,11 @@ func (f *Format) walkFilesystem(ctx context.Context) func() error { } } +// applyFormatters func (f *Format) applyFormatters(ctx context.Context) func() error { - // create our own errgroup for concurrent formatting tasks - fg, ctx := errgroup.WithContext(ctx) + // create our own errgroup for concurrent formatting tasks. + // we don't want a cancel clause, in order to let formatters run up to the end. + fg := errgroup.Group{} // simple optimization to avoid too many concurrent formatting tasks // we can queue them up faster than the formatters can process them, this paces things a bit fg.SetLimit(runtime.NumCPU())