fix(elixir): address review and CI failures

This commit is contained in:
Benedikt Schnatterbeck 2026-09-22 10:33:33 +01:00
parent 433b5a4e63
commit c807e92057
No known key found for this signature in database
17 changed files with 234 additions and 79 deletions

File diff suppressed because one or more lines are too long

View file

@ -484,6 +484,7 @@ import { resolveSwiftImportTarget } from '../../src/core/ingestion/languages/swi
import { resolveRustImportTarget } from '../../src/core/ingestion/languages/rust/import-target.ts';
import { resolveZigImportInternal } from '../../src/core/ingestion/import-resolvers/zig.ts';
import { resolvePythonImportTarget } from '../../src/core/ingestion/languages/python/import-target.ts';
import { elixirScopeResolver } from '../../src/core/ingestion/languages/elixir/scope-resolver.ts';
import { makeJsResolveImportTarget } from '../../src/core/ingestion/languages/javascript/import-target.ts';
import { makeVueResolveImportTarget } from '../../src/core/ingestion/languages/vue/import-target.ts';
// The two `ScopeResolver`s, not their inner resolvers — see `RESOLVE_HOOK`.
@ -600,6 +601,7 @@ const HEAP_BUDGETED = [
'go',
'cpp',
'objc',
'elixir',
];
// javascript, typescript and vue were budgeted here until #2953 and are now
// BOUNDED, which is a demotion in gate strength and a promotion in what the
@ -627,7 +629,7 @@ const HEAP_BUDGETED = [
* their hooks declare three or four parameters — so their numbers stay exactly
* where they were.
*/
const CONTEXT_LANGS = ['php', 'java', 'kotlin', 'python', 'swift'];
const CONTEXT_LANGS = ['php', 'java', 'kotlin', 'python', 'swift', 'elixir'];
/**
* Needs `node --expose-gc` to force collection for a clean delta; without it
@ -759,6 +761,7 @@ const EXTENSION = {
c: '.c',
cpp: '.cpp',
objc: '.m',
elixir: '.ex',
zig: '.zig',
};
/** C and C++ resolve `#include` against HEADERS, which reach the resolver
@ -883,6 +886,7 @@ function uniqueDir(lang, d, i) {
// `resolutionConfig` load-bearing. Odd `i` is the header.
if (lang === 'c' || lang === 'cpp') return i % 2 === 1 ? `include/comp${d}` : `src/comp${d}`;
if (lang === 'objc') return `src/comp${d}`;
if (lang === 'elixir') return `lib/App${d}`;
if (lang === 'ruby') return `lib/mod${d}`;
// One flat `src/mod{d}/` per index and NO nested slice, on purpose: a Zig
// import is spelled RELATIVE TO THE IMPORTER, and `uniqueTarget` does not
@ -979,6 +983,7 @@ function collideDir(lang, d, i) {
if (lang === 'vue') return `src/pkg${d}/components`;
if (lang === 'c' || lang === 'cpp') return i % 2 === 1 ? `svc${d}/include` : `svc${d}/src`;
if (lang === 'objc') return `svc${d}/src`;
if (lang === 'elixir') return `lib/Svc${d}/App`;
if (lang === 'ruby') return `svc${d}/lib/models`;
// Rust's reasoning, verbatim: the resolver walks path components and probes
// `.has()`, never searches, so file count is not an axis its cost has and a
@ -1408,6 +1413,9 @@ function uniqueTarget(lang, { local, r, d, j, dirs }) {
if (lang === 'objc') {
return local ? `comp${j % dirs}/file${j}.m` : `vendor${(r >>> 4) % 97}/missing.m`;
}
if (lang === 'elixir') {
return local ? `App${j % dirs}.file${j}` : `Vendor${(r >>> 4) % 97}.Missing`;
}
if (lang === 'ruby') {
return local
? `mod${d}/file${j}`
@ -1641,6 +1649,9 @@ function collideTarget(lang, { local, r, d, j, dirs }) {
if (lang === 'objc') {
return local ? `src/file${j}.m` : `vendor${(r >>> 4) % 97}/missing.m`;
}
if (lang === 'elixir') {
return local ? `App.file${j}` : `Vendor${(r >>> 4) % 97}.Missing`;
}
if (lang === 'ruby') {
// `models/mod{n}.rb` in every package. Ruby answers `require` from a keyed
// suffix map, so the repeated basename cannot grow a bucket: this arm
@ -1926,6 +1937,15 @@ function resolveOne(lang, from, target, pass) {
if (lang === 'objc') {
return objectiveCScopeResolver.resolveImportTarget(target, from, allFilePaths, pass.config);
}
if (lang === 'elixir') {
return elixirScopeResolver.resolveImportTarget(
target,
from,
allFilePaths,
pass.config,
contextFor(pass, { kind: 'namespace', localName: 'X', importedName: 'X', targetRaw: target }),
);
}
if (lang === 'csharp' || lang === 'csharp_csproj') {
return resolveCsharpImportTarget(
{ kind: 'namespace', localName: '_', importedName: '_', targetRaw: target },
@ -2147,6 +2167,7 @@ const HEAP_PROBE_TARGET = {
python: 'vendor0.deep.missing',
c: 'vendor0/missing.h',
objc: 'vendor0/missing.m',
elixir: 'Vendor0.Missing',
// The entries below cover the BOUNDED tier — see `HEAP_BOUNDED`, which
// derives to cobol, swift and rust; the rest were promoted. Same rule as the
// budgeted ones above: a spelling `uniqueTarget` already mints for that language, and
@ -2346,6 +2367,14 @@ const CONTEXT_PROBE = {
probeFile('Sources/Client/Main.swift', [['Class', 'Client.Main']]),
],
},
elixir: {
from: 'lib/Client.ex',
target: 'Demo.Target',
parsedFiles: [
probeFile('lib/Client.ex', [['Class', 'Client']]),
probeFile('lib/Target.ex', [['Class', 'Demo.Target']]),
],
},
};
/** Resolve the probe twice through `resolveOne` — once with the pass's parsed
@ -2430,6 +2459,7 @@ const LANG_REGISTRY = {
c: SupportedLanguages.C,
cpp: SupportedLanguages.CPlusPlus,
objc: SupportedLanguages.ObjectiveC,
elixir: SupportedLanguages.Elixir,
zig: SupportedLanguages.Zig,
};
const LANGS = Object.keys(LANG_REGISTRY);

View file

@ -111,6 +111,7 @@ export const findDeclarationNode = (root: any): any | null => {
'declaration', // Go: type X struct
'object_declaration', // Kotlin: object
'impl_item', // Rust: impl
'call', // Elixir: defmodule
]);
// Iterative DFS — avoids stack overflow on deeply nested ASTs.

View file

@ -298,6 +298,15 @@ class ElixirHarvester {
visitCalls(node);
for (let i = 0; i < memberNodes.length; i++) {
const member = memberNodes[i]!;
const pipe = isPipe(member.parent) ? member.parent : undefined;
const pipedCall = pipe?.namedChildren[1];
if (pipedCall?.type === 'call') {
const parentIndex = callSites.get(key(pipedCall)) ?? -1;
if (parentIndex >= 0) {
sites[i] = { ...sites[i]!, parent: [parentIndex, 0] };
continue;
}
}
for (let ancestor = member.parent; ancestor; ancestor = ancestor.parent) {
if (ancestor.type !== 'call') continue;
const args = ancestor.namedChildren.find((child) => child.type === 'arguments');

View file

@ -372,7 +372,16 @@ function extractElixirFrameworkFacts(tree: Parser.Tree): readonly ElixirFramewor
const inPair = keywords?.namedChildren.find(
(value) => value.type === 'pair' && value.namedChild(0)?.text.trim() === 'in:',
);
const model = alias(inPair?.namedChild(1) as SyntaxNode);
const inExpression = values.find(
(value) =>
value.type === 'binary_operator' &&
value.childForFieldName?.('left')?.type === 'identifier' &&
value.childForFieldName?.('right')?.type === 'alias' &&
/\bin\b/.test(value.text),
);
const model = alias(
(inPair?.namedChild(1) ?? inExpression?.childForFieldName?.('right')) as SyntaxNode,
);
if (model)
facts.push({ kind: 'query', model, method: 'from', line: node.startPosition.row + 1 });
}
@ -687,7 +696,13 @@ function emitElixirScopeCaptures(
startCol: node.startPosition.column,
});
} else if (target?.type === 'alias') {
add('@import.statement', node, { '@import.source': target, '@import.wildcard': target });
// `bindingAugmentations` can only add references after wildcard imports
// are finalized, so an `except` import must not emit a wildcard here.
if (!except)
add('@import.statement', node, {
'@import.source': target,
'@import.wildcard': target,
});
if (except?.type === 'list') {
const excluded = except.namedChildren
.flatMap((entry) => (entry.type === 'keywords' ? entry.namedChildren : [entry]))

View file

@ -104,7 +104,12 @@ function populateElixirImportFilters(
const bucket = augmentations.get(scopeId) ?? new Map<string, BindingRef[]>();
augmentations.set(scopeId, bucket);
for (const def of targets[0]!.localDefs) {
if ((def.type !== 'Function' && def.type !== 'Macro') || def.isExported === false) continue;
if (
(def.type !== 'Function' && def.type !== 'Macro') ||
def.isExported === false ||
def.qualifiedName !== `${fact.target}.${callableName(def)}`
)
continue;
if (fact.mode === 'only' && fact.category === 'functions' && def.type !== 'Function')
continue;
if (fact.mode === 'only' && fact.category === 'macros' && def.type !== 'Macro') continue;

View file

@ -104,6 +104,7 @@ function isBehaviourModule(node: SyntaxNode): boolean {
const visit = (child: SyntaxNode): void => {
if (found || (child.type === 'call' && keyword(child) === 'defmodule' && child !== node))
return;
if (child.type === 'call' && keyword(child) === 'quote') return;
if (child.type === 'unary_operator' && child.text.startsWith('@callback')) found = true;
for (let i = 0; !found && i < child.namedChildCount; i++) {
const next = child.namedChild(i);

View file

@ -928,8 +928,9 @@ export const findEnclosingClassInfo = (
/**
* Optional: the type a CONTAINER node declares
* (`LanguageProvider.resolveContainerTypeOwner`). Consulted for every
* `CLASS_CONTAINER_TYPES` node the walk meets, before the generic name-child
* derivation, for languages whose containers are named from context (a
* ancestor the walk meets; it must return null for nodes it does not own.
* This supports languages whose containers are outside `CLASS_CONTAINER_TYPES`
* (for example, Elixir's `defmodule` call) or named from context (a
* binding wrapper, an enclosing callable, an anonymous ordinal). Null falls
* through to the generic derivation.
*/
@ -1010,9 +1011,8 @@ export const findEnclosingClassInfo = (
};
}
}
// Provider-owned containers may use grammar nodes outside the generic
// class list (Elixir's `defmodule` is a call). Returning null preserves
// every existing provider's fallback behavior.
// Provider-owned containers may use grammar nodes outside the generic class
// list (Elixir's `defmodule` is a call). Returning null preserves fallback.
if (resolveContainerTypeOwner !== undefined) {
const containerOwner = resolveContainerTypeOwner(current, filePath);
if (containerOwner !== null) {

View file

@ -537,40 +537,17 @@ describe('Elixir Phoenix/Ecto pipeline', () => {
expect(callTargets('MyApp.AliasContainment.after_block')).not.toContain(
'MyApp.Tasks.ExampleTask.run',
);
expect(graph.relationships).not.toEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'CALLS',
source: 'MyApp.Blog.wildcard_private',
target: 'MyApp.Helpers.private',
}),
expect.objectContaining({
type: 'CALLS',
source: 'MyApp.Blog.wildcard_private_macro',
target: 'MyApp.Helpers.private_macro',
}),
expect.objectContaining({
type: 'CALLS',
source: 'MyApp.Blog.except_private',
target: 'MyApp.Helpers.private',
}),
expect.objectContaining({
type: 'CALLS',
source: 'MyApp.Blog.except_private_macro',
target: 'MyApp.Helpers.private_macro',
}),
expect.objectContaining({
type: 'CALLS',
source: 'MyApp.Blog.callback_call',
target: 'MyApp.Behaviour.required',
}),
expect.objectContaining({
type: 'CALLS',
source: 'MyApp.Behaviour.required',
target: 'MyApp.Behaviour.required',
}),
]),
);
for (const [source, target] of [
['MyApp.Blog.wildcard_private', 'MyApp.Helpers.private'],
['MyApp.Blog.wildcard_private_macro', 'MyApp.Helpers.private_macro'],
['MyApp.Blog.except_private', 'MyApp.Helpers.private'],
['MyApp.Blog.except_private_macro', 'MyApp.Helpers.private_macro'],
['MyApp.Blog.callback_call', 'MyApp.Behaviour.required'],
['MyApp.Behaviour.required', 'MyApp.Behaviour.required'],
])
expect(graph.relationships).not.toContainEqual(
expect.objectContaining({ type: 'CALLS', source, target }),
);
expect(graph.relationships).toEqual(
expect.arrayContaining([
expect.objectContaining({
@ -580,18 +557,13 @@ describe('Elixir Phoenix/Ecto pipeline', () => {
}),
]),
);
expect(graph.relationships).not.toEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'METHOD_IMPLEMENTS',
source: 'MyApp.ArityImplementation.mismatch',
}),
expect.objectContaining({
type: 'METHOD_IMPLEMENTS',
source: 'MyApp.AmbiguousImplementation.ambiguous',
}),
]),
);
for (const source of [
'MyApp.ArityImplementation.mismatch',
'MyApp.AmbiguousImplementation.ambiguous',
])
expect(graph.relationships).not.toContainEqual(
expect.objectContaining({ type: 'METHOD_IMPLEMENTS', source }),
);
expect(
graph.nodes.filter(
(node) => node.label === 'Class' && node.qualifiedName === 'MyApp.Blog.Post',

View file

@ -19,6 +19,7 @@ describe('filesystem-walker', () => {
await fs.mkdir(path.join(tmpDir, 'src'), { recursive: true });
await fs.mkdir(path.join(tmpDir, 'src', 'components'), { recursive: true });
await fs.mkdir(path.join(tmpDir, 'src', 'deps', 'sample', 'lib'), { recursive: true });
await fs.mkdir(path.join(tmpDir, 'deps', 'sample', 'lib'), { recursive: true });
await fs.mkdir(path.join(tmpDir, 'node_modules', 'lodash'), { recursive: true });
await fs.mkdir(path.join(tmpDir, '.git'), { recursive: true });
@ -32,6 +33,11 @@ describe('filesystem-walker', () => {
path.join(tmpDir, 'src', 'deps', 'sample', 'lib', 'sample.ex'),
'defmodule Sample do\nend\n',
);
await fs.writeFile(
path.join(tmpDir, 'deps', 'sample', 'lib', 'sample.ex'),
'defmodule Dependency do\nend\n',
);
await fs.writeFile(path.join(tmpDir, 'mix.exs'), 'defmodule Fixture.MixProject do\nend\n');
await fs.writeFile(
path.join(tmpDir, 'node_modules', 'lodash', 'index.js'),
'module.exports = {}',
@ -72,10 +78,11 @@ describe('filesystem-walker', () => {
expect(paths.every((p) => !p.includes('node_modules'))).toBe(true);
});
it('skips nested Mix dependency directories', async () => {
it('skips root Mix dependencies without excluding nested first-party deps directories', async () => {
const files = await walkRepositoryPaths(tmpDir);
const paths = files.map((f) => f.path.replace(/\\/g, '/'));
expect(paths.every((p) => !p.includes('/deps/') && !p.startsWith('deps/'))).toBe(true);
expect(paths).not.toContain('deps/sample/lib/sample.ex');
expect(paths).toContain('src/deps/sample/lib/sample.ex');
});
it('skips .git directory', async () => {

View file

@ -1010,7 +1010,7 @@ describe('Tree-sitter multi-language parsing', () => {
});
});
describe('Elixir', () => {
describe.skipIf(!isLanguageAvailable(SupportedLanguages.Elixir))('Elixir', () => {
const elixirQueries = () => getProvider(SupportedLanguages.Elixir).treeSitterQueries;
async function loadElixir(): Promise<void> {

View file

@ -114,12 +114,20 @@ describe('Elixir CFG visitor', () => {
expect(read).toBeGreaterThanOrEqual(0);
expect(nested[wrapper]!.parent).toEqual([evalSite, 0]);
expect(nested[read]!.parent).toEqual([wrapper, 0]);
const piped = cfg.blocks.find((block) => block.text.includes('|> Code.eval_string'))!
.statements![0]!.sites!;
const pipedRead = piped.findIndex((site) => site.kind === 'member-read');
const pipedEval = piped.findIndex(
(site) => site.kind === 'call' && site.callee === 'Code.eval_string',
);
expect(pipedRead).toBeGreaterThanOrEqual(0);
expect(piped[pipedRead]!.parent).toEqual([pipedEval, 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);
expect(findings).toHaveLength(3);
});
it('uses source-range parents for repeated callees and shifted pipe arguments', async () => {

View file

@ -61,6 +61,23 @@ describe('Elixir semantic graph', () => {
expect(graph.nodes.map((node) => node.properties.qualifiedName)).not.toContain(name);
});
it('does not classify a module as an interface from a quoted callback', async () => {
await loadLanguage(SupportedLanguages.Elixir, 'quoted-callback.ex');
const parser = new Parser();
parser.setLanguage(
getLanguageGrammar(SupportedLanguages.Elixir) as Parameters<Parser['setLanguage']>[0],
);
const graph = extractElixirSemanticGraph(
parser.parse(`defmodule M do
quote do
@callback run(term()) :: term()
end
end`),
'lib/quoted_callback.ex',
);
expect(graph.nodes.find((node) => node.properties.qualifiedName === 'M')?.label).toBe('Class');
});
it('emits canonical zero-arity ordinary and guarded declarations', async () => {
await loadLanguage(SupportedLanguages.Elixir, 'zero-arity.ex');
const parser = new Parser();
@ -245,12 +262,10 @@ describe('Elixir semantic graph', () => {
expect.objectContaining({ elixirKind: 'genserver-call', target: 'Worker' }),
]),
);
expect(evidence).not.toEqual(
expect.arrayContaining([
expect.objectContaining({ elixirKind: 'supervisor-child', target: 'dynamic_child' }),
expect.objectContaining({ elixirKind: 'genserver-cast' }),
]),
expect(evidence).not.toContainEqual(
expect.objectContaining({ elixirKind: 'supervisor-child', target: 'dynamic_child' }),
);
expect(evidence).not.toContainEqual(expect.objectContaining({ elixirKind: 'genserver-cast' }));
expect(graph.relationships.filter((edge) => edge.type === 'DECLARES')).toHaveLength(4);
});
});

View file

@ -16,6 +16,7 @@ import {
STRUCTURAL_LABELS,
} from '../../src/core/embeddings/types.js';
import { STALE_HASH_SENTINEL } from '../../src/core/lbug/schema.js';
import { extractStructuralNames } from '../../src/core/embeddings/structural-extractor.js';
const CLASS_CHUNK_SIZE = 90;
const CLASS_OVERLAP = 10;
@ -32,6 +33,15 @@ describe('embedding schema column contracts', () => {
expect(EMBEDDABLE_LABELS).toContain(LABEL_PROTOCOL);
expect(EMBEDDABLE_LABELS).toContain(LABEL_CATEGORY);
});
it('extracts method names from an Elixir defmodule snippet', async () => {
await expect(
extractStructuralNames(
'defmodule Example do\n def run(value), do: value\nend',
'example.ex',
),
).resolves.toMatchObject({ methodNames: ['run'] });
});
});
// ────────────────────────────────────────────────────────────────────────────

View file

@ -300,8 +300,7 @@ describe('PARSE_CACHE_VERSION', () => {
for (const taken of [
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108,
109,
104, 105, 106, 107, 108, 109,
]) {
expect(Number(PARSE_CACHE_VERSION.split('+', 1)[0])).not.toBe(taken);
}

View file

@ -56,12 +56,16 @@ describe('Elixir scope captures', () => {
.filter((def) => def.qualifiedName === 'Actual.Name.same')
.map((def) => def.type),
).toEqual(expect.arrayContaining(['Function', 'Macro']));
expect(parsed?.parsedImports).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: 'wildcard', targetRaw: 'Filtered' }),
]),
);
expect(parsed?.parsedImports.some((imp) => imp.targetRaw === 'Filtered')).toBe(false);
expect(parsed?.parsedImports.some((imp) => imp.targetRaw === 'Helpers')).toBe(false);
expect(
elixirProvider.collectCaptureSideChannel?.('lib/filename_does_not_define_the_module.ex'),
).toMatchObject({
kind: 'elixir',
importExcepts: [
expect.objectContaining({ target: 'Filtered', excluded: [{ name: 'hidden', arity: 1 }] }),
],
});
const calls = parsed?.referenceSites ?? [];
expect(calls.filter((site) => site.name === 'allowed').map((site) => site.arity)).toEqual([1]);
expect(
@ -90,6 +94,21 @@ describe('Elixir scope captures', () => {
});
});
it('records a `from p in Schema` query fact without relying on Repo calls', async () => {
await loadLanguage(SupportedLanguages.Elixir, 'from-query.ex');
extractParsedFile(
elixirProvider,
'defmodule Queries do\n def all, do: from p in MyApp.Post\nend',
'lib/from_query.ex',
);
expect(elixirProvider.collectCaptureSideChannel?.('lib/from_query.ex')).toMatchObject({
kind: 'elixir',
frameworkFacts: [
expect.objectContaining({ kind: 'query', method: 'from', model: 'MyApp.Post' }),
],
});
});
it('has identical cold and cached-tree ParsedFile output', async () => {
await loadLanguage(SupportedLanguages.Elixir, 'cached.ex');
const source =

View file

@ -65,10 +65,10 @@ describe('Elixir import except augmentation', () => {
const importer = {
filePath: 'importer.ex',
moduleScope: importerScope,
parsedImports: [{ kind: 'wildcard', targetRaw: 'Filtered', declaredAtScope: importerScope }],
parsedImports: [],
localDefs: [],
referenceSites: [],
scopes: [],
scopes: [{ id: importerScope, range: { startLine: 1, startCol: 0, endLine: 9, endCol: 0 } }],
captureSideChannel: {
kind: 'elixir',
importExcepts: [
@ -92,6 +92,7 @@ describe('Elixir import except augmentation', () => {
callable('visible', 1),
callable('hidden', 1),
callable('hidden', 2),
{ ...callable('nested', 0), qualifiedName: 'Filtered.Nested.nested' },
callable('private', 0, false),
],
} as unknown as ParsedFile;
@ -110,6 +111,7 @@ describe('Elixir import except augmentation', () => {
.get('hidden')
.map((binding: { def: SymbolDefinition }) => binding.def.parameterCount),
).toEqual([2]);
expect(augmentations.get(importerScope)!.has('nested')).toBe(false);
});
it('filters category-only imports by exported Function or Macro kind', () => {