Skip to content

Commit

Permalink
Escape glob patterns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
konstin committed Sep 26, 2024
1 parent 23b010c commit b2fd51c
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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))?
{
Expand Down Expand Up @@ -1284,11 +1286,12 @@ fn is_excluded_from_workspace(
workspace: &ToolUvWorkspace,
) -> Result<bool, WorkspaceError> {
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))?
{
Expand All @@ -1309,11 +1312,12 @@ fn is_included_in_workspace(
workspace: &ToolUvWorkspace,
) -> Result<bool, WorkspaceError> {
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))?
{
Expand Down

0 comments on commit b2fd51c

Please sign in to comment.