diff --git a/scripts/memory-core-baseline.txt b/scripts/memory-core-baseline.txt index 7e979160d..2a83f613e 100644 --- a/scripts/memory-core-baseline.txt +++ b/scripts/memory-core-baseline.txt @@ -69,7 +69,7 @@ src/pipeline/pass_infrascan.c 2 src/pipeline/pass_k8s.c 6 src/pipeline/pass_lsp_cross.c 49 src/pipeline/pass_parallel.c 31 -src/pipeline/pass_pkgmap.c 81 +src/pipeline/pass_pkgmap.c 80 src/pipeline/pass_semantic.c 3 src/pipeline/pass_tests.c 3 src/pipeline/pass_usages.c 4 diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index afedba700..9ec1dea67 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -1075,6 +1075,40 @@ static bool pkgmap_is_psr4_prefix(const char *pkg_name) { return n > 0 && pkg_name[n - SKIP_ONE] == '\\'; } +/* How far below the repository root an entry sits: 0 for the root itself. */ +static int pkgmap_entry_depth(const char *entry_rel) { + if (!entry_rel[0] || strcmp(entry_rel, ".") == 0) { + return 0; + } + int depth = SKIP_ONE; + for (const char *p = entry_rel; *p; p++) { + if (*p == '/') { + depth++; + } + } + 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 + * 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. */ +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; + int by_name = strcmp(a->pkg_name, b->pkg_name); + 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); + if (depth_a != depth_b) { + return depth_a < depth_b ? -SKIP_ONE : SKIP_ONE; + } + return strcmp(a->entry_rel, b->entry_rel); +} + CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_count, const char *project_name) { /* Count total entries */ @@ -1086,39 +1120,50 @@ CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_cou return NULL; } - CBMHashTable *map = cbm_ht_create(PKGMAP_HT_INIT); - int merged = 0; - + const cbm_pkg_entry_t **candidates = + cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)total * sizeof(*candidates)); + if (!candidates) { + return NULL; + } + int candidate_count = 0; for (int w = 0; w < worker_count; w++) { - cbm_pkg_entries_t *we = &worker_entries[w]; + const cbm_pkg_entries_t *we = &worker_entries[w]; for (int i = 0; i < we->count; i++) { if (!we->items[i].pkg_name || !we->items[i].entry_rel) { continue; /* package-boundary metadata has no import specifier */ } - /* Convert entry_rel to QN: project.dir.parts. A PSR-4 namespace - * prefix (key ends in '\\') keeps its repo-relative DIRECTORY - * instead: its readers map `Prefix\Sub\Class` onto a file path - * (/Sub/Class.php), which a dotted QN cannot express (#1186). */ - const char *entry_rel = we->items[i].entry_rel; - char *qn = pkgmap_is_psr4_prefix(we->items[i].pkg_name) - ? cbm_strndup(entry_rel, strlen(entry_rel)) - : cbm_pipeline_fqn_module(project_name, entry_rel); - if (!qn) { - continue; - } - - /* Check for duplicate — first wins */ - if (cbm_ht_has(map, we->items[i].pkg_name)) { - free(qn); - continue; - } - - /* Transfer ownership: key = strdup'd pkg_name, value = qn */ - char *key = strdup(we->items[i].pkg_name); - cbm_ht_set(map, key, qn); - merged++; + candidates[candidate_count++] = &we->items[i]; } } + qsort(candidates, (size_t)candidate_count, sizeof(*candidates), pkgmap_candidate_cmp); + + CBMHashTable *map = cbm_ht_create(PKGMAP_HT_INIT); + int merged = 0; + + for (int i = 0; i < candidate_count; i++) { + const cbm_pkg_entry_t *entry = candidates[i]; + /* Duplicate name: the first in merge order wins. */ + if (cbm_ht_has(map, entry->pkg_name)) { + continue; + } + /* Convert entry_rel to QN: project.dir.parts. A PSR-4 namespace + * prefix (key ends in '\\') keeps its repo-relative DIRECTORY + * instead: its readers map `Prefix\Sub\Class` onto a file path + * (/Sub/Class.php), which a dotted QN cannot express (#1186). */ + const char *entry_rel = entry->entry_rel; + char *qn = pkgmap_is_psr4_prefix(entry->pkg_name) + ? cbm_strndup(entry_rel, strlen(entry_rel)) + : cbm_pipeline_fqn_module(project_name, entry_rel); + if (!qn) { + continue; + } + + /* Transfer ownership: key = strdup'd pkg_name, value = qn */ + char *key = strdup(entry->pkg_name); + cbm_ht_set(map, key, qn); + merged++; + } + cbm_free(CBM_MEM_CLASS_OTHER, candidates); if (merged == 0) { cbm_ht_free(map); diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index 21f433c31..057f20aa5 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -2474,6 +2474,61 @@ TEST(ei_php_psr4_missing_class_leaves_type_evidence_unknown) { PASS(); } +/* Two manifests can declare the same package name: a repository's own go.mod + * and a test fixture's copy of it. Which extraction worker parses which manifest + * is thread scheduling, so the package map must not depend on it. The manifest + * nearest the repository root wins, and an equal depth is settled by path. + * Found on cli/cli, where the fixture won in about one index run in thirty and + * the IMPORTS edges fell from 3,869 to 690. */ +static char *ei_pkgmap_winner(const char *first_worker_manifest, + const char *second_worker_manifest) { + static const char go_mod[] = "module github.com/acme/tool/v2\n\ngo 1.22\n"; + cbm_pkg_entries_t workers[2]; + cbm_pkg_entries_init(&workers[0]); + cbm_pkg_entries_init(&workers[1]); + bool parsed = cbm_pkgmap_try_parse("go.mod", first_worker_manifest, go_mod, (int)strlen(go_mod), + &workers[0]) && + cbm_pkgmap_try_parse("go.mod", second_worker_manifest, go_mod, + (int)strlen(go_mod), &workers[1]); + CBMHashTable *map = parsed ? cbm_pkgmap_build(workers, 2, "proj") : NULL; + const char *qn = map ? (const char *)cbm_ht_get(map, "github.com/acme/tool/v2") : 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_duplicate_package_name_prefers_root_manifest_in_any_worker_order) { + char *want = cbm_pipeline_fqn_module("proj", ""); + char *root_first = ei_pkgmap_winner("go.mod", ".github/codeql/tests/fixture/go.mod"); + char *fixture_first = ei_pkgmap_winner(".github/codeql/tests/fixture/go.mod", "go.mod"); + bool root_first_ok = want && root_first && strcmp(root_first, want) == 0; + bool fixture_first_ok = want && fixture_first && strcmp(fixture_first, want) == 0; + free(want); + free(root_first); + free(fixture_first); + ASSERT_TRUE(root_first_ok); + ASSERT_TRUE(fixture_first_ok); + PASS(); +} + +TEST(ei_pkgmap_duplicate_package_name_at_equal_depth_is_settled_by_path) { + char *want = cbm_pipeline_fqn_module("proj", "examples/alpha"); + char *alpha_first = ei_pkgmap_winner("examples/alpha/go.mod", "examples/beta/go.mod"); + char *beta_first = ei_pkgmap_winner("examples/beta/go.mod", "examples/alpha/go.mod"); + bool alpha_first_ok = want && alpha_first && strcmp(alpha_first, want) == 0; + bool beta_first_ok = want && beta_first && strcmp(beta_first, want) == 0; + free(want); + free(alpha_first); + free(beta_first); + ASSERT_TRUE(alpha_first_ok); + ASSERT_TRUE(beta_first_ok); + PASS(); +} + /* ═══════════════════════════════════════════════════════════════════════════ * SUITE registration * ═══════════════════════════════════════════════════════════════════════════ */ @@ -2581,4 +2636,6 @@ SUITE(edge_imports) { RUN_TEST(ei_php_binding_snapshot_requires_current_complete_evidence); 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_duplicate_package_name_at_equal_depth_is_settled_by_path); }