Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion src/utils/glob.mts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ function ignorePatternToMinimatch(pattern: string): string {
return `${negatedPrefix}${matchEverywherePrefix}${escapedPatternWithoutLeadingSlash}${matchInsideSuffix}`
}

// fast-glob silently discards `ignore` entries that end in `/` (it
// treats them as literal directory paths, not glob patterns). The
// gitignore convention of writing directory entries as `dist/` lands
// here as `**/dist/` after `ignorePatternToMinimatch`, which fast-glob
// then drops — defeating the entire ignore. Strip the trailing slash
// so fast-glob actually honors the pattern.
function stripTrailingSlash(pattern: string): string {
if (
pattern.length > 1 &&
pattern.charCodeAt(pattern.length - 1) === 47 /*'/'*/
Comment thread
jdalton marked this conversation as resolved.
) {
return pattern.slice(0, -1)
}
return pattern
}

function workspacePatternToGlobPattern(workspace: string): string {
const { length } = workspace
if (!length) {
Expand Down Expand Up @@ -252,7 +268,9 @@ export async function globWithGitIgnore(
absolute: true,
cwd,
dot: true,
ignore: hasNegatedPattern ? defaultIgnore : [...ignores],
ignore: hasNegatedPattern
? defaultIgnore
: [...ignores].map(stripTrailingSlash),
...additionalOptions,
} as GlobOptions

Expand Down
19 changes: 19 additions & 0 deletions src/utils/glob.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ describe('glob utilities', () => {
])
})

it('should skip directories ignored via trailing-slash patterns', async () => {
// fast-glob silently drops `ignore` entries that end in `/`, so
// the pattern produced for `dist/` (`**/dist/`) used to do
// nothing at the walk level and dist contents leaked through.
mockTestFs({
[`${mockFixturePath}/.gitignore`]: 'dist/\n',
[`${mockFixturePath}/package.json`]: '{}',
[`${mockFixturePath}/dist/a.json`]: '{}',
})

const results = await globWithGitIgnore(['**/*.json'], {
cwd: mockFixturePath,
})

expect(results.map(normalizePath).sort()).toEqual([
`${mockFixturePath}/package.json`,
])
})

it('should combine filter with negated gitignore patterns', async () => {
mockTestFs({
[`${mockFixturePath}/.gitignore`]: 'build/**\n!build/manifest.json',
Expand Down