Skip to content

Commit eaf5904

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 DeusData#1355. Signed-off-by: Yyunozor <yyunozor@icloud.com>
1 parent 2f9828d commit eaf5904

6 files changed

Lines changed: 326 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);
@@ -668,6 +669,15 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
668669
if (cbm_suppress_cross_language_suffix_match(lang, target_node->file_path, res.strategy)) {
669670
return 0;
670671
}
672+
/* #1355: `import { eq } from "drizzle-orm"` binds `eq` to a package that is
673+
* not in the indexed tree, so a project-wide same-name guess must not turn
674+
* `eq(...)` into a CALLS edge to an unrelated project `eq`. Placed with the
675+
* #725 guard, after the service-pattern bypasses above, so no HTTP/route
676+
* edge can be lost to it. */
677+
if (cbm_suppress_external_import_shadow(call->callee_name, res.strategy, file_imports, imp_keys,
678+
imp_count)) {
679+
return 0;
680+
}
671681
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys, imp_vals,
672682
imp_count, drop_plain_call);
673683
return SKIP_ONE;
@@ -827,8 +837,8 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
827837
continue;
828838
}
829839
total_calls++;
830-
if (resolve_single_call(ctx, call, &result->resolved_calls, rel, module_qn, imp_keys,
831-
imp_vals, imp_count, files[i].language)) {
840+
if (resolve_single_call(ctx, call, &result->resolved_calls, &result->imports, rel,
841+
module_qn, imp_keys, imp_vals, imp_count, files[i].language)) {
832842
resolved++;
833843
} else {
834844
unresolved++;

‎src/pipeline/pass_parallel.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,6 +2569,14 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
25692569
* CALLS edge across a language boundary. */
25702570
continue;
25712571
}
2572+
if (target_node && source_node->id != target_node->id &&
2573+
cbm_suppress_external_import_shadow(call->callee_name, res.strategy, &result->imports,
2574+
imp_keys, imp_count)) {
2575+
/* #1355: same guard as pass_calls.c — a bare call bound by an
2576+
* external package import must not become a CALLS edge to an
2577+
* unrelated project symbol of the same name. */
2578+
continue;
2579+
}
25722580
if (!target_node || source_node->id == target_node->id) {
25732581
/* HTTP/ASYNC calls to an EXTERNAL client library (`requests.get(url)`)
25742582
* resolve to an unindexed QN (target_node == NULL), but their edge

‎src/pipeline/pipeline.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ bool cbm_suppress_weak_member_match(bool enabled, bool is_method, const char *st
291291
* Pure; unit-tested in test_registry.c. */
292292
bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_bound,
293293
const char *strategy);
294+
/* #1355: drop a project-wide same-name guess (suffix_match / unique_name /
295+
* field_type_hint / fuzzy) for a BARE call whose name the calling file binds to
296+
* a NON-RELATIVE (package) import that resolved to nothing in the graph —
297+
* `import { eq } from "drizzle-orm"` must not make `eq(...)` a CALLS edge to an
298+
* unrelated project `eq`. A name the import map does bind, a relative
299+
* specifier, a member/qualified callee, and every import-/receiver-aware
300+
* strategy are all kept. Pure; unit-tested in test_registry.c. */
301+
bool cbm_suppress_external_import_shadow(const char *callee_name, const char *strategy,
302+
const CBMImportArray *file_imports,
303+
const char **import_map_keys, int import_map_count);
294304

295305
/* #725: drop a suffix_match CALLS edge when the caller language and the
296306
* target file's language disagree. unique_name (candidates == 1) is #1572

‎src/pipeline/registry.c‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,88 @@ bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_b
501501
return weak_short_name_strategy(strategy);
502502
}
503503

504+
/* A module specifier that names a path inside the indexed tree ("./x", "../x",
505+
* "/abs/x") rather than an external package. Package specifiers are everything
506+
* else — "drizzle-orm", "rxjs/operators", "@scope/pkg". */
507+
static bool specifier_is_relative(const char *module_path) {
508+
return module_path && (module_path[0] == '.' || module_path[0] == '/');
509+
}
510+
511+
static bool import_map_binds(const char **import_map_keys, int import_map_count,
512+
const char *local_name) {
513+
if (!import_map_keys || import_map_count <= 0) {
514+
return false;
515+
}
516+
for (int i = 0; i < import_map_count; i++) {
517+
if (import_map_keys[i] && strcmp(import_map_keys[i], local_name) == 0) {
518+
return true;
519+
}
520+
}
521+
return false;
522+
}
523+
524+
/* #1355: a bare call whose name the calling file binds to an EXTERNAL package
525+
* import must not fall back to a project-wide same-name guess.
526+
*
527+
* `import { eq } from "drizzle-orm"` binds `eq` in this file's scope. When the
528+
* package is not part of the indexed tree it materializes no IMPORTS edge, so
529+
* the import map has no entry, strategies 1-2 miss, and strategy 3/4 attach
530+
* `eq(users.id, id)` to whatever project symbol happens to share the simple
531+
* name — a CALLS edge into an unrelated local helper. The explicit import
532+
* statement is positive evidence, taken from the caller's own source, that the
533+
* identifier does NOT denote that symbol.
534+
*
535+
* Fires only when ALL of:
536+
* - the callee is a BARE identifier: member (`x.foo()`) and package/namespace
537+
* qualified callees are the receiver-aware guards' business, not this one;
538+
* - the match came from a project-wide guess (suffix_match / unique_name;
539+
* field_type_hint / fuzzy listed for the same defensive reason as the TS/JS
540+
* guard) — every import-, receiver- or module-aware strategy is KEPT;
541+
* - the file imports that exact local name from a NON-RELATIVE specifier;
542+
* - no import-map key binds the name, i.e. that import resolved to nothing in
543+
* the graph. A name the map does bind already had its chance at strategy 1
544+
* and is import-aware by construction.
545+
*
546+
* Relative specifiers are deliberately excluded: they name a path inside the
547+
* indexed tree, so a missing IMPORTS edge is an in-project resolution gap and
548+
* the same-name fallback can still be right. A name imported from both a
549+
* relative and a package specifier keeps the edge — the relative binding wins
550+
* the tie explicitly, so the outcome does not depend on import order.
551+
*
552+
* Pure + side-effect-free so the contract is unit-testable without a pipeline. */
553+
bool cbm_suppress_external_import_shadow(const char *callee_name, const char *strategy,
554+
const CBMImportArray *file_imports,
555+
const char **import_map_keys, int import_map_count) {
556+
if (!callee_name || !callee_name[0] || !strategy || !strategy[0]) {
557+
return false;
558+
}
559+
if (strcmp(strategy, "suffix_match") != 0 && strcmp(strategy, "unique_name") != 0 &&
560+
strcmp(strategy, "field_type_hint") != 0 && strcmp(strategy, "fuzzy") != 0) {
561+
return false;
562+
}
563+
if (strchr(callee_name, '.') != NULL || strstr(callee_name, "::") != NULL) {
564+
return false;
565+
}
566+
if (!file_imports || file_imports->count <= 0) {
567+
return false;
568+
}
569+
if (import_map_binds(import_map_keys, import_map_count, callee_name)) {
570+
return false;
571+
}
572+
bool external_binding = false;
573+
for (int i = 0; i < file_imports->count; i++) {
574+
const CBMImport *imp = &file_imports->items[i];
575+
if (!imp->local_name || !imp->module_path || strcmp(imp->local_name, callee_name) != 0) {
576+
continue;
577+
}
578+
if (specifier_is_relative(imp->module_path)) {
579+
return false; /* an in-tree binding for this name — never suppress */
580+
}
581+
external_binding = true;
582+
}
583+
return external_binding;
584+
}
585+
504586
static bool js_ts_family(CBMLanguage lang) {
505587
return lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX ||
506588
lang == CBM_LANG_ARKTS;

‎tests/test_pipeline.c‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4976,6 +4976,129 @@ static int count_nodes_named(cbm_store_t *s, const char *project, const char *na
49764976
* (axios.get, api.patch on a renamed-axios instance, supertest request(app).get).
49774977
* The regex false edge must stay suppressed in parallel too. CBM_WORKERS forces
49784978
* >1 worker so the parallel path is taken regardless of the host core count. */
4979+
/* #1355: write the shared external-import-shadow fixture into `dir`.
4980+
* `pad_files` filler modules push the run over MIN_FILES_FOR_PARALLEL so the
4981+
* same tree can be indexed by both resolvers. */
4982+
static void write_external_import_shadow_fixture(const char *dir, int pad_files) {
4983+
/* The lone project symbols named eq/sql — ordinary local helpers. */
4984+
write_temp_file(dir, "src/text-utils.ts",
4985+
"export function eq(a: string, b: string): boolean {\n"
4986+
" return a.trim() === b.trim();\n"
4987+
"}\n"
4988+
"export function sql(chunk: string): string {\n"
4989+
" return chunk.replace(/\\s+/g, ' ');\n"
4990+
"}\n"
4991+
"export function normalize(s: string): string {\n"
4992+
" return s.trim().toLowerCase();\n"
4993+
"}\n");
4994+
/* `eq` and `sql` come from an external package that is not in the tree, so
4995+
* the registry falls through to a project-wide same-name guess. These are
4996+
* the fabricated edges. `normalize` is imported relatively from the SAME
4997+
* file the guess would have picked — it must survive. */
4998+
write_temp_file(dir, "src/queries.ts",
4999+
"import { eq, sql } from 'drizzle-orm';\n"
5000+
"import { normalize } from './text-utils';\n"
5001+
"export function buildQuery(id: string): unknown {\n"
5002+
" return [eq({ id }, id), sql('select 1'), normalize(id)];\n"
5003+
"}\n");
5004+
/* No import of `eq` at all: nothing in the caller's source contradicts the
5005+
* guess, so the pre-existing fallback keeps its edge. */
5006+
write_temp_file(dir, "src/no-import.ts",
5007+
"export function callsWithoutImport(a: string, b: string): boolean {\n"
5008+
" return eq(a, b);\n"
5009+
"}\n");
5010+
for (int i = 0; i < pad_files; i++) {
5011+
char name[64];
5012+
char body[128];
5013+
snprintf(name, sizeof(name), "src/pad_%02d.ts", i);
5014+
snprintf(body, sizeof(body), "export function shadowPad%02d(): number { return %d; }\n", i,
5015+
i);
5016+
write_temp_file(dir, name, body);
5017+
}
5018+
}
5019+
5020+
/* #1355: assert the guard's whole contract against one indexed store. */
5021+
static int assert_external_import_shadow_contract(const char *dir, const char *db_name) {
5022+
char db_path[512];
5023+
snprintf(db_path, sizeof(db_path), "%s/%s", dir, db_name);
5024+
cbm_pipeline_t *p = cbm_pipeline_new(dir, db_path, CBM_MODE_FULL);
5025+
if (!p) {
5026+
return 1;
5027+
}
5028+
if (cbm_pipeline_run(p) != 0) {
5029+
cbm_pipeline_free(p);
5030+
return 2;
5031+
}
5032+
const char *project = cbm_pipeline_project_name(p);
5033+
cbm_store_t *s = cbm_store_open_path(db_path);
5034+
if (!s) {
5035+
cbm_pipeline_free(p);
5036+
return 3;
5037+
}
5038+
int rc = 0;
5039+
/* (1) the reported bug: an externally-imported name must not bind to the
5040+
* local homonym (RED before the fix, on both resolvers). */
5041+
if (cross_file_call_exists(s, project, "buildQuery", "eq")) {
5042+
rc = 4;
5043+
}
5044+
if (rc == 0 && cross_file_call_exists(s, project, "buildQuery", "sql")) {
5045+
rc = 5;
5046+
}
5047+
/* (2) the relative import to the very same file still resolves. */
5048+
if (rc == 0 && !cross_file_call_exists(s, project, "buildQuery", "normalize")) {
5049+
rc = 6;
5050+
}
5051+
/* (3) a bare call the caller never imports keeps its pre-existing edge. */
5052+
if (rc == 0 && !cross_file_call_exists(s, project, "callsWithoutImport", "eq")) {
5053+
rc = 7;
5054+
}
5055+
cbm_store_close(s);
5056+
cbm_pipeline_free(p);
5057+
return rc;
5058+
}
5059+
5060+
TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355) {
5061+
char tmp[256];
5062+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ext_import_shadow_XXXXXX");
5063+
if (!cbm_mkdtemp(tmp)) {
5064+
FAIL("tmpdir");
5065+
}
5066+
/* Enough files that CBM_WORKERS can take the fused-parallel path; the same
5067+
* tree is then indexed by each resolver in turn, because the guard lives at
5068+
* two independent emit sites (pass_calls.c and pass_parallel.c). */
5069+
write_external_import_shadow_fixture(tmp, 50);
5070+
5071+
char *old_workers = getenv("CBM_WORKERS");
5072+
char *saved_workers = old_workers ? strdup(old_workers) : NULL;
5073+
char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
5074+
char *saved_single = old_single ? strdup(old_single) : NULL;
5075+
5076+
cbm_setenv("CBM_INDEX_SINGLE_THREAD", "1", 1);
5077+
int sequential = assert_external_import_shadow_contract(tmp, "shadow-sequential.db");
5078+
5079+
cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
5080+
cbm_setenv("CBM_WORKERS", "4", 1);
5081+
int parallel = assert_external_import_shadow_contract(tmp, "shadow-parallel.db");
5082+
5083+
if (saved_workers) {
5084+
cbm_setenv("CBM_WORKERS", saved_workers, 1);
5085+
free(saved_workers);
5086+
} else {
5087+
cbm_unsetenv("CBM_WORKERS");
5088+
}
5089+
if (saved_single) {
5090+
cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1);
5091+
free(saved_single);
5092+
} else {
5093+
cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
5094+
}
5095+
th_rmtree(tmp);
5096+
5097+
ASSERT_EQ(sequential, 0);
5098+
ASSERT_EQ(parallel, 0);
5099+
PASS();
5100+
}
5101+
49795102
TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges) {
49805103
char tmp[256];
49815104
snprintf(tmp, sizeof(tmp), "/tmp/cbm_tsjs_par_XXXXXX");
@@ -13324,6 +13447,7 @@ SUITE(pipeline) {
1332413447
RUN_TEST(pipeline_python_receiver_parallel_suppresses_weak_method_edges);
1332513448
RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge);
1332613449
RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge);
13450+
RUN_TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355);
1332713451
RUN_TEST(pipeline_parallel_python_cross_only_dunder_gets_synthetic_carrier);
1332813452
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1332913453
RUN_TEST(pipeline_arg_url_rejects_non_http_slash_arguments);

0 commit comments

Comments
 (0)