fix: handle invalid working directories gracefully with --full-path#1917
fix: handle invalid working directories gracefully with --full-path#1917tmccombs merged 5 commits intosharkdp:masterfrom
Conversation
tmccombs
left a comment
There was a problem hiding this comment.
This is a decent attempt, although there are some minor issues.
however, I'm not sure this is the best approach. One thing I notice is that we are calling current_dir() every time we construct an absolute path.
I wonder if maybe it would be better to re-architect this so that we only call current_dir() once, at the beginning, and if that succeeds, then we can create absolute paths from that infallibly afterwords, and if it fails we can fail early, without needing to check for every entry. And it would probably be more performant as well.
It would mean that if the current directory disappears while fd is running you will continue to get results that may no longer be valid, which may be good or bad depending on the situation.
|
You're right, caching cwd once at startup is the better approach. I reworked the PR: Config now gets a |
…harkdp#1900) - Add FatalError variant to WorkerResult for unrecoverable errors - Extract search_str_for_entry() helper to propagate IO errors from path_absolute_form - Propagate fatal errors through job and batch execution paths - Add regression test for invalid cwd scenario - Update CHANGELOG for sharkdp#1900
Instead of calling current_dir() for every entry when --full-path is set, cache it once in Config at startup. This eliminates the need for FatalError handling and makes path resolution infallible during the walk. If the cwd can't be retrieved, fd now fails early with a clear error message.
| match entry_path.file_name() { | ||
| Some(filename) => Cow::Borrowed(filename), | ||
| None => unreachable!( | ||
| "Encountered file system entry without a file name. This should only \ |
There was a problem hiding this comment.
I know you cut and pasted this code so no need to fix this in this PR, but we already have a better approach here:
Lines 132 to 146 in 7027d45
I think we should expose DirEntry::file_name() and then just use that rather than doing this.
There was a problem hiding this comment.
Noted, will keep in mind for a follow-up.
| pub fn make_absolute(path: &Path, cwd: &Path) -> PathBuf { | ||
| if path.is_absolute() { | ||
| return path.to_path_buf(); | ||
| } | ||
| let path = path.strip_prefix(".").unwrap_or(path); | ||
| cwd.join(path) | ||
| } |
There was a problem hiding this comment.
| pub fn make_absolute(path: &Path, cwd: &Path) -> PathBuf { | |
| if path.is_absolute() { | |
| return path.to_path_buf(); | |
| } | |
| let path = path.strip_prefix(".").unwrap_or(path); | |
| cwd.join(path) | |
| } | |
| pub fn make_absolute<'a, 'b>(path: &'a Path, cwd: &'b Path) -> Cow<'a, Path> { | |
| if path.is_absolute() { | |
| return Cow::Borrowed(path); | |
| } | |
| let path = path.strip_prefix(".").unwrap_or(path); | |
| Cow::Owned(cwd.join(path)) | |
| } |
There was a problem hiding this comment.
nit: it seems slightly more intuitive to me for the cwd to come first, but that might just be me.
| pub search_full_path: bool, | ||
| /// Cached current working directory for absolute path construction. | ||
| /// Populated when `--full-path` is set; `None` means search by filename only. | ||
| pub cwd: Option<PathBuf>, |
There was a problem hiding this comment.
nit: perhaps full_path_base would be more indicative of how this is used?
| ) -> Cow<'a, OsStr> { | ||
| if let Some(cwd) = cwd { | ||
| let abs_path = filesystem::make_absolute(entry_path, cwd); | ||
| Cow::Owned(abs_path.into_os_string()) |
There was a problem hiding this comment.
I know this is the same as what we had before, and doesn't need to be changed in this PR, but this could actually be a Borrowed, if the original path was absolute.
It would be a little simpler if we changed the new make_absolute function to return a Cow<'a, OsStr> directly instead of a PathBuf or Cow
Fixes #1900
When using
--full-path, fd panics if the current working directory becomes invalid (e.g., deleted or inaccessible) during execution. This occurs because the code attempts to resolve relative paths after the cwd is no longer accessible.This fix replaces the panic with a graceful error message, allowing fd to exit cleanly instead of crashing.
Changes:
cargo testandcargo clippylocally