Skip to content

Commit 30633b4

Browse files
committed
fix(registry): stop external package imports binding to project homonyms
A bare call whose name the calling file imports from a package outside the indexed tree was bound to whatever project symbol shared the simple name. An `import { eq, sql } from "drizzle-orm"` plus an unrelated local module exporting `eq`/`sql` produced two CALLS edges from the caller into that module, strategy unique_name — the same defect on the sequential and the fused-parallel resolver. The TS-LSP is not involved: it declines the external names, and the textual registry fallback fires anyway. The package materializes no node, so resolve_import_node returns NULL, no IMPORTS edge is written, and the per-file import map carries no key for the name. Strategies 1-2 miss and resolve_name_lookup binds the call by simple name. Confidence does not separate the cases: the fabricated edge lands at 0.75 whenever the file also imports anything from the target's module. Add cbm_suppress_external_import_shadow(), a pure predicate beside the existing perl/tsjs/cross-language guards and called at the same two emit sites. It drops the edge only when the callee is a bare identifier, the strategy is a project-wide guess (suffix_match / unique_name / field_type_hint / fuzzy), the file imports that exact local name from a non-relative specifier, and no import-map key binds it. Relative specifiers are excluded on purpose: they name a path inside the tree, so a missing IMPORTS edge there is an in-project resolution gap and the fallback can still be right. A name imported both relatively and from a package keeps its edge, independently of extraction order. Addresses the external-import sub-case of #1355. Signed-off-by: Yyunozor <yyunozor@icloud.com>
1 parent 34d18ae commit 30633b4

6 files changed

Lines changed: 330 additions & 3 deletions

File tree

