Skip to content

Commit 0274f69

Browse files
antespichidev
authored andcommitted
fix(514): resolve TS imports through index.ts barrels
Signed-off-by: Antonio Espinosa <[email protected]>
1 parent ff54949 commit 0274f69

2 files changed

Lines changed: 204 additions & 0 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,91 @@ static char *pxc_import_value_qn(CBMLanguage lang, const CBMFileResult *result,
14191419
return qualified;
14201420
}
14211421

1422+
/* JS/TS barrels. An import that lands on a Folder (`./svc` -> svc/index.ts)
1423+
* or on a module that only re-exports the name never reaches the declaring
1424+
* module, so `module.Name` is not in the registry and DI member calls on the
1425+
* imported type stay unresolved. Follow the barrel's own IMPORTS edges (its
1426+
* `export ... from` statements) to the one module that defines `name`. Those
1427+
* edges do not record which names each statement re-exports, so the
1428+
* definition node is the proof; two different definitions fail closed. */
1429+
enum { PXC_BARREL_MAX_DEPTH = 8 };
1430+
1431+
static bool pxc_is_js_family(CBMLanguage lang) {
1432+
return lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX;
1433+
}
1434+
1435+
/* QN of `name` when the file at rel_path defines it, else NULL. */
1436+
static char *pxc_defined_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_name,
1437+
const char *rel_path, const char *name) {
1438+
char *qn = cbm_pipeline_fqn_compute(project_name, rel_path, name);
1439+
const cbm_gbuf_node_t *node = qn ? cbm_gbuf_find_by_qn(gbuf, qn) : NULL;
1440+
if (node && node->label && strcmp(node->label, "Module") != 0 &&
1441+
strcmp(node->label, "Folder") != 0 && strcmp(node->label, "File") != 0) {
1442+
return qn;
1443+
}
1444+
free(qn);
1445+
return NULL;
1446+
}
1447+
1448+
/* `visited` holds barrel file QNs already searched for `name`: barrels form
1449+
* DAGs (and cycles) where one lib index is reachable through many paths, and
1450+
* re-walking it on each path is exponential in the depth cap. */
1451+
static char *pxc_barrel_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_name,
1452+
const cbm_gbuf_node_t *target, const char *name, int depth,
1453+
CBMHashTable *visited) {
1454+
if (!target || !target->label || !target->file_path || depth > PXC_BARREL_MAX_DEPTH) {
1455+
return NULL;
1456+
}
1457+
static const char *const index_names[] = {"index.ts", "index.tsx", "index.mts",
1458+
"index.js", "index.jsx", "index.mjs"};
1459+
char rel[CBM_SZ_1K];
1460+
const cbm_gbuf_node_t *file_node = NULL;
1461+
if (strcmp(target->label, "Module") == 0) {
1462+
snprintf(rel, sizeof(rel), "%s", target->file_path);
1463+
char *file_qn = cbm_pipeline_fqn_compute(project_name, rel, "__file__");
1464+
file_node = file_qn ? cbm_gbuf_find_by_qn(gbuf, file_qn) : NULL;
1465+
free(file_qn);
1466+
} else if (strcmp(target->label, "Folder") == 0) {
1467+
for (size_t i = 0; i < sizeof(index_names) / sizeof(index_names[0]) && !file_node; i++) {
1468+
snprintf(rel, sizeof(rel), "%s/%s", target->file_path, index_names[i]);
1469+
char *file_qn = cbm_pipeline_fqn_compute(project_name, rel, "__file__");
1470+
file_node = file_qn ? cbm_gbuf_find_by_qn(gbuf, file_qn) : NULL;
1471+
free(file_qn);
1472+
}
1473+
}
1474+
if (!file_node || cbm_ht_has(visited, file_node->qualified_name)) {
1475+
return NULL;
1476+
}
1477+
cbm_ht_set(visited, file_node->qualified_name, (void *)file_node);
1478+
char *defined = pxc_defined_symbol_qn(gbuf, project_name, rel, name);
1479+
if (defined) {
1480+
return defined;
1481+
}
1482+
1483+
const cbm_gbuf_edge_t **edges = NULL;
1484+
int edge_count = 0;
1485+
if (cbm_gbuf_find_edges_by_source_type(gbuf, file_node->id, "IMPORTS", &edges, &edge_count) !=
1486+
0) {
1487+
return NULL;
1488+
}
1489+
char *found = NULL;
1490+
for (int i = 0; i < edge_count; i++) {
1491+
const cbm_gbuf_node_t *next = cbm_gbuf_find_by_id(gbuf, edges[i]->target_id);
1492+
char *qn = pxc_barrel_symbol_qn(gbuf, project_name, next, name, depth + 1, visited);
1493+
if (!qn) {
1494+
continue;
1495+
}
1496+
if (found && strcmp(found, qn) != 0) {
1497+
free(found);
1498+
free(qn);
1499+
return NULL;
1500+
}
1501+
free(found);
1502+
found = qn;
1503+
}
1504+
return found;
1505+
}
1506+
14221507
static bool pxc_import_map_has_local(const char *const *keys, int count, const char *local_name) {
14231508
if (!keys || !local_name)
14241509
return false;
@@ -1568,6 +1653,22 @@ int cbm_pxc_build_import_map_ex(const cbm_gbuf_t *gbuf, const char *project_name
15681653
free(local);
15691654
continue;
15701655
}
1656+
if (pxc_is_js_family(lang) && target->label && target->file_path) {
1657+
char *direct = strcmp(target->label, "Module") == 0
1658+
? pxc_defined_symbol_qn(gbuf, project_name, target->file_path, local)
1659+
: NULL;
1660+
char *barrel = NULL;
1661+
CBMHashTable *visited = direct ? NULL : cbm_ht_create(16);
1662+
if (visited) {
1663+
barrel = pxc_barrel_symbol_qn(gbuf, project_name, target, local, 0, visited);
1664+
cbm_ht_free(visited);
1665+
}
1666+
free(direct);
1667+
if (barrel) {
1668+
free(value);
1669+
value = barrel;
1670+
}
1671+
}
15711672
keys[count] = local;
15721673
vals[count] = value;
15731674
count++;

‎tests/test_lsp_resolution_probe.c‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,108 @@ TEST(lrp_ts_s8_field_type_hint) {
12721272
PASS();
12731273
}
12741274

1275+
/* Barrels: a type imported through an index.ts that re-exports it must resolve
1276+
* to the defining module. Covers a dotted folder imported by alias (B1), a lib
1277+
* barrel with a named re-export (B2), a relative import of a barrel folder
1278+
* (B3) and a nested `export *` barrel (B4). The import used to stop at the
1279+
* Folder/barrel module, so every DI call below had no CALLS edge. */
1280+
TEST(lrp_ts_barrel_reexport_member_calls) {
1281+
static const LRP_File f[] = {
1282+
{"apps/api/tsconfig.json", "{\"compilerOptions\":{\"baseUrl\":\"./\",\"paths\":{"
1283+
"\"@acme/shipping\":[\"libs/shipping/src\"],"
1284+
"\"@acme/shipping/*\":[\"libs/shipping/src/*\"]}}}\n"},
1285+
{"apps/api/libs/shipping/src/application/service/label-print.service/"
1286+
"label-print.service.ts",
1287+
"import { LabelPrintParams } from \"./label-print.interface\";\n"
1288+
"export class LabelPrintService {\n"
1289+
" printStream(params: LabelPrintParams): string {\n"
1290+
" return this.printWithStats(params).body;\n"
1291+
" }\n"
1292+
" printWithStats(params: LabelPrintParams): { body: string; pages: number } {\n"
1293+
" return { body: params.shipmentId, pages: 1 };\n"
1294+
" }\n"
1295+
"}\n"},
1296+
{"apps/api/libs/shipping/src/application/service/label-print.service/"
1297+
"label-print.interface.ts",
1298+
"export interface LabelPrintParams {\n"
1299+
" shipmentId: string;\n"
1300+
"}\n"},
1301+
{"apps/api/libs/shipping/src/application/service/label-print.service/index.ts",
1302+
"export * from \"./label-print.service\";\n"
1303+
"export * from \"./label-print.interface\";\n"},
1304+
{"apps/api/libs/shipping/src/domain/tracking/tracking-code.service.ts",
1305+
"export class TrackingCodeService {\n"
1306+
" generate(shipmentId: string): string {\n"
1307+
" return shipmentId;\n"
1308+
" }\n"
1309+
"}\n"},
1310+
{"apps/api/libs/shipping/src/domain/tracking/index.ts",
1311+
"export * from \"./tracking-code.service\";\n"},
1312+
{"apps/api/libs/shipping/src/domain/carrier.service.ts",
1313+
"export class CarrierService {\n"
1314+
" quote(weightKg: number): number {\n"
1315+
" return weightKg * 2;\n"
1316+
" }\n"
1317+
"}\n"},
1318+
{"apps/api/libs/shipping/src/index.ts",
1319+
"export * from \"./domain/tracking\";\n"
1320+
"export {\n"
1321+
" CarrierService,\n"
1322+
"} from \"./domain/carrier.service\";\n"
1323+
"export type { LabelPrintParams } from \"./application/service/label-print.service\";\n"},
1324+
{"apps/api/libs/invoice/src/application/command/handler/ship-invoice.handler.ts",
1325+
"import { LabelPrintService } from "
1326+
"\"@acme/shipping/application/service/label-print.service\";\n"
1327+
"import { CarrierService, TrackingCodeService } from \"@acme/shipping\";\n"
1328+
"export class ShipInvoiceHandler {\n"
1329+
" constructor(\n"
1330+
" private readonly labelPrintService: LabelPrintService,\n"
1331+
" private readonly carrierService: CarrierService,\n"
1332+
" private readonly trackingCodeService: TrackingCodeService,\n"
1333+
" ) {}\n"
1334+
" execute(shipmentId: string): string {\n"
1335+
" const cost = this.carrierService.quote(3);\n"
1336+
" const code = this.trackingCodeService.generate(shipmentId);\n"
1337+
" const label = this.labelPrintService.printStream({ shipmentId });\n"
1338+
" return `${label}:${code}:${cost}`;\n"
1339+
" }\n"
1340+
"}\n"},
1341+
{"apps/api/libs/invoice/src/application/service/label-preview.service.ts",
1342+
"import { LabelPrintService } from "
1343+
"\"../../../../shipping/src/application/service/label-print.service\";\n"
1344+
"export class LabelPreviewService {\n"
1345+
" constructor(private readonly labelPrintService: LabelPrintService) {}\n"
1346+
" preview(shipmentId: string): number {\n"
1347+
" return this.labelPrintService.printWithStats({ shipmentId }).pages;\n"
1348+
" }\n"
1349+
"}\n"},
1350+
};
1351+
LRP_Proj lp;
1352+
cbm_store_t *store = lrp_index(&lp, f, (int)(sizeof(f) / sizeof(f[0])));
1353+
ASSERT_NOT_NULL(store);
1354+
1355+
int b1 = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "ShipInvoiceHandler.execute",
1356+
"LabelPrintService.printStream");
1357+
int b2 = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "ShipInvoiceHandler.execute",
1358+
"CarrierService.quote");
1359+
int b3 = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "LabelPreviewService.preview",
1360+
"LabelPrintService.printWithStats");
1361+
int b4 = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "ShipInvoiceHandler.execute",
1362+
"TrackingCodeService.generate");
1363+
int self =
1364+
lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "LabelPrintService.printStream",
1365+
"LabelPrintService.printWithStats");
1366+
printf(" [ts/barrel] B1=%d B2=%d B3=%d B4=%d self=%d\n", b1, b2, b3, b4, self);
1367+
lrp_cleanup(&lp, store);
1368+
1369+
ASSERT_EQ(b1, 1);
1370+
ASSERT_EQ(b2, 1);
1371+
ASSERT_EQ(b3, 1);
1372+
ASSERT_EQ(b4, 1);
1373+
ASSERT_EQ(self, 1);
1374+
PASS();
1375+
}
1376+
12751377
/* ══════════════════════════════════════════════════════════════════════
12761378
* ─── GROUP 7: JAVA (lsp_cross NOT WIRED) ─────────────────────────────
12771379
* ══════════════════════════════════════════════════════════════════════
@@ -1919,6 +2021,7 @@ SUITE(lsp_resolution_probe) {
19192021
RUN_TEST(lrp_ts_s6_inherited_method);
19202022
RUN_TEST(lrp_ts_s7_generic);
19212023
RUN_TEST(lrp_ts_s8_field_type_hint);
2024+
RUN_TEST(lrp_ts_barrel_reexport_member_calls);
19222025

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

0 commit comments

Comments
 (0)