Merge pull request #2345 from DeusData/fix/issue-2127

fix(pipeline): never bind an external Python import to a same-named project symbol (#2127)
This commit is contained in:
Martin Vogel
2026-10-01 22:38:18 +02:00
committed by GitHub
8 changed files with 456 additions and 6 deletions
+11 -3
View File
@@ -645,7 +645,8 @@ static const cbm_gbuf_node_t *calls_find_source(cbm_pipeline_ctx_t *ctx, const c
/* Resolve one call and emit the appropriate edge. Returns 1 if resolved, 0 if not. */
static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBMFileResult *result,
const char *rel, const char *module_qn, const char **imp_keys,
const char **imp_vals, int imp_count, CBMLanguage lang) {
const char **imp_vals, int imp_count, CBMLanguage lang,
const CBMImportArray *imports) {
const CBMResolvedCallArray *lsp_calls = &result->resolved_calls;
const cbm_gbuf_node_t *source_node = calls_find_source(ctx, rel, call->enclosing_func_qn);
if (!source_node) {
@@ -847,7 +848,14 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
call->receiver_is_self_attribute, call->callee_name,
res.strategy)) ||
cbm_suppress_weak_local_binding_call(suppress_weak_local_binding,
call->callee_is_locally_bound, res.strategy);
call->callee_is_locally_bound, res.strategy) ||
/* Import-binding suppression (#2127): an external `from m import f`
* binds `f` for the whole module, so a weak short-name match onto a
* project `X.f` outside m is fabricated. Python-only (the import
* paths are module chains) — MUST match pass_parallel.c exactly. */
(lang == CBM_LANG_PYTHON && cbm_suppress_weak_import_bound_call(true, true, res.strategy) &&
cbm_python_import_binding_contradicts(imports, call->callee_name, res.qualified_name,
ctx->gbuf, ctx->project_name, rel));
/* Service-pattern HTTP/ASYNC calls to an EXTERNAL client library (e.g.
* `requests.get("/api/orders/{id}")`) resolve to a QN containing the library
@@ -1039,7 +1047,7 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
}
total_calls++;
if (resolve_single_call(ctx, call, result, rel, module_qn, imp_keys, imp_vals,
imp_count, files[i].language)) {
imp_count, files[i].language, &result->imports)) {
resolved++;
} else {
unresolved++;
+8 -1
View File
@@ -3052,7 +3052,14 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
call->receiver_is_self_attribute,
call->callee_name, res.strategy)) ||
cbm_suppress_weak_local_binding_call(suppress_weak_local_binding,
call->callee_is_locally_bound, res.strategy);
call->callee_is_locally_bound, res.strategy) ||
/* Import-binding suppression (#2127) — see pass_calls.c; this gate
* MUST stay identical to the one there. */
(lang == CBM_LANG_PYTHON &&
cbm_suppress_weak_import_bound_call(true, true, res.strategy) &&
cbm_python_import_binding_contradicts(&result->imports, call->callee_name,
res.qualified_name, rc->main_gbuf,
rc->project_name, rel));
/* Service-pattern HTTP/ASYNC client call (`requests.get(url)`): the
* service signal lives in the callee_name. The registry can mis-resolve
+217 -2
View File
@@ -1543,6 +1543,213 @@ static bool import_targetable_label(const char *label) {
return false;
}
/* #2127: a Python import path spells the module chain that owns the imported
* name (`from unittest.mock import patch` -> unittest, mock). Strategy 3 matches
* the leaf name alone, so an EXTERNAL import (stdlib / third party: no project
* module, Strategy 1 misses) bound to any same-named project definition -- a
* REST view's `patch` handler became the target of every mock.patch call at
* import_map confidence. For such an external import, accept a symbol hit only
* when the module segments preceding `name` in the import path occur, in
* order, among the hit's enclosing QN segments -- a src/ layout or sys.path
* root the module resolver missed (`from template_tests.utils import setup` ->
* proj.tests.template_tests.utils.setup) still qualifies. Relative prefixes
* (leading dots) carry no segment and are skipped. Imports of project modules
* are never judged by it (they may re-export from anywhere). Python only. */
enum { PY_IMPORT_MAX_SEGS = 64 };
bool cbm_python_import_path_matches_qn(const char *module_path, const char *name,
const char *hit_qn) {
if (!module_path || !name || !hit_qn) {
return true;
}
char path[1024];
snprintf(path, sizeof(path), "%s", module_path);
char *as = strstr(path, " as ");
if (as) {
*as = '\0';
}
const char *p = path;
while (*p == '.') {
p++;
}
/* Segments of the import path; name_at = index of the LAST `name`. */
const char *segs[PY_IMPORT_MAX_SEGS];
size_t lens[PY_IMPORT_MAX_SEGS];
int nseg = 0;
int name_at = -1;
size_t name_len = strlen(name);
while (*p && nseg < PY_IMPORT_MAX_SEGS) {
const char *dot = strchr(p, '.');
size_t len = dot ? (size_t)(dot - p) : strlen(p);
if (len == name_len && strncmp(p, name, len) == 0) {
name_at = nseg;
}
segs[nseg] = p;
lens[nseg] = len;
nseg++;
if (!dot) {
break;
}
p = dot + SKIP_ONE;
}
if (name_at <= 0) {
return true; /* no module chain before the name: nothing to check */
}
/* Ordered-subsequence match against the hit's enclosing segments (every
* segment before the hit's own leaf). */
const char *leaf = strrchr(hit_qn, '.');
const char *q = hit_qn;
int want = 0;
while (want < name_at && leaf && q < leaf) {
const char *dot = strchr(q, '.');
size_t len = (size_t)(dot - q);
if (len == lens[want] && strncmp(q, segs[want], len) == 0) {
want++;
}
q = dot + SKIP_ONE;
}
return want == name_at;
}
/* #2127, call side: an imported name binds the file's identifier to that
* import for the whole module, exactly like a parameter binds it for a body.
* When the import could not be materialized (external module) the resolver
* falls through to project-wide short-name strategies and binds `patch(...)`
* (or `mock.patch(...)`) to whatever project definition shares the leaf. True
* when the callee's root identifier is bound by this file's Python imports and
* EVERY such binding's module chain (`unittest.mock` for `from unittest.mock
* import patch`; the callee's own dotted path for a root `import a.b`) is
* absent from `resolved_qn`'s enclosing segments AND the binding is external
* (no IMPORTS edge materialized it: the module is not in the project). An
* internal binding is never judged here -- a project module can re-export a
* name from anywhere -- and a chain consistent with the target (src/ layout)
* keeps the edge. `gbuf` NULL means "no IMPORTS edges" (all external). */
static bool python_import_local_materialized(const cbm_gbuf_t *gbuf, const char *project_name,
const char *rel_path, const char *local_name) {
if (!gbuf || !rel_path) {
return false;
}
char *file_qn = cbm_pipeline_fqn_compute(project_name, rel_path, "__file__");
const cbm_gbuf_node_t *file_node = file_qn ? cbm_gbuf_find_by_qn(gbuf, file_qn) : NULL;
safe_free(file_qn);
const cbm_gbuf_edge_t **edges = NULL;
int edge_count = 0;
if (!file_node || cbm_gbuf_find_edges_by_source_type(gbuf, file_node->id, "IMPORTS", &edges,
&edge_count) != 0) {
return false;
}
char needle[CBM_SZ_256];
snprintf(needle, sizeof(needle), "\"local_name\":\"%s\"", local_name);
for (int i = 0; i < edge_count; i++) {
if (edges[i]->properties_json && strstr(edges[i]->properties_json, needle)) {
return true;
}
}
return false;
}
bool cbm_python_import_binding_contradicts(const CBMImportArray *imports, const char *callee_name,
const char *resolved_qn, const cbm_gbuf_t *gbuf,
const char *project_name, const char *rel_path) {
if (!imports || imports->count <= 0 || !callee_name || !callee_name[0] || !resolved_qn ||
!resolved_qn[0]) {
return false;
}
const char *root_end = strchr(callee_name, '.');
size_t root_len = root_end ? (size_t)(root_end - callee_name) : strlen(callee_name);
bool bound = false;
for (int i = 0; i < imports->count; i++) {
const CBMImport *imp = &imports->items[i];
if (!imp->local_name || !imp->module_path || strlen(imp->local_name) != root_len ||
strncmp(imp->local_name, callee_name, root_len) != 0) {
continue;
}
char full[1024];
snprintf(full, sizeof(full), "%s", imp->module_path);
char *as = strstr(full, " as ");
if (as) {
*as = '\0';
}
const char *path_leaf_seg = strrchr(full, '.');
bool from_import = path_leaf_seg && strcmp(path_leaf_seg + SKIP_ONE, imp->local_name) == 0;
if (!as && !from_import && strncmp(full, callee_name, root_len) == 0 &&
(full[root_len] == '.' || full[root_len] == '\0')) {
/* `import a.b` binds the ROOT package `a`: the callee already
* spells its own full dotted path (`a.b.f()`). */
snprintf(full, sizeof(full), "%s", callee_name);
}
/* Otherwise (`from m import x [as y]`) only the import's own module
* chain is evidence: members reached THROUGH x (`x.objects.create`)
* may live on any type, so they are not compared. */
const char *leaf = strrchr(full, '.');
leaf = leaf ? leaf + SKIP_ONE : full;
if (cbm_python_import_path_matches_qn(full, leaf, resolved_qn) ||
python_import_local_materialized(gbuf, project_name, rel_path, imp->local_name)) {
return false; /* consistent with the target, or a project import */
}
bound = true;
}
return bound;
}
/* #2127: whether a Python import's module lives in this project. Relative
* imports always do; otherwise the module part (the path minus the imported
* name) must resolve to a graph node. Strategy 1 has already failed on the
* full path when this is asked. */
static bool python_import_module_in_project(const cbm_pipeline_ctx_t *ctx, const char *source_rel,
const char *module_path) {
if (!module_path || module_path[0] == '.') {
return true;
}
char part[1024];
snprintf(part, sizeof(part), "%s", module_path);
char *as = strstr(part, " as ");
if (as) {
*as = '\0';
}
char *dot = strrchr(part, '.');
if (!dot) {
return false; /* `import x`: Strategy 1 already missed x itself */
}
*dot = '\0';
char *qn = cbm_pipeline_resolve_module(ctx, source_rel, part);
bool found = qn && cbm_gbuf_find_by_qn(ctx->gbuf, qn) != NULL;
safe_free(qn);
return found;
}
/* `import a.b` / `import a` (as opposed to `from a import b`): every segment
* names a module. The extractor binds the ROOT (`a`) for a dotted plain
* import and the leaf for a from-import; a dot-less path is always a plain
* import. An aliased form is ambiguous and reads as a from-import. */
static bool python_is_plain_module_import(const CBMImport *imp) {
const char *p = imp->module_path;
if (!p || p[0] == '.' || strstr(p, " as ")) {
return false;
}
const char *first_dot = strchr(p, '.');
if (!first_dot) {
return true;
}
const char *leaf = strrchr(p, '.') + SKIP_ONE;
size_t root_len = (size_t)(first_dot - p);
return imp->local_name && strlen(imp->local_name) == root_len &&
strncmp(imp->local_name, p, root_len) == 0 && strcmp(leaf, imp->local_name) != 0;
}
/* #2127: Strategy 3 hit filter for an EXTERNAL Python import. The hit must
* spell the import's module chain (a src/ layout the module resolver missed
* still does), and whatever names a module -- an enclosing path segment, or
* any segment of a plain `import x` -- can only be a Module/File node (`import
* copy` must not fall back to a project method called `copy`). */
static bool python_external_hit_rejected(const CBMImport *imp, const char *name,
bool enclosing_segment, const cbm_gbuf_node_t *hit) {
if (!cbm_python_import_path_matches_qn(imp->module_path, name, hit->qualified_name)) {
return true;
}
bool names_module = enclosing_segment || python_is_plain_module_import(imp);
return names_module && strcmp(hit->label, "Module") != 0 && strcmp(hit->label, "File") != 0;
}
/* #1934: whether the name-guess import fallbacks — Strategy 1b (sibling file,
* whose label filter admits symbols) and Strategy 3 (symbol name) — may run
* for imports from this language. A Go import path names a package — never a
@@ -1882,8 +2089,8 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
src_base = pb + SKIP_ONE;
}
}
const bool symbol_fallback_allowed =
cbm_import_symbol_fallback_allowed(cbm_language_for_filename(src_base));
const CBMLanguage src_lang = cbm_language_for_filename(src_base);
const bool symbol_fallback_allowed = cbm_import_symbol_fallback_allowed(src_lang);
/* Strategy 1b: sibling-file resolution for build/markup grammars whose
* import string is a sibling filename or directory (SCSS partials, Just/
@@ -2033,6 +2240,11 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
*dot = '\0';
end = dot;
}
/* #2127: a Python import whose module is not in the project (stdlib /
* third party) may only fall back to a hit that spells its chain. */
const bool py_external =
symbol_fallback_allowed && src_lang == CBM_LANG_PYTHON &&
!python_import_module_in_project(ctx, source_rel, imp->module_path);
for (int ci = 0; symbol_fallback_allowed && ci < ncands; ci++) {
const cbm_gbuf_node_t **hits = NULL;
int n = 0;
@@ -2054,6 +2266,9 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
strcmp(cand->qualified_name, source_file_qn) == 0) {
continue; /* self */
}
if (py_external && python_external_hit_rejected(imp, cands[ci], ci > 0, cand)) {
continue; /* #2127: not the external module's symbol */
}
if (!best || (cand->qualified_name && best->qualified_name &&
strcmp(cand->qualified_name, best->qualified_name) < 0)) {
best = cand;
+10
View File
@@ -334,6 +334,16 @@ bool cbm_weak_member_unique_name_exempt(bool is_python, bool receiver_is_self_at
bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_bound,
const char *strategy);
/* Import-binding counterpart (#2127). True when the callee's root identifier
* is bound by an import of this file whose module chain contradicts the
* resolved target (`from unittest.mock import patch; patch()` must not bind a
* project `PkgConfigView.patch`), and the match came from a weak short-name
* strategy. Same drop-list as the guards above; the language set lives at the
* call sites and must be identical in pass_calls.c and pass_parallel.c.
* Pure; unit-tested in test_registry.c. */
bool cbm_suppress_weak_import_bound_call(bool enabled, bool import_binding_contradicts,
const char *strategy);
/* #725: drop a suffix_match CALLS edge when the caller language and the
* target file's language disagree. unique_name (candidates == 1) is #1572
* and is left alone; same_module / import_map / lsp_* are kept. JS/TS/TSX
+17
View File
@@ -300,6 +300,23 @@ static inline int cbm_pipeline_check_cancel(const cbm_pipeline_ctx_t *ctx) {
* ei_go_import_never_binds_symbol. */
bool cbm_import_symbol_fallback_allowed(CBMLanguage lang);
/* #2127: true when the module segments preceding `name` in a Python import
* path (`unittest.mock.patch` -> unittest, mock) occur, in order, among the
* enclosing segments of `hit_qn`. Leading relative dots and ` as alias` are
* ignored; a path with no module chain before `name` always matches. Gates
* the import resolver's symbol-name fallback for Python. */
bool cbm_python_import_path_matches_qn(const char *module_path, const char *name,
const char *hit_qn);
/* #2127: true when the callee's root identifier is bound by the file's Python
* imports and every such binding is EXTERNAL (no IMPORTS edge from `rel_path`
* in `gbuf` carries its local name; NULL gbuf = none) with a module chain that
* contradicts `resolved_qn`. Feeds cbm_suppress_weak_import_bound_call at both
* resolver call sites. */
bool cbm_python_import_binding_contradicts(const CBMImportArray *imports, const char *callee_name,
const char *resolved_qn, const cbm_gbuf_t *gbuf,
const char *project_name, const char *rel_path);
/* Check if a file path is worth tracking for git history analysis. */
bool cbm_is_trackable_file(const char *path);
+16
View File
@@ -695,6 +695,22 @@ bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_b
return weak_short_name_strategy(strategy);
}
/* Import-binding counterpart of the parameter guard above (#2127). An import
* binds the identifier for the whole module; when that import is external it
* never materializes, and the chain falls through to project-wide short-name
* strategies (`from unittest.mock import patch; patch()` -> a REST view's
* `PkgConfigView.patch`). The caller decides whether the binding contradicts
* the target (cbm_python_import_binding_contradicts: a SCOPE FACT about this
* file, not a spelling list); only weak strategies are dropped, so import_map /
* same_module / lsp_* edges are untouched. Pure; unit-tested in test_registry.c. */
bool cbm_suppress_weak_import_bound_call(bool enabled, bool import_binding_contradicts,
const char *strategy) {
if (!enabled || !import_binding_contradicts) {
return false;
}
return weak_short_name_strategy(strategy);
}
static bool js_ts_family(CBMLanguage lang) {
return lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX ||
lang == CBM_LANG_ARKTS;