‎src/pipeline/pass_calls.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ static const cbm_gbuf_node_t *calls_find_source(cbm_pipeline_ctx_t *ctx, const c
476476

477477
/* Resolve one call and emit the appropriate edge. Returns 1 if resolved, 0 if not. */
478478
static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
479-
const CBMResolvedCallArray *lsp_calls, const char *rel,
479+
const CBMResolvedCallArray *lsp_calls,
480+
const CBMImportArray *file_imports, const char *rel,
480481
const char *module_qn, const char **imp_keys, const char **imp_vals,
481482
int imp_count, CBMLanguage lang) {
482483
const cbm_gbuf_node_t *source_node = calls_find_source(ctx, rel, call->enclosing_func_qn);
@@ -651,6 +652,15 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
651652
if (cbm_suppress_cross_language_suffix_match(lang, target_node->file_path, res.strategy)) {
652653
return 0;
653654
}
655+
/* #1355: `import { eq } from "drizzle-orm"` binds `eq` to a package that is
656+
* not in the indexed tree, so a project-wide same-name guess must not turn
657+
* `eq(...)` into a CALLS edge to an unrelated project `eq`. Placed with the
658+
* #725 guard, after the service-pattern bypasses above, so no HTTP/route
659+
* edge can be lost to it. */
660+
if (cbm_suppress_external_import_shadow(call->callee_name, res.strategy, file_imports, imp_keys,
661+
imp_count)) {
662+
return 0;
663+
}
654664
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys, imp_vals,
655665
imp_count, tsjs_drop_plain_call);
656666
return SKIP_ONE;
@@ -810,8 +820,8 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
810820
continue;
811821
}
812822
total_calls++;
813-
if (resolve_single_call(ctx, call, &result->resolved_calls, rel, module_qn, imp_keys,
814-
imp_vals, imp_count, files[i].language)) {
823+
if (resolve_single_call(ctx, call, &result->resolved_calls, &result->imports, rel,
824+
module_qn, imp_keys, imp_vals, imp_count, files[i].language)) {
815825
resolved++;
816826
} else {
817827
unresolved++;

‎src/pipeline/pass_parallel.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,14 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
25402540
* CALLS edge across a language boundary. */
25412541
continue;
25422542
}
2543+
if (target_node && source_node->id != target_node->id &&
2544+
cbm_suppress_external_import_shadow(call->callee_name, res.strategy, &result->imports,
2545+
imp_keys, imp_count)) {
2546+
/* #1355: same guard as pass_calls.c — a bare call bound by an
2547+
* external package import must not become a CALLS edge to an
2548+
* unrelated project symbol of the same name. */
2549+
continue;
2550+
}
25432551
if (!target_node || source_node->id == target_node->id) {
25442552
/* HTTP/ASYNC calls to an EXTERNAL client library (`requests.get(url)`)
25452553
* resolve to an unindexed QN (target_node == NULL), but their edge

‎src/pipeline/pipeline.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ bool cbm_perl_suppress_generic_match(bool is_perl, bool is_method, const char *c
275275
* Pure; unit-tested in test_registry.c. */
276276
bool cbm_tsjs_suppress_weak_method_match(bool is_tsjs, bool is_method, const char *strategy);
277277

278+
/* #1355: drop a project-wide same-name guess (suffix_match / unique_name /
279+
* field_type_hint / fuzzy) for a BARE call whose name the calling file binds to
280+
* a NON-RELATIVE (package) import that resolved to nothing in the graph —
281+
* `import { eq } from "drizzle-orm"` must not make `eq(...)` a CALLS edge to an
282+
* unrelated project `eq`. A name the import map does bind, a relative
283+
* specifier, a member/qualified callee, and every import-/receiver-aware
284+
* strategy are all kept. Pure; unit-tested in test_registry.c. */
285+
bool cbm_suppress_external_import_shadow(const char *callee_name, const char *strategy,
286+
const CBMImportArray *file_imports,
287+
const char **import_map_keys, int import_map_count);
288+
278289
/* #725: drop a suffix_match CALLS edge when the caller language and the
279290
* target file's language disagree. unique_name (candidates == 1) is #1572
280291
* and is left alone; same_module / import_map / lsp_* are kept. JS/TS/TSX

‎src/pipeline/registry.c‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,88 @@ bool cbm_tsjs_suppress_weak_method_match(bool is_tsjs, bool is_method, const cha
456456
strcmp(strategy, "field_type_hint") == 0 || strcmp(strategy, "fuzzy") == 0;
457457
}
458458

459+
/* A module specifier that names a path inside the indexed tree ("./x", "../x",
460+
* "/abs/x") rather than an external package. Package specifiers are everything
461+
* else — "drizzle-orm", "rxjs/operators", "@scope/pkg". */
462+
static bool specifier_is_relative(const char *module_path) {
463+
return module_path && (module_path[0] == '.' || module_path[0] == '/');
464+
}
465+
466+
static bool import_map_binds(const char **import_map_keys, int import_map_count,
467+
const char *local_name) {
468+
if (!import_map_keys || import_map_count <= 0) {
469+
return false;
470+
}
471+
for (int i = 0; i < import_map_count; i++) {
472+
if (import_map_keys[i] && strcmp(import_map_keys[i], local_name) == 0) {
473+
return true;
474+
}
475+
}
476+
return false;
477+
}
478+
479+
/* #1355: a bare call whose name the calling file binds to an EXTERNAL package
480+
* import must not fall back to a project-wide same-name guess.
481+
*
482+
* `import { eq } from "drizzle-orm"` binds `eq` in this file's scope. When the
483+
* package is not part of the indexed tree it materializes no IMPORTS edge, so
484+
* the import map has no entry, strategies 1-2 miss, and strategy 3/4 attach
485+
* `eq(users.id, id)` to whatever project symbol happens to share the simple
486+
* name — a CALLS edge into an unrelated local helper. The explicit import
487+
* statement is positive evidence, taken from the caller's own source, that the
488+
* identifier does NOT denote that symbol.
489+
*
490+
* Fires only when ALL of:
491+
* - the callee is a BARE identifier: member (`x.foo()`) and package/namespace
492+
* qualified callees are the receiver-aware guards' business, not this one;
493+
* - the match came from a project-wide guess (suffix_match / unique_name;
494+
* field_type_hint / fuzzy listed for the same defensive reason as the TS/JS
495+
* guard) — every import-, receiver- or module-aware strategy is KEPT;
496+
* - the file imports that exact local name from a NON-RELATIVE specifier;
497+
* - no import-map key binds the name, i.e. that import resolved to nothing in
498+
* the graph. A name the map does bind already had its chance at strategy 1
499+
* and is import-aware by construction.
500+
*
501+
* Relative specifiers are deliberately excluded: they name a path inside the
502+
* indexed tree, so a missing IMPORTS edge is an in-project resolution gap and
503+
* the same-name fallback can still be right. A name imported from both a
504+
* relative and a package specifier keeps the edge — the relative binding wins
505+
* the tie explicitly, so the outcome does not depend on import order.
506+
*
507+
* Pure + side-effect-free so the contract is unit-testable without a pipeline. */
508+
bool cbm_suppress_external_import_shadow(const char *callee_name, const char *strategy,
509+
const CBMImportArray *file_imports,
510+
const char **import_map_keys, int import_map_count) {
511+
if (!callee_name || !callee_name[0] || !strategy || !strategy[0]) {
512+
return false;
513+
}
514+
if (strcmp(strategy, "suffix_match") != 0 && strcmp(strategy, "unique_name") != 0 &&
515+
strcmp(strategy, "field_type_hint") != 0 && strcmp(strategy, "fuzzy") != 0) {
516+
return false;
517+
}
518+
if (strchr(callee_name, '.') != NULL || strstr(callee_name, "::") != NULL) {
519+
return false;
520+
}
521+
if (!file_imports || file_imports->count <= 0) {
522+
return false;
523+
}
524+
if (import_map_binds(import_map_keys, import_map_count, callee_name)) {
525+
return false;
526+
}
527+
bool external_binding = false;
528+
for (int i = 0; i < file_imports->count; i++) {
529+
const CBMImport *imp = &file_imports->items[i];
530+
if (!imp->local_name || !imp->module_path || strcmp(imp->local_name, callee_name) != 0) {
531+
continue;
532+
}
533+
if (specifier_is_relative(imp->module_path)) {
534+
return false; /* an in-tree binding for this name — never suppress */
535+
}
536+
external_binding = true;
537+
}
538+
return external_binding;
539+
}
540+
459541
static bool js_ts_family(CBMLanguage lang) {
460542
return lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX;
461543
}

‎tests/test_pipeline.c‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4649,6 +4649,129 @@ static int count_nodes_named(cbm_store_t *s, const char *project, const char *na
46494649
* (axios.get, api.patch on a renamed-axios instance, supertest request(app).get).
46504650
* The regex false edge must stay suppressed in parallel too. CBM_WORKERS forces
46514651
* >1 worker so the parallel path is taken regardless of the host core count. */
4652+
/* #1355: write the shared external-import-shadow fixture into `dir`.
4653+
* `pad_files` filler modules push the run over MIN_FILES_FOR_PARALLEL so the
4654+
* same tree can be indexed by both resolvers. */
4655+
static void write_external_import_shadow_fixture(const char *dir, int pad_files) {
4656+
/* The lone project symbols named eq/sql — ordinary local helpers. */
4657+
write_temp_file(dir, "src/text-utils.ts",
4658+
"export function eq(a: string, b: string): boolean {\n"
4659+
" return a.trim() === b.trim();\n"
4660+
"}\n"
4661+
"export function sql(chunk: string): string {\n"
4662+
" return chunk.replace(/\\s+/g, ' ');\n"
4663+
"}\n"
4664+
"export function normalize(s: string): string {\n"
4665+
" return s.trim().toLowerCase();\n"
4666+
"}\n");
4667+
/* `eq` and `sql` come from an external package that is not in the tree, so
4668+
* the registry falls through to a project-wide same-name guess. These are
4669+
* the fabricated edges. `normalize` is imported relatively from the SAME
4670+
* file the guess would have picked — it must survive. */
4671+
write_temp_file(dir, "src/queries.ts",
4672+
"import { eq, sql } from 'drizzle-orm';\n"
4673+
"import { normalize } from './text-utils';\n"
4674+
"export function buildQuery(id: string): unknown {\n"
4675+
" return [eq({ id }, id), sql('select 1'), normalize(id)];\n"
4676+
"}\n");
4677+
/* No import of `eq` at all: nothing in the caller's source contradicts the
4678+
* guess, so the pre-existing fallback keeps its edge. */
4679+
write_temp_file(dir, "src/no-import.ts",
4680+
"export function callsWithoutImport(a: string, b: string): boolean {\n"
4681+
" return eq(a, b);\n"
4682+
"}\n");
4683+
for (int i = 0; i < pad_files; i++) {
4684+
char name[64];
4685+
char body[128];
4686+
snprintf(name, sizeof(name), "src/pad_%02d.ts", i);
4687+
snprintf(body, sizeof(body), "export function shadowPad%02d(): number { return %d; }\n", i,
4688+
i);
4689+
write_temp_file(dir, name, body);
4690+
}
4691+
}
4692+
4693+
/* #1355: assert the guard's whole contract against one indexed store. */
4694+
static int assert_external_import_shadow_contract(const char *dir, const char *db_name) {
4695+
char db_path[512];
4696+
snprintf(db_path, sizeof(db_path), "%s/%s", dir, db_name);
4697+
cbm_pipeline_t *p = cbm_pipeline_new(dir, db_path, CBM_MODE_FULL);
4698+
if (!p) {
4699+
return 1;
4700+
}
4701+
if (cbm_pipeline_run(p) != 0) {
4702+
cbm_pipeline_free(p);
4703+
return 2;
4704+
}
4705+
const char *project = cbm_pipeline_project_name(p);
4706+
cbm_store_t *s = cbm_store_open_path(db_path);
4707+
if (!s) {
4708+
cbm_pipeline_free(p);
4709+
return 3;
4710+
}
4711+
int rc = 0;
4712+
/* (1) the reported bug: an externally-imported name must not bind to the
4713+
* local homonym (RED before the fix, on both resolvers). */
4714+
if (cross_file_call_exists(s, project, "buildQuery", "eq")) {
4715+
rc = 4;
4716+
}
4717+
if (rc == 0 && cross_file_call_exists(s, project, "buildQuery", "sql")) {
4718+
rc = 5;
4719+
}
4720+
/* (2) the relative import to the very same file still resolves. */
4721+
if (rc == 0 && !cross_file_call_exists(s, project, "buildQuery", "normalize")) {
4722+
rc = 6;
4723+
}
4724+
/* (3) a bare call the caller never imports keeps its pre-existing edge. */
4725+
if (rc == 0 && !cross_file_call_exists(s, project, "callsWithoutImport", "eq")) {
4726+
rc = 7;
4727+
}
4728+
cbm_store_close(s);
4729+
cbm_pipeline_free(p);
4730+
return rc;
4731+
}
4732+
4733+
TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355) {
4734+
char tmp[256];
4735+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ext_import_shadow_XXXXXX");
4736+
if (!cbm_mkdtemp(tmp)) {
4737+
FAIL("tmpdir");
4738+
}
4739+
/* Enough files that CBM_WORKERS can take the fused-parallel path; the same
4740+
* tree is then indexed by each resolver in turn, because the guard lives at
4741+
* two independent emit sites (pass_calls.c and pass_parallel.c). */
4742+
write_external_import_shadow_fixture(tmp, 50);
4743+
4744+
char *old_workers = getenv("CBM_WORKERS");
4745+
char *saved_workers = old_workers ? strdup(old_workers) : NULL;
4746+
char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
4747+
char *saved_single = old_single ? strdup(old_single) : NULL;
4748+
4749+
cbm_setenv("CBM_INDEX_SINGLE_THREAD", "1", 1);
4750+
int sequential = assert_external_import_shadow_contract(tmp, "shadow-sequential.db");
4751+
4752+
cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
4753+
cbm_setenv("CBM_WORKERS", "4", 1);
4754+
int parallel = assert_external_import_shadow_contract(tmp, "shadow-parallel.db");
4755+
4756+
if (saved_workers) {
4757+
cbm_setenv("CBM_WORKERS", saved_workers, 1);
4758+
free(saved_workers);
4759+
} else {
4760+
cbm_unsetenv("CBM_WORKERS");
4761+
}
4762+
if (saved_single) {
4763+
cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1);
4764+
free(saved_single);
4765+
} else {
4766+
cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
4767+
}
4768+
th_rmtree(tmp);
4769+
4770+
ASSERT_EQ(sequential, 0);
4771+
ASSERT_EQ(parallel, 0);
4772+
PASS();
4773+
}
4774+
46524775
TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges) {
46534776
char tmp[256];
46544777
snprintf(tmp, sizeof(tmp), "/tmp/cbm_tsjs_par_XXXXXX");
@@ -12123,6 +12246,7 @@ SUITE(pipeline) {
1212312246
#endif
1212412247
RUN_TEST(pipeline_tsjs_receiver_suppresses_weak_method_edge);
1212512248
RUN_TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges);
12249+
RUN_TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355);
1212612250
RUN_TEST(pipeline_parallel_python_cross_only_dunder_gets_synthetic_carrier);
1212712251
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1212812252
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);

0 commit comments

Comments
 (0)