From 9ce6522f8ab07ede986ff9aa3e218ba3c816d63a Mon Sep 17 00:00:00 2001 From: Jeremy Fleischman Date: Sat, 12 Oct 2024 15:08:59 -0500 Subject: [PATCH] fix: normalize and enforce stdin paths as well See https://github.com/numtide/treefmt/pull/440 for the last attempt to do this because (at the time) this "is the file in the tree root?" check relied upon checking for concrete files in the filesystem. As of https://github.com/numtide/treefmt/commit/ff3de21a2b35a3218e845609ccd5a6a7215e9fdc, that's longer the case, so we can make this code a bit more consistent. --- cmd/format/format.go | 38 +++++++++++++++++++------------------- cmd/root_test.go | 8 ++++++++ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/cmd/format/format.go b/cmd/format/format.go index 95896e18..2c3c77fd 100644 --- a/cmd/format/format.go +++ b/cmd/format/format.go @@ -168,31 +168,31 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string) return fmt.Errorf("invalid walk type: %w", err) } - if walkType == walk.Stdin { + if walkType == walk.Stdin && len(paths) != 1 { // check we have only received one path arg which we use for the file extension / matching to formatters - if len(paths) != 1 { - return fmt.Errorf("exactly one path should be specified when using the --stdin flag") + return fmt.Errorf("exactly one path should be specified when using the --stdin flag") + } + + // checks all paths are contained within the tree root and exist + // also "normalize" paths so they're relative to cfg.TreeRoot + for i, path := range paths { + absolutePath, err := filepath.Abs(path) + if err != nil { + return fmt.Errorf("error computing absolute path of %s: %w", path, err) } - } else { - // checks all paths are contained within the tree root and exist - // also "normalize" paths so they're relative to cfg.TreeRoot - for i, path := range paths { - absolutePath, err := filepath.Abs(path) - if err != nil { - return fmt.Errorf("error computing absolute path of %s: %w", path, err) - } - relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath) - if err != nil { - return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err) - } + relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath) + if err != nil { + return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err) + } - if strings.HasPrefix(relativePath, "..") { - return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot) - } + if strings.HasPrefix(relativePath, "..") { + return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot) + } - paths[i] = relativePath + paths[i] = relativePath + if walkType != walk.Stdin { if _, err = os.Stat(absolutePath); err != nil { return fmt.Errorf("path %s not found", path) } diff --git a/cmd/root_test.go b/cmd/root_test.go index 2aab982a..6365f807 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -748,6 +748,14 @@ func TestStdin(t *testing.T) { as.Equal(`{ ...}: "hello" `, string(out)) + // try a file that's outside of the project root + contents = `{ foo, ... }: "hello"` + os.Stdin = test.TempFile(t, "", "stdin", &contents) + + out, _, err = treefmt(t, "-C", tempDir, "--allow-missing-formatter", "--stdin", "../test.nix") + as.Errorf(err, "path ../test.nix not inside the tree root %s", tempDir) + as.Equal("", string(out)) + // try some markdown instead contents = ` | col1 | col2 |