Skip to content

Commit 767c4f8

Browse files
committed
feat(core): Cache ignore instances for performance
This commit optimizes file filtering by caching compiled `ignore` instances within the `GitIgnoreParser` instead of raw string arrays. Previously, `.gitignore` files were parsed into an array of rule strings on every check, leading to redundant processing. The `node-ignore` library supports adding pre-compiled `ignore` instances, which is more efficient. This change modifies the internal cache from `Map<string, string[]>` to `Map<string, Ignore>` and updates the logic in `isIgnored` to create, cache, and reuse these compiled instances. This reduces the overhead associated with parsing ignore files, especially in large projects with many nested `.gitignore` files. Fixes #11610
1 parent 6f4b2ad commit 767c4f8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/core/src/utils/gitIgnoreParser.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
import * as fs from 'node:fs';
88
import * as path from 'node:path';
9-
import ignore from 'ignore';
9+
import ignore, { type Ignore } from 'ignore';
1010

1111
export interface GitIgnoreFilter {
1212
isIgnored(filePath: string): boolean;
1313
}
1414

1515
export class GitIgnoreParser implements GitIgnoreFilter {
1616
private projectRoot: string;
17-
private cache: Map<string, string[]> = new Map();
18-
private globalPatterns: string[] | undefined;
17+
private cache: Map<string, Ignore> = new Map();
18+
private globalPatterns: Ignore | undefined;
1919
private processedExtraPatterns: string[] = [];
2020

2121
constructor(
@@ -32,12 +32,12 @@ export class GitIgnoreParser implements GitIgnoreFilter {
3232
}
3333
}
3434

35-
private loadPatternsForFile(patternsFilePath: string): string[] {
35+
private loadPatternsForFile(patternsFilePath: string): Ignore {
3636
let content: string;
3737
try {
3838
content = fs.readFileSync(patternsFilePath, 'utf-8');
3939
} catch (_error) {
40-
return [];
40+
return ignore();
4141
}
4242

4343
const isExcludeFile = patternsFilePath.endsWith(
@@ -52,7 +52,7 @@ export class GitIgnoreParser implements GitIgnoreFilter {
5252
.join(path.posix.sep);
5353

5454
const rawPatterns = content.split('\n');
55-
return this.processPatterns(rawPatterns, relativeBaseDir);
55+
return ignore().add(this.processPatterns(rawPatterns, relativeBaseDir));
5656
}
5757

5858
private processPatterns(
@@ -155,7 +155,7 @@ export class GitIgnoreParser implements GitIgnoreFilter {
155155
);
156156
this.globalPatterns = fs.existsSync(excludeFile)
157157
? this.loadPatternsForFile(excludeFile)
158-
: [];
158+
: ignore();
159159
}
160160
ig.add(this.globalPatterns);
161161

@@ -198,7 +198,7 @@ export class GitIgnoreParser implements GitIgnoreFilter {
198198
this.cache.set(dir, patterns);
199199
ig.add(patterns);
200200
} else {
201-
this.cache.set(dir, []); // Cache miss
201+
this.cache.set(dir, ignore());
202202
}
203203
}
204204
}

0 commit comments

Comments
 (0)