Skip to content

Commit 797a11a

Browse files
committed
fix(registry): keep workspace-sibling imports out of the external-import guard
A per-language blast-radius census on twelve public repositories found one regression class in the DeusData#1355 guard, and it is not per-language: it is per-topology. In a workspace monorepo a BARE specifier can still name code inside the indexed tree. `import { eq } from "drizzle-orm"` written inside the drizzle-orm repository is a sibling package, not a dependency. Measured on drizzle-team/drizzle-orm at b7862528, full index, both resolvers: the guard removed 2609 CALLS edges. Resolving each removed edge's specifier against the repository's own package manifests puts 1377 of them (52.8%) on the file the specifier actually names. 690 of those are strict: the specifier names a subpath, that subpath maps to one directory, and the target file is inside it. The other 687 come from the bare package root, where the check can only confirm the target is somewhere in the package, so read 690 as the defensible floor. Only 373 removed edges pointed at a third-party package, which is the defect DeusData#1355 reports. The same census on eleven other repositories (flask, scrapy, express, zustand, got, cobra, gin, chi, ripgrep, gson, jq) removed 12 CALLS edges in total: nine fabricated, one lost (a documentation example importing the repository's own published name), two weak links between a shell command and a manifest key. The regression is specific to trees that ship the package they import. Consult the pipeline package map before calling a specifier external. It is keyed by the `name` of every manifest found in the tree, so a workspace registers each of its own packages there, and a subpath specifier is walked back one slash at a time ("drizzle-orm/pg-core" -> "drizzle-orm"). A specifier the tree itself claims is now treated exactly like a relative one -- kept. The check is deliberately a NAME test rather than a resolution test. A workspace package usually points `main` at a build artifact ("./index.cjs") that is not checked in, so asking whether the entry file exists in the graph answers "no" for exactly the monorepos this has to protect. Whether the tree claims the name is decidable from the manifest alone. Passing NULL restores the previous specifier-shape-only contract, which is what the unit tests exercise. After the change the same census removes 391 edges on drizzle-orm, 375 of them CALLS, and every one resolves to a third-party package; of the other eleven repositories only got changes, recovering its self-referencing edge. Signed-off-by: Yyunozor <yyunozor@icloud.com>
1 parent a8293c4 commit 797a11a

6 files changed

Lines changed: 263 additions & 39 deletions

File tree

‎src/pipeline/pass_calls.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
675675
* #725 guard, after the service-pattern bypasses above, so no HTTP/route
676676
* edge can be lost to it. */
677677
if (cbm_suppress_external_import_shadow(call->callee_name, res.strategy, file_imports, imp_keys,
678-
imp_count)) {
678+
imp_count, cbm_pipeline_get_pkgmap())) {
679679
return 0;
680680
}
681681
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys, imp_vals,

‎src/pipeline/pass_parallel.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
25712571
}
25722572
if (target_node && source_node->id != target_node->id &&
25732573
cbm_suppress_external_import_shadow(call->callee_name, res.strategy, &result->imports,
2574-
imp_keys, imp_count)) {
2574+
imp_keys, imp_count, cbm_pipeline_get_pkgmap())) {
25752575
/* #1355: same guard as pass_calls.c — a bare call bound by an
25762576
* external package import must not become a CALLS edge to an
25772577
* unrelated project symbol of the same name. */

‎src/pipeline/pipeline.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
#include <stdint.h>
2020
#include <stdatomic.h>
2121

22-
#include "discover/discover.h" /* cbm_ignored_file_t (#963) */
23-
#include "foundation/constants.h" /* CBM_SZ_512 */
22+
#include "discover/discover.h" /* cbm_ignored_file_t (#963) */
23+
#include "foundation/constants.h" /* CBM_SZ_512 */
24+
#include "foundation/hash_table.h" /* CBMHashTable (#1355 pkgmap lookup) */
2425

2526
/* Forward declarations */
2627
typedef struct cbm_store cbm_store_t;
@@ -297,10 +298,14 @@ bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_b
297298
* `import { eq } from "drizzle-orm"` must not make `eq(...)` a CALLS edge to an
298299
* unrelated project `eq`. A name the import map does bind, a relative
299300
* specifier, a member/qualified callee, and every import-/receiver-aware
300-
* strategy are all kept. Pure; unit-tested in test_registry.c. */
301+
* strategy are all kept. `indexed_packages` is the pipeline package map: a
302+
* specifier naming a package the tree itself declares (a workspace sibling)
303+
* counts as in-tree and is kept too; NULL disables that check.
304+
* Pure; unit-tested in test_registry.c. */
301305
bool cbm_suppress_external_import_shadow(const char *callee_name, const char *strategy,
302306
const CBMImportArray *file_imports,
303-
const char **import_map_keys, int import_map_count);
307+
const char **import_map_keys, int import_map_count,
308+
const CBMHashTable *indexed_packages);
304309

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

‎src/pipeline/registry.c‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,43 @@ static bool specifier_is_relative(const char *module_path) {
528528
return false;
529529
}
530530

