diff --git a/packages/core/src/utils/gitIgnoreParser.test.ts b/packages/core/src/utils/gitIgnoreParser.test.ts index 1990332091..95f8dbbe64 100644 --- a/packages/core/src/utils/gitIgnoreParser.test.ts +++ b/packages/core/src/utils/gitIgnoreParser.test.ts @@ -234,4 +234,20 @@ src/*.tmp ); }); }); + describe('Escaped Characters', () => { + beforeEach(async () => { + await setupGitRepo(); + }); + + it('should correctly handle escaped characters in .gitignore', async () => { + await createTestFile('.gitignore', '\\#foo\n\\!bar'); + // Create files with special characters in names + await createTestFile('bla/#foo', 'content'); + await createTestFile('bla/!bar', 'content'); + + // These should be ignored based on the escaped patterns + expect(parser.isIgnored('bla/#foo')).toBe(true); + expect(parser.isIgnored('bla/!bar')).toBe(true); + }); + }); }); diff --git a/packages/core/src/utils/gitIgnoreParser.ts b/packages/core/src/utils/gitIgnoreParser.ts index 21d83651a5..762015db8d 100644 --- a/packages/core/src/utils/gitIgnoreParser.ts +++ b/packages/core/src/utils/gitIgnoreParser.ts @@ -35,7 +35,10 @@ export class GitIgnoreParser implements GitIgnoreFilter { const relativeBaseDir = isExcludeFile ? '.' - : path.dirname(path.relative(this.projectRoot, patternsFilePath)); + : path + .dirname(path.relative(this.projectRoot, patternsFilePath)) + .split(path.sep) + .join(path.posix.sep); return content .split('\n') @@ -68,11 +71,11 @@ export class GitIgnoreParser implements GitIgnoreFilter { if (!isAnchoredInFile && !p.includes('/')) { // If no slash and not anchored in file, it matches files in any // subdirectory. - newPattern = path.join('**', p); + newPattern = path.posix.join('**', p); } // Prepend the .gitignore file's directory. - newPattern = path.join(relativeBaseDir, newPattern); + newPattern = path.posix.join(relativeBaseDir, newPattern); // Anchor the pattern to a nested gitignore directory. if (!newPattern.startsWith('/')) { @@ -89,9 +92,6 @@ export class GitIgnoreParser implements GitIgnoreFilter { newPattern = '!' + newPattern; } - // Even in windows, Ignore expects forward slashes. - newPattern = newPattern.replace(/\\/g, '/'); - return newPattern; }) .filter((p) => p !== ''); @@ -173,6 +173,7 @@ export class GitIgnoreParser implements GitIgnoreFilter { const gitignorePath = path.join(dir, '.gitignore'); if (fs.existsSync(gitignorePath)) { const patterns = this.loadPatternsForFile(gitignorePath); + this.cache.set(dir, patterns); ig.add(patterns); } else {