summaryrefslogtreecommitdiff
path: root/packages/cli/src/utils/loadIgnorePatterns.ts
blob: 34efc8c8b8ae1bb89213e427dfb2162481526d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import * as path from 'node:path';
import { GitIgnoreParser } from '@gemini-cli/core';

const GEMINI_IGNORE_FILE_NAME = '.geminiignore';

/**
 * Loads and parses a .geminiignore file from the given workspace root.
 * The .geminiignore file follows a format similar to .gitignore.
 *
 * @param workspaceRoot The absolute path to the workspace root where the .geminiignore file is expected.
 * @returns An array of glob patterns extracted from the .geminiignore file. Returns an empty array
 *          if the file does not exist or contains no valid patterns.
 */
export async function loadGeminiIgnorePatterns(
  workspaceRoot: string,
): Promise<string[]> {
  const parser = new GitIgnoreParser(workspaceRoot);

  try {
    await parser.loadPatterns(GEMINI_IGNORE_FILE_NAME);
  } catch (error: unknown) {
    const ignoreFilePath = path.join(workspaceRoot, GEMINI_IGNORE_FILE_NAME);
    if (
      error instanceof Error &&
      'code' in error &&
      typeof error.code === 'string'
    ) {
      if (error.code === 'ENOENT') {
        // .geminiignore not found, which is fine.
      } else {
        // Other error reading the file (e.g., permissions)
        console.warn(
          `[WARN] Could not read .geminiignore file at ${ignoreFilePath}: ${error.message}`,
        );
      }
    } else {
      // For other types of errors, or if code is not available
      console.warn(
        `[WARN] An unexpected error occurred while trying to read ${ignoreFilePath}: ${String(error)}`,
      );
    }
  }
  const loadedPatterns = parser.getPatterns();
  if (loadedPatterns.length > 0) {
    console.log(
      `[INFO] Loaded ${loadedPatterns.length} patterns from .geminiignore`,
    );
  }
  return loadedPatterns;
}