From b2fd51c4eb9ed0c572b63ad2beda951c3642eb17 Mon Sep 17 00:00:00 2001 From: konstin Date: Thu, 26 Sep 2024 16:43:26 +0200 Subject: [PATCH] Escape glob patterns Previously, we could have interpreted the base directory as part of the glob. I initially tried to use globwalk, which also supports using multiple patterns at once, but it always seems to descend and not stop at emitting the exact matched directory, it also emits the children. --- crates/uv-workspace/src/workspace.rs | 36 +++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/crates/uv-workspace/src/workspace.rs b/crates/uv-workspace/src/workspace.rs index 22d811c4c639f..bad39966b092b 100644 --- a/crates/uv-workspace/src/workspace.rs +++ b/crates/uv-workspace/src/workspace.rs @@ -1,7 +1,8 @@ //! Resolve the current [`ProjectWorkspace`] or [`Workspace`]. use either::Either; -use glob::{glob, GlobError, PatternError}; +use glob::glob; +use globwalk::GlobWalkerBuilder; use rustc_hash::FxHashSet; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; @@ -637,11 +638,12 @@ impl Workspace { // Add all other workspace members. for member_glob in workspace_definition.clone().members.unwrap_or_default() { - let absolute_glob = workspace_root - .simplified() - .join(member_glob.as_str()) - .to_string_lossy() - .to_string(); + let absolute_glob = PathBuf::from(glob::Pattern::escape( + workspace_root.simplified().to_string_lossy().as_ref(), + )) + .join(member_glob.as_str()) + .to_string_lossy() + .to_string(); for member_root in glob(&absolute_glob) .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))? { @@ -1284,11 +1286,12 @@ fn is_excluded_from_workspace( workspace: &ToolUvWorkspace, ) -> Result { for exclude_glob in workspace.exclude.iter().flatten() { - let absolute_glob = workspace_root - .simplified() - .join(exclude_glob.as_str()) - .to_string_lossy() - .to_string(); + let absolute_glob = PathBuf::from(glob::Pattern::escape( + workspace_root.simplified().to_string_lossy().as_ref(), + )) + .join(exclude_glob.as_str()) + .to_string_lossy() + .to_string(); for excluded_root in glob(&absolute_glob) .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))? { @@ -1309,11 +1312,12 @@ fn is_included_in_workspace( workspace: &ToolUvWorkspace, ) -> Result { for member_glob in workspace.members.iter().flatten() { - let absolute_glob = workspace_root - .simplified() - .join(member_glob.as_str()) - .to_string_lossy() - .to_string(); + let absolute_glob = PathBuf::from(glob::Pattern::escape( + workspace_root.simplified().to_string_lossy().as_ref(), + )) + .join(member_glob.as_str()) + .to_string_lossy() + .to_string(); for member_root in glob(&absolute_glob) .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))? {