Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(ingest): link helper-loaded and dist/ build-path imports to their sources

KageBinary458e1fbbd0f8
diff --git a/core-ingestion/src/__tests__/resolveEdges.scope.test.ts b/core-ingestion/src/__tests__/resolveEdges.scope.test.ts
index 27dfd3a8..6fed647b 100644
--- a/core-ingestion/src/__tests__/resolveEdges.scope.test.ts
+++ b/core-ingestion/src/__tests__/resolveEdges.scope.test.ts
@@ -1,12 +1,17 @@
// Copyright 2026 Ix Infrastructure Inc.
-// Regression tests: JS/TS calls resolved by name alone. Every vitest file's
-// `describe(...)` (a global / an import from 'vitest') got a CALLS edge to an
-// unrelated `SampleCommand.describe` method in test-fixtures.
+// Regression tests for two ingestion defects:
+// 1. JS/TS calls resolved by name alone: every vitest file's `describe(...)`
+// (a global / an import from 'vitest') got a CALLS edge to an unrelated
+// `SampleCommand.describe` method in test-fixtures.
+// 2. Module paths passed to an import helper as string literals — including
+// paths into another package's build output (`dist/x.js` -> `src/x.ts`) —
+// produced no IMPORTS edge.
import { describe, expect, it } from 'vitest';
import { parseFile, resolveEdges, type FileParseResult, type ResolvedEdge } from '../index.js';
+import { buildPatch, buildPatchWithResolution } from '../patch-builder.js';
function parse(filePath: string, source: string): FileParseResult {
const result = parseFile(filePath, source);
@@ -176,3 +181,96 @@ describe('call resolution respects lexical scope (JS/TS)', () => {
]);
});
});
+
+describe('dynamic and helper-literal imports (JS/TS)', () => {
+ const LOADER_PATH = 'ix-cli/src/cli/commands/ingestion-loader.ts';
+ const LOADER_SOURCE = [
+ 'import { dirname, resolve } from "node:path";',
+ 'const importModule = async (specifier: string) => import(specifier);',
+ 'function resolveIngestionModule(relativePath: string): string { return resolve(dirname("."), relativePath); }',
+ 'export async function loadIngestionModules() {',
+ ' const local = await import("./watch-utils.js");',
+ ' return Promise.all([',
+ ' importModule(resolveIngestionModule("../../../../core-ingestion/dist/index.js")),',
+ ' importModule(resolveIngestionModule("../../../../core-ingestion/dist/languages.js")),',
+ ' importModule(resolveIngestionModule("../../../../core-ingestion/dist/missing.js")),',
+ ' local,',
+ ' ]);',
+ '}',
+ '',
+ ].join('\n');
+
+ const files = () => [
+ parse(LOADER_PATH, LOADER_SOURCE),
+ parse('ix-cli/src/cli/commands/watch-utils.ts', 'export function debounce() {}\n'),
+ parse('core-ingestion/src/index.ts', 'export function parseFile() {}\n'),
+ parse('core-ingestion/src/languages.ts', 'export function languageFromPath() {}\n'),
+ ];
+
+ const importsFrom = (edges: ResolvedEdge[], src: string) =>
+ edges.filter(e => e.predicate === 'IMPORTS' && e.srcFilePath === src).map(e => e.dstFilePath).sort();
+
+ it('resolves `await import("./x.js")` and helper literals into dist/ to their src/ sources', () => {
+ expect(importsFrom(resolveEdges(files()), LOADER_PATH)).toEqual([
+ 'core-ingestion/src/index.ts',
+ 'core-ingestion/src/languages.ts',
+ 'ix-cli/src/cli/commands/watch-utils.ts',
+ ]);
+ });
+
+ it('maps .mjs -> .mts and .cjs -> .cts build paths', () => {
+ const loader = parse(
+ 'tools/load.ts',
+ 'export const a = import("../lib/dist/esm.mjs");\nexport const b = loadModule("../lib/dist/cjs.cjs");\n',
+ );
+ const mts = { ...parse('lib/src/esm.ts', 'export const x = 1;\n'), filePath: 'lib/src/esm.mts' };
+ const cts = { ...parse('lib/src/cjs.ts', 'export const y = 1;\n'), filePath: 'lib/src/cjs.cts' };
+
+ expect(importsFrom(resolveEdges([loader, mts, cts]), 'tools/load.ts')).toEqual([
+ 'lib/src/cjs.cts',
+ 'lib/src/esm.mts',
+ ]);
+ });
+
+ it('prefers a tracked dist/ file over the src/ mapping', () => {
+ const loader = parse('app/load.ts', 'export const m = import("../pkg/dist/x.js");\n');
+ const dist = parse('pkg/dist/x.js', 'export const x = 1;\n');
+ const src = parse('pkg/src/x.ts', 'export const x = 1;\n');
+
+ expect(importsFrom(resolveEdges([loader, dist, src]), 'app/load.ts')).toEqual(['pkg/dist/x.js']);
+ });
+
+ it('ignores string literals passed to non-import calls', () => {
+ const reader = parse(
+ 'src/reader.ts',
+ [
+ 'import { readFileSync } from "node:fs";',
+ 'export function read() { return readFileSync("./other.ts", "utf8") + describeModule; }',
+ 'const describeModule = String("./other.ts");',
+ '',
+ ].join('\n'),
+ );
+ const other = parse('src/other.ts', 'export const x = 1;\n');
+
+ expect(reader.relationships.filter(r => r.importVia === 'helper')).toEqual([]);
+ expect(importsFrom(resolveEdges([reader, other]), 'src/reader.ts')).toEqual([]);
+ });
+
+ it('emits a patch edge only for helper imports that resolved', () => {
+ const [loader, ...rest] = files();
+ const resolved = resolveEdges([loader, ...rest]);
+ const patch = buildPatchWithResolution(loader, 'hash', 'ws', resolved);
+ const importEdges = patch.ops.filter(op => op.type === 'UpsertEdge' && op.predicate === 'IMPORTS');
+
+ // node:path + watch-utils.js (real imports) + index.js + languages.js
+ // (resolved helper literals); the unresolvable missing.js is dropped.
+ expect(importEdges).toHaveLength(4);
+ const claims = patch.ops.filter(op => op.type === 'AssertClaim' && String(op.field).startsWith('imports:'));
+ expect(claims.map(op => op.value).sort()).toEqual(['index.js', 'languages.js', 'node:path', 'watch-utils.js']);
+
+ // Without resolution a helper literal is only a guess: no edge at all.
+ const unresolved = buildPatch(loader, 'hash', 'ws');
+ const guessed = unresolved.ops.filter(op => op.type === 'UpsertEdge' && op.predicate === 'IMPORTS');
+ expect(guessed).toHaveLength(2); // node:path + the real `import("./watch-utils.js")`
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index e4bc4bb0..59cbf52e 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -222,6 +222,13 @@ export interface ParsedRelationship {
* file. Absent means "at least one member-shaped use, or unknown".
*/
bareCall?: true;
+ /**
+ * JS/TS IMPORTS only: `'helper'` marks a module path that was not written in
+ * an import statement but passed as a string literal to an import helper
+ * (`importModule(resolveModule("../../pkg/dist/x.js"))`). It is inferred, so
+ * it becomes an edge only when it resolves to exactly one tracked file.
+ */
+ importVia?: 'helper';
}
/**
@@ -549,6 +556,42 @@ function unwrapImportSpecifier(rawValue: string): string {
.replace(/[>"'`]$/, '');
}
+// Callee text that marks a call as an import helper: `importModule(...)`,
+// `resolveIngestionModule(...)`, `require.resolve(...)`, `lazyImport(...)`.
+const JS_IMPORT_HELPER_CALLEE = /import|require|module/i;
+// Module paths a JS/TS runtime can load: the source and build extensions.
+const JS_MODULE_PATH_EXTENSION = /\.(?:[cm]?[jt]s|[jt]sx)$/;
+
+/**
+ * The module path a JS/TS call passes to an import helper, or null.
+ *
+ * A dynamic `import("./x.js")` is already an IMPORTS edge (see the query), but
+ * loaders often route the specifier through a helper first, e.g.
+ * `importModule(resolveIngestionModule("../../../../core-ingestion/dist/languages.js"))`.
+ * Deliberately conservative, because this infers an import from a string:
+ * - the callee's text must name importing (`JS_IMPORT_HELPER_CALLEE`),
+ * - the FIRST argument must be a plain string literal (no substitutions),
+ * - relative (`./`, `../`) and ending in a JS/TS module extension.
+ * `readFileSync("./x.ts")` and `path.join(dir, "x.js")` are never matched. The
+ * resolver then keeps the edge only when the path names exactly one tracked
+ * file (mapping a build path such as `dist/x.js` back to `src/x.ts`).
+ */
+function jsTsHelperImportSpecifier(callNode: any): string | null {
+ if (callNode?.type !== 'call_expression') return null;
+ const fn = callNode.childForFieldName?.('function');
+ if (!fn || (fn.type !== 'identifier' && fn.type !== 'member_expression')) return null;
+ if (!JS_IMPORT_HELPER_CALLEE.test(fn.text)) return null;
+ const firstArg = callNode.childForFieldName?.('arguments')?.namedChildren?.[0];
+ if (!firstArg) return null;
+ if (firstArg.type !== 'string' &&
+ !(firstArg.type === 'template_string' && !firstArg.namedChildren.some((c: any) => c.type === 'template_substitution'))) {
+ return null;
+ }
+ const spec = unwrapImportSpecifier(firstArg.text);
+ if (!spec.startsWith('./') && !spec.startsWith('../')) return null;
+ return JS_MODULE_PATH_EXTENSION.test(spec) ? spec : null;
+}
+
/**
* Walk a JS/TS `import_statement` node and return its named/default bindings.
* Namespace imports (`* as ns`) are intentionally skipped: a call like `ns.foo()`
@@ -2505,6 +2548,8 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
// that CALLS relationship is a plain identifier call (see ParsedRelationship.bareCall).
const jsTsCallBareOnly = new Map<string, boolean>();
const jsTsCallRelationship = new Map<string, ParsedRelationship>();
+ // JS/TS: helper-literal module paths already emitted (see importVia).
+ const jsTsHelperImports = new Set<string>();
if (isJsTs) {
for (const match of pass2Matches) {
const importSource = match.captures.find((c: any) => c.name === 'import.source');
@@ -2971,6 +3016,20 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
const callName = match.captures.find((c: any) => c.name === 'call.name');
if (callName) {
const callee = callName.node.text;
+ // JavaScript `require("./x")` is already an IMPORTS edge via its own query.
+ if (isJsTs && !(language === SupportedLanguages.JavaScript && callee === 'require')) {
+ const helperImport = jsTsHelperImportSpecifier(match.captures.find((c: any) => c.name === 'call')?.node);
+ if (helperImport && !jsTsHelperImports.has(helperImport)) {
+ jsTsHelperImports.add(helperImport);
+ relationships.push({
+ srcName: fileName,
+ dstName: normalizeCapturedImport(helperImport, language),
+ predicate: 'IMPORTS',
+ importRaw: helperImport,
+ importVia: 'helper',
+ });
+ }
+ }
if (!callee || callee.length <= 1) continue;
const phpCallKind = language === SupportedLanguages.PHP
? callName.node.parent?.type === 'object_creation_expression'
@@ -4348,9 +4407,33 @@ export function resolveEdges(
? ['.jsx', '.tsx', '.d.ts']
: ['.tsx', '.d.ts', '.jsx'];
for (const suffix of suffixes) candidatePaths.push(`${base}${suffix}`);
+ } else if (extension === '.mjs' || extension === '.cjs') {
+ // `.mts` compiles to `.mjs` and `.cts` to `.cjs`, as `.ts` does to `.js`.
+ const sourceExt = extension === '.mjs' ? '.mts' : '.cts';
+ const suffixes = srcLanguage === SupportedLanguages.JavaScript
+ ? [extension, sourceExt]
+ : [sourceExt, extension];
+ for (const suffix of suffixes) candidatePaths.push(`${base}${suffix}`);
} else {
candidatePaths.push(target);
}
+ // A path into build output (`../pkg/dist/x.js`, typical of a dynamic
+ // import of a sibling package's compiled module) names a file that is
+ // not tracked; its source is the same path under `src/`. Tried only
+ // after every direct candidate, so a tracked `dist/` file still wins.
+ // Mirrors `sourceForms` in ix-cli's explain/text-references.ts, but uses
+ // the dist segment nearest the file: that is the package's outDir.
+ const lastDist = target.lastIndexOf('/dist/');
+ const distAt = lastDist !== -1 ? lastDist + 1 : target.startsWith('dist/') ? 0 : -1;
+ if (distAt !== -1) {
+ const distPrefix = `${target.slice(0, distAt)}dist/`;
+ const srcPrefix = `${target.slice(0, distAt)}src/`;
+ for (const candidatePath of [...candidatePaths]) {
+ if (candidatePath.startsWith(distPrefix)) {
+ candidatePaths.push(srcPrefix + candidatePath.slice(distPrefix.length));
+ }
+ }
+ }
}
for (const candidatePath of candidatePaths) {
@@ -4577,6 +4660,9 @@ export function resolveEdges(
for (const entry of phpType.entries) importedFilePaths.add(entry.filePath);
continue;
}
+ // A helper-literal import is inferred and binds no names: it becomes an
+ // IMPORTS edge below but does not widen call-resolution scope.
+ if (rel.importVia === 'helper') continue;
for (const fp of resolveImportTargets(srcFilePath, srcLanguage, rel.dstName, rel.importRaw)) {
importedFilePaths.add(fp);
}
@@ -4589,7 +4675,7 @@ export function resolveEdges(
const fpResult = resultsByPath.get(fp);
if (!fpResult) continue;
for (const rel of fpResult.relationships) {
- if (rel.predicate !== 'IMPORTS') continue;
+ if (rel.predicate !== 'IMPORTS' || rel.importVia === 'helper') continue;
for (const transitiveFp of resolveImportTargets(fp, fpResult.language, rel.dstName, rel.importRaw)) {
if (!importedFilePaths.has(transitiveFp)) transitiveFilePaths.add(transitiveFp);
}
@@ -4651,6 +4737,13 @@ export function resolveEdges(
const importMatches = pythonHasRelativeModuleImport && rel.importRaw === undefined
? []
: resolveImportTargets(srcFilePath, result.language, rel.dstName, rel.importRaw);
+ // A helper-literal import (`importModule(resolve("../x/dist/y.js"))`) is
+ // inferred from a string, so it is kept only when that path names
+ // exactly one tracked file — never through the package fallbacks below.
+ if (rel.importVia === 'helper' && importMatches.length !== 1) {
+ if (importMatches.length > 1) stats.skippedAmbiguous++;
+ continue;
+ }
if (importMatches.length === 1) {
const fp = importMatches[0];
resolved.push({
diff --git a/core-ingestion/src/patch-builder.ts b/core-ingestion/src/patch-builder.ts
index 892677c0..ad7bee39 100644
--- a/core-ingestion/src/patch-builder.ts
+++ b/core-ingestion/src/patch-builder.ts
@@ -181,7 +181,10 @@ export function buildPatch(
previousSourceHash?: string,
multiRepo?: MultiRepoContext,
): GraphPatchPayload {
- const { entities, chunks, relationships } = result;
+ const { entities, chunks } = result;
+ // A helper-literal import is inferred and only means something once resolved
+ // to a tracked file; without resolution it would be a dangling edge.
+ const relationships = result.relationships.filter(r => r.importVia !== 'helper');
// Two paths, deliberately distinct (see toMemberRelativePath):
// - filePath (workspace-relative, repo-prefixed in a co-ingest) is PROVENANCE:
// source_uri / file_uri / patch id. Reads reconstruct an absolute path by
@@ -496,7 +499,11 @@ export function buildPatchWithResolution(
});
}
- const { entities, chunks, relationships } = result;
+ const { entities, chunks } = result;
+ // A helper-literal import (see ParsedRelationship.importVia) is inferred from a
+ // string, so it is emitted only when resolveEdges tied it to exactly one file.
+ const relationships = result.relationships.filter(r =>
+ r.importVia !== 'helper' || edgeResolution.has(`${r.srcName}:${r.predicate}:${r.dstName}`));
// filePath = workspace-relative PROVENANCE (source_uri / file_uri / patch id);
// idPath = member-relative IDENTITY (node / edge / chunk ids) so a member's ids are
// byte-identical solo vs. co-ingested. Edge matching still keys on the full