Skip to content

fix: allow globstars to match paths containing newlines - #202

Merged
mrmlnc merged 2 commits into
micromatch:masterfrom
koding88:fix/globstar-newlines
Aug 27, 2026
Merged

fix: allow globstars to match paths containing newlines#202
mrmlnc merged 2 commits into
micromatch:masterfrom
koding88:fix/globstar-newlines

Conversation

@koding88

@koding88 koding88 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Ensures globstar (**) patterns correctly match file paths containing newline characters (\n or \r), aligning globstar behavior with single-star (*) wildcards and POSIX/Unix filesystem semantics.

Root Cause

The globstar generator in lib/parse.js used . inside its negative-lookahead loop:

`(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`

In JavaScript RegExp without the s (dotAll) flag, . does not match line terminators (\n, \r, \u2028, \u2029). Meanwhile, single-star * uses [^/]*? which matches any character except / (including newlines). As a result, ** unexpectedly failed to match a superset of * on valid Unix paths containing newlines.

Changes

  • Replace . with [\s\S] in the globstar() helper across both full parse and fastpath paths (lib/parse.js).
  • Add regression tests covering globstar matching against multiline path segments (test/globstars.js).

Verification

  • npm test: 1,997/1,997 tests pass (1,996 existing + 1 regression suite covering hello\nworld, foo/hello\nworld, etc.).
  • All existing dotfile, slash-anchoring, and Windows/POSIX path semantics remain byte-identical.

The globstar pattern previously used '.' in its negative lookahead loop,
which failed to match paths containing newline characters ('\n' or '\r').
Since single star '*' uses '[^/]*?' (which matches newlines), '**' was
failing to match a superset of '*'.

Replacing '.' with '[\s\S]' ensures globstars match any character while
preserving all dotfile and slash anchoring rules.
Copilot AI lite review requested due to automatic review settings August 25, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a regex edge case in picomatch’s globstar (**) parsing so that globstars can match paths containing newline characters, aligning globstar behavior with single-star matching on platforms where newlines are valid filename characters.

Changes:

  • Update the globstar regex generator in lib/parse.js (both full parse and fastpaths) to use [\s\S] instead of . so matches include line terminators.
  • Add regression tests covering globstar matches against path strings containing \n.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/globstars.js Adds regression tests for globstar matching when path segments contain newline characters.
lib/parse.js Adjusts globstar regex generation to match any character including line terminators, fixing ** newline behavior (and mirroring the change in fastpaths).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/globstars.js
Comment on lines +438 to +441
assert(isMatch('hello\nworld', '**'));
assert(isMatch('foo/hello\nworld', '**/*'));
assert(isMatch('foo\nbar/bar', '**/bar'));
assert(isMatch('foo\nbar/baz\nqux', '**/**'));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added \r and \r\n test cases to verify full line-terminator coverage in 1470a59.

@mrmlnc
mrmlnc merged commit 838c9d7 into micromatch:master Aug 27, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants