Skip to content

escape character with brace syntax breaks glob match #502

Description

@magicdawn

Bug

escape character with brace syntax breaks glob match

Reproduce Link

https://stackblitz.com/edit/fast-glob-quote-issue-mj24gmzv?file=index.js

import fg from 'fast-glob';
const { glob } = fg;

// .{txt}
{
  const files = await glob(`./files/*\\[id\\]*.{txt}`);
  console.log(`fast-glob using \`brace{}\` for extension`, { files });
}
{
  const files = await glob(`./files/*${fg.escapePath('[id]')}*.{txt}`);
  console.log(`fast-glob using \`brace{}\` for extension`, { files });
}

// .(txt)
{
  const files = await glob(`./files/*\\[id\\]*.(txt)`);
  console.log(`fast-glob using \`regex group()\` for extension`, { files });
}
{
  const files = await glob(`./files/*${fg.escapePath('[id]')}*.(txt)`);
  console.log(`fast-glob using \`regex group()\` for extension`, { files });
}

// .@(txt)
{
  const files = await glob(`./files/*\\[id\\]*.@(txt)`);
  console.log(`fast-glob using \`extglob @()\` for extension`, { files });
}
{
  const files = await glob(`./files/*${fg.escapePath('[id]')}*.@(txt)`);
  console.log(`fast-glob using \`extglob @()\` for extension`, { files });
}

result

fast-glob using `brace{}` for extension { files: [] }
fast-glob using `brace{}` for extension { files: [] }
fast-glob using `regex group()` for extension {
  files: [ './files/some [id] foo.txt', "./files/some [id]'s file.txt" ]
}
fast-glob using `regex group()` for extension {
  files: [ './files/some [id] foo.txt', "./files/some [id]'s file.txt" ]
}
fast-glob using `extglob @()` for extension {
  files: [ './files/some [id] foo.txt', "./files/some [id]'s file.txt" ]
}
fast-glob using `extglob @()` for extension {
  files: [ './files/some [id] foo.txt', "./files/some [id]'s file.txt" ]
}

What's happening

when filename includes [], use manual escape \\[id\\] or fg.escapePath('[id]') + brace .{txt} will result an empty match. but it's ok when change .{txt} to .(txt) or .@(txt)

here using single .txt extension is simplified for for demo purpose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions