diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 1c6a99c36..71779cda7 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -407,6 +407,8 @@ Each language implements `LanguageProvider` (`language-provider.ts`). Key fields 18 providers in `languages/index.ts` via `satisfies Record` — missing a language is a compile error. +Elixir's experimental CFG and framework facts are provider-owned. Its taint model covers local bindings only; destructured Phoenix heads, arbitrary `Repo` receivers, callable-value dispatch, captured-closure taint, macro expansion, and cross-process `receive` flow remain explicit gaps. + ### Unified capture tags Per-language tree-sitter queries use different AST node names but produce the **same semantic capture tags**: `@definition.class`, `@definition.function`, `@call.name`, `@import.source`, `@reference.inherits`. Downstream extraction needs no language branching. Defined in `tree-sitter-queries.ts`. diff --git a/README.md b/README.md index 0a1b53435..5346c81d4 100644 --- a/README.md +++ b/README.md @@ -682,10 +682,13 @@ GitNexus builds a complete knowledge graph of your codebase through a multi-phas | Objective-C | ✓ | — | ✓ | ✓ | ✓ | — | — | — | — | | Dart | ✓ | — | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | | Zig | ✓ | — | ✓ | — | ✓ | ✓ | ✓ | — | ✓ | +| Elixir (experimental) | ✓ | — | ✓ | ✓ | — | — | ✓ | ✓ | — | **Imports** — cross-file import resolution · **Named Bindings** — `import { X as Y }` / re-export tracking · **Exports** — public/exported symbol detection · **Heritage** — class inheritance, interfaces, mixins · **Type Annotations** — explicit type extraction for receiver resolution · **Constructor Inference** — infer receiver type from constructor calls (`self`/`this` resolution included for all languages) · **Config** — language toolchain config parsing (tsconfig, go.mod, etc.) · **Frameworks** — AST-based framework pattern detection · **Entry Points** — entry point scoring heuristics -**Control flow (CFG, opt-in `--pdg`)** — per-function control-flow graphs (`BasicBlock` nodes + `CFG` edges) feeding the PDG/taint substrate, currently **TypeScript & JavaScript** (#2081 M1); other languages planned. Off by default. +**Control flow (CFG, opt-in `--pdg`)** — per-function control-flow graphs (`BasicBlock` nodes + `CFG` edges) feeding the PDG/taint substrate. TypeScript, JavaScript, and experimental Elixir support it; other languages are planned. Off by default. + +Elixir support is experimental. Its provider-owned CFG supports local flow only; destructured Phoenix heads, arbitrary `Repo` receivers, callable-value dispatch, captured-closure taint, macro expansion, and cross-process `receive` flow are not modelled. ### Multi-Repo Architecture diff --git a/gitnexus/README.md b/gitnexus/README.md index 6a0866733..6172a5204 100644 --- a/gitnexus/README.md +++ b/gitnexus/README.md @@ -427,7 +427,7 @@ GitNexus supports indexing multiple repositories. Each `gitnexus analyze` regist ## Supported Languages -TypeScript, JavaScript, Python, Java, C, C++, C#, Go, Rust, PHP, Kotlin, Swift, Ruby, Dart, Zig +TypeScript, JavaScript, Python, Java, C, C++, C#, Go, Rust, PHP, Kotlin, Swift, Ruby, Dart, Zig, and experimental Elixir ### Language Feature Matrix @@ -448,6 +448,7 @@ TypeScript, JavaScript, Python, Java, C, C++, C#, Go, Rust, PHP, Kotlin, Swift, | C++ | — | — | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | | Dart | ✓ | — | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | | Zig | ✓ | — | ✓ | — | ✓ | ✓ | ✓ | — | ✓ | +| Elixir (experimental) | ✓ | — | ✓ | ✓ | — | — | ✓ | ✓ | — | **Imports** — cross-file import resolution · **Named Bindings** — `import { X as Y }` / re-export tracking · **Exports** — public/exported symbol detection · **Heritage** — class inheritance, interfaces, mixins · **Type Annotations** — explicit type extraction for receiver resolution · **Constructor Inference** — infer receiver type from constructor calls (`self`/`this` resolution included for all languages) · **Config** — language toolchain config parsing (tsconfig, go.mod, etc.) · **Frameworks** — AST-based framework pattern detection · **Entry Points** — entry point scoring heuristics diff --git a/gitnexus/src/config/ignore-service.ts b/gitnexus/src/config/ignore-service.ts index 8727fdd94..4156535c2 100644 --- a/gitnexus/src/config/ignore-service.ts +++ b/gitnexus/src/config/ignore-service.ts @@ -25,7 +25,6 @@ const DEFAULT_IGNORE_LIST = new Set([ // Dependencies 'node_modules', - 'deps', 'bower_components', 'jspm_packages', 'vendor', // PHP/Go @@ -391,6 +390,9 @@ export const isHardcodedIgnoredDirectoryAtPath = ( const relative = nodePath.relative(repoRoot, directoryPath).replace(/\\/g, '/'); if (isRootArtifactDirectory(relative, name)) return true; + if (name === 'deps' && relative === 'deps' && existsSync(nodePath.join(repoRoot, 'mix.exs'))) + return true; + return name === 'env' && existsSync(nodePath.join(directoryPath, 'pyvenv.cfg')); }; diff --git a/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts b/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts index e4de3278c..476c9355d 100644 --- a/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts +++ b/gitnexus/src/core/ingestion/cfg/visitors/elixir.ts @@ -126,37 +126,70 @@ class ElixirHarvester { return this.byName.get(name) ?? this.bind(name, node, 'var'); } - private identifiers(node: SyntaxNode): string[] { + private identifiers(node: SyntaxNode, excludeBodies = false): string[] { const out: string[] = []; const walk = (n: SyntaxNode): void => { if (n !== node && isFunction(n)) return; if (n.type === 'identifier' && n.text !== '_' && !/^(do|end|when)$/.test(n.text)) out.push(n.text); const callTarget = n.type === 'call' ? n.childForFieldName?.('target') : undefined; - for (const c of n.namedChildren) if (c !== callTarget) walk(c); + for (const c of n.namedChildren) + if ( + c !== callTarget && + !( + excludeBodies && + ['do_block', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + c.type, + ) + ) + ) + walk(c); }; walk(node); return out; } - facts(node: SyntaxNode): StatementFacts { + facts(node: SyntaxNode, header = false): StatementFacts { const defs: number[] = [], uses: number[] = []; const match = node.type === 'binary_operator' && /^\s*(=|<-)\s*$/.test(node.children.find((c) => !c.isNamed)?.text ?? ''); const left = match ? node.namedChildren[0] : undefined; - if (left) for (const name of this.identifiers(left)) defs.push(this.bind(name, left)); + const leftNames = left ? this.identifiers(left) : []; + const pinnedNames = new Set(); + if (left) { + const collectPins = (n: SyntaxNode): void => { + if (n.type === 'unary_operator' && n.text.startsWith('^')) { + const id = n.namedChildren.find((child) => child.type === 'identifier'); + if (id) pinnedNames.add(id.text); + } + for (const child of n.namedChildren) collectPins(child); + }; + collectPins(left); + } + if (left) + for (const name of leftNames) { + const pinned = pinnedNames.has(name); + if (pinned) uses.push(this.read(name, left)); + else defs.push(this.bind(name, left)); + } const bareCallee = node.type === 'call' && node.childForFieldName?.('target')?.type === 'identifier' ? node.childForFieldName('target')!.text : undefined; - for (const name of this.identifiers(node)) { + for (const name of this.identifiers(node, header)) { if (name === bareCallee) continue; - if (left && this.identifiers(left).includes(name)) continue; + if (left && leftNames.includes(name)) continue; uses.push(this.read(name, node)); } const sites: SiteRecord[] = []; + const memberNodes: SyntaxNode[] = []; + const callSites = new Map(); + const key = (n: SyntaxNode) => `${n.startIndex}:${n.endIndex}`; + const isPipe = (n: SyntaxNode | null | undefined) => + n?.type === 'binary_operator' && + n.children.find((child) => !child.isNamed)?.text.trim() === '|>'; // Phoenix exposes request input both through conn.params-like fields and // directly through the conventional `params["key"]` map argument. Keep // these as member-read facts: the language-neutral taint matcher owns the @@ -171,6 +204,7 @@ class ElixirHarvester { object: this.read(root.text, root), property: root.text, }); + memberNodes.push(n); } } if (n.type === 'call' && n.parent?.type === 'access_call') { @@ -183,9 +217,19 @@ class ElixirHarvester { object: this.read(root.text, root), property: property.text, }); + memberNodes.push(n); } } - for (const c of n.namedChildren) visitAccesses(c); + for (const c of n.namedChildren) + if ( + !( + header && + ['do_block', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + c.type, + ) + ) + ) + visitAccesses(c); }; visitAccesses(node); const visitCalls = (n: SyntaxNode): void => { @@ -201,17 +245,79 @@ class ElixirHarvester { const args = argumentsNode?.namedChildren.map((arg) => [ ...new Set(this.identifiers(arg).map((name) => this.read(name, arg))), ]); + const pipeLeft = isPipe(n.parent) ? n.parent.namedChildren[0] : undefined; + if (pipeLeft) { + const callArgs = args ?? []; + callArgs.unshift([ + ...new Set(this.identifiers(pipeLeft).map((name) => this.read(name, pipeLeft))), + ]); + sites.push({ + kind: 'call', + callee, + ...(callArgs.some((arg) => arg.length) ? { args: callArgs } : {}), + at: [start(n), n.startPosition.column], + }); + callSites.set(key(n), sites.length - 1); + for (const child of n.namedChildren) visitCalls(child); + return; + } + let parent: [number, number] | undefined; + for (let ancestor = n.parent; ancestor; ancestor = ancestor.parent) { + if (ancestor.type !== 'call') continue; + const parentIndex = callSites.get(key(ancestor)) ?? -1; + const parentArgs = ancestor.namedChildren.find((child) => child.type === 'arguments'); + const rawIndex = + parentArgs?.namedChildren.findIndex( + (arg) => arg.startIndex <= n.startIndex && arg.endIndex >= n.endIndex, + ) ?? -1; + if (rawIndex >= 0 && parentIndex >= 0) + parent = [parentIndex, rawIndex + (isPipe(ancestor.parent) ? 1 : 0)]; + break; + } sites.push({ kind: 'call', callee, ...(args?.some((arg) => arg.length) ? { args } : {}), + ...(parent ? { parent } : {}), at: [start(n), n.startPosition.column], }); + callSites.set(key(n), sites.length - 1); } } - for (const c of n.namedChildren) visitCalls(c); + for (const c of n.namedChildren) + if ( + !( + header && + ['do_block', 'else_block', 'rescue_block', 'catch_block', 'after_block'].includes( + c.type, + ) + ) + ) + visitCalls(c); }; visitCalls(node); + for (let i = 0; i < memberNodes.length; i++) { + const member = memberNodes[i]!; + for (let ancestor = member.parent; ancestor; ancestor = ancestor.parent) { + if (ancestor.type !== 'call') continue; + const args = ancestor.namedChildren.find((child) => child.type === 'arguments'); + const argIndex = + args?.namedChildren.findIndex( + (arg) => + arg === member || + (arg.startIndex <= member.startIndex && arg.endIndex >= member.endIndex), + ) ?? -1; + if (argIndex >= 0) { + const parentIndex = callSites.get(key(ancestor)) ?? -1; + if (parentIndex >= 0) + sites[i] = { + ...sites[i]!, + parent: [parentIndex, argIndex + (isPipe(ancestor.parent) ? 1 : 0)], + }; + } + break; + } + } return { line: start(node), defs: [...new Set(defs)], @@ -238,8 +344,8 @@ function buildFunctionCfg(fn: SyntaxNode, filePath: string): FunctionCfg | undef child.type, ), ); - const block = (node: SyntaxNode) => - builder.newBlock(start(node), end(node), node.text, 'normal', harvest.facts(node)); + const block = (node: SyntaxNode, header = false) => + builder.newBlock(start(node), end(node), node.text, 'normal', harvest.facts(node, header)); const keyword = (node: SyntaxNode): string | undefined => { if (node.type !== 'call') return undefined; const name = node.namedChildren.find((child) => child.type === 'identifier')?.text; @@ -270,6 +376,11 @@ function buildFunctionCfg(fn: SyntaxNode, filePath: string): FunctionCfg | undef }; const visit = (node: SyntaxNode, incoming: readonly number[]): number[] => builder.withNesting(() => { + if ( + node.type === 'anonymous_function' && + (node.startIndex !== fn.startIndex || node.endIndex !== fn.endIndex) + ) + return [...incoming]; const form = keyword(node); if (!form && node.type === 'binary_operator' && /\b(and|or)\b|&&|\|\|/.test(node.text)) { const control = block(node); @@ -284,7 +395,7 @@ function buildFunctionCfg(fn: SyntaxNode, filePath: string): FunctionCfg | undef builder.connect(incoming, normal); return [normal]; } - const header = block(node); + const header = block(node, true); builder.connect(incoming, header); const join = builder.newBlock(end(node), end(node), '', 'normal'); const doBlock = node.namedChildren.find((child) => child.type === 'do_block'); @@ -390,7 +501,7 @@ function buildFunctionCfg(fn: SyntaxNode, filePath: string): FunctionCfg | undef return builder.finish(harvest.all); } } - const exits = visitSeq(statementsOf(body), [entry]); + const exits = visitSeq(body.type === 'do_block' ? statementsOf(body) : [body], [entry]); builder.connect(exits, builder.exitIndex, 'seq'); return builder.finish(harvest.all); } diff --git a/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts b/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts index e20164212..979e67e3d 100644 --- a/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts +++ b/gitnexus/src/core/ingestion/import-resolvers/configs/elixir.ts @@ -63,12 +63,9 @@ const elixirModuleStrategy: ImportResolverStrategy = (rawImportPath, _filePath, for (const prefix of prefixes) { for (const ext of ELIXIR_EXTS) { const candidate = `${prefix}${relPath}${ext}`; - for (const fp of ctx.allFileList) { - if (fp === candidate || fp.endsWith(`/${candidate}`)) { - files.push(fp); - break; - } - } + const exact = ctx.allFileList.find((fp) => fp === candidate); + const fallback = ctx.allFileList.find((fp) => fp.endsWith(`/${candidate}`)); + if (exact ?? fallback) files.push(exact ?? fallback!); } // Preserve the existing root-priority rule for each expanded alias. if (files.length > resolvedAt) break; diff --git a/gitnexus/src/core/ingestion/languages/elixir.ts b/gitnexus/src/core/ingestion/languages/elixir.ts index 998f597ec..c23ce5c04 100644 --- a/gitnexus/src/core/ingestion/languages/elixir.ts +++ b/gitnexus/src/core/ingestion/languages/elixir.ts @@ -227,16 +227,33 @@ function extractElixirFrameworkFacts(tree: Parser.Tree): readonly ElixirFramewor | SyntaxNode | undefined; if (kind === 'scope' && body) { - const path = elixirString(values[0]); - const mod = alias(values[1]) ?? scopeModule; - if (path !== undefined) { + const keywordValue = (name: string) => + values + .find((value) => value.type === 'keywords') + ?.namedChildren.find( + (value) => + value.type === 'pair' && + value.namedChild(0)?.text.trim().replace(/:$/, '') === name, + ) + ?.namedChild(1) as SyntaxNode | undefined; + const scopePath = keywordValue('path'); + const scopeAlias = keywordValue('alias'); + const path = elixirString(values[0]) ?? elixirString(scopePath); + const mod = alias(values[1]) ?? alias(scopeAlias) ?? scopeModule; + { const scopedPipelines = [ ...pipelines, ...body.namedChildren.flatMap((child) => { if (child?.type !== 'call' || callKeyword(child as SyntaxNode) !== 'pipe_through') return []; const pipeline = args(child as SyntaxNode)[0]; - return pipeline?.type === 'atom' ? [pipeline.text.slice(1)] : []; + return pipeline?.type === 'atom' + ? [pipeline.text.slice(1)] + : pipeline?.type === 'list' + ? pipeline.namedChildren + .filter((entry) => entry.type === 'atom') + .map((entry) => entry.text.slice(1)) + : []; }), ]; for (const child of body.namedChildren) @@ -244,7 +261,7 @@ function extractElixirFrameworkFacts(tree: Parser.Tree): readonly ElixirFramewor child && (child.type !== 'call' || callKeyword(child as SyntaxNode) !== 'pipe_through') ) - visit(child as SyntaxNode, `${prefix}${path}`, scopedPipelines, mod); + visit(child as SyntaxNode, `${prefix}${path ?? ''}`, scopedPipelines, mod); } return; } @@ -535,13 +552,15 @@ function emitElixirScopeCaptures( current; current = current.parent ) - if (current === ancestor) return true; + if (current.startIndex === ancestor.startIndex && current.endIndex === ancestor.endIndex) + return true; return false; }; const visibleAt = (candidate: SyntaxNode): boolean => { for (let current = candidate.parent; current; current = current.parent) { if (!contains(current as SyntaxNode, node)) return false; - if (current === owner) return true; + if (current.startIndex === owner.startIndex && current.endIndex === owner.endIndex) + return true; } return false; }; @@ -549,7 +568,6 @@ function emitElixirScopeCaptures( if ( candidate.type === 'call' && callKeyword(candidate) === 'alias' && - enclosingModule(candidate) === owner && candidate.startIndex < node.startIndex && visibleAt(candidate) ) { @@ -601,7 +619,7 @@ function emitElixirScopeCaptures( '@declaration.is-exported': kind === 'defp' || kind === 'defmacrop' || kind === 'defguardp' ? 'false' : 'true', }); - if (kind === 'defdelegate' && signature.type === 'call') { + if (kind === 'defdelegate') { const target = keywordValue(node, 'to'); const delegated = keywordValue(node, 'as'); if (target?.type === 'alias') @@ -724,7 +742,12 @@ function emitElixirScopeCaptures( } } else if (node.type === 'unary_operator' && node.text.startsWith('@behaviour')) { const operand = node.childForFieldName?.('operand') ?? node.namedChild(0); - const behaviour = operand?.type === 'call' ? firstAliasArgument(operand) : undefined; + const behaviour = + operand?.type === 'call' + ? firstAliasArgument(operand) + : operand?.type === 'atom' + ? operand.text.slice(1) + : undefined; if (behaviour) add('@reference.inherits', node, { '@reference.name': behaviour }); } else if ( node.type === 'unary_operator' && diff --git a/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts b/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts index b03e2a463..f1fbda955 100644 --- a/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts +++ b/gitnexus/src/core/ingestion/languages/elixir/scope-resolver.ts @@ -192,6 +192,7 @@ function emitElixirBehaviourMethodEdges( graph: KnowledgeGraph, parsedFiles: readonly ParsedFile[], nodeLookup: GraphNodeLookup, + indexes: ScopeResolutionIndexes, ): void { const emitted = new Set( [...graph.iterRelationshipsByType('METHOD_IMPLEMENTS')].map( @@ -204,10 +205,15 @@ function emitElixirBehaviourMethodEdges( const behaviours = parsedFiles.flatMap((file) => file.localDefs.filter((def) => def.qualifiedName === site.name && def.type === 'Interface'), ); - const implementations = parsed.localDefs.filter((def) => def.type === 'Class'); - if (behaviours.length !== 1 || implementations.length !== 1) continue; + let scope = indexes.scopeTree.getScope(site.inScope); + let implementation: SymbolDefinition | undefined; + while (scope) { + implementation = scope.ownedDefs.find((def) => def.type === 'Class'); + if (implementation) break; + scope = scope.parent ? indexes.scopeTree.getScope(scope.parent) : undefined; + } + if (behaviours.length !== 1 || !implementation) continue; const behaviour = behaviours[0]!; - const implementation = implementations[0]!; for (const contract of parsedFiles .flatMap((file) => file.localDefs) .filter( @@ -270,19 +276,24 @@ function emitElixirFrameworkEdges( file.localDefs.filter( (def) => def.type === 'Function' && - def.qualifiedName === `${fact.handler}.${fact.action}`, + def.qualifiedName === `${target.qualifiedName}.${fact.action}`, ), ) : target ? [target] : []; - if (handler.length !== 1) continue; - const handlerId = resolveDefGraphId(handler[0]!.filePath, handler[0]!, nodeLookup); + const uniqueHandlers = [...new Map(handler.map((def) => [def.nodeId, def])).values()]; + if (uniqueHandlers.length !== 1) continue; + const handlerId = resolveDefGraphId( + uniqueHandlers[0]!.filePath, + uniqueHandlers[0]!, + nodeLookup, + ); if (!handlerId) continue; const key = routeNodeKey(fact.method, fact.path); const routeId = generateId('Route', key); const routeProperties = { - filePath: handler[0]!.filePath, + filePath: uniqueHandlers[0]!.filePath, startLine: toZeroBasedLine(fact.line), endLine: toZeroBasedLine(fact.line), handlerSymbolId: handlerId, @@ -296,7 +307,7 @@ function emitElixirFrameworkEdges( label: 'Route', properties: { name: fact.path, method: fact.method, ...routeProperties }, }); - const fileId = generateId('File', handler[0]!.filePath); + const fileId = generateId('File', uniqueHandlers[0]!.filePath); graph.addRelationship({ id: generateId('HANDLES_ROUTE', `${fileId}->${routeId}`), sourceId: fileId, @@ -390,8 +401,8 @@ export const elixirScopeResolver: ScopeResolver = { propagatesReturnTypesAcrossImports: false, resolveQualifiedReceiverMember: resolveElixirCapturedAliasMember, populateNamespaceSiblings: populateElixirImportFilters, - emitPostResolutionEdges(graph, parsedFiles, nodeLookup) { - emitElixirBehaviourMethodEdges(graph, parsedFiles, nodeLookup); + emitPostResolutionEdges(graph, parsedFiles, nodeLookup, indexes) { + emitElixirBehaviourMethodEdges(graph, parsedFiles, nodeLookup, indexes); emitElixirFrameworkEdges(graph, parsedFiles, nodeLookup); }, }; diff --git a/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts b/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts index 771319743..0fa5e4249 100644 --- a/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts +++ b/gitnexus/src/core/ingestion/languages/elixir/semantic-graph.ts @@ -181,10 +181,11 @@ export function extractElixirSemanticGraph( const isMixFile = filePath === 'mix.exs' || filePath.endsWith('/mix.exs'); const collectDefinitions = (node: SyntaxNode): void => { - if (node.type === 'call' && DECLARATIONS.has(keyword(node) ?? '')) { - const name = declarationName(node); + if (!isQuoted(node) && node.type === 'call' && DECLARATIONS.has(keyword(node) ?? '')) { + const info = declaration(node); + const owner = enclosingModule(node); const body = definitionBody(node); - if (name && body) definitions.set(name, body); + if (info && owner && body) definitions.set(`${owner.name}:${info.name}/${info.arity}`, body); } for (let i = 0; i < node.namedChildCount; i++) { const child = node.namedChild(i); @@ -242,7 +243,9 @@ export function extractElixirSemanticGraph( if (appsPath) addEvidence('mix-umbrella', appsPath, node, { appsPath }); const deps = keywordValue(metadata, 'deps'); emitMixDependencies( - deps?.type === 'call' && keyword(deps) === 'deps' ? definitions.get('deps') : deps, + deps?.type === 'call' && keyword(deps) === 'deps' + ? definitions.get(`${enclosingModule(node)?.name}:deps/0`) + : deps, node, ); } @@ -348,6 +351,15 @@ export function extractElixirSemanticGraph( : {}), ...(ownerId ? { ownerId } : {}), }); + if (ownerId && (label === 'Function' || label === 'Macro')) + relationships.push({ + id: generateId('HAS_METHOD', `${ownerId}->${id}`), + sourceId: ownerId, + targetId: id, + type: 'HAS_METHOD', + confidence: 1, + reason: 'elixir module function', + }); if (label === 'Class' || label === 'Interface') moduleIds.set(qualifiedName, id); }; diff --git a/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts b/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts index 94f6727ba..cbb88948a 100644 --- a/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts +++ b/gitnexus/src/core/ingestion/method-extractors/configs/elixir.ts @@ -100,9 +100,9 @@ function extractElixirParameters(node: SyntaxNode): ParameterInfo[] { if (p.type === 'identifier') { params.push({ name: p.text, type: null, isOptional: false, isVariadic: false }); } else if (p.type === 'binary_operator') { - // default value: `param \\ default` or `param // default` + const operator = p.children.find((child) => !child.isNamed)?.text; const left = p.childForFieldName?.('left'); - if (left?.type === 'identifier') { + if (operator === '\\\\' && left?.type === 'identifier') { params.push({ name: left.text, type: null, isOptional: true, isVariadic: false }); } } @@ -122,7 +122,10 @@ function extractElixirVisibility(node: SyntaxNode): MethodVisibility { function extractElixirOwnerName(node: SyntaxNode): string | undefined { let cur: SyntaxNode | null | undefined = node; while (cur) { - if (cur.type === 'call' && callKeyword(cur) === 'defmodule') { + if ( + cur.type === 'call' && + (callKeyword(cur) === 'defmodule' || callKeyword(cur) === 'defprotocol') + ) { const pArgs = findArguments(cur); if (pArgs) { for (let i = 0; i < pArgs.namedChildCount; i++) { diff --git a/gitnexus/test/integration/elixir-pdg-persistence.test.ts b/gitnexus/test/integration/elixir-pdg-persistence.test.ts index 67ee55411..ba1ad6276 100644 --- a/gitnexus/test/integration/elixir-pdg-persistence.test.ts +++ b/gitnexus/test/integration/elixir-pdg-persistence.test.ts @@ -92,17 +92,17 @@ withTestLbugDB( it('persists representative CDG and reaching-definition results without receive cross-process flow', async () => { const controls = (await backend.callTool('pdg_query', { mode: 'controls', - target: 'guarded', + target: 'mailbox', })) as any; const flows = (await backend.callTool('pdg_query', { mode: 'flows', - target: 'guarded', - variable: 'rebound', + target: 'mailbox', + variable: 'message', })) as any; expect(controls).not.toHaveProperty('error'); expect(controls.results.length).toBeGreaterThan(0); expect(flows).not.toHaveProperty('error'); - expect(flows.results.length).toBeGreaterThan(0); + expect(flows.results).toEqual([]); expect(JSON.stringify([...controls.results, ...flows.results])).not.toMatch( /message-delivery/i, ); diff --git a/gitnexus/test/integration/elixir-phoenix-ecto-pipeline.test.ts b/gitnexus/test/integration/elixir-phoenix-ecto-pipeline.test.ts index 77613fbfd..cd682a000 100644 --- a/gitnexus/test/integration/elixir-phoenix-ecto-pipeline.test.ts +++ b/gitnexus/test/integration/elixir-phoenix-ecto-pipeline.test.ts @@ -43,6 +43,10 @@ defmodule MyAppWeb.Router do scope "/", MyAppWeb do get "/root", PostController, :index end + scope alias: MyAppWeb do + pipe_through [:browser, :api] + get "/keyword", PostController, :index + end end `, ); @@ -411,6 +415,12 @@ describe('Elixir Phoenix/Ecto pipeline', () => { filePath: 'lib/my_app_web/controllers/post_controller.ex', startLine: expect.any(Number), }), + expect.objectContaining({ + name: '/keyword', + filePath: 'lib/my_app_web/controllers/post_controller.ex', + startLine: expect.any(Number), + middleware: ['browser', 'api'], + }), ]), ); expect(routes.map((route) => route.name)).not.toContain('//root'); diff --git a/gitnexus/test/integration/resolvers/elixir.test.ts b/gitnexus/test/integration/resolvers/elixir.test.ts new file mode 100644 index 000000000..e587b7095 --- /dev/null +++ b/gitnexus/test/integration/resolvers/elixir.test.ts @@ -0,0 +1,37 @@ +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'path'; +import { + getRelationships, + runPipelineFromRepo, + type PipelineResult, + writeFixtureRepo, +} from './helpers.js'; + +describe('Elixir scope resolution', () => { + let result: PipelineResult; + let root = ''; + + beforeAll(async () => { + root = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-elixir-resolver-')); + writeFixtureRepo(root, { + 'lib/my_app.ex': ` + defmodule MyApp.Target do + def run, do: :ok + end + defmodule MyApp.Caller do + def invoke, do: MyApp.Target.run() + end + `, + }); + result = await runPipelineFromRepo(root, () => {}); + }, 60_000); + + afterAll(() => fs.rmSync(root, { recursive: true, force: true })); + + it('registers the Elixir resolver and resolves qualified calls', () => { + const calls = getRelationships(result, 'CALLS'); + expect(calls.some((edge) => edge.source === 'invoke' && edge.target === 'run')).toBe(true); + }); +}); diff --git a/gitnexus/test/integration/tree-sitter-languages.test.ts b/gitnexus/test/integration/tree-sitter-languages.test.ts index 4f0ebfbb3..ce976f89c 100644 --- a/gitnexus/test/integration/tree-sitter-languages.test.ts +++ b/gitnexus/test/integration/tree-sitter-languages.test.ts @@ -1013,12 +1013,12 @@ describe('Tree-sitter multi-language parsing', () => { describe('Elixir', () => { const elixirQueries = () => getProvider(SupportedLanguages.Elixir).treeSitterQueries; - function loadElixirOrSkip() { - return loadLanguage(SupportedLanguages.Elixir).catch(() => null); + async function loadElixir(): Promise { + await loadLanguage(SupportedLanguages.Elixir); } it('parses modules, functions, and protocols', async () => { - if (!(await loadElixirOrSkip())) return; + await loadElixir(); const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries()); const defs = extractDefinitions(matches); const defTypes = defs.map((d) => d.type); @@ -1037,7 +1037,7 @@ describe('Tree-sitter multi-language parsing', () => { }); it('captures import/alias/use/require as @import', async () => { - if (!(await loadElixirOrSkip())) return; + await loadElixir(); const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries()); const importSources: string[] = []; for (const match of matches) { @@ -1049,7 +1049,7 @@ describe('Tree-sitter multi-language parsing', () => { }); it('extracts real calls once and skips definitions/attributes', async () => { - if (!(await loadElixirOrSkip())) return; + await loadElixir(); const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries()); const provider = getProvider(SupportedLanguages.Elixir); const callNames: string[] = []; @@ -1071,7 +1071,7 @@ describe('Tree-sitter multi-language parsing', () => { }); it('resolves defmodule as the enclosing owner for functions', async () => { - if (!(await loadElixirOrSkip())) return; + await loadElixir(); const { matches } = parseAndQuery(parser, readFixture('simple.ex'), elixirQueries()); const provider = getProvider(SupportedLanguages.Elixir); let createNode: any; diff --git a/gitnexus/test/unit/elixir-cfg.test.ts b/gitnexus/test/unit/elixir-cfg.test.ts index 28cb561f8..d4b4a6c16 100644 --- a/gitnexus/test/unit/elixir-cfg.test.ts +++ b/gitnexus/test/unit/elixir-cfg.test.ts @@ -45,6 +45,25 @@ describe('Elixir CFG visitor', () => { ); }); + it('treats pinned patterns as reads and keeps header facts outside branch bodies', async () => { + await loadLanguage(SupportedLanguages.Elixir, 'pin-header.ex'); + const parser = new Parser(); + parser.setLanguage( + getLanguageGrammar(SupportedLanguages.Elixir) as Parameters[0], + ); + const root = parser.parse(`def f(user_id, pair, flag) do + {^user_id, id} = pair + if flag do body_only(id) end + end`).rootNode; + const cfg = createElixirCfgVisitor().buildFunctionCfg(root.namedChildren[0]!, 'pin-header.ex')!; + const match = cfg.blocks.find((block) => block.text.includes('{^user_id, id}'))! + .statements![0]!; + expect(cfg.bindings[match.uses[0]!]!.name).toBe('user_id'); + expect(cfg.bindings[match.defs[0]!]!.name).toBe('id'); + const header = cfg.blocks.find((block) => block.text.startsWith('if flag'))!.statements![0]!; + expect(header.uses.map((index) => cfg.bindings[index]!.name)).toEqual(['flag']); + }); + it('propagates Phoenix parameters to dynamic-eval and raw-SQL text only', async () => { await loadLanguage(SupportedLanguages.Elixir, 'taint.ex'); const parser = new Parser(); @@ -69,6 +88,76 @@ describe('Elixir CFG visitor', () => { ]); }); + it('records nested parent sites and taints direct and piped parameter reads', async () => { + await loadLanguage(SupportedLanguages.Elixir, 'nested-sites.ex'); + const parser = new Parser(); + parser.setLanguage( + getLanguageGrammar(SupportedLanguages.Elixir) as Parameters[0], + ); + const root = parser.parse(`def f(params) do + Code.eval_string(wrapper(params["code"])) + Code.eval_string(params["code"]) + params["code"] |> Code.eval_string + end`).rootNode; + const cfg = createElixirCfgVisitor().buildFunctionCfg( + root.namedChildren[0]!, + 'nested-sites.ex', + )!; + const nested = cfg.blocks.find((block) => block.text.includes('wrapper(params'))! + .statements![0]!.sites!; + const wrapper = nested.findIndex((site) => site.kind === 'call' && site.callee === 'wrapper'); + const read = nested.findIndex((site) => site.kind === 'member-read'); + const evalSite = nested.findIndex( + (site) => site.kind === 'call' && site.callee === 'Code.eval_string', + ); + expect(wrapper).toBeGreaterThanOrEqual(0); + expect(read).toBeGreaterThanOrEqual(0); + expect(nested[wrapper]!.parent).toEqual([evalSite, 0]); + expect(nested[read]!.parent).toEqual([wrapper, 0]); + const findings = computeTaintFlows( + cfg, + computeReachingDefs(cfg), + matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([])), + ).findings.filter((finding) => finding.sink.entryName === 'eval_string'); + expect(findings).toHaveLength(2); + }); + + it('uses source-range parents for repeated callees and shifted pipe arguments', async () => { + await loadLanguage(SupportedLanguages.Elixir, 'parent-ranges.ex'); + const parser = new Parser(); + parser.setLanguage( + getLanguageGrammar(SupportedLanguages.Elixir) as Parameters[0], + ); + const root = parser.parse(`def f(params, input, code) do + wrapper(wrapper(params["x"])) + input |> sink(wrapper(code)) + (input |> foo()) + sink() + end`).rootNode; + const cfg = createElixirCfgVisitor().buildFunctionCfg( + root.namedChildren[0]!, + 'parent-ranges.ex', + )!; + const repeated = cfg.blocks.find((block) => block.text.includes('wrapper(wrapper'))! + .statements![0]!.sites!; + const wrappers = repeated + .map((site, index) => ({ site, index })) + .filter(({ site }) => site.kind === 'call' && site.callee === 'wrapper'); + const read = repeated.find((site) => site.kind === 'member-read')!; + expect(read.parent).toEqual([wrappers[1]!.index, 0]); + const piped = cfg.blocks.find((block) => block.text.includes('sink(wrapper'))!.statements![0]! + .sites!; + const sink = piped.findIndex((site) => site.kind === 'call' && site.callee === 'sink'); + const inner = piped.find((site) => site.kind === 'call' && site.callee === 'wrapper')!; + expect(inner.parent).toEqual([sink, 1]); + const addition = cfg.blocks.find((block) => block.text.includes('foo()) + sink'))! + .statements![0]!.sites!; + const foo = addition.find((site) => site.kind === 'call' && site.callee === 'foo')!; + const standaloneSink = addition.find((site) => site.kind === 'call' && site.callee === 'sink')!; + expect(foo.parent).toBeUndefined(); + expect(standaloneSink.parent).toBeUndefined(); + expect(standaloneSink.args).toBeUndefined(); + }); + it('uses Ecto adapter SQL argument one while preserving Repo SQL argument zero', async () => { await loadLanguage(SupportedLanguages.Elixir, 'adapter-taint.ex'); const parser = new Parser(); @@ -114,10 +203,7 @@ describe('Elixir CFG visitor', () => { matchFunctionSites(cfg, ELIXIR_TAINT_MODEL, buildTaintImportIndex([])), ).findings; expect(cfg.blocks.some((block) => block.text.includes('Code.eval_string'))).toBe(true); - expect(findings.map((finding) => finding.sink.entryName).sort()).toEqual([ - 'apply', - 'eval_string', - ]); + expect(findings.map((finding) => finding.sink.entryName)).toEqual(['apply']); }); it('does not flow taint sequentially between anonymous-function clauses', async () => { @@ -190,7 +276,7 @@ end`; const cfg = cfgs.find((candidate) => candidate.blocks.some((block) => block.text.includes('receive do')), )!; - expect(cfg.blocks).toHaveLength(51); + expect(cfg.blocks.some((block) => block.text.includes('receive do'))).toBe(true); expect( new Set(cfg.edges.filter((edge) => edge.kind.startsWith('cond')).map((edge) => edge.from)) .size, @@ -203,11 +289,14 @@ end`; (block) => block.text.includes('value <- values') && block.statements![0]!.defs.length > 0, ), ).toBe(true); + const closure = cfgs.find((candidate) => + candidate.blocks.some((block) => block.text.includes('fallback')), + )!; expect( - cfg.blocks.some( - (block) => - block.text.includes('fallback') && - block.statements![0]!.sites?.some((site) => site.kind === 'call'), + closure.blocks.some((block) => + block.statements?.some((statement) => + statement.sites?.some((site) => site.kind === 'call'), + ), ), ).toBe(true); }); diff --git a/gitnexus/test/unit/elixir-import-resolver.test.ts b/gitnexus/test/unit/elixir-import-resolver.test.ts index dbc24fe44..bf09a778c 100644 --- a/gitnexus/test/unit/elixir-import-resolver.test.ts +++ b/gitnexus/test/unit/elixir-import-resolver.test.ts @@ -11,4 +11,17 @@ describe('Elixir import resolver', () => { }), ).toEqual({ kind: 'files', files: ['lib/prefix/one.ex', 'lib/prefix/two.ex'] }); }); + + it('prefers an exact root module candidate regardless of file-list order', () => { + const resolve = createImportResolver(elixirImportConfig); + for (const allFileList of [ + ['apps/umbrella/lib/my_app/user.ex', 'lib/my_app/user.ex'], + ['lib/my_app/user.ex', 'apps/umbrella/lib/my_app/user.ex'], + ]) { + expect(resolve('MyApp.User', 'lib/app.ex', { allFileList })).toEqual({ + kind: 'files', + files: ['lib/my_app/user.ex'], + }); + } + }); }); diff --git a/gitnexus/test/unit/elixir-semantic-graph.test.ts b/gitnexus/test/unit/elixir-semantic-graph.test.ts index dc745d061..928ad460c 100644 --- a/gitnexus/test/unit/elixir-semantic-graph.test.ts +++ b/gitnexus/test/unit/elixir-semantic-graph.test.ts @@ -101,6 +101,9 @@ describe('Elixir semantic graph', () => { }), ]), ); + expect(graph.relationships).toContainEqual( + expect.objectContaining({ type: 'HAS_METHOD', reason: 'elixir module function' }), + ); }); it('keeps repeated modules as separate ambiguous candidates without qualified call edges', async () => { diff --git a/gitnexus/test/unit/ignore-service.test.ts b/gitnexus/test/unit/ignore-service.test.ts index fe6cf4602..a565ede38 100644 --- a/gitnexus/test/unit/ignore-service.test.ts +++ b/gitnexus/test/unit/ignore-service.test.ts @@ -51,7 +51,6 @@ describe('shouldIgnorePath', () => { describe('dependency directories', () => { it.each([ 'node_modules', - 'deps', 'vendor', 'third_party', '3rdparty', @@ -258,7 +257,7 @@ describe('shouldIgnorePath', () => { describe('isHardcodedIgnoredDirectory', () => { it('returns true for known ignored directories', () => { expect(isHardcodedIgnoredDirectory('node_modules')).toBe(true); - expect(isHardcodedIgnoredDirectory('deps')).toBe(true); + expect(isHardcodedIgnoredDirectory('deps')).toBe(false); expect(isHardcodedIgnoredDirectory('.git')).toBe(true); expect(isHardcodedIgnoredDirectory('dist')).toBe(true); expect(isHardcodedIgnoredDirectory('monaco-workers')).toBe(true); @@ -582,8 +581,8 @@ describe('createIgnoreFilter', () => { const mockPath = { name: 'node_modules', relative: () => 'node_modules' } as any; expect(filter.childrenIgnored(mockPath)).toBe(true); - const mixDepsPath = { name: 'deps', relative: () => 'src/deps' } as any; - expect(filter.childrenIgnored(mixDepsPath)).toBe(true); + const sourceDepsPath = { name: 'deps', relative: () => 'src/deps' } as any; + expect(filter.childrenIgnored(sourceDepsPath)).toBe(false); const srcPath = { name: 'src', relative: () => 'src' } as any; expect(filter.childrenIgnored(srcPath)).toBe(false);