diff --git a/cli/format.go b/cli/format.go index 93593bbc..edc8762f 100644 --- a/cli/format.go +++ b/cli/format.go @@ -201,6 +201,9 @@ func (f *Format) Run() (err error) { func (f *Format) walkFilesystem(ctx context.Context) func() error { return func() error { + // close the files channel when we're done walking the file system + defer close(f.filesCh) + eg, ctx := errgroup.WithContext(ctx) pathsCh := make(chan string, BatchSize) @@ -261,9 +264,6 @@ func (f *Format) walkFilesystem(ctx context.Context) func() error { return fmt.Errorf("failed to create walker: %w", err) } - // close the files channel when we're done walking the file system - defer close(f.filesCh) - // if no cache has been configured, or we are processing from stdin, we invoke the walker directly if f.NoCache || f.Stdin { return walker.Walk(ctx, func(file *walk.File, err error) error { diff --git a/cli/format_test.go b/cli/format_test.go index 303cbfcc..72540277 100644 --- a/cli/format_test.go +++ b/cli/format_test.go @@ -604,11 +604,19 @@ func TestStdIn(t *testing.T) { os.Stdin = prevStdIn }) - // + // omit the required filename parameter contents := `{ foo, ... }: "hello"` os.Stdin = test.TempFile(t, "", "stdin", &contents) + // we get an error about the missing filename parameter. + out, err := cmd(t, "-C", tempDir, "--allow-missing-formatter", "--stdin") + as.EqualError(err, "only one path should be specified when using the --stdin flag") + as.Equal("", string(out)) - out, err := cmd(t, "-C", tempDir, "--allow-missing-formatter", "--stdin", "test.nix") + // now pass along the filename parameter + contents = `{ foo, ... }: "hello"` + os.Stdin = test.TempFile(t, "", "stdin", &contents) + + out, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "--stdin", "test.nix") as.NoError(err) assertStats(t, as, 1, 1, 1, 1)