Skip to content

Commit 60d96ab

Browse files
antespichidev
authored andcommitted
fix(514): preserve dotted filename
Signed-off-by: Antonio Espinosa <[email protected]>
1 parent 0274f69 commit 60d96ab

5 files changed

Lines changed: 204 additions & 10 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,10 @@ char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *sou
15061506
if (amap) {
15071507
char *aliased = cbm_path_alias_resolve(amap, module_path);
15081508
if (aliased) {
1509-
char *qn = cbm_pipeline_fqn_module(ctx->project_name, aliased);
1509+
/* Like step 1: the alias resolver already removed an explicit
1510+
* file extension, so keep dotted basenames such as
1511+
* `cat.service` intact. */
1512+
char *qn = cbm_pipeline_fqn_folder(ctx->project_name, aliased);
15101513
free(aliased);
15111514
return qn;
15121515
}

‎src/pipeline/path_alias.c‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,23 @@ enum {
4747

4848
/* ── Helpers ───────────────────────────────────────────────────── */
4949

50-
/* Strip .ts/.tsx/.js/.jsx in place. Returns its argument. */
50+
/* Strip an explicit JS/TS module extension in place, keeping dotted
51+
* basenames such as "cat.service". Same set as the relative resolver in
52+
* fqn.c. Returns its argument. */
5153
static char *strip_resolved_ext(char *path) {
5254
if (!path) {
5355
return path;
5456
}
57+
static const char *const extensions[] = {
58+
".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts", ".json",
59+
};
5560
size_t len = strlen(path);
56-
if (len > 3 && path[len - 3] == '.' && (path[len - 2] == 't' || path[len - 2] == 'j') &&
57-
path[len - 1] == 's') {
58-
path[len - 3] = '\0';
59-
return path;
60-
}
61-
if (len > 4 && path[len - 4] == '.' && (path[len - 3] == 't' || path[len - 3] == 'j') &&
62-
path[len - 2] == 's' && path[len - 1] == 'x') {
63-
path[len - 4] = '\0';
61+
for (size_t i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++) {
62+
size_t ext_len = strlen(extensions[i]);
63+
if (len > ext_len && strcmp(path + len - ext_len, extensions[i]) == 0) {
64+
path[len - ext_len] = '\0';
65+
break;
66+
}
6467
}
6568
return path;
6669
}

‎tests/repro/repro_issue514.c‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,117 @@ TEST(repro_issue514_data_flow_surfaces_arg_expr) {
197197
PASS();
198198
}
199199

200+
/*
201+
* Sub-claim (B), minimal form from the issue thread: a NestJS controller
202+
* calling a service injected through a constructor parameter property
203+
* (`constructor(private readonly catService: CatService)`) must produce an
204+
* inbound caller for CatService.findAll.
205+
*/
206+
static const RFile k_nest_files[] = {
207+
{"src/cats/cat.service.ts", "export class CatService {\n"
208+
" findAll(): string[] {\n"
209+
" return [\"a\"];\n"
210+
" }\n"
211+
"}\n"},
212+
{"src/cats/cat.controller.ts", "import { CatService } from \"./cat.service\";\n"
213+
"\n"
214+
"export class CatController {\n"
215+
" constructor(private readonly catService: CatService) {}\n"
216+
"\n"
217+
" list() {\n"
218+
" return this.catService.findAll();\n"
219+
" }\n"
220+
"}\n"},
221+
};
222+
223+
/* Run trace_path inbound depth 1 on fn_name against the fixture project.
224+
* rh_index_files restores CBM_CACHE_DIR after indexing; point the query at
225+
* the fixture's cache so trace_path finds the project. */
226+
static char *rh514_trace_inbound(RProj *lp, const char *fn_name) {
227+
char args[512];
228+
snprintf(args, sizeof(args),
229+
"{\"function_name\":\"%s\","
230+
"\"project\":\"%s\","
231+
"\"direction\":\"inbound\","
232+
"\"depth\":1}",
233+
fn_name, lp->project);
234+
235+
const char *prior_cache_dir = getenv("CBM_CACHE_DIR");
236+
char *saved_cache_dir = prior_cache_dir ? strdup(prior_cache_dir) : NULL;
237+
cbm_setenv("CBM_CACHE_DIR", lp->cachedir, 1);
238+
char *resp = cbm_mcp_handle_tool(lp->srv, "trace_path", args);
239+
if (saved_cache_dir) {
240+
cbm_setenv("CBM_CACHE_DIR", saved_cache_dir, 1);
241+
free(saved_cache_dir);
242+
} else {
243+
cbm_unsetenv("CBM_CACHE_DIR");
244+
}
245+
return resp;
246+
}
247+
248+
TEST(repro_issue514_ctor_param_property_callers) {
249+
RProj lp;
250+
cbm_store_t *store =
251+
rh_index_files(&lp, k_nest_files, (int)(sizeof(k_nest_files) / sizeof(k_nest_files[0])));
252+
ASSERT_NOT_NULL(store);
253+
254+
char *resp = rh514_trace_inbound(&lp, "findAll");
255+
ASSERT_NOT_NULL(resp);
256+
fprintf(stderr, " [514] trace_path inbound response: %.400s\n", resp);
257+
258+
ASSERT_NULL(strstr(resp, "function not found"));
259+
ASSERT_NOT_NULL(strstr(resp, "CatController.list"));
260+
261+
free(resp);
262+
rh_cleanup(&lp, store);
263+
PASS();
264+
}
265+
266+
/* Same DI shape, but the injected class is imported through a tsconfig
267+
* `paths` alias, the norm in NestJS monorepos. The alias branch of import
268+
* resolution stripped the dotted basename (`cat.service` -> `cat`), so the
269+
* IMPORTS edge missed the module and the DI call lost its target. */
270+
static const RFile k_nest_alias_files[] = {
271+
{"apps/api/tsconfig.json",
272+
"{\"compilerOptions\":{\"baseUrl\":\"./\",\"paths\":{\"@app/*\":[\"src/*\"]}}}\n"},
273+
{"apps/api/src/cats/cat.service.ts", "export class CatService {\n"
274+
" findAll(): string[] {\n"
275+
" return [\"a\"];\n"
276+
" }\n"
277+
"}\n"},
278+
{"apps/api/src/cats/cat.controller.ts",
279+
"import { CatService } from \"@app/cats/cat.service\";\n"
280+
"\n"
281+
"export class CatController {\n"
282+
" constructor(private readonly catService: CatService) {}\n"
283+
"\n"
284+
" list() {\n"
285+
" return this.catService.findAll();\n"
286+
" }\n"
287+
"}\n"},
288+
};
289+
290+
TEST(repro_issue514_ctor_param_property_alias_import_callers) {
291+
RProj lp;
292+
cbm_store_t *store = rh_index_files(
293+
&lp, k_nest_alias_files, (int)(sizeof(k_nest_alias_files) / sizeof(k_nest_alias_files[0])));
294+
ASSERT_NOT_NULL(store);
295+
296+
char *resp = rh514_trace_inbound(&lp, "findAll");
297+
ASSERT_NOT_NULL(resp);
298+
fprintf(stderr, " [514] trace_path inbound (alias) response: %.400s\n", resp);
299+
300+
ASSERT_NULL(strstr(resp, "function not found"));
301+
ASSERT_NOT_NULL(strstr(resp, "CatController.list"));
302+
303+
free(resp);
304+
rh_cleanup(&lp, store);
305+
PASS();
306+
}
307+
200308
/* ── Suite ─────────────────────────────────────────────────────────────────── */
201309
SUITE(repro_issue514) {
202310
RUN_TEST(repro_issue514_data_flow_surfaces_arg_expr);
311+
RUN_TEST(repro_issue514_ctor_param_property_callers);
312+
RUN_TEST(repro_issue514_ctor_param_property_alias_import_callers);
203313
}

‎tests/test_edge_imports.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,66 @@ TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682) {
597597
PASS();
598598
}
599599

600+
/* #514 follow-up: the tsconfig `paths` branch must preserve dotted basenames
601+
* like the relative branch (#1682). It stripped `.service` from
602+
* `@app/cats/cat.service`, missed the module, and bound the import by last
603+
* segment to an unrelated node. */
604+
TEST(ei_typescript_dotted_alias_import_targets_source_module) {
605+
static const char *service_path = "apps/api/src/cats/cat.service.ts";
606+
static const char *controller_path = "apps/api/src/cats/cat.controller.ts";
607+
static const EILangFile f[] = {
608+
{"apps/api/tsconfig.json",
609+
"{\"compilerOptions\":{\"baseUrl\":\"./\",\"paths\":{\"@app/*\":[\"src/*\"]}}}\n"},
610+
{"apps/api/src/cats/cat.service.ts", "export class CatService {\n"
611+
" findAll(): string[] {\n"
612+
" return [\"a\"];\n"
613+
" }\n"
614+
"}\n"},
615+
{"apps/api/src/cats/cat.controller.ts",
616+
"import { CatService } from \"@app/cats/cat.service\";\n"
617+
"export class CatController {\n"
618+
" constructor(private readonly catService: CatService) {}\n"
619+
"}\n"},
620+
{"apps/api/src/other/service.ts", "export function service(): number {\n"
621+
" return 1;\n"
622+
"}\n"},
623+
};
624+
625+
EILangProj lp;
626+
cbm_store_t *store = ei_index_files(&lp, f, (int)(sizeof(f) / sizeof(f[0])));
627+
ASSERT_NOT_NULL(store);
628+
629+
int64_t controller_id = ei_node_id_for_file_label(store, lp.project, controller_path, "File");
630+
ASSERT_GT(controller_id, 0);
631+
632+
cbm_edge_t *edges = NULL;
633+
int edge_count = 0;
634+
ASSERT_EQ(
635+
cbm_store_find_edges_by_source_type(store, controller_id, "IMPORTS", &edges, &edge_count),
636+
CBM_STORE_OK);
637+
638+
bool saw_import = false;
639+
bool target_ok = false;
640+
for (int i = 0; i < edge_count; i++) {
641+
const char *props = edges[i].properties_json ? edges[i].properties_json : "";
642+
if (!strstr(props, "\"local_name\":\"CatService\"")) {
643+
continue;
644+
}
645+
cbm_node_t *target = (cbm_node_t *)calloc(1, sizeof(cbm_node_t));
646+
ASSERT_NOT_NULL(target);
647+
ASSERT_EQ(cbm_store_find_node_by_id(store, edges[i].target_id, target), CBM_STORE_OK);
648+
saw_import = true;
649+
target_ok = target->file_path && strcmp(target->file_path, service_path) == 0;
650+
cbm_store_free_nodes(target, 1);
651+
}
652+
cbm_store_free_edges(edges, edge_count);
653+
ei_cleanup(&lp, store);
654+
655+
ASSERT_TRUE(saw_import);
656+
ASSERT_TRUE(target_ok);
657+
PASS();
658+
}
659+
600660
/* TypeScript: default import `import helper from './util'`. */
601661
TEST(ei_typescript_default_import) {
602662
static const EILangFile f[] = {
@@ -2564,6 +2624,7 @@ SUITE(edge_imports) {
25642624
/* ── GREEN GUARDS — TypeScript (must stay passing) ── */
25652625
RUN_TEST(ei_typescript_named_relative_import);
25662626
RUN_TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682);
2627+
RUN_TEST(ei_typescript_dotted_alias_import_targets_source_module);
25672628
RUN_TEST(ei_typescript_default_import);
25682629
RUN_TEST(ei_typescript_namespace_import);
25692630
RUN_TEST(ei_typescript_aliased_import);

‎tests/test_path_alias.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ TEST(path_alias_strips_ext) {
155155
PASS();
156156
}
157157

158+
/* Same extension set as the relative resolver (#1682); dotted basenames
159+
* without a module extension stay intact. */
160+
TEST(path_alias_strips_module_ext_keeps_dotted_basename) {
161+
cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*");
162+
char *r = cbm_path_alias_resolve(m, "@/lib/util.mjs");
163+
ASSERT_NOT_NULL(r);
164+
ASSERT_STR_EQ(r, "src/lib/util");
165+
free(r);
166+
r = cbm_path_alias_resolve(m, "@/cats/cat.service");
167+
ASSERT_NOT_NULL(r);
168+
ASSERT_STR_EQ(r, "src/cats/cat.service");
169+
free(r);
170+
free_map(m);
171+
PASS();
172+
}
173+
158174
/* ── baseUrl fallback for non-relative, non-package imports ────── */
159175

160176
TEST(path_alias_baseurl_fallback) {
@@ -557,6 +573,7 @@ void suite_path_alias(void) {
557573
RUN_TEST(path_alias_specificity_longest_first);
558574
RUN_TEST(path_alias_exact_match);
559575
RUN_TEST(path_alias_strips_ext);
576+
RUN_TEST(path_alias_strips_module_ext_keeps_dotted_basename);
560577
RUN_TEST(path_alias_baseurl_fallback);
561578
RUN_TEST(path_alias_null_safety);
562579
RUN_TEST(path_alias_find_for_file_nearest_ancestor);

0 commit comments

Comments
 (0)