From 15db7f459c049fcd044cc6106e7be294d9b53ce5 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Fri, 12 Jan 2024 15:15:51 +0000 Subject: [PATCH] fix: duplicate processing in ordered formatters (#24) Fixes a bug with formatters processing paths out of order. Signed-off-by: Brian McGee Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/24 Co-authored-by: Brian McGee Co-committed-by: Brian McGee --- internal/cli/format_test.go | 2 +- internal/format/format.go | 4 ++++ internal/walk/git.go | 6 ++---- nix/packages.nix | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/cli/format_test.go b/internal/cli/format_test.go index cc89893..e3c285e 100644 --- a/internal/cli/format_test.go +++ b/internal/cli/format_test.go @@ -57,7 +57,7 @@ func TestDependencyCycle(t *testing.T) { }) _, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir) - as.ErrorContains(err, "formatter cycle detected a -> b -> c") + as.ErrorContains(err, "formatter cycle detected") } func TestSpecifyingFormatters(t *testing.T) { diff --git a/internal/format/format.go b/internal/format/format.go index 76a9fb1..460e506 100644 --- a/internal/format/format.go +++ b/internal/format/format.go @@ -122,6 +122,10 @@ func (f *Formatter) SetChild(formatter *Formatter) { // Wants is used to test if a Formatter wants path based on it's configured Includes and Excludes patterns. // Returns true if the Formatter should be applied to path, false otherwise. func (f *Formatter) Wants(path string) bool { + if f.parent != nil { + // we don't accept this path directly, our parent will forward it + return false + } match := !PathMatches(path, f.excludes) && PathMatches(path, f.includes) if match { f.log.Debugf("match: %v", path) diff --git a/internal/walk/git.go b/internal/walk/git.go index 5d037d1..09fd3c7 100644 --- a/internal/walk/git.go +++ b/internal/walk/git.go @@ -3,9 +3,10 @@ package walk import ( "context" "fmt" - "github.com/go-git/go-git/v5" "os" "path/filepath" + + "github.com/go-git/go-git/v5" ) type gitWalker struct { @@ -18,14 +19,12 @@ func (g *gitWalker) Root() string { } func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error { - idx, err := g.repo.Storer.Index() if err != nil { return fmt.Errorf("%w: failed to open index", err) } for _, entry := range idx.Entries { - select { case <-ctx.Done(): return ctx.Err() @@ -38,7 +37,6 @@ func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error { return err } } - } return nil diff --git a/nix/packages.nix b/nix/packages.nix index 0259322..a430734 100644 --- a/nix/packages.nix +++ b/nix/packages.nix @@ -28,7 +28,7 @@ nativeBuildInputs = # we need some formatters available for the tests - (import ./formatters.nix pkgs); + import ./formatters.nix pkgs; preCheck = '' XDG_CACHE_HOME=$(mktemp -d)