Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix globstar expansion logic to prevent infinite loop when file path contains ** #6174

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions Sources/TuistSupport/Utils/Glob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,37 @@ public class Glob: Collection {
}

private func expandGlobstar(pattern: String) -> [String] {
guard pattern.contains("**") else {
// Split pattern string by slash to find globstar.
let patternComponents = pattern.components(separatedBy: "/")

// We are only interested in the first globstar since that is where we want to separate the pattern string.
guard let pivot = patternComponents.firstIndex(of: "**") else {
return [pattern]
}

var results = [String]()
var parts = pattern.components(separatedBy: "**")
let firstPart = parts.removeFirst()
var lastPart = parts.joined(separator: "**")

// Part before the first globstar
let firstPartLowerBound = 0
let firstPartUpperBound = pivot
let firstPartComponents: ArraySlice<String> = if firstPartLowerBound < firstPartUpperBound {
patternComponents[firstPartLowerBound ..< firstPartUpperBound]
} else {
[]
}
let firstPart = firstPartComponents.joined(separator: "/")

// Part after the first globstar
let lastPartLowerBound = pivot + 1
let lastPartUpperBound = patternComponents.count
let lastPartComponents: ArraySlice<String> = if lastPartLowerBound < lastPartUpperBound {
patternComponents[lastPartLowerBound ..< lastPartUpperBound]
} else {
[]
}
var lastPart = lastPartComponents.joined(separator: "/")

// Find subdirectories
let fileManager = FileManager.default

var directories = fileManager.subdirectoriesResolvingSymbolicLinks(atPath: firstPart)
Expand Down