531+
/* True when the specifier names a package that the indexed tree DECLARES —
532+
* a workspace sibling, not a third-party dependency. `indexed_packages` is the
533+
* pipeline's package map, keyed by the `name` of every manifest found in the
534+
* tree (package.json, go.mod, Cargo.toml, ...), so a pnpm/npm/cargo workspace
535+
* registers each of its own packages here.
536+
*
537+
* The key is the bare package name, so a subpath specifier is walked back one
538+
* slash at a time: "drizzle-orm/pg-core" -> "drizzle-orm". Scoped names keep
539+
* their leading segment ("@scope/pkg/sub" -> "@scope/pkg" -> "@scope").
540+
*
541+
* Deliberately a NAME test, not a resolution test. A workspace package usually
542+
* points `main`/`module` at a build artifact ("./index.cjs") that is not
543+
* checked in, so asking whether the entry file exists in the graph answers
544+
* "no" for exactly the monorepos this has to protect. Whether the tree claims
545+
* the name is decidable from the manifest alone. */
546+
static bool specifier_names_indexed_package(const CBMHashTable *indexed_packages,
547+
const char *module_path) {
548+
if (!indexed_packages || !module_path || !module_path[0]) {
549+
return false;
550+
}
551+
if (cbm_ht_has(indexed_packages, module_path)) {
552+
return true;
553+
}
554+
char buf[CBM_SZ_512];
555+
if (strlen(module_path) >= sizeof(buf)) {
556+
return false;
557+
}
558+
snprintf(buf, sizeof(buf), "%s", module_path);
559+
for (char *slash = strrchr(buf, '/'); slash != NULL; slash = strrchr(buf, '/')) {
560+
*slash = '\0';
561+
if (cbm_ht_has(indexed_packages, buf)) {
562+
return true;
563+
}
564+
}
565+
return false;
566+
}
567+
531568
static bool import_map_binds(const char **import_map_keys, int import_map_count,
532569
const char *local_name) {
533570
if (!import_map_keys || import_map_count <= 0) {
@@ -569,10 +606,17 @@ static bool import_map_binds(const char **import_map_keys, int import_map_count,
569606
* relative and a package specifier keeps the edge — the relative binding wins
570607
* the tie explicitly, so the outcome does not depend on import order.
571608
*
609+
* `indexed_packages` extends that same reasoning to workspace monorepos, where
610+
* a BARE specifier also names in-tree code: `import { eq } from "drizzle-orm"`
611+
* inside the drizzle-orm repository is a sibling package, not a dependency. A
612+
* specifier the tree's own manifests claim is treated exactly like a relative
613+
* one. NULL disables the check and restores the specifier-shape-only contract.
614+
*
572615
* Pure + side-effect-free so the contract is unit-testable without a pipeline. */
573616
bool cbm_suppress_external_import_shadow(const char *callee_name, const char *strategy,
574617
const CBMImportArray *file_imports,
575-
const char **import_map_keys, int import_map_count) {
618+
const char **import_map_keys, int import_map_count,
619+
const CBMHashTable *indexed_packages) {
576620
if (!callee_name || !callee_name[0] || !strategy || !strategy[0]) {
577621
return false;
578622
}
@@ -595,7 +639,8 @@ bool cbm_suppress_external_import_shadow(const char *callee_name, const char *st
595639
if (!imp->local_name || !imp->module_path || strcmp(imp->local_name, callee_name) != 0) {
596640
continue;
597641
}
598-
if (specifier_is_relative(imp->module_path)) {
642+
if (specifier_is_relative(imp->module_path) ||
643+
specifier_names_indexed_package(indexed_packages, imp->module_path)) {
599644
return false; /* an in-tree binding for this name — never suppress */
600645
}
601646
external_binding = true;

‎tests/test_pipeline.c‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5112,6 +5112,116 @@ TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355) {
51125112
PASS();
51135113
}
51145114

5115+
/* #1355 / #1732: the same tree, but the bare specifier now names a WORKSPACE
5116+
* package that the repository itself ships. `main` deliberately points at a
5117+
* build artifact that is not checked in — the shape every real monorepo has,
5118+
* and the reason an entry-file lookup cannot answer this question. */
5119+
static void write_workspace_shadow_fixture(const char *dir, int pad_files) {
5120+
/* One directory level only: write_temp_file's mkdir is documented as
5121+
* "simple version, one level", and a deeper path fails silently — which
5122+
* makes the fixture look healthy while the file never lands. */
5123+
write_temp_file(dir, "lib/package.json",
5124+
"{\n \"name\": \"@fixture/lib\",\n \"main\": \"./dist/index.js\"\n}\n");
5125+
write_temp_file(dir, "lib/index.ts",
5126+
"export function wsEq(a: string, b: string): boolean {\n"
5127+
" return a === b;\n"
5128+
"}\n");
5129+
write_temp_file(dir, "app/package.json", "{\n \"name\": \"@fixture/app\"\n}\n");
5130+
/* The lone project symbol named wsSpec — an ordinary local helper that a
5131+
* project-wide guess would happily bind an external `wsSpec` to. */
5132+
write_temp_file(dir, "app/spec-helpers.ts",
5133+
"export function wsSpec(label: string): string {\n"
5134+
" return label;\n"
5135+
"}\n");
5136+
write_temp_file(dir, "app/query.ts",
5137+
"import { wsEq } from '@fixture/lib';\n"
5138+
"import { wsSpec } from 'vitest';\n"
5139+
"export function wsBuild(id: string): unknown {\n"
5140+
" return [wsEq(id, id), wsSpec('case')];\n"
5141+
"}\n");
5142+
for (int i = 0; i < pad_files; i++) {
5143+
char name[64];
5144+
char body[128];
5145+
snprintf(name, sizeof(name), "app/pad_%02d.ts", i);
5146+
snprintf(body, sizeof(body), "export function wsPad%02d(): number { return %d; }\n", i, i);
5147+
write_temp_file(dir, name, body);
5148+
}
5149+
}
5150+
5151+
static int assert_workspace_shadow_contract(const char *dir, const char *db_name) {
5152+
char db_path[512];
5153+
snprintf(db_path, sizeof(db_path), "%s/%s", dir, db_name);
5154+
cbm_pipeline_t *p = cbm_pipeline_new(dir, db_path, CBM_MODE_FULL);
5155+
if (!p) {
5156+
return 1;
5157+
}
5158+
if (cbm_pipeline_run(p) != 0) {
5159+
cbm_pipeline_free(p);
5160+
return 2;
5161+
}
5162+
const char *project = cbm_pipeline_project_name(p);
5163+
cbm_store_t *s = cbm_store_open_path(db_path);
5164+
if (!s) {
5165+
cbm_pipeline_free(p);
5166+
return 3;
5167+
}
5168+
int rc = 0;
5169+
/* (1) the workspace sibling: the tree declares @fixture/lib, so the bare
5170+
* specifier is in-project and the fallback keeps its chance. RED without
5171+
* the package-map check — this is the 1377-edge class measured on
5172+
* drizzle-team/drizzle-orm. */
5173+
if (!cross_file_call_exists(s, project, "wsBuild", "wsEq")) {
5174+
rc = 4;
5175+
}
5176+
/* (2) a genuine third-party package in the very same file is still cut:
5177+
* the check must not disarm the guard wholesale. */
5178+
if (rc == 0 && cross_file_call_exists(s, project, "wsBuild", "wsSpec")) {
5179+
rc = 5;
5180+
}
5181+
cbm_store_close(s);
5182+
cbm_pipeline_free(p);
5183+
return rc;
5184+
}
5185+
5186+
TEST(pipeline_external_import_shadow_keeps_workspace_sibling_issue1355) {
5187+
char tmp[256];
5188+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ws_import_shadow_XXXXXX");
5189+
if (!cbm_mkdtemp(tmp)) {
5190+
FAIL("tmpdir");
5191+
}
5192+
write_workspace_shadow_fixture(tmp, EXTERNAL_IMPORT_SHADOW_PARALLEL_PAD);
5193+
5194+
const char *old_workers = getenv("CBM_WORKERS");
5195+
char *saved_workers = old_workers ? strdup(old_workers) : NULL;
5196+
const char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
5197+
char *saved_single = old_single ? strdup(old_single) : NULL;
5198+
5199+
cbm_setenv("CBM_INDEX_SINGLE_THREAD", "1", 1);
5200+
int sequential = assert_workspace_shadow_contract(tmp, "ws-sequential.db");
5201+
5202+
cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
5203+
cbm_setenv("CBM_WORKERS", "4", 1);
5204+
int parallel = assert_workspace_shadow_contract(tmp, "ws-parallel.db");
5205+
5206+
if (saved_workers) {
5207+
cbm_setenv("CBM_WORKERS", saved_workers, 1);
5208+
free(saved_workers);
5209+
} else {
5210+
cbm_unsetenv("CBM_WORKERS");
5211+
}
5212+
if (saved_single) {
5213+
cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1);
5214+
free(saved_single);
5215+
} else {
5216+
cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
5217+
}
5218+
th_rmtree(tmp);
5219+
5220+
ASSERT_EQ(sequential, 0);
5221+
ASSERT_EQ(parallel, 0);
5222+
PASS();
5223+
}
5224+
51155225
TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges) {
51165226
char tmp[256];
51175227
snprintf(tmp, sizeof(tmp), "/tmp/cbm_tsjs_par_XXXXXX");
@@ -13461,6 +13571,7 @@ SUITE(pipeline) {
1346113571
RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge);
1346213572
RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge);
1346313573
RUN_TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355);
13574+
RUN_TEST(pipeline_external_import_shadow_keeps_workspace_sibling_issue1355);
1346413575
RUN_TEST(pipeline_parallel_python_cross_only_dunder_gets_synthetic_carrier);
1346513576
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1346613577
RUN_TEST(pipeline_arg_url_rejects_non_http_slash_arguments);

0 commit comments

Comments
 (0)