diff --git a/cli/internal/context/context.go b/cli/internal/context/context.go index 3cc307a6d7d01..913a2f0bc7e4e 100644 --- a/cli/internal/context/context.go +++ b/cli/internal/context/context.go @@ -11,7 +11,7 @@ import ( "turbo/internal/backends" "turbo/internal/config" "turbo/internal/fs" - "turbo/internal/fs/globby" + "turbo/internal/globby" "turbo/internal/util" mapset "github.com/deckarep/golang-set" @@ -180,7 +180,7 @@ func WithGraph(rootpath string, config *config.Config) Option { globalDeps := make(util.Set) if len(pkg.Turbo.GlobalDependencies) > 0 { - f := globby.GlobFiles(rootpath, &pkg.Turbo.GlobalDependencies, nil) + f := globby.GlobFiles(rootpath, pkg.Turbo.GlobalDependencies, []string{}) for _, val := range f { globalDeps.Add(val) } @@ -243,7 +243,7 @@ func WithGraph(rootpath string, config *config.Config) Option { "**/tests/**/*", } - f := globby.GlobFiles(rootpath, &justJsons, &ignore) + f := globby.GlobFiles(rootpath, justJsons, ignore) for i, val := range f { _, val := i, val // https://golang.org/doc/faq#closures_and_goroutines diff --git a/cli/internal/fs/copy_file.go b/cli/internal/fs/copy_file.go index d7eaeca703a28..4f89d81cbf536 100644 --- a/cli/internal/fs/copy_file.go +++ b/cli/internal/fs/copy_file.go @@ -85,9 +85,12 @@ func WalkMode(rootPath string, callback func(name string, isDir bool, mode os.Fi } else if !info.IsDir() { return callback(rootPath, false, info.Mode()) } - return godirwalk.Walk(rootPath, &godirwalk.Options{Callback: func(name string, info *godirwalk.Dirent) error { - return callback(name, info.IsDir(), info.ModeType()) - }}) + return godirwalk.Walk(rootPath, &godirwalk.Options{ + Callback: func(name string, info *godirwalk.Dirent) error { + return callback(name, info.IsDir(), info.ModeType()) + }, + Unsorted: true, + }) } // SameFile returns true if the two given paths refer to the same physical diff --git a/cli/internal/fs/globby/globby.go b/cli/internal/fs/globby/globby.go deleted file mode 100644 index 61c0d46c032cb..0000000000000 --- a/cli/internal/fs/globby/globby.go +++ /dev/null @@ -1,79 +0,0 @@ -package globby - -import ( - "fmt" - "io/fs" - "path/filepath" - "strings" - - "github.com/bmatcuk/doublestar/v4" -) - -// // GlobList accepts a list of doublestar directive globs and returns a list of files matching them -// func Globby(base string, globs []string) ([]string, error) { -// ignoreList := []string{} -// actualGlobs := []string{} -// for _, output := range globs { -// if strings.HasPrefix(output, "!") { -// ignoreList = append(ignoreList, strings.TrimPrefix(output, "!")) -// } else { -// actualGlobs = append(actualGlobs, output) -// } -// } -// files := []string{} -// for _, glob := range actualGlobs { -// matches, err := doublestar.Glob(os.DirFS(base), glob) -// if err != nil { -// return nil, err -// } -// for _, match := range matches { -// for _, ignore := range ignoreList { -// if isMatch, _ := doublestar.PathMatch(ignore, match); !isMatch { -// files = append(files, match) -// } -// } -// } -// } -// } - -func GlobFiles(ws_path string, include_pattens *[]string, exclude_pattens *[]string) []string { - var include []string - var exclude []string - var result []string - - for _, p := range *include_pattens { - include = append(include, filepath.Join(ws_path, p)) - } - - for _, p := range *exclude_pattens { - exclude = append(exclude, filepath.Join(ws_path, p)) - } - - var include_pattern = "{" + strings.Join(include, ",") + "}" - var exclude_pattern = "{" + strings.Join(exclude, ",") + "}" - var _ = filepath.Walk(ws_path, func(p string, info fs.FileInfo, err error) error { - if err != nil { - fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", p, err) - return err - } - - if val, _ := doublestar.PathMatch(exclude_pattern, p); val { - if info.IsDir() { - return filepath.SkipDir - } - return nil - } - - if info.IsDir() { - return nil - } - - if val, _ := doublestar.PathMatch(include_pattern, p); val || len(*include_pattens) == 0 { - result = append(result, p) - } - - return nil - }) - - return result -} diff --git a/cli/internal/globby/globby.go b/cli/internal/globby/globby.go new file mode 100644 index 0000000000000..66174f9857454 --- /dev/null +++ b/cli/internal/globby/globby.go @@ -0,0 +1,47 @@ +package globby + +import ( + "turbo/internal/fs" + + "path/filepath" + "strings" + + "github.com/bmatcuk/doublestar/v4" +) + +func GlobFiles(basePath string, includePatterns []string, excludePatterns []string) []string { + var include []string + var exclude []string + var result []string + + for _, p := range includePatterns { + include = append(include, filepath.Join(basePath, p)) + } + + for _, p := range excludePatterns { + exclude = append(exclude, filepath.Join(basePath, p)) + } + + includePattern := "{" + strings.Join(include, ",") + "}" + excludePattern := "{" + strings.Join(exclude, ",") + "}" + _ = fs.Walk(basePath, func(p string, isDir bool) error { + if val, _ := doublestar.PathMatch(excludePattern, p); val { + if isDir { + return filepath.SkipDir + } + return nil + } + + if isDir { + return nil + } + + if val, _ := doublestar.PathMatch(includePattern, p); val || len(includePatterns) == 0 { + result = append(result, p) + } + + return nil + }) + + return result +} diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index de66df041595c..44a8370962cb5 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -20,7 +20,7 @@ import ( "turbo/internal/context" "turbo/internal/core" "turbo/internal/fs" - "turbo/internal/fs/globby" + "turbo/internal/globby" "turbo/internal/scm" "turbo/internal/ui" "turbo/internal/util" @@ -583,7 +583,7 @@ func (c *RunCommand) Run(args []string) int { if runOptions.cache && (pipeline.Cache == nil || *pipeline.Cache) { targetLogger.Debug("caching output", "outputs", outputs) ignore := []string{} - filesToBeCached := globby.GlobFiles(pack.Dir, &outputs, &ignore) + filesToBeCached := globby.GlobFiles(pack.Dir, outputs, ignore) if err := turboCache.Put(pack.Dir, hash, int(time.Since(cmdTime).Milliseconds()), filesToBeCached); err != nil { c.logError(targetLogger, "", fmt.Errorf("Error caching output: %w", err)) }