fix: handle stat errors in file helpers#10084
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the IsFile and IsDir utility functions to simplify error handling, ensuring they return false on any os.Stat error. A new test case is also included to verify this behavior. The reviewer recommends adding documentation comments to these exported functions to align with Go best practices.
| @@ -267,14 +267,18 @@ func AbsolutePaths(workspace string, paths []string) []string { | |||
|
|
|||
| func IsFile(path string) bool { | |||
There was a problem hiding this comment.
Exported functions should have a documentation comment according to Go best practices. This provides clarity on the function's behavior, such as whether it follows symlinks and how it handles errors.
| func IsFile(path string) bool { | |
| // IsFile returns true if the path exists and is a regular file. Symlinks are followed. | |
| func IsFile(path string) bool { |
References
- All exported names should have a doc comment. This is a standard practice in Go to improve code maintainability and discoverability. (link)
| return info.Mode().IsRegular() | ||
| } | ||
|
|
||
| func IsDir(path string) bool { |
There was a problem hiding this comment.
Exported functions should have a documentation comment according to Go best practices. This clarifies that the function follows symlinks and returns false on any error.
| func IsDir(path string) bool { | |
| // IsDir returns true if the path exists and is a directory. Symlinks are followed. | |
| func IsDir(path string) bool { |
References
- All exported names should have a doc comment. This is a standard practice in Go to improve code maintainability and discoverability. (link)
Description
IsFileandIsDirassumedos.Statalways returnsFileInfounless the path is missing. Butfile/child, wherefileis a regular file, returns a stat error and nil info, so the old code panics.This returns
falseon any stat error, same vibe as "not a file/dir". tiny fix, low-key useful.Repro:
IsFile("<tmp>/file/child")orIsDir("<tmp>/file/child")IsFile/IsDirfalseUser facing changes
No CLI output changes, just avoids a crash for bad local paths.
Testing
go test ./pkg/skaffold/util -run 'TestIsFileIsDir|TestIsFileIsDirStatError'\n-go test ./pkg/skaffold/util\n-make quicktest