feat: file nodes, arrow function extraction, parallel I/O
- Create file-kind nodes for each parsed source file - Add isInsideClassLikeNode() for method vs function detection - Extract arrow functions and function expressions from variable declarators - Batch file I/O with FILE_IO_BATCH_SIZE=10 using Promise.all - Add symlink cycle detection with visitedDirs Set in scanDirectory - Add lazy grammar loading with exported getGrammar() function - Add indexFileWithContent() for pre-read content processing - Add tests for file nodes and arrow function extraction
This commit is contained in:
@@ -156,6 +156,14 @@ function loadGrammar(language: Language): unknown | null {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a grammar by language, loading it lazily if needed.
|
||||
* Exported for direct grammar access without parser initialization.
|
||||
*/
|
||||
export function getGrammar(language: string): unknown | null {
|
||||
return loadGrammar(language as Language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a parser for the specified language
|
||||
*/
|
||||
|
||||
+143
-29
@@ -18,10 +18,16 @@ import {
|
||||
import { QueryBuilder } from '../db/queries';
|
||||
import { extractFromSource } from './tree-sitter';
|
||||
import { detectLanguage, isLanguageSupported } from './grammars';
|
||||
import { logDebug } from '../errors';
|
||||
import { logDebug, logWarn } from '../errors';
|
||||
import { captureException } from '../sentry';
|
||||
import { validatePathWithinRoot } from '../utils';
|
||||
|
||||
/**
|
||||
* Number of files to read in parallel during indexing.
|
||||
* File reads are I/O-bound; batching overlaps I/O wait with CPU parse work.
|
||||
*/
|
||||
const FILE_IO_BATCH_SIZE = 10;
|
||||
|
||||
/**
|
||||
* Progress callback for indexing operations
|
||||
*/
|
||||
@@ -129,22 +135,25 @@ export function scanDirectory(
|
||||
): string[] {
|
||||
const files: string[] = [];
|
||||
let count = 0;
|
||||
const visitedRealPaths = new Set<string>(); // Symlink cycle detection
|
||||
// Track visited real paths to detect symlink cycles
|
||||
const visitedDirs = new Set<string>();
|
||||
|
||||
function walk(dir: string): void {
|
||||
// Symlink cycle detection: resolve real path and skip if already visited
|
||||
// Resolve real path to detect symlink cycles
|
||||
let realDir: string;
|
||||
try {
|
||||
const realDir = fs.realpathSync(dir);
|
||||
if (visitedRealPaths.has(realDir)) {
|
||||
logDebug('Skipping directory to prevent symlink cycle', { dir, realDir });
|
||||
return;
|
||||
}
|
||||
visitedRealPaths.add(realDir);
|
||||
realDir = fs.realpathSync(dir);
|
||||
} catch {
|
||||
// If realpath fails, skip this directory
|
||||
logDebug('Skipping unresolvable directory', { dir });
|
||||
return;
|
||||
}
|
||||
|
||||
if (visitedDirs.has(realDir)) {
|
||||
logDebug('Skipping already-visited directory (symlink cycle)', { dir, realDir });
|
||||
return;
|
||||
}
|
||||
visitedDirs.add(realDir);
|
||||
|
||||
// Check for .codegraphignore marker file - skip entire directory tree if present
|
||||
const ignoreMarker = path.join(dir, CODEGRAPH_IGNORE_MARKER);
|
||||
if (fs.existsSync(ignoreMarker)) {
|
||||
@@ -283,10 +292,11 @@ export class ExtractionOrchestrator {
|
||||
};
|
||||
}
|
||||
|
||||
// Phase 2: Parse files
|
||||
// Phase 2: Parse files (read in parallel batches, parse/store sequentially)
|
||||
const total = files.length;
|
||||
let processed = 0;
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
for (let i = 0; i < files.length; i += FILE_IO_BATCH_SIZE) {
|
||||
if (signal?.aborted) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -299,26 +309,69 @@ export class ExtractionOrchestrator {
|
||||
};
|
||||
}
|
||||
|
||||
const filePath = files[i]!;
|
||||
onProgress?.({
|
||||
phase: 'parsing',
|
||||
current: i + 1,
|
||||
total,
|
||||
currentFile: filePath,
|
||||
});
|
||||
const batch = files.slice(i, i + FILE_IO_BATCH_SIZE);
|
||||
|
||||
const result = await this.indexFile(filePath);
|
||||
// Read files in parallel (with path validation before any I/O)
|
||||
const fileContents = await Promise.all(
|
||||
batch.map(async (fp) => {
|
||||
try {
|
||||
const fullPath = validatePathWithinRoot(this.rootDir, fp);
|
||||
if (!fullPath) {
|
||||
logWarn('Path traversal blocked in batch reader', { filePath: fp });
|
||||
return { filePath: fp, content: null as string | null, stats: null as fs.Stats | null, error: new Error('Path traversal blocked') };
|
||||
}
|
||||
const content = await fsp.readFile(fullPath, 'utf-8');
|
||||
const stats = await fsp.stat(fullPath);
|
||||
return { filePath: fp, content, stats, error: null as Error | null };
|
||||
} catch (err) {
|
||||
return { filePath: fp, content: null as string | null, stats: null as fs.Stats | null, error: err as Error };
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
if (result.errors.length > 0) {
|
||||
errors.push(...result.errors);
|
||||
}
|
||||
// Parse and store sequentially
|
||||
for (const { filePath, content, stats, error } of fileContents) {
|
||||
if (signal?.aborted) {
|
||||
return {
|
||||
success: false,
|
||||
filesIndexed,
|
||||
filesSkipped,
|
||||
nodesCreated: totalNodes,
|
||||
edgesCreated: totalEdges,
|
||||
errors: [{ message: 'Aborted', severity: 'error' }, ...errors],
|
||||
durationMs: Date.now() - startTime,
|
||||
};
|
||||
}
|
||||
|
||||
if (result.nodes.length > 0) {
|
||||
filesIndexed++;
|
||||
totalNodes += result.nodes.length;
|
||||
totalEdges += result.edges.length;
|
||||
} else if (result.errors.length === 0) {
|
||||
filesSkipped++;
|
||||
processed++;
|
||||
onProgress?.({
|
||||
phase: 'parsing',
|
||||
current: processed,
|
||||
total,
|
||||
currentFile: filePath,
|
||||
});
|
||||
|
||||
if (error || content === null || stats === null) {
|
||||
errors.push({
|
||||
message: `Failed to read file: ${error instanceof Error ? error.message : String(error)}`,
|
||||
severity: 'error',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const result = await this.indexFileWithContent(filePath, content, stats);
|
||||
|
||||
if (result.errors.length > 0) {
|
||||
errors.push(...result.errors);
|
||||
}
|
||||
|
||||
if (result.nodes.length > 0) {
|
||||
filesIndexed++;
|
||||
totalNodes += result.nodes.length;
|
||||
totalEdges += result.edges.length;
|
||||
} else if (result.errors.length === 0) {
|
||||
filesSkipped++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,6 +510,67 @@ export class ExtractionOrchestrator {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Index a single file with pre-read content and stats.
|
||||
* Used by the parallel batch reader to avoid redundant file I/O.
|
||||
*/
|
||||
async indexFileWithContent(
|
||||
relativePath: string,
|
||||
content: string,
|
||||
stats: fs.Stats
|
||||
): Promise<ExtractionResult> {
|
||||
// Prevent path traversal
|
||||
const fullPath = validatePathWithinRoot(this.rootDir, relativePath);
|
||||
if (!fullPath) {
|
||||
logWarn('Path traversal blocked in indexFileWithContent', { relativePath });
|
||||
return {
|
||||
nodes: [],
|
||||
edges: [],
|
||||
unresolvedReferences: [],
|
||||
errors: [{ message: 'Path traversal blocked', severity: 'error' }],
|
||||
durationMs: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Check file size
|
||||
if (stats.size > this.config.maxFileSize) {
|
||||
return {
|
||||
nodes: [],
|
||||
edges: [],
|
||||
unresolvedReferences: [],
|
||||
errors: [
|
||||
{
|
||||
message: `File exceeds max size (${stats.size} > ${this.config.maxFileSize})`,
|
||||
severity: 'warning',
|
||||
},
|
||||
],
|
||||
durationMs: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Detect language
|
||||
const language = detectLanguage(relativePath);
|
||||
if (!isLanguageSupported(language)) {
|
||||
return {
|
||||
nodes: [],
|
||||
edges: [],
|
||||
unresolvedReferences: [],
|
||||
errors: [],
|
||||
durationMs: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Extract from source
|
||||
const result = extractFromSource(relativePath, content, language);
|
||||
|
||||
// Store in database
|
||||
if (result.nodes.length > 0 || result.errors.length === 0) {
|
||||
this.storeExtractionResult(relativePath, content, language, stats, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store extraction result in database
|
||||
*/
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import { SyntaxNode, Tree } from 'tree-sitter';
|
||||
import * as crypto from 'crypto';
|
||||
import * as path from 'path';
|
||||
import {
|
||||
Language,
|
||||
Node,
|
||||
@@ -875,7 +876,28 @@ export class TreeSitterExtractor {
|
||||
|
||||
try {
|
||||
this.tree = parser.parse(this.source);
|
||||
|
||||
// Create file node representing the source file
|
||||
const fileNode: Node = {
|
||||
id: `file:${this.filePath}`,
|
||||
kind: 'file',
|
||||
name: path.basename(this.filePath),
|
||||
qualifiedName: this.filePath,
|
||||
filePath: this.filePath,
|
||||
language: this.language,
|
||||
startLine: 1,
|
||||
endLine: this.source.split('\n').length,
|
||||
startColumn: 0,
|
||||
endColumn: 0,
|
||||
isExported: false,
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
this.nodes.push(fileNode);
|
||||
|
||||
// Push file node onto stack so top-level declarations get contains edges
|
||||
this.nodeStack.push(fileNode.id);
|
||||
this.visitNode(this.tree.rootNode);
|
||||
this.nodeStack.pop();
|
||||
} catch (error) {
|
||||
captureException(error, { operation: 'tree-sitter-parse', filePath: this.filePath, language: this.language });
|
||||
this.errors.push({
|
||||
@@ -905,7 +927,7 @@ export class TreeSitterExtractor {
|
||||
// Check for function declarations
|
||||
// For Python/Ruby, function_definition inside a class should be treated as method
|
||||
if (this.extractor.functionTypes.includes(nodeType)) {
|
||||
if (this.nodeStack.length > 0 && this.extractor.methodTypes.includes(nodeType)) {
|
||||
if (this.isInsideClassLikeNode() && this.extractor.methodTypes.includes(nodeType)) {
|
||||
// Inside a class - treat as method
|
||||
this.extractMethod(node);
|
||||
skipChildren = true; // extractMethod visits children via visitFunctionBody
|
||||
@@ -956,9 +978,17 @@ export class TreeSitterExtractor {
|
||||
else if (this.extractor.typeAliasTypes.includes(nodeType)) {
|
||||
this.extractTypeAlias(node);
|
||||
}
|
||||
// Check for arrow functions / function expressions assigned to variables (JS/TS)
|
||||
else if (nodeType === 'variable_declarator') {
|
||||
const valueNode = getChildByField(node, 'value');
|
||||
if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
|
||||
this.extractFunctionVariable(node);
|
||||
skipChildren = true;
|
||||
}
|
||||
}
|
||||
// Check for variable declarations (const, let, var, etc.)
|
||||
// Only extract top-level variables (not inside functions/methods)
|
||||
else if (this.extractor.variableTypes.includes(nodeType) && this.nodeStack.length === 0) {
|
||||
else if (this.extractor.variableTypes.includes(nodeType) && !this.isInsideClassLikeNode()) {
|
||||
this.extractVariable(node);
|
||||
skipChildren = true; // extractVariable handles children
|
||||
}
|
||||
@@ -1060,6 +1090,81 @@ export class TreeSitterExtractor {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current node stack indicates we are inside a class-like node
|
||||
* (class, struct, interface, trait). File nodes do not count as class-like.
|
||||
*/
|
||||
private isInsideClassLikeNode(): boolean {
|
||||
if (this.nodeStack.length === 0) return false;
|
||||
const parentId = this.nodeStack[this.nodeStack.length - 1];
|
||||
if (!parentId) return false;
|
||||
const parentNode = this.nodes.find((n) => n.id === parentId);
|
||||
if (!parentNode) return false;
|
||||
return (
|
||||
parentNode.kind === 'class' ||
|
||||
parentNode.kind === 'struct' ||
|
||||
parentNode.kind === 'interface' ||
|
||||
parentNode.kind === 'trait' ||
|
||||
parentNode.kind === 'enum'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract an arrow function or function expression assigned to a variable.
|
||||
* Handles patterns like: const foo = () => {} or const bar = function() {}
|
||||
*/
|
||||
private extractFunctionVariable(node: SyntaxNode): void {
|
||||
if (!this.extractor) return;
|
||||
|
||||
// Only handle variable_declarator where value is arrow_function or function_expression
|
||||
if (node.type !== 'variable_declarator') return;
|
||||
|
||||
const nameNode = getChildByField(node, 'name');
|
||||
const valueNode = getChildByField(node, 'value');
|
||||
|
||||
if (!nameNode || !valueNode) return;
|
||||
if (valueNode.type !== 'arrow_function' && valueNode.type !== 'function_expression') return;
|
||||
|
||||
const name = getNodeText(nameNode, this.source);
|
||||
if (!name) return;
|
||||
|
||||
// Check if exported by walking parents
|
||||
let isExported = false;
|
||||
let current = node.parent;
|
||||
while (current) {
|
||||
if (current.type === 'export_statement') {
|
||||
isExported = true;
|
||||
break;
|
||||
}
|
||||
if (current.type === 'program' || current.type === 'module') break;
|
||||
current = current.parent;
|
||||
}
|
||||
|
||||
// Build signature from the arrow function parameters
|
||||
let signature: string | undefined;
|
||||
const params = getChildByField(valueNode, 'parameters');
|
||||
if (params) {
|
||||
signature = `${name}${getNodeText(params, this.source)}`;
|
||||
}
|
||||
|
||||
// Check if async
|
||||
const isAsync = this.extractor.isAsync?.(valueNode);
|
||||
|
||||
const funcNode = this.createNode('function', name, node, {
|
||||
isExported,
|
||||
signature: signature || undefined,
|
||||
isAsync,
|
||||
});
|
||||
|
||||
// Push to stack and visit body for call extraction
|
||||
this.nodeStack.push(funcNode.id);
|
||||
const body = getChildByField(valueNode, this.extractor.bodyField);
|
||||
if (body) {
|
||||
this.visitFunctionBody(body, funcNode.id);
|
||||
}
|
||||
this.nodeStack.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a function
|
||||
*/
|
||||
@@ -1160,10 +1265,10 @@ export class TreeSitterExtractor {
|
||||
private extractMethod(node: SyntaxNode): void {
|
||||
if (!this.extractor) return;
|
||||
|
||||
// For most languages, only extract as method if inside a class
|
||||
// For most languages, only extract as method if inside a class-like node
|
||||
// But Go methods are top-level with a receiver, so always treat them as methods
|
||||
if (this.nodeStack.length === 0 && this.language !== 'go') {
|
||||
// Top-level and not Go, treat as function
|
||||
if (!this.isInsideClassLikeNode() && this.language !== 'go') {
|
||||
// Not inside a class-like node and not Go, treat as function
|
||||
this.extractFunction(node);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user