Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/glob/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ struct Wildcard {
/// Matches zero or more characters, except for path separators ('/' or '\').
/// "**"
/// Matches zero or more characters, including path separators.
/// Must match a complete path segment, i.e. followed by a path separator or
/// at the end of the pattern.
/// Must be a complete path segment, i.e. preceded by a path separator (or
/// the start of the pattern) and followed by one (or the end of the
/// pattern). Anywhere else ("a**") it behaves like "*".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code

/// "[ab]"
/// Matches one of the characters contained in the brackets.
/// Character ranges (e.g. "[a-z]") are also supported.
Expand Down Expand Up @@ -199,8 +200,18 @@ fn glob_match_impl(
'to_else: {
match ch {
b'*' => {
// `**` is a globstar only at the start of a segment: after a `/`
// or at `glob_start` (the pattern or, as in `{**/a,**/b}`, the
// brace branch; `<=` because backtracking out of a branch can
// land before its start). Anywhere else (`a**`) each `*` is a
// plain wildcard, and this must be decided before
// `skip_globstars` folds any following `/**` segments into this
// one: those belong to the rest of the pattern when this `**`
// is not a globstar (`a**/**` means `a*/**`).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code

let is_globstar = (state.glob_index as usize) + 1 < glob.len()
&& glob[state.glob_index as usize + 1] == b'*';
&& glob[state.glob_index as usize + 1] == b'*'
&& (state.glob_index <= glob_start
|| glob[state.glob_index as usize - 1] == b'/');
if is_globstar {
skip_globstars(glob, &mut state.glob_index);
}
Expand Down Expand Up @@ -232,13 +243,7 @@ fn glob_match_impl(
continue 'main_loop;
}

// subtract glob_start from glob index before checking if length is less than 3. Given the pattern:
// {**/a,**/b}
// if we start at index 6 (start of **/b pattern), we don't want to index into the pattern before it
if (state.glob_index.saturating_sub(glob_start) < 3
|| glob[state.glob_index as usize - 3] == b'/')
&& (!is_end_invalid || glob[state.glob_index as usize] == b'/')
{
if !is_end_invalid || glob[state.glob_index as usize] == b'/' {
if is_end_invalid {
state.glob_index += 1;
}
Expand Down
65 changes: 65 additions & 0 deletions test/js/bun/glob/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,71 @@ describe("Glob.match", () => {
expect(new Glob("foo/**").match("foo/\u0041\u030A")).toBeTrue(); // "A" with ring
expect(new Glob("foo/**").match("foo/\u00C5")).toBeTrue(); // "Å" single character
});

test("** inside a segment stays a plain * when ** segments follow it", () => {
// `a**` is `a*` (see "bash extra_stars" and "trailing globstar patterns"), so
// `a**/**` is `a*/**`: the `/**` is its own segment and the path still needs
// a `/`. The matcher used to fold the following `**` segments into the inline
// `**` before noticing it was not a globstar, which left plain `a*` behind.
expect(new Glob("a**/**").match("ab")).toBeFalse();
expect(new Glob("a**/**").match("a")).toBeFalse();
expect(new Glob("a/b**/**").match("a/b")).toBeFalse();
expect(new Glob("?**/**").match("b")).toBeFalse();
expect(new Glob("[a]**/**").match("a")).toBeFalse();
expect(new Glob("**/a**/**").match("x/ab")).toBeFalse();
expect(new Glob("{x,a}**/**").match("ab")).toBeFalse();
expect(new Glob("a**/**/**").match("ab")).toBeFalse();
expect(new Glob("a**/**/b").match("ab")).toBeFalse();
expect(new Glob("a**/**/*.js").match("ab.js")).toBeFalse();
expect(new Glob("!a**/**").match("ab")).toBeTrue();

// With the `/` present, the trailing `**` segments match as usual.
expect(new Glob("a**/**").match("a/")).toBeTrue();
expect(new Glob("a**/**").match("ab/")).toBeTrue();
expect(new Glob("a**/**").match("ab/c")).toBeTrue();
expect(new Glob("a**/**").match("ab/c/d")).toBeTrue();
expect(new Glob("a**/**/b").match("ab/b")).toBeTrue();
expect(new Glob("a**/**/b").match("ab/x/y/b")).toBeTrue();
expect(new Glob("a**/**/*.js").match("ab/c/d.js")).toBeTrue();
expect(new Glob("**/a**/**").match("x/ab/c")).toBeTrue();
expect(new Glob("!a**/**").match("ab/c")).toBeFalse();

// Every inline form must agree with its `*` spelling on every path.
const paths = [
"",
..."a ab axb ba a/ ab/ a/b ab/b ab/c ab/c/ ab/c/d ab/x/y/b x/ab x/ab/c ab.js ab/c.js".split(" "),
];
const spellings: [inline: string, plain: string][] = [
["a**/**", "a*/**"],
["a**/**/**", "a*/**/**"],
["a**/**/", "a*/**/"],
["a**/**/b", "a*/**/b"],
["a**/**/*", "a*/**/*"],
["a**/**/*.js", "a*/**/*.js"],
["?**/**", "?*/**"],
["[a]**/**", "[a]*/**"],
["**/a**/**", "**/a*/**"],
["{x,a}**/**", "{x,a}*/**"],
["!a**/**", "!a*/**"],
];
const matches = (pattern: string) => paths.map(path => ({ path, matches: new Glob(pattern).match(path) }));
for (const [inline, plain] of spellings) {
expect(matches(inline), `${inline} should match the same paths as ${plain}`).toEqual(matches(plain));
}

// Runs of real globstar segments still collapse into one.
expect(new Glob("**/**").match("a")).toBeTrue();
expect(new Glob("a/**/**").match("a/")).toBeTrue();
expect(new Glob("a/**/**/b").match("a/b")).toBeTrue();
expect(new Glob("a/**/**/*").match("a/")).toBeFalse();
expect(new Glob("{**/**/a,**/b}").match("x/y/b")).toBeTrue();

// A failing brace branch backtracks to the pattern-initial `**`, which is
// then re-examined from inside the branch and must still be a globstar.
expect(new Glob("**/{a,b}").match("x/y/b")).toBeTrue();
expect(new Glob("!**/{a,b}").match("x/y/b")).toBeFalse();
expect(new Glob("**/{a,b}").match("x/y/c")).toBeFalse();
});
});

function returnError(cb: () => any): Error | undefined {
Expand Down
Loading