|
| 1 | +/** |
| 2 | + * Shared directory exclusion list for workspace scanning operations. |
| 3 | + * |
| 4 | + * Provides a comprehensive default set of directory names to skip during |
| 5 | + * recursive file discovery (prompt completions, QL code search, etc.) |
| 6 | + * and makes the set configurable via the `CODEQL_MCP_SCAN_EXCLUDE_DIRS` |
| 7 | + * environment variable. |
| 8 | + * |
| 9 | + * The VS Code extension surfaces this as the `codeql-mcp.scanExcludeDirs` |
| 10 | + * setting, which is passed through to the server as an env var. |
| 11 | + * |
| 12 | + * ## Configuration |
| 13 | + * |
| 14 | + * The env var accepts a comma-separated list of directory names. |
| 15 | + * Entries prefixed with `!` remove a default from the set (negation). |
| 16 | + * All other entries are added to the default set. |
| 17 | + * |
| 18 | + * Examples: |
| 19 | + * - `CODEQL_MCP_SCAN_EXCLUDE_DIRS="custom-build,tmp-output"` — adds |
| 20 | + * - `CODEQL_MCP_SCAN_EXCLUDE_DIRS="!build,!dist"` — removes `build` and `dist` from defaults |
| 21 | + * - `CODEQL_MCP_SCAN_EXCLUDE_DIRS="custom-dir,!coverage"` — adds `custom-dir`, removes `coverage` |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Comprehensive default set of directory names to skip during workspace |
| 26 | + * scanning. Sorted alphabetically. |
| 27 | + * |
| 28 | + * This list is the union of directories previously skipped in |
| 29 | + * `prompt-completions.ts` and `search-ql-code.ts`, plus additional |
| 30 | + * well-known directories that never contain CodeQL source files and |
| 31 | + * would slow down scanning. |
| 32 | + */ |
| 33 | +export const DEFAULT_SCAN_EXCLUDE_DIRS: readonly string[] = [ |
| 34 | + '.cache', |
| 35 | + '.codeql', |
| 36 | + '.git', |
| 37 | + '.github', |
| 38 | + '.idea', |
| 39 | + '.mypy_cache', |
| 40 | + '.pytest_cache', |
| 41 | + '.tmp', |
| 42 | + '.tox', |
| 43 | + '.vscode', |
| 44 | + '.yarn', |
| 45 | + '__pycache__', |
| 46 | + 'build', |
| 47 | + 'coverage', |
| 48 | + 'dist', |
| 49 | + 'node_modules', |
| 50 | + 'out', |
| 51 | + 'target', |
| 52 | + 'vendor', |
| 53 | +]; |
| 54 | + |
| 55 | +/** |
| 56 | + * Parse the `CODEQL_MCP_SCAN_EXCLUDE_DIRS` environment variable and |
| 57 | + * merge with defaults. |
| 58 | + * |
| 59 | + * @returns A `Set` of directory names to exclude from scanning. |
| 60 | + */ |
| 61 | +export function getScanExcludeDirs(): Set<string> { |
| 62 | + const result = new Set(DEFAULT_SCAN_EXCLUDE_DIRS); |
| 63 | + |
| 64 | + const envValue = process.env.CODEQL_MCP_SCAN_EXCLUDE_DIRS; |
| 65 | + if (!envValue) { |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + const entries = envValue.split(',').map(e => e.trim()).filter(e => e.length > 0); |
| 70 | + |
| 71 | + for (const entry of entries) { |
| 72 | + if (entry.startsWith('!')) { |
| 73 | + // Negation: remove from defaults |
| 74 | + result.delete(entry.slice(1).trim()); |
| 75 | + } else { |
| 76 | + // Addition: add to the set |
| 77 | + result.add(entry); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return result; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Check whether a directory name should be excluded from scanning. |
| 86 | + * |
| 87 | + * @param dirName - The directory basename to check. |
| 88 | + * @param excludeSet - Optional pre-computed exclusion set. When omitted, |
| 89 | + * `getScanExcludeDirs()` is called (reads the env var). |
| 90 | + * @returns `true` if the directory should be skipped. |
| 91 | + */ |
| 92 | +export function isScanExcluded( |
| 93 | + dirName: string, |
| 94 | + excludeSet?: Set<string>, |
| 95 | +): boolean { |
| 96 | + const set = excludeSet ?? getScanExcludeDirs(); |
| 97 | + return set.has(dirName); |
| 98 | +} |
0 commit comments