github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/watch/paths.go (about) 1 package watch 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/pkg/errors" 9 10 "github.com/tilt-dev/tilt/internal/ospath" 11 ) 12 13 func greatestExistingAncestor(path string) (string, error) { 14 if path == string(filepath.Separator) || 15 path == fmt.Sprintf("%s%s", filepath.VolumeName(path), string(filepath.Separator)) { 16 return "", fmt.Errorf("cannot watch root directory") 17 } 18 19 _, err := os.Stat(path) 20 if err != nil && !os.IsNotExist(err) { 21 return "", errors.Wrapf(err, "os.Stat(%q)", path) 22 } 23 24 if os.IsNotExist(err) { 25 return greatestExistingAncestor(filepath.Dir(path)) 26 } 27 28 return path, nil 29 } 30 31 // If we're recursively watching a path, it doesn't 32 // make sense to watch any of its descendants. 33 func dedupePathsForRecursiveWatcher(paths []string) []string { 34 result := []string{} 35 for _, current := range paths { 36 isCovered := false 37 hasRemovals := false 38 39 for i, existing := range result { 40 if ospath.IsChild(existing, current) { 41 // The path is already covered, so there's no need to include it 42 isCovered = true 43 break 44 } 45 46 if ospath.IsChild(current, existing) { 47 // Mark the element empty fo removal. 48 result[i] = "" 49 hasRemovals = true 50 } 51 } 52 53 if !isCovered { 54 result = append(result, current) 55 } 56 57 if hasRemovals { 58 // Remove all the empties 59 newResult := []string{} 60 for _, r := range result { 61 if r != "" { 62 newResult = append(newResult, r) 63 } 64 } 65 result = newResult 66 } 67 } 68 return result 69 }