Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/pipeline/pass_pkgmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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/<name> before <name>). */
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 '\\'. */
Expand All @@ -1089,23 +1108,35 @@ 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;
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);
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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/pipeline/pipeline_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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). */
Expand Down
71 changes: 71 additions & 0 deletions tests/test_edge_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name> first and <name> 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
* ═══════════════════════════════════════════════════════════════════════════ */
Expand Down Expand Up @@ -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);
}
Loading