diff --git a/internal/infrastructure/configuration/loader.go b/internal/infrastructure/configuration/loader.go index 93d2e80fe..bac412c51 100644 --- a/internal/infrastructure/configuration/loader.go +++ b/internal/infrastructure/configuration/loader.go @@ -80,30 +80,35 @@ func (l *Loader) Resolve(basePath, overlayPath string) (*Document, error) { if err != nil { return nil, fmt.Errorf("%w: config source %s: %w", ErrUnreadable, basePath, err) } + if err := validateOverlayPath(overlayPath, basePath, info.IsDir()); err != nil { + return nil, err + } if info.IsDir() { - if overlayPath != "" { - overlayInfo, statErr := os.Stat(overlayPath) - if statErr != nil { - return nil, fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, statErr) - } - if !overlayInfo.IsDir() { - return nil, fmt.Errorf("config overlay %s must be a directory when config source %s is a directory", overlayPath, basePath) - } - } return l.Dir(basePath, overlayPath) } - if overlayPath != "" { - overlayInfo, statErr := os.Stat(overlayPath) - if statErr != nil { - return nil, fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, statErr) - } - if overlayInfo.IsDir() { - return nil, fmt.Errorf("config overlay %s must be a file when config source %s is a file", overlayPath, basePath) - } - } return l.overlayFile(basePath, overlayPath) } +// validateOverlayPath checks that overlayPath, when non-empty, exists and has +// the same filesystem kind as basePath: a directory overlay for a directory +// source and a file overlay for a file source. +func validateOverlayPath(overlayPath, basePath string, baseIsDir bool) error { + if overlayPath == "" { + return nil + } + overlayInfo, err := os.Stat(overlayPath) + if err != nil { + return fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, err) + } + if overlayInfo.IsDir() != baseIsDir { + if baseIsDir { + return fmt.Errorf("config overlay %s must be a directory when config source %s is a directory", overlayPath, basePath) + } + return fmt.Errorf("config overlay %s must be a file when config source %s is a file", overlayPath, basePath) + } + return nil +} + // Overlay loads basePath, then decodes overlayPath into the same value so // only the fields the overlay sets are replaced -- every field it omits keeps // the base value. This lets a deployment-specific file (config.docker.toml) diff --git a/internal/tools/builtin/grep.go b/internal/tools/builtin/grep.go index e6140817f..1624b92c5 100644 --- a/internal/tools/builtin/grep.go +++ b/internal/tools/builtin/grep.go @@ -376,60 +376,68 @@ func grepFallback(ctx context.Context, p GrepParams, searchPath, cwd string, tar } } results = append(results, res...) - } else { - err = filepath.WalkDir(searchPath, func(walkPath string, d os.DirEntry, err error) error { - if err != nil { - return nil //nolint:nilerr // intentionally skip inaccessible entries - } - - select { - case <-ctx.Done(): - return ctx.Err() - default: - } - - if d.IsDir() { - // Skip hidden directories. - if d.Name() != "" && d.Name()[0] == '.' && walkPath != searchPath { - return filepath.SkipDir - } - return nil - } - - // Skip hidden files. - if d.Name() != "" && d.Name()[0] == '.' { - return nil - } - - // Include filter. - if p.Include != "" { - if matched, _ := filepath.Match(p.Include, d.Name()); !matched { - return nil - } - } - - res, err := grepFile(ctx, walkPath, matcher, p.ContextBefore, p.ContextAfter) - if err != nil { - return nil //nolint:nilerr // intentionally skip files we can't read - } - for i := range res { - res[i].relPath, _ = filepath.Rel(cwd, walkPath) - res[i].relPath = filepath.ToSlash(res[i].relPath) - if res[i].relPath == "" || res[i].relPath == "." { - res[i].relPath = filepath.ToSlash(filepath.Base(walkPath)) - } - } - results = append(results, res...) - return nil - }) - if err != nil && !errors.Is(err, ctx.Err()) { - return "", err - } + } else if err := grepDirFallback(ctx, searchPath, cwd, p, matcher, &results); err != nil { + return "", err } return formatGrepResults(results), nil } +// grepDirFallback walks searchPath and appends every readable, non-hidden file +// matching the include filter to results. It skips hidden directories and +// unreadable files without failing the whole search. +func grepDirFallback(ctx context.Context, searchPath, cwd string, p GrepParams, matcher func(string) bool, results *[]grepResult) error { + err := filepath.WalkDir(searchPath, func(walkPath string, d os.DirEntry, err error) error { + if err != nil { + return nil //nolint:nilerr // intentionally skip inaccessible entries + } + + select { + case <-ctx.Done(): + return ctx.Err() + default: + } + + if d.IsDir() { + // Skip hidden directories. + if d.Name() != "" && d.Name()[0] == '.' && walkPath != searchPath { + return filepath.SkipDir + } + return nil + } + + // Skip hidden files. + if d.Name() != "" && d.Name()[0] == '.' { + return nil + } + + // Include filter. + if p.Include != "" { + if matched, _ := filepath.Match(p.Include, d.Name()); !matched { + return nil + } + } + + res, err := grepFile(ctx, walkPath, matcher, p.ContextBefore, p.ContextAfter) + if err != nil { + return nil //nolint:nilerr // intentionally skip files we can't read + } + for i := range res { + res[i].relPath, _ = filepath.Rel(cwd, walkPath) + res[i].relPath = filepath.ToSlash(res[i].relPath) + if res[i].relPath == "" || res[i].relPath == "." { + res[i].relPath = filepath.ToSlash(filepath.Base(walkPath)) + } + } + *results = append(*results, res...) + return nil + }) + if err != nil && !errors.Is(err, ctx.Err()) { + return err + } + return nil +} + // buildMatcher returns a line matcher based on grep parameters. func buildMatcher(p GrepParams) (func(string) bool, error) { if !p.Literal { diff --git a/internal/tools/builtin/read.go b/internal/tools/builtin/read.go index 982bd67fe..ae9e4898a 100644 --- a/internal/tools/builtin/read.go +++ b/internal/tools/builtin/read.go @@ -97,22 +97,8 @@ func suggestPaths(cwd, missing string) string { // shape of the real misses (internal/theme/theme.go, where the directory // exists but holds colors.go), and it avoids the walk entirely. if dir := filepath.Dir(missing); dir != "" { - if entries, err := os.ReadDir(dir); err == nil { - names := make([]string, 0, maxPathSuggestions) - for _, e := range entries { - if len(names) >= maxPathSuggestions { - names = append(names, "...") - break - } - names = append(names, e.Name()) - } - if len(names) > 0 { - rel, relErr := filepath.Rel(cwd, dir) - if relErr != nil { - rel = dir - } - return fmt.Sprintf("%s exists and contains: %s", rel, strings.Join(names, ", ")) - } + if suggestion := dirContentsSuggestion(cwd, dir); suggestion != "" { + return suggestion } } @@ -150,6 +136,31 @@ func suggestPaths(cwd, missing string) string { return "did you mean: " + strings.Join(found, ", ") } +// dirContentsSuggestion lists the entries in an existing directory as a +// "did you mean" hint, or returns "" when the directory is unreadable or empty. +func dirContentsSuggestion(cwd, dir string) string { + entries, err := os.ReadDir(dir) + if err != nil { + return "" + } + names := make([]string, 0, maxPathSuggestions) + for _, e := range entries { + if len(names) >= maxPathSuggestions { + names = append(names, "...") + break + } + names = append(names, e.Name()) + } + if len(names) == 0 { + return "" + } + rel, relErr := filepath.Rel(cwd, dir) + if relErr != nil { + rel = dir + } + return fmt.Sprintf("%s exists and contains: %s", rel, strings.Join(names, ", ")) +} + // skipSuggestionDir keeps the suggestion walk out of directories that are large // and never the answer. func skipSuggestionDir(name string) bool {