From 8ccdcbc9b26a789bfc71063e2cecf1d7acc5f3cf Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sat, 14 Nov 2026 11:50:12 +0100 Subject: [PATCH] fix(pkgmap): keep one manifest's candidates in its parser's order cd318253e made the package map independent of worker scheduling by sorting every candidate of a package name by depth and path before the first-wins insertion. That also reordered the candidates of ONE manifest: a pyproject.toml pushes src/ first and second, the first had always won, and after the sort the shallower second one did, for every Python package. Imports that used to bind their own package then fell to the same-name fallback: in flask, `from flaskr import create_app` in examples/tutorial bound the celery example's create_app. The dispatcher now stamps each entry with the manifest that yielded it and its position among that manifest's entries, and the merge orders by package name, the manifest's depth and path, and that position. Different manifests are still ordered by the repository, not by the run; one manifest's entries keep the order its parser chose. ei_pkgmap_one_manifest_keeps_its_candidate_order hands a pyproject.toml and a sibling's to the merge in both worker orders; ei_python_example_project_import_binds_its_own_package indexes the flask shape and checks the import's target file. Both fail without this change. Signed-off-by: Martin Vogel --- src/pipeline/pass_pkgmap.c | 47 +++++++++++++++++---- src/pipeline/pipeline_internal.h | 2 + tests/test_edge_imports.c | 71 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 8 deletions(-) diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index 9ec1dea67..05d024b50 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -120,6 +120,8 @@ static void pkg_entries_push(cbm_pkg_entries_t *e, char *pkg_name, char *entry_r e->items[e->count].pkg_name = pkg_name; e->items[e->count].entry_rel = entry_rel; e->items[e->count].manifest_rel = NULL; + e->items[e->count].source_rel = NULL; + e->items[e->count].source_seq = 0; e->count++; } @@ -128,6 +130,7 @@ void cbm_pkg_entries_free(cbm_pkg_entries_t *e) { free(e->items[i].pkg_name); free(e->items[i].entry_rel); cbm_free(CBM_MEM_CLASS_OTHER, e->items[i].manifest_rel); + cbm_free(CBM_MEM_CLASS_OTHER, e->items[i].source_rel); } free(e->items); e->items = NULL; @@ -1014,8 +1017,8 @@ static void parse_package_swift(const char *source, int source_len, const char * /* ── Public: manifest detection + parsing ──────────────────────── */ -bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char *source, - int source_len, cbm_pkg_entries_t *entries) { +static bool pkgmap_parse_by_basename(const char *basename, const char *rel_path, const char *source, + int source_len, cbm_pkg_entries_t *entries) { if (!basename || !source || source_len <= 0) { return false; } @@ -1067,6 +1070,22 @@ bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char return false; } +/* Parse one manifest and stamp what it yielded with where it came from. The + * merge orders manifests by that, and keeps one manifest's entries in the order + * its parser pushed them (a pyproject.toml pushes src/ before ). */ +bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char *source, + int source_len, cbm_pkg_entries_t *entries) { + int before = entries ? entries->count : 0; + if (!pkgmap_parse_by_basename(basename, rel_path, source, source_len, entries)) { + return false; + } + for (int i = before; rel_path && i < entries->count; i++) { + entries->items[i].source_rel = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, rel_path); + entries->items[i].source_seq = i - before; + } + return true; +} + /* ── Merge: per-worker entries → hash table ────────────────────── */ /* A composer.json `autoload.psr-4` key: a namespace prefix ending in '\\'. */ @@ -1089,11 +1108,14 @@ static int pkgmap_entry_depth(const char *entry_rel) { return depth; } -/* Merge order: by package name, then the entry nearest the repository root, - * then by path. Two manifests can declare one package name (a repository's +/* Merge order: by package name, then the manifest nearest the repository root, + * then by manifest path, then the order in which that manifest's parser pushed + * its entries. Two manifests can declare one package name (a repository's * go.mod and a test fixture's copy of it), and which worker parsed which is - * thread scheduling. Ordering the candidates here makes the winner a property - * of the repository, not of the run. */ + * thread scheduling. Ordering the manifests here makes the winner a property of + * the repository, not of the run. One manifest's own entries keep their order: + * its parser puts the preferred candidate first. An entry no manifest stamped + * is ordered by its own path. */ static int pkgmap_candidate_cmp(const void *lhs, const void *rhs) { const cbm_pkg_entry_t *a = *(const cbm_pkg_entry_t *const *)lhs; const cbm_pkg_entry_t *b = *(const cbm_pkg_entry_t *const *)rhs; @@ -1101,11 +1123,20 @@ static int pkgmap_candidate_cmp(const void *lhs, const void *rhs) { if (by_name != 0) { return by_name; } - int depth_a = pkgmap_entry_depth(a->entry_rel); - int depth_b = pkgmap_entry_depth(b->entry_rel); + const char *from_a = a->source_rel ? a->source_rel : a->entry_rel; + const char *from_b = b->source_rel ? b->source_rel : b->entry_rel; + int depth_a = pkgmap_entry_depth(from_a); + int depth_b = pkgmap_entry_depth(from_b); if (depth_a != depth_b) { return depth_a < depth_b ? -SKIP_ONE : SKIP_ONE; } + int by_source = strcmp(from_a, from_b); + if (by_source != 0) { + return by_source; + } + if (a->source_seq != b->source_seq) { + return a->source_seq < b->source_seq ? -SKIP_ONE : SKIP_ONE; + } return strcmp(a->entry_rel, b->entry_rel); } diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h index 75a3c5d77..ba1dc75a1 100644 --- a/src/pipeline/pipeline_internal.h +++ b/src/pipeline/pipeline_internal.h @@ -388,6 +388,8 @@ typedef struct { char *pkg_name; /* heap: "@myorg/pkg", "github.com/foo/bar" */ char *entry_rel; /* heap: "packages/pkg/src/index" (no extension) */ char *manifest_rel; /* heap: owning SwiftPM manifest, NULL for other ecosystems */ + char *source_rel; /* heap: the manifest file that yielded this entry; NULL if hand-built */ + int source_seq; /* position among that manifest's entries, in the parser's order */ } cbm_pkg_entry_t; /* Growable array of package entries (per-worker, no thread contention). */ diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index 057f20aa5..582ab5333 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -2529,6 +2529,75 @@ TEST(ei_pkgmap_duplicate_package_name_at_equal_depth_is_settled_by_path) { PASS(); } +/* One manifest can yield two candidates for its own package name: a + * pyproject.toml registers src/ first and second, and the first + * has always won. Ordering the manifests of a repository (above) must not + * reorder the candidates of one manifest. When the nearest-the-root rule was + * applied to every candidate of a name, the second one won for every Python + * package, and flask's `from flaskr import create_app` in examples/tutorial + * bound to the celery example's create_app. */ +static char *ei_pkgmap_pyproject_winner(bool tutorial_parsed_by_first_worker) { + static const char tutorial[] = "[project]\nname = \"flaskr\"\n"; + static const char celery[] = "[project]\nname = \"flask-example-celery\"\n"; + cbm_pkg_entries_t workers[2]; + cbm_pkg_entries_init(&workers[0]); + cbm_pkg_entries_init(&workers[1]); + int tutorial_worker = tutorial_parsed_by_first_worker ? 0 : 1; + bool parsed = + cbm_pkgmap_try_parse("pyproject.toml", "examples/tutorial/pyproject.toml", tutorial, + (int)strlen(tutorial), &workers[tutorial_worker]) && + cbm_pkgmap_try_parse("pyproject.toml", "examples/celery/pyproject.toml", celery, + (int)strlen(celery), &workers[1 - tutorial_worker]); + CBMHashTable *map = parsed ? cbm_pkgmap_build(workers, 2, "proj") : NULL; + const char *qn = map ? (const char *)cbm_ht_get(map, "flaskr") : NULL; + char *winner = qn ? strdup(qn) : NULL; + if (map) { + cbm_pkgmap_free(map); + } + cbm_pkg_entries_free(&workers[0]); + cbm_pkg_entries_free(&workers[1]); + return winner; +} + +TEST(ei_pkgmap_one_manifest_keeps_its_candidate_order) { + char *want = cbm_pipeline_fqn_module("proj", "examples/tutorial/src/flaskr/__init__"); + char *tutorial_first = ei_pkgmap_pyproject_winner(true); + char *celery_first = ei_pkgmap_pyproject_winner(false); + bool tutorial_first_ok = want && tutorial_first && strcmp(tutorial_first, want) == 0; + bool celery_first_ok = want && celery_first && strcmp(celery_first, want) == 0; + free(want); + free(tutorial_first); + free(celery_first); + ASSERT_TRUE(tutorial_first_ok); + ASSERT_TRUE(celery_first_ok); + PASS(); +} + +/* The same defect seen from the graph: an example project's test imports its + * own package by name, and a sibling example defines a function of the same + * name. The import must bind the importing project's own package. */ +TEST(ei_python_example_project_import_binds_its_own_package) { + static const EILangFile f[] = { + {"examples/celery/pyproject.toml", "[project]\nname = \"flask-example-celery\"\n"}, + {"examples/celery/src/task_app/__init__.py", "def create_app():\n return 1\n"}, + {"examples/tutorial/pyproject.toml", "[project]\nname = \"flaskr\"\n"}, + {"examples/tutorial/flaskr/__init__.py", "def create_app(config=None):\n return 2\n"}, + {"examples/tutorial/tests/conftest.py", + "from flaskr import create_app\n\ndef app():\n return create_app()\n"}}; + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, f, 5); + char target[512] = ""; + int edges = + store ? ei_import_target_path(store, lp.project, "examples/tutorial/tests/conftest.py", + "create_app", target, sizeof(target)) + : -1; + bool own_package = strcmp(target, "examples/tutorial/flaskr/__init__.py") == 0; + ei_cleanup(&lp, store); + ASSERT_EQ(edges, 1); + ASSERT_TRUE(own_package); + PASS(); +} + /* ═══════════════════════════════════════════════════════════════════════════ * SUITE registration * ═══════════════════════════════════════════════════════════════════════════ */ @@ -2637,5 +2706,7 @@ SUITE(edge_imports) { RUN_TEST(ei_php_binding_snapshot_groups_kinds_and_invalidates_failed_rebuild); RUN_TEST(ei_php_psr4_missing_class_leaves_type_evidence_unknown); RUN_TEST(ei_pkgmap_duplicate_package_name_prefers_root_manifest_in_any_worker_order); + RUN_TEST(ei_pkgmap_one_manifest_keeps_its_candidate_order); + RUN_TEST(ei_python_example_project_import_binds_its_own_package); RUN_TEST(ei_pkgmap_duplicate_package_name_at_equal_depth_is_settled_by_path); }