Skip to content

Fix gitignore ignoring an entire checkout under an anchored ancestor path - #277

Merged
sindresorhus merged 1 commit into
sindresorhus:mainfrom
willemkokke:fix-anchored-gitignore-absolute
Jul 6, 2026
Merged

sindresorhus merged 1 commit into
sindresorhus:mainfrom
willemkokke:fix-anchored-gitignore-absolute

Conversation

@willemkokke

@willemkokke willemkokke commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #278

Calling globby with {gitignore: true, absolute: true} returns zero files when the project's absolute path contains a path segment matching an anchored .gitignore pattern (a pattern with a leading /, e.g. /build). git check-ignore reports those files as not ignored, so globby diverges from git.

Reproduction

mkdir -p /tmp/globby-bug && cd /tmp/globby-bug
npm i globby
printf '/tmp\n' > .gitignore     # anchored: git only ignores <repo>/tmp
printf '# readme\n' > readme.md
node --input-type=module -e "import {globby} from 'globby'; console.log(await globby('*.md', {cwd: '/tmp/globby-bug', gitignore: true, absolute: true}))"
# globby 16.2.0 → []                              (bug: whole checkout ignored)
# this PR       → [ '/tmp/globby-bug/readme.md' ]  (correct — matches git)

The checkout lives under /tmp, so the anchored /tmp — which git scopes to <repo>/tmp only — is the trigger. (cwd is passed explicitly so the path stays /tmp/... on macOS, where cd /tmp resolves to /private/tmp.)

Cause

An anchored pattern like /foo is relative to the repository root, so git only ignores <repo>/foo. As a traversal optimization, globby forwards .gitignore patterns to fast-glob's ignore and expands directory patterns to globs. Because /foo looks like an absolute path, that expansion resolves it against the real filesystem — and when the checkout lives under a matching /foo/… directory, /foo is a real ancestor directory that expands to /foo/**, which matches every absolute candidate path and prunes the whole tree.

Real-world trigger: markdownlint-cli2 globs with {gitignore: true, absolute: true}; on a CI runner that checks out under /build/…, /home/… or /workspace/… with a matching anchored line, it silently lints 0 files and passes.

Fix

Drop the leading slash when handing anchored patterns to fast-glob, so they stay anchored to the cwd instead of the filesystem root. fast-glob still skips the directory during traversal (the optimization is kept), and globby's cwd-relative predicate continues to enforce the exact gitignore semantics. Stripping happens after directory normalization, so /foo/ stays anchored (foo/**) rather than being turned into a recursive **/foo/**. Un-anchored patterns and the negation / git-root short-circuits are unchanged.

Test

Adds a regression test (covering globby, globbySync and globbyStream via the shared helper) that builds a repo whose leading path segment matches an anchored pattern and asserts only <repo>/<segment> is ignored — not everything, and not a same-named directory nested deeper.

…r path

An anchored `.gitignore` pattern like `/foo` is relative to the repository
root, so like git it should only ignore `<repo>/foo`. globby forwards ignore
patterns to fast-glob to skip directories during traversal, expanding directory
patterns to globs along the way. Because `/foo` looks like an absolute path,
that expansion resolved it against the real filesystem: when the checkout lived
under a matching `/foo/…` directory (e.g. a CI runner at `/build/…` or
`/home/…`), `/foo` was a real ancestor directory and expanded to `/foo/**`,
ignoring the whole tree — so `globby(patterns, {gitignore: true, absolute:
true})` returned nothing, diverging from git.

Drop the leading slash when handing anchored patterns to fast-glob so they stay
anchored to the cwd. fast-glob still skips the directory during traversal, and
the cwd-relative predicate keeps enforcing the exact gitignore semantics.
@sindresorhus
sindresorhus merged commit e7bd988 into sindresorhus:main Jul 6, 2026
4 checks passed
@willemkokke
willemkokke deleted the fix-anchored-gitignore-absolute branch July 6, 2026 00:31
jimmyandrade added a commit to itgalaxy/webfont that referenced this pull request Jul 6, 2026
Add a contract test for sindresorhus/globby#277 (anchored ancestor path)
and basic resolveInputSources coverage so the Dependabot patch is guarded.

Co-authored-by: Cursor <cursoragent@cursor.com>
jimmyandrade added a commit to itgalaxy/webfont that referenced this pull request Jul 6, 2026
* chore(deps): bump globby from 16.2.0 to 16.2.1

Bumps [globby](https://github.com/sindresorhus/globby) from 16.2.0 to 16.2.1.
- [Release notes](https://github.com/sindresorhus/globby/releases)
- [Commits](sindresorhus/globby@v16.2.0...v16.2.1)

---
updated-dependencies:
- dependency-name: globby
  dependency-version: 16.2.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* test: document globby 16.2.1 gitignore regression in inputSource

Add a contract test for sindresorhus/globby#277 (anchored ancestor path)
and basic resolveInputSources coverage so the Dependabot patch is guarded.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jimmy Andrade <contato@jimmyandrade.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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.

gitignore: true + absolute: true ignores all files when the checkout path matches an anchored .gitignore pattern

2 participants