Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 44 additions & 0 deletions packages/core/src/services/fileDiscoveryService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,49 @@ describe('FileDiscoveryService', () => {
const paths = service.getAllIgnoreFilePaths();
expect(paths[0]).toBe(path.join(projectRoot, '.gitignore'));
});

it('should exclude directories from getIgnoreFilePaths (#19868)', async () => {
// Create a directory that shares a name with a customIgnoreFilePaths entry
await fs.mkdir(path.join(projectRoot, 'node_modules'), {
recursive: true,
});

const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['node_modules'],
});
const paths = service.getIgnoreFilePaths();

// node_modules/ is a directory, not a file — it should be excluded
expect(paths).not.toContain(path.join(projectRoot, 'node_modules'));
});

it('should exclude directories from getAllIgnoreFilePaths (#19868)', async () => {
await fs.mkdir(path.join(projectRoot, 'node_modules'), {
recursive: true,
});

const service = new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['node_modules'],
});
const paths = service.getAllIgnoreFilePaths();

expect(paths).not.toContain(path.join(projectRoot, 'node_modules'));
// .gitignore should still be present
expect(paths).toContain(path.join(projectRoot, '.gitignore'));
});

it('should not crash when customIgnoreFilePaths contains directory names (#19868)', async () => {
await fs.mkdir(path.join(projectRoot, 'node_modules'), {
recursive: true,
});
await fs.mkdir(path.join(projectRoot, 'temp'), { recursive: true });

// This is the exact user scenario from issue #19868
expect(() => {
new FileDiscoveryService(projectRoot, {
customIgnoreFilePaths: ['node_modules/', 'temp/', 'cache/'],
});
}).not.toThrow();
});
});
});
8 changes: 6 additions & 2 deletions packages/core/src/services/fileDiscoveryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,12 @@ export class FileDiscoveryService {
this.defaultFilterFileOptions.respectGitIgnore
) {
const gitIgnorePath = path.join(this.projectRoot, '.gitignore');
if (fs.existsSync(gitIgnorePath)) {
paths.push(gitIgnorePath);
try {
if (fs.statSync(gitIgnorePath).isFile()) {
paths.push(gitIgnorePath);
}
} catch {
// File does not exist or cannot be accessed, ignore.
}
}
return paths.concat(this.getIgnoreFilePaths());
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utils/filesearch/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export function loadIgnoreRules(

for (const filePath of ignoreFiles) {
if (fs.existsSync(filePath)) {
ignorer.add(fs.readFileSync(filePath, 'utf8'));
try {
ignorer.add(fs.readFileSync(filePath, 'utf8'));
} catch {
// Skip files that can't be read (e.g. directories, permission errors)
}
}
Comment thread
suhaan-24 marked this conversation as resolved.
Resolved
}

Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/utils/ignoreFileParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ export class IgnoreFileParser implements IgnoreFileFilter {
.slice()
.reverse()
.map((fileName) => path.join(this.projectRoot, fileName))
.filter((filePath) => fs.existsSync(filePath));
.filter((filePath) => {
try {
return fs.statSync(filePath).isFile();
} catch {
return false;
}
});
}

/**
Expand Down