Skip to content

Commit 0521d82

Browse files
committed
fix(514): search a re-exported module once per name, not once per walk
The barrel walker kept a visited set of file QNs. A barrel with both `export * from './m'` and `export { a as b } from './m'` searched m for `b` through the star edge first, marked m visited, and then skipped m when the renamed edge asked for `a`. The call to `b` got no edge. The visited set is now keyed on (file QN, name). Each key is an owned string stored as its own value and freed with the table. Tested: new lrp_ts_barrel_star_and_renamed_same_module fails before (renamed=0) and passes after. Suites extraction_imports, edge_imports, path_alias, mcp, lsp_resolution_probe: 622 passed, 4 skipped. Signed-off-by: chidev <[email protected]>
1 parent cf70a88 commit 0521d82

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,19 @@ static bool pxc_edge_string_prop(const char *json, const char *key, char *out, s
14681468
return true;
14691469
}
14701470

1471-
/* `visited` holds barrel file QNs already searched for `name`: barrels form
1471+
static void pxc_free_visit_key(const char *key, void *value, void *userdata) {
1472+
(void)key;
1473+
(void)userdata;
1474+
free(value);
1475+
}
1476+
1477+
/* Free a `visited` table built by pxc_barrel_symbol_qn and its owned keys. */
1478+
static void pxc_barrel_visited_free(CBMHashTable *visited) {
1479+
cbm_ht_foreach(visited, pxc_free_visit_key, NULL);
1480+
cbm_ht_free(visited);
1481+
}
1482+
1483+
/* `visited` holds (barrel file QN, name) pairs already searched: barrels form
14721484
* DAGs (and cycles) where one lib index is reachable through many paths, and
14731485
* re-walking it on each path is exponential in the depth cap. */
14741486
static char *pxc_barrel_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_name,
@@ -1501,10 +1513,24 @@ static char *pxc_barrel_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_na
15011513
free(file_qn);
15021514
}
15031515
}
1504-
if (!file_node || cbm_ht_has(visited, file_node->qualified_name)) {
1516+
if (!file_node || !file_node->qualified_name) {
1517+
return NULL;
1518+
}
1519+
/* Key on file and name: a named re-export renames the symbol, so one
1520+
* file can be searched for two names in one walk (`export *` and
1521+
* `export { a as b }` from the same module). The key owns its bytes and
1522+
* is the stored value; pxc_barrel_visited_free releases it. */
1523+
size_t vk_len = strlen(file_node->qualified_name) + 1 + strlen(name) + 1;
1524+
char *visit_key = (char *)malloc(vk_len);
1525+
if (!visit_key) {
1526+
return NULL;
1527+
}
1528+
snprintf(visit_key, vk_len, "%s\x1f%s", file_node->qualified_name, name);
1529+
if (cbm_ht_has(visited, visit_key)) {
1530+
free(visit_key);
15051531
return NULL;
15061532
}
1507-
cbm_ht_set(visited, file_node->qualified_name, (void *)file_node);
1533+
cbm_ht_set(visited, visit_key, visit_key);
15081534
char *defined = pxc_defined_symbol_qn(gbuf, project_name, rel, name);
15091535
if (defined) {
15101536
return defined;
@@ -1710,7 +1736,7 @@ int cbm_pxc_build_import_map_ex(const cbm_gbuf_t *gbuf, const char *project_name
17101736
CBMHashTable *visited = direct ? NULL : cbm_ht_create(16);
17111737
if (visited) {
17121738
barrel = pxc_barrel_symbol_qn(gbuf, project_name, target, local, 0, visited);
1713-
cbm_ht_free(visited);
1739+
pxc_barrel_visited_free(visited);
17141740
}
17151741
free(direct);
17161742
if (barrel) {

‎tests/test_lsp_resolution_probe.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,36 @@ TEST(lrp_ts_barrel_reexport_member_calls) {
13851385
PASS();
13861386
}
13871387

1388+
/* One barrel re-exports the same module twice: `export *` and a renamed
1389+
* `export { innerFn as renamedFn }`. The walk searches impl for renamedFn
1390+
* through the star edge first; it must still search impl for innerFn
1391+
* through the renamed edge, so the call binds to impl's innerFn. */
1392+
TEST(lrp_ts_barrel_star_and_renamed_same_module) {
1393+
static const LRP_File f[] = {
1394+
{"src/lib/impl.ts", "export function innerFn(x: number): number { return x - 1; }\n"
1395+
"export function starFn(x: number): number { return x * 2; }\n"},
1396+
{"src/lib/barrel.ts", "export * from './impl';\n"
1397+
"export { innerFn as renamedFn } from './impl';\n"},
1398+
{"src/app.ts", "import { renamedFn, starFn } from './lib/barrel';\n"
1399+
"export function run(x: number): number {\n"
1400+
" return renamedFn(x) + starFn(x);\n"
1401+
"}\n"},
1402+
};
1403+
LRP_Proj lp;
1404+
cbm_store_t *store = lrp_index(&lp, f, (int)(sizeof(f) / sizeof(f[0])));
1405+
ASSERT_NOT_NULL(store);
1406+
1407+
int renamed = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "app.run",
1408+
"impl.innerFn");
1409+
int star = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "app.run", "impl.starFn");
1410+
printf(" [ts/barrel-same-module] renamed=%d star=%d\n", renamed, star);
1411+
lrp_cleanup(&lp, store);
1412+
1413+
ASSERT_EQ(renamed, 1);
1414+
ASSERT_EQ(star, 1);
1415+
PASS();
1416+
}
1417+
13881418
/* ══════════════════════════════════════════════════════════════════════
13891419
* ─── GROUP 7: JAVA (lsp_cross NOT WIRED) ─────────────────────────────
13901420
* ══════════════════════════════════════════════════════════════════════
@@ -2033,6 +2063,7 @@ SUITE(lsp_resolution_probe) {
20332063
RUN_TEST(lrp_ts_s7_generic);
20342064
RUN_TEST(lrp_ts_s8_field_type_hint);
20352065
RUN_TEST(lrp_ts_barrel_reexport_member_calls);
2066+
RUN_TEST(lrp_ts_barrel_star_and_renamed_same_module);
20362067

20372068
/* ── Java (lsp_cross NOT WIRED) — S1 GREEN, S2–S8 RED reproductions ── */
20382069
RUN_TEST(lrp_java_s1_crossfile_call);

0 commit comments

Comments
 (0)