diff --git a/__tests__/extraction.test.ts b/__tests__/extraction.test.ts index 8619d12d7..598276740 100644 --- a/__tests__/extraction.test.ts +++ b/__tests__/extraction.test.ts @@ -9,7 +9,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import { CodeGraph } from '../src'; -import { extractFromSource, scanDirectory, buildDefaultIgnore, discoverEmbeddedRepoRoots, buildScopeIgnore } from '../src/extraction'; +import { extractFromSource, scanDirectory, buildDefaultIgnore, discoverEmbeddedRepoRoots, buildScopeIgnore, getMaxFileSize } from '../src/extraction'; import { detectLanguage, isLanguageSupported, getSupportedLanguages, initGrammars, loadAllGrammars, isSourceFile } from '../src/extraction/grammars'; import { stripCppTemplateArgs, blankCppExportMacros, blankCppInlineMacros, blankMetalAttributes, blankCudaConstructs, blankCppAnnotationMacroCalls, blankCppApiPrefixMacros, blankCppInlineAnnotationMacros, recoverMangledCppName } from '../src/extraction/languages/c-cpp'; import { normalizePath } from '../src/utils'; @@ -10939,3 +10939,32 @@ import DataStore from '../data/DataStore'; }); }); }); + +describe('CODEGRAPH_MAX_FILE_SIZE env var (#1016)', () => { + const originalEnv = process.env.CODEGRAPH_MAX_FILE_SIZE; + afterEach(() => { + if (originalEnv === undefined) delete process.env.CODEGRAPH_MAX_FILE_SIZE; + else process.env.CODEGRAPH_MAX_FILE_SIZE = originalEnv; + }); + + it('returns 1 MB default when env var is not set', () => { + delete process.env.CODEGRAPH_MAX_FILE_SIZE; + expect(getMaxFileSize()).toBe(1024 * 1024); + }); + + it('returns custom value when env var is a valid positive integer', () => { + process.env.CODEGRAPH_MAX_FILE_SIZE = '5242880'; + expect(getMaxFileSize()).toBe(5242880); + }); + + it('falls back to default for invalid values', () => { + process.env.CODEGRAPH_MAX_FILE_SIZE = 'notanumber'; + expect(getMaxFileSize()).toBe(1024 * 1024); + + process.env.CODEGRAPH_MAX_FILE_SIZE = '-1'; + expect(getMaxFileSize()).toBe(1024 * 1024); + + process.env.CODEGRAPH_MAX_FILE_SIZE = '0'; + expect(getMaxFileSize()).toBe(1024 * 1024); + }); +}); diff --git a/src/extraction/index.ts b/src/extraction/index.ts index bda1746ba..edbf736ba 100644 --- a/src/extraction/index.ts +++ b/src/extraction/index.ts @@ -120,9 +120,18 @@ export function hashContent(content: string): string { /** * Skip files larger than this (bytes). Generated bundles, minified JS, and * vendored blobs blow the WASM heap and the worker-recycle budget for no useful - * symbols. 1 MB covers essentially all hand-written source. + * symbols. 1 MB covers essentially all hand-written source. Override via + * CODEGRAPH_MAX_FILE_SIZE (bytes) for projects with legitimate large source files. */ -const MAX_FILE_SIZE = 1024 * 1024; +export function getMaxFileSize(): number { + const envVal = process.env.CODEGRAPH_MAX_FILE_SIZE; + if (envVal !== undefined) { + const parsed = parseInt(envVal, 10); + if (parsed > 0 && !isNaN(parsed)) return parsed; + } + return 1024 * 1024; +} +const MAX_FILE_SIZE = getMaxFileSize(); /** * Directory names that are dependency, build, cache, or tooling output across the