Skip to content

fix: propagate error in sort_paths instead of panicking#2188

Open
suhr25 wants to merge 2 commits intoconda:mainfrom
suhr25:fix/sort-paths-panic
Open

fix: propagate error in sort_paths instead of panicking#2188
suhr25 wants to merge 2 commits intoconda:mainfrom
suhr25:fix/sort-paths-panic

Conversation

@suhr25
Copy link
Contributor

@suhr25 suhr25 commented Mar 7, 2026

Description

sort_paths previously used .unwrap() on Path::strip_prefix, which could panic if a path was not under base_path. This change replaces the panic with proper error propagation by returning an io::Error. The two call sites now propagate the error using ?, aligning the implementation with the documented behavior of the package writing APIs.

Fixes

  • Replace .unwrap() on strip_prefix with proper error handling
  • Update sort_paths to return Result<(Vec<PathBuf>, Vec<PathBuf>), io::Error>
  • Propagate the error at both call sites using ?
  • Provide a clearer error message when a path is outside base_path

How Has This Been Tested?

Tested by passing a path that is not under the provided base_path. Before the fix the code panicked due to .unwrap(). After the fix it correctly returns an io::Error.

AI Disclosure

  • This PR contains AI-generated content.
  • I have tested any AI-generated content in my PR.
  • I take responsibility for any AI-generated content in my PR.

Tools: Chatgpt, Claude

Checklist

  • Code compiles successfully
  • Tests pass
  • No breaking changes
  • Error handling follows existing project patterns

… under base_path

Signed-off-by: suhr25 <suhridmarwah07@gmail.com>
@suhr25 suhr25 changed the title fix: return error instead of panicking in sort_paths when path is not… fix: propagate error in sort_paths instead of panicking Mar 7, 2026
- Gate `StreamExt` and `AsyncReadExt` imports with `#[cfg(feature = "reqwest")]`
  since they are only used in reqwest-gated code paths
- Rename `|_|` to `|_e|` in map_err to satisfy clippy::map_err_ignore

Signed-off-by: suhr25 <suhridmarwah07@gmail.com>
@suhr25
Copy link
Contributor Author

suhr25 commented Mar 7, 2026

Hi @baszalmstra,
Please take a look at this PR.
Thanks!

@zelosleone
Copy link

Just wanted to left a comment since the pr interested me, but using a loop by replacing an iterator is not a correct approach in my opinion. You could just use collect and still use the simple iterator.

Comment on lines +79 to +91
for p in paths {
let rel = p.strip_prefix(base_path).map_err(|_e| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"path '{}' is not under base path '{}'",
p.display(),
base_path.display()
),
)
})?;
relative_paths.push(rel.to_path_buf());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing a for loop is not better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants