From 803cbf5cf645441fcc622dc4824a83bf39c279bb Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Fri, 2 Oct 2026 11:10:21 +0200 Subject: [PATCH 1/4] fix(514): preserve dotted filename Signed-off-by: Antonio Espinosa --- src/pipeline/pass_pkgmap.c | 5 +- src/pipeline/path_alias.c | 21 ++++--- tests/repro/repro_issue514.c | 110 +++++++++++++++++++++++++++++++++++ tests/test_edge_imports.c | 61 +++++++++++++++++++ tests/test_path_alias.c | 17 ++++++ 5 files changed, 204 insertions(+), 10 deletions(-) diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index afedba700e..a2a29944b6 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -1550,7 +1550,10 @@ char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *sou if (amap) { char *aliased = cbm_path_alias_resolve(amap, module_path); if (aliased) { - char *qn = cbm_pipeline_fqn_module(ctx->project_name, aliased); + /* Like step 1: the alias resolver already removed an explicit + * file extension, so keep dotted basenames such as + * `cat.service` intact. */ + char *qn = cbm_pipeline_fqn_folder(ctx->project_name, aliased); free(aliased); return qn; } diff --git a/src/pipeline/path_alias.c b/src/pipeline/path_alias.c index 7ef54c3691..dc144a1683 100644 --- a/src/pipeline/path_alias.c +++ b/src/pipeline/path_alias.c @@ -47,20 +47,23 @@ enum { /* ── Helpers ───────────────────────────────────────────────────── */ -/* Strip .ts/.tsx/.js/.jsx in place. Returns its argument. */ +/* Strip an explicit JS/TS module extension in place, keeping dotted + * basenames such as "cat.service". Same set as the relative resolver in + * fqn.c. Returns its argument. */ static char *strip_resolved_ext(char *path) { if (!path) { return path; } + static const char *const extensions[] = { + ".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts", ".json", + }; size_t len = strlen(path); - if (len > 3 && path[len - 3] == '.' && (path[len - 2] == 't' || path[len - 2] == 'j') && - path[len - 1] == 's') { - path[len - 3] = '\0'; - return path; - } - if (len > 4 && path[len - 4] == '.' && (path[len - 3] == 't' || path[len - 3] == 'j') && - path[len - 2] == 's' && path[len - 1] == 'x') { - path[len - 4] = '\0'; + for (size_t i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++) { + size_t ext_len = strlen(extensions[i]); + if (len > ext_len && strcmp(path + len - ext_len, extensions[i]) == 0) { + path[len - ext_len] = '\0'; + break; + } } return path; } diff --git a/tests/repro/repro_issue514.c b/tests/repro/repro_issue514.c index 96f255045a..abf2be6259 100644 --- a/tests/repro/repro_issue514.c +++ b/tests/repro/repro_issue514.c @@ -197,7 +197,117 @@ TEST(repro_issue514_data_flow_surfaces_arg_expr) { PASS(); } +/* + * Sub-claim (B), minimal form from the issue thread: a NestJS controller + * calling a service injected through a constructor parameter property + * (`constructor(private readonly catService: CatService)`) must produce an + * inbound caller for CatService.findAll. + */ +static const RFile k_nest_files[] = { + {"src/cats/cat.service.ts", "export class CatService {\n" + " findAll(): string[] {\n" + " return [\"a\"];\n" + " }\n" + "}\n"}, + {"src/cats/cat.controller.ts", "import { CatService } from \"./cat.service\";\n" + "\n" + "export class CatController {\n" + " constructor(private readonly catService: CatService) {}\n" + "\n" + " list() {\n" + " return this.catService.findAll();\n" + " }\n" + "}\n"}, +}; + +/* Run trace_path inbound depth 1 on fn_name against the fixture project. + * rh_index_files restores CBM_CACHE_DIR after indexing; point the query at + * the fixture's cache so trace_path finds the project. */ +static char *rh514_trace_inbound(RProj *lp, const char *fn_name) { + char args[512]; + snprintf(args, sizeof(args), + "{\"function_name\":\"%s\"," + "\"project\":\"%s\"," + "\"direction\":\"inbound\"," + "\"depth\":1}", + fn_name, lp->project); + + const char *prior_cache_dir = getenv("CBM_CACHE_DIR"); + char *saved_cache_dir = prior_cache_dir ? strdup(prior_cache_dir) : NULL; + cbm_setenv("CBM_CACHE_DIR", lp->cachedir, 1); + char *resp = cbm_mcp_handle_tool(lp->srv, "trace_path", args); + if (saved_cache_dir) { + cbm_setenv("CBM_CACHE_DIR", saved_cache_dir, 1); + free(saved_cache_dir); + } else { + cbm_unsetenv("CBM_CACHE_DIR"); + } + return resp; +} + +TEST(repro_issue514_ctor_param_property_callers) { + RProj lp; + cbm_store_t *store = + rh_index_files(&lp, k_nest_files, (int)(sizeof(k_nest_files) / sizeof(k_nest_files[0]))); + ASSERT_NOT_NULL(store); + + char *resp = rh514_trace_inbound(&lp, "findAll"); + ASSERT_NOT_NULL(resp); + fprintf(stderr, " [514] trace_path inbound response: %.400s\n", resp); + + ASSERT_NULL(strstr(resp, "function not found")); + ASSERT_NOT_NULL(strstr(resp, "CatController.list")); + + free(resp); + rh_cleanup(&lp, store); + PASS(); +} + +/* Same DI shape, but the injected class is imported through a tsconfig + * `paths` alias, the norm in NestJS monorepos. The alias branch of import + * resolution stripped the dotted basename (`cat.service` -> `cat`), so the + * IMPORTS edge missed the module and the DI call lost its target. */ +static const RFile k_nest_alias_files[] = { + {"apps/api/tsconfig.json", + "{\"compilerOptions\":{\"baseUrl\":\"./\",\"paths\":{\"@app/*\":[\"src/*\"]}}}\n"}, + {"apps/api/src/cats/cat.service.ts", "export class CatService {\n" + " findAll(): string[] {\n" + " return [\"a\"];\n" + " }\n" + "}\n"}, + {"apps/api/src/cats/cat.controller.ts", + "import { CatService } from \"@app/cats/cat.service\";\n" + "\n" + "export class CatController {\n" + " constructor(private readonly catService: CatService) {}\n" + "\n" + " list() {\n" + " return this.catService.findAll();\n" + " }\n" + "}\n"}, +}; + +TEST(repro_issue514_ctor_param_property_alias_import_callers) { + RProj lp; + cbm_store_t *store = rh_index_files( + &lp, k_nest_alias_files, (int)(sizeof(k_nest_alias_files) / sizeof(k_nest_alias_files[0]))); + ASSERT_NOT_NULL(store); + + char *resp = rh514_trace_inbound(&lp, "findAll"); + ASSERT_NOT_NULL(resp); + fprintf(stderr, " [514] trace_path inbound (alias) response: %.400s\n", resp); + + ASSERT_NULL(strstr(resp, "function not found")); + ASSERT_NOT_NULL(strstr(resp, "CatController.list")); + + free(resp); + rh_cleanup(&lp, store); + PASS(); +} + /* ── Suite ─────────────────────────────────────────────────────────────────── */ SUITE(repro_issue514) { RUN_TEST(repro_issue514_data_flow_surfaces_arg_expr); + RUN_TEST(repro_issue514_ctor_param_property_callers); + RUN_TEST(repro_issue514_ctor_param_property_alias_import_callers); } diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index 21f433c319..efa1c1dd10 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -597,6 +597,66 @@ TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682) { PASS(); } +/* #514 follow-up: the tsconfig `paths` branch must preserve dotted basenames + * like the relative branch (#1682). It stripped `.service` from + * `@app/cats/cat.service`, missed the module, and bound the import by last + * segment to an unrelated node. */ +TEST(ei_typescript_dotted_alias_import_targets_source_module) { + static const char *service_path = "apps/api/src/cats/cat.service.ts"; + static const char *controller_path = "apps/api/src/cats/cat.controller.ts"; + static const EILangFile f[] = { + {"apps/api/tsconfig.json", + "{\"compilerOptions\":{\"baseUrl\":\"./\",\"paths\":{\"@app/*\":[\"src/*\"]}}}\n"}, + {"apps/api/src/cats/cat.service.ts", "export class CatService {\n" + " findAll(): string[] {\n" + " return [\"a\"];\n" + " }\n" + "}\n"}, + {"apps/api/src/cats/cat.controller.ts", + "import { CatService } from \"@app/cats/cat.service\";\n" + "export class CatController {\n" + " constructor(private readonly catService: CatService) {}\n" + "}\n"}, + {"apps/api/src/other/service.ts", "export function service(): number {\n" + " return 1;\n" + "}\n"}, + }; + + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, f, (int)(sizeof(f) / sizeof(f[0]))); + ASSERT_NOT_NULL(store); + + int64_t controller_id = ei_node_id_for_file_label(store, lp.project, controller_path, "File"); + ASSERT_GT(controller_id, 0); + + cbm_edge_t *edges = NULL; + int edge_count = 0; + ASSERT_EQ( + cbm_store_find_edges_by_source_type(store, controller_id, "IMPORTS", &edges, &edge_count), + CBM_STORE_OK); + + bool saw_import = false; + bool target_ok = false; + for (int i = 0; i < edge_count; i++) { + const char *props = edges[i].properties_json ? edges[i].properties_json : ""; + if (!strstr(props, "\"local_name\":\"CatService\"")) { + continue; + } + cbm_node_t *target = (cbm_node_t *)calloc(1, sizeof(cbm_node_t)); + ASSERT_NOT_NULL(target); + ASSERT_EQ(cbm_store_find_node_by_id(store, edges[i].target_id, target), CBM_STORE_OK); + saw_import = true; + target_ok = target->file_path && strcmp(target->file_path, service_path) == 0; + cbm_store_free_nodes(target, 1); + } + cbm_store_free_edges(edges, edge_count); + ei_cleanup(&lp, store); + + ASSERT_TRUE(saw_import); + ASSERT_TRUE(target_ok); + PASS(); +} + /* TypeScript: default import `import helper from './util'`. */ TEST(ei_typescript_default_import) { static const EILangFile f[] = { @@ -2496,6 +2556,7 @@ SUITE(edge_imports) { /* ── GREEN GUARDS — TypeScript (must stay passing) ── */ RUN_TEST(ei_typescript_named_relative_import); RUN_TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682); + RUN_TEST(ei_typescript_dotted_alias_import_targets_source_module); RUN_TEST(ei_typescript_default_import); RUN_TEST(ei_typescript_namespace_import); RUN_TEST(ei_typescript_aliased_import); diff --git a/tests/test_path_alias.c b/tests/test_path_alias.c index 9158c04535..0361227b49 100644 --- a/tests/test_path_alias.c +++ b/tests/test_path_alias.c @@ -155,6 +155,22 @@ TEST(path_alias_strips_ext) { PASS(); } +/* Same extension set as the relative resolver (#1682); dotted basenames + * without a module extension stay intact. */ +TEST(path_alias_strips_module_ext_keeps_dotted_basename) { + cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*"); + char *r = cbm_path_alias_resolve(m, "@/lib/util.mjs"); + ASSERT_NOT_NULL(r); + ASSERT_STR_EQ(r, "src/lib/util"); + free(r); + r = cbm_path_alias_resolve(m, "@/cats/cat.service"); + ASSERT_NOT_NULL(r); + ASSERT_STR_EQ(r, "src/cats/cat.service"); + free(r); + free_map(m); + PASS(); +} + /* ── baseUrl fallback for non-relative, non-package imports ────── */ TEST(path_alias_baseurl_fallback) { @@ -557,6 +573,7 @@ void suite_path_alias(void) { RUN_TEST(path_alias_specificity_longest_first); RUN_TEST(path_alias_exact_match); RUN_TEST(path_alias_strips_ext); + RUN_TEST(path_alias_strips_module_ext_keeps_dotted_basename); RUN_TEST(path_alias_baseurl_fallback); RUN_TEST(path_alias_null_safety); RUN_TEST(path_alias_find_for_file_nearest_ancestor); From 2dffaf033f67c825dc1016b9da81523156499dd5 Mon Sep 17 00:00:00 2001 From: chidev Date: Fri, 13 Nov 2026 11:19:44 -0600 Subject: [PATCH 2/4] fix(514): keep non-JS alias imports on their module A path-alias import of a dotted TypeScript file (~/cats/cat.service) must keep the dotted name, but an alias import of a non-JS file (~/styles/globals.css, ~/styles.css?url) must still drop its extension, as it does on main. Keeping every extension sent the CSS imports away from their module nodes. The alias branch of cbm_pipeline_resolve_module now decides from the file path, because filename dots and path separators both become dots in a qualified name: - an explicit JS/TS or JSON extension names its module; - a module at exactly the imported path wins (styles.css, also beside styles.css.ts or styles/css.ts); - else a dotted-name module at that path plus a module extension wins (theme.css.ts imported as ~/theme.css); - else the extension drops, as on main. A bundler ?query and a #fragment are cut before the extension is read. The shared alias resolver in path_alias.c is back to main's behaviour, so doc links keep the file identity they had. Refs #514 Signed-off-by: chidev --- src/pipeline/pass_pkgmap.c | 58 ++++++++++++++++++++-- src/pipeline/path_alias.c | 21 ++++---- tests/repro/repro_issue514.c | 76 ++++++++++++----------------- tests/test_edge_imports.c | 95 ++++++++++++++++++++++++++++++++++-- tests/test_path_alias.c | 17 ------- 5 files changed, 183 insertions(+), 84 deletions(-) diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index a2a29944b6..2c5f538dcb 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -1524,6 +1524,59 @@ static char *resolve_backslash_prefix(CBMHashTable *map, const char *module_path return NULL; } +/* Map an alias-resolved file path to its module QN. Explicit JS/TS and + * JSON extensions name a module even when a dotted-name module also exists. + * For another indexed extension a QN alone proves nothing: filename dots and + * path separators both become dots. So the file path decides, in this order: + * a module at exactly that path (styles.css, also beside styles.css.ts), then + * a dotted-name module at that path plus a module extension (theme.css.ts + * imported as ~/theme.css), then the extension drops. A bundler ?query or a + * #fragment is not part of the extension. Unknown suffixes such as + * cat.service stay intact. The shared alias resolver is unchanged, so doc + * links keep the file identity they had. */ +static char *alias_module_qn(const cbm_pipeline_ctx_t *ctx, char *aliased) { + char *query = strchr(aliased, '?'); + if (query) { + *query = '\0'; + } + const char *slash = strrchr(aliased, '/'); + const char *base = slash ? slash + SKIP_ONE : aliased; + char *ext = strrchr(base, '.'); + char *fragment = ext ? strchr(ext, '#') : NULL; + if (fragment) { + *fragment = '\0'; + } + static const char *const module_exts[] = {".ts", ".tsx", ".js", ".jsx", ".mjs", + ".cjs", ".mts", ".cts", ".json"}; + for (size_t i = 0; ext && i < sizeof(module_exts) / sizeof(module_exts[0]); i++) { + if (strcmp(ext, module_exts[i]) == 0) { + return cbm_pipeline_fqn_module(ctx->project_name, aliased); + } + } + char *kept = cbm_pipeline_fqn_folder(ctx->project_name, aliased); + if (!kept || !ext || ext == base || cbm_language_for_extension(ext) == CBM_LANG_COUNT) { + return kept; + } + char *stripped = cbm_pipeline_fqn_module(ctx->project_name, aliased); + const cbm_gbuf_node_t *node = + ctx->gbuf && stripped ? cbm_gbuf_find_by_qn(ctx->gbuf, stripped) : NULL; + bool exact = node && node->label && strcmp(node->label, "Module") == 0 && node->file_path && + strcmp(node->file_path, aliased) == 0; + node = ctx->gbuf && !exact ? cbm_gbuf_find_by_qn(ctx->gbuf, kept) : NULL; + size_t len = strlen(aliased); + if (node && node->label && strcmp(node->label, "Module") == 0 && node->file_path && + strncmp(node->file_path, aliased, len) == 0) { + for (size_t i = 0; i < sizeof(module_exts) / sizeof(module_exts[0]); i++) { + if (strcmp(node->file_path + len, module_exts[i]) == 0) { + safe_free(stripped); + return kept; + } + } + } + safe_free(kept); + return stripped; +} + char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *source_rel, const char *module_path) { if (!ctx || !module_path) { @@ -1550,10 +1603,7 @@ char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *sou if (amap) { char *aliased = cbm_path_alias_resolve(amap, module_path); if (aliased) { - /* Like step 1: the alias resolver already removed an explicit - * file extension, so keep dotted basenames such as - * `cat.service` intact. */ - char *qn = cbm_pipeline_fqn_folder(ctx->project_name, aliased); + char *qn = alias_module_qn(ctx, aliased); free(aliased); return qn; } diff --git a/src/pipeline/path_alias.c b/src/pipeline/path_alias.c index dc144a1683..7ef54c3691 100644 --- a/src/pipeline/path_alias.c +++ b/src/pipeline/path_alias.c @@ -47,23 +47,20 @@ enum { /* ── Helpers ───────────────────────────────────────────────────── */ -/* Strip an explicit JS/TS module extension in place, keeping dotted - * basenames such as "cat.service". Same set as the relative resolver in - * fqn.c. Returns its argument. */ +/* Strip .ts/.tsx/.js/.jsx in place. Returns its argument. */ static char *strip_resolved_ext(char *path) { if (!path) { return path; } - static const char *const extensions[] = { - ".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts", ".json", - }; size_t len = strlen(path); - for (size_t i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++) { - size_t ext_len = strlen(extensions[i]); - if (len > ext_len && strcmp(path + len - ext_len, extensions[i]) == 0) { - path[len - ext_len] = '\0'; - break; - } + if (len > 3 && path[len - 3] == '.' && (path[len - 2] == 't' || path[len - 2] == 'j') && + path[len - 1] == 's') { + path[len - 3] = '\0'; + return path; + } + if (len > 4 && path[len - 4] == '.' && (path[len - 3] == 't' || path[len - 3] == 'j') && + path[len - 2] == 's' && path[len - 1] == 'x') { + path[len - 4] = '\0'; } return path; } diff --git a/tests/repro/repro_issue514.c b/tests/repro/repro_issue514.c index abf2be6259..18d078e251 100644 --- a/tests/repro/repro_issue514.c +++ b/tests/repro/repro_issue514.c @@ -197,29 +197,6 @@ TEST(repro_issue514_data_flow_surfaces_arg_expr) { PASS(); } -/* - * Sub-claim (B), minimal form from the issue thread: a NestJS controller - * calling a service injected through a constructor parameter property - * (`constructor(private readonly catService: CatService)`) must produce an - * inbound caller for CatService.findAll. - */ -static const RFile k_nest_files[] = { - {"src/cats/cat.service.ts", "export class CatService {\n" - " findAll(): string[] {\n" - " return [\"a\"];\n" - " }\n" - "}\n"}, - {"src/cats/cat.controller.ts", "import { CatService } from \"./cat.service\";\n" - "\n" - "export class CatController {\n" - " constructor(private readonly catService: CatService) {}\n" - "\n" - " list() {\n" - " return this.catService.findAll();\n" - " }\n" - "}\n"}, -}; - /* Run trace_path inbound depth 1 on fn_name against the fixture project. * rh_index_files restores CBM_CACHE_DIR after indexing; point the query at * the fixture's cache so trace_path finds the project. */ @@ -245,25 +222,7 @@ static char *rh514_trace_inbound(RProj *lp, const char *fn_name) { return resp; } -TEST(repro_issue514_ctor_param_property_callers) { - RProj lp; - cbm_store_t *store = - rh_index_files(&lp, k_nest_files, (int)(sizeof(k_nest_files) / sizeof(k_nest_files[0]))); - ASSERT_NOT_NULL(store); - - char *resp = rh514_trace_inbound(&lp, "findAll"); - ASSERT_NOT_NULL(resp); - fprintf(stderr, " [514] trace_path inbound response: %.400s\n", resp); - - ASSERT_NULL(strstr(resp, "function not found")); - ASSERT_NOT_NULL(strstr(resp, "CatController.list")); - - free(resp); - rh_cleanup(&lp, store); - PASS(); -} - -/* Same DI shape, but the injected class is imported through a tsconfig +/* The injected class is imported through a tsconfig * `paths` alias, the norm in NestJS monorepos. The alias branch of import * resolution stripped the dotted basename (`cat.service` -> `cat`), so the * IMPORTS edge missed the module and the DI call lost its target. */ @@ -275,6 +234,8 @@ static const RFile k_nest_alias_files[] = { " return [\"a\"];\n" " }\n" "}\n"}, + {"apps/api/src/other/cat.service.ts", + "export class CatService { findAll(): string[] { return [\"decoy\"]; } }\n"}, {"apps/api/src/cats/cat.controller.ts", "import { CatService } from \"@app/cats/cat.service\";\n" "\n" @@ -293,12 +254,36 @@ TEST(repro_issue514_ctor_param_property_alias_import_callers) { &lp, k_nest_alias_files, (int)(sizeof(k_nest_alias_files) / sizeof(k_nest_alias_files[0]))); ASSERT_NOT_NULL(store); - char *resp = rh514_trace_inbound(&lp, "findAll"); + char caller_qn[512]; + char target_qn[512]; + snprintf(caller_qn, sizeof(caller_qn), "%s.apps.api.src.cats.cat.controller.CatController.list", + lp.project); + snprintf(target_qn, sizeof(target_qn), "%s.apps.api.src.cats.cat.service.CatService.findAll", + lp.project); + cbm_node_t *caller = calloc(1, sizeof(*caller)); + cbm_node_t *target = calloc(1, sizeof(*target)); + ASSERT_NOT_NULL(caller); + ASSERT_NOT_NULL(target); + ASSERT_EQ(cbm_store_find_node_by_qn(store, lp.project, caller_qn, caller), CBM_STORE_OK); + ASSERT_EQ(cbm_store_find_node_by_qn(store, lp.project, target_qn, target), CBM_STORE_OK); + cbm_edge_t *edges = NULL; + int edge_count = 0; + ASSERT_EQ(cbm_store_find_edges_by_source_type(store, caller->id, "CALLS", &edges, &edge_count), + CBM_STORE_OK); + fprintf(stderr, " [514] alias caller CALLS edges=%d; expected target=%s\n", edge_count, + target_qn); + ASSERT_EQ(edge_count, 1); + ASSERT_EQ(edges[0].target_id, target->id); + cbm_store_free_edges(edges, edge_count); + cbm_store_free_nodes(caller, 1); + cbm_store_free_nodes(target, 1); + + /* A qualified lookup selects the intended method despite its decoy. */ + char *resp = rh514_trace_inbound(&lp, target_qn); ASSERT_NOT_NULL(resp); fprintf(stderr, " [514] trace_path inbound (alias) response: %.400s\n", resp); - ASSERT_NULL(strstr(resp, "function not found")); - ASSERT_NOT_NULL(strstr(resp, "CatController.list")); + ASSERT_NOT_NULL(strstr(resp, caller_qn)); free(resp); rh_cleanup(&lp, store); @@ -308,6 +293,5 @@ TEST(repro_issue514_ctor_param_property_alias_import_callers) { /* ── Suite ─────────────────────────────────────────────────────────────────── */ SUITE(repro_issue514) { RUN_TEST(repro_issue514_data_flow_surfaces_arg_expr); - RUN_TEST(repro_issue514_ctor_param_property_callers); RUN_TEST(repro_issue514_ctor_param_property_alias_import_callers); } diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index efa1c1dd10..af94ac3c5c 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -273,7 +273,7 @@ typedef struct { } EIFileEdgeExpect; static int ei_file_edges_match(const EILangFile *files, int nfiles, const EIFileEdgeExpect *want, - int nwant, int expected_count) { + int nwant, int expected_count, const char *target_label) { EILangProj lp; cbm_store_t *store = ei_index_files(&lp, files, nfiles); int ok = store && cbm_store_count_edges_by_type(store, lp.project, "IMPORTS") == expected_count; @@ -293,7 +293,14 @@ static int ei_file_edges_match(const EILangFile *files, int nfiles, const EIFile cbm_node_t target; memset(&target, 0, sizeof(target)); if (cbm_store_find_node_by_id(store, edges[j].target_id, &target) == CBM_STORE_OK) { - found = target.file_path && strcmp(target.file_path, want[i].target) == 0; + found = target.file_path && strcmp(target.file_path, want[i].target) == 0 && + (!target_label || + (target.label && strcmp(target.label, target_label) == 0)); + if (!found && target_label && edge_count == 1) { + fprintf(stderr, " [IMPORTS] %s -> %s (%s), expected %s (%s)\n", + want[i].importer, target.file_path ? target.file_path : "", + target.label ? target.label : "", want[i].target, target_label); + } cbm_node_free_fields(&target); } if (found) { @@ -359,7 +366,7 @@ TEST(ei_relative_astro_css_imports_issue2558) { }; ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want, - (int)(sizeof(want) / sizeof(want[0])), 12)); + (int)(sizeof(want) / sizeof(want[0])), 12, NULL)); PASS(); } @@ -386,7 +393,7 @@ TEST(ei_nix_parent_relative_import_issue2797) { }; ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want, - (int)(sizeof(want) / sizeof(want[0])), 3)); + (int)(sizeof(want) / sizeof(want[0])), 3, NULL)); PASS(); } @@ -404,7 +411,7 @@ TEST(ei_thrift_parent_relative_import_issue2797) { }; ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want, - (int)(sizeof(want) / sizeof(want[0])), 1)); + (int)(sizeof(want) / sizeof(want[0])), 1, NULL)); PASS(); } @@ -657,6 +664,83 @@ TEST(ei_typescript_dotted_alias_import_targets_source_module) { PASS(); } +/* The alias branch keeps a dotted basename, but an alias import of a non-JS + * file still names that file: `~/styles/globals.css` and the bundler query + * form `~/styles.css?url` reach the CSS modules, not src/styles/css.ts or the + * unrelated `css` function a last-segment guess finds. A dotted name that is + * itself a module stays whole: theme.css.ts imported as `~/theme.css` or + * `~/theme.css.ts`, and `~/cats/cat.service`. Explicit .mjs/.cjs/.mts/.cts/.json imports + * drop their module extension even beside a dotted TypeScript module. The + * reverse import ~/styles/css still names src/styles/css.ts, and ~/cat.service + * must keep its basename even with src/cat/service.ts alongside it. A file + * that exists under the imported name wins over a dotted module beside it + * (tokens.css over tokens.css.ts), and a #fragment does not hide the + * extension (~/tokens.css#frag from another folder, with unrelated functions + * named like its segments). */ +TEST(ei_typescript_alias_import_non_js_file_targets_its_module) { + static const EILangFile f[] = { + {"tsconfig.json", + "{\"compilerOptions\":{\"baseUrl\":\".\",\"paths\":{\"~/*\":[\"src/*\"]}}}\n"}, + {"src/styles/globals.css", "body { margin: 0; }\n"}, + {"src/styles.css", "html { color: black; }\n"}, + {"src/styles/css.ts", "export const decoy = 1;\n"}, + {"src/cat.service.ts", "export const cat = 1;\n"}, + {"src/cat/service.ts", "export const decoy = 2;\n"}, + {"src/theme.css.ts", "export const vars = { gap: 4 };\n"}, + {"src/worker.task.mjs", "export function run() {}\n"}, + {"src/worker.task.mjs.ts", "export function other() {}\n"}, + {"src/helper.task.cjs", "exports.helper = function() {};\n"}, + {"src/typed.task.mts", "export function typed() {}\n"}, + {"src/compat.task.cts", "export function compat() {}\n"}, + {"src/config.task.json", "{\"gap\":4}\n"}, + {"src/cats/cat.service.ts", "export class CatService {\n" + " findAll(): string[] { return []; }\n" + "}\n"}, + {"src/lib/highlighter.ts", "export function css(): number { return 1; }\n" + "export function service(): number { return 2; }\n"}, + {"src/app.ts", "import '~/styles/globals.css';\n" + "import appCss from '~/styles.css?url';\n" + "import { run } from '~/worker.task.mjs';\n" + "import { helper } from '~/helper.task.cjs';\n" + "import { typed } from '~/typed.task.mts';\n" + "import { compat } from '~/compat.task.cts';\n" + "import config from '~/config.task.json';\n" + "import { CatService } from '~/cats/cat.service';\n" + "export function app(): unknown[] { return [appCss, CatService]; }\n"}, + {"src/panel.ts", "import { vars } from '~/theme.css';\n" + "export function panel(): number { return vars.gap; }\n"}, + {"src/card.ts", "import { vars } from '~/theme.css.ts';\n" + "export function card(): number { return vars.gap; }\n"}, + {"src/plain.ts", "import appCss from '~/styles.css';\n"}, + {"src/nested.ts", "import { decoy } from '~/styles/css';\n"}, + {"src/cat-user.ts", "import { cat } from '~/cat.service';\n"}, + {"src/tokens.css", "a { gap: 4px; }\n"}, + {"src/tokens.css.ts", "export const tokens = 4;\n"}, + {"src/tokens-plain.ts", "import tokensCss from '~/tokens.css';\n"}, + {"src/tokens-url.ts", "import tokensUrl from '~/tokens.css?url';\n"}, + {"aaa/decoy.ts", "export function tokens(): number { return 1; }\n" + "export function frag(): number { return 2; }\n"}, + {"apps/frag.ts", "import tokensFrag from '~/tokens.css#frag';\n"}, + {"src/lib/worker.ts", "export const w = 1;\n"}, + {"src/worker-user.ts", "import W from '~/lib/worker.ts?worker';\n"}, + }; + static const EIFileEdgeExpect want[] = { + {"src/app.ts", "src/styles/globals.css"}, {"src/app.ts", "src/styles.css"}, + {"src/app.ts", "src/cats/cat.service.ts"}, {"src/app.ts", "src/worker.task.mjs"}, + {"src/app.ts", "src/helper.task.cjs"}, {"src/app.ts", "src/typed.task.mts"}, + {"src/app.ts", "src/compat.task.cts"}, {"src/app.ts", "src/config.task.json"}, + {"src/panel.ts", "src/theme.css.ts"}, {"src/card.ts", "src/theme.css.ts"}, + {"src/plain.ts", "src/styles.css"}, {"src/nested.ts", "src/styles/css.ts"}, + {"src/cat-user.ts", "src/cat.service.ts"}, {"src/tokens-plain.ts", "src/tokens.css"}, + {"src/tokens-url.ts", "src/tokens.css"}, {"apps/frag.ts", "src/tokens.css"}, + {"src/worker-user.ts", "src/lib/worker.ts"}, + }; + + ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want, + (int)(sizeof(want) / sizeof(want[0])), 17, "Module")); + PASS(); +} + /* TypeScript: default import `import helper from './util'`. */ TEST(ei_typescript_default_import) { static const EILangFile f[] = { @@ -2557,6 +2641,7 @@ SUITE(edge_imports) { RUN_TEST(ei_typescript_named_relative_import); RUN_TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682); RUN_TEST(ei_typescript_dotted_alias_import_targets_source_module); + RUN_TEST(ei_typescript_alias_import_non_js_file_targets_its_module); RUN_TEST(ei_typescript_default_import); RUN_TEST(ei_typescript_namespace_import); RUN_TEST(ei_typescript_aliased_import); diff --git a/tests/test_path_alias.c b/tests/test_path_alias.c index 0361227b49..9158c04535 100644 --- a/tests/test_path_alias.c +++ b/tests/test_path_alias.c @@ -155,22 +155,6 @@ TEST(path_alias_strips_ext) { PASS(); } -/* Same extension set as the relative resolver (#1682); dotted basenames - * without a module extension stay intact. */ -TEST(path_alias_strips_module_ext_keeps_dotted_basename) { - cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*"); - char *r = cbm_path_alias_resolve(m, "@/lib/util.mjs"); - ASSERT_NOT_NULL(r); - ASSERT_STR_EQ(r, "src/lib/util"); - free(r); - r = cbm_path_alias_resolve(m, "@/cats/cat.service"); - ASSERT_NOT_NULL(r); - ASSERT_STR_EQ(r, "src/cats/cat.service"); - free(r); - free_map(m); - PASS(); -} - /* ── baseUrl fallback for non-relative, non-package imports ────── */ TEST(path_alias_baseurl_fallback) { @@ -573,7 +557,6 @@ void suite_path_alias(void) { RUN_TEST(path_alias_specificity_longest_first); RUN_TEST(path_alias_exact_match); RUN_TEST(path_alias_strips_ext); - RUN_TEST(path_alias_strips_module_ext_keeps_dotted_basename); RUN_TEST(path_alias_baseurl_fallback); RUN_TEST(path_alias_null_safety); RUN_TEST(path_alias_find_for_file_nearest_ancestor); From 31de63afb5767e9c30b8344797a7852ad4a47032 Mon Sep 17 00:00:00 2001 From: chidev Date: Fri, 13 Nov 2026 12:57:54 -0600 Subject: [PATCH 3/4] fix(514): keep a question mark in a folder name out of the query cut An alias import of ~/dir?name/file was cut at the question mark and reached src/dir.ts. Only a question mark in the file name starts a bundler query. The dotted-alias test now also checks the Module label. Refs #514 Signed-off-by: chidev --- src/pipeline/pass_pkgmap.c | 12 ++++++------ tests/test_edge_imports.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index 2c5f538dcb..a46dfc5b30 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -1531,16 +1531,16 @@ static char *resolve_backslash_prefix(CBMHashTable *map, const char *module_path * a module at exactly that path (styles.css, also beside styles.css.ts), then * a dotted-name module at that path plus a module extension (theme.css.ts * imported as ~/theme.css), then the extension drops. A bundler ?query or a - * #fragment is not part of the extension. Unknown suffixes such as - * cat.service stay intact. The shared alias resolver is unchanged, so doc - * links keep the file identity they had. */ + * #fragment on the basename is not part of the extension; a `?` in a folder + * name is part of the path. Unknown suffixes such as cat.service stay intact. The shared alias + * resolver is unchanged, so doc links keep the file identity they had. */ static char *alias_module_qn(const cbm_pipeline_ctx_t *ctx, char *aliased) { - char *query = strchr(aliased, '?'); + char *slash = strrchr(aliased, '/'); + char *base = slash ? slash + SKIP_ONE : aliased; + char *query = strchr(base, '?'); if (query) { *query = '\0'; } - const char *slash = strrchr(aliased, '/'); - const char *base = slash ? slash + SKIP_ONE : aliased; char *ext = strrchr(base, '.'); char *fragment = ext ? strchr(ext, '#') : NULL; if (fragment) { diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index af94ac3c5c..ed0a4d9f7f 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -653,7 +653,8 @@ TEST(ei_typescript_dotted_alias_import_targets_source_module) { ASSERT_NOT_NULL(target); ASSERT_EQ(cbm_store_find_node_by_id(store, edges[i].target_id, target), CBM_STORE_OK); saw_import = true; - target_ok = target->file_path && strcmp(target->file_path, service_path) == 0; + target_ok = target->file_path && strcmp(target->file_path, service_path) == 0 && + target->label && strcmp(target->label, "Module") == 0; cbm_store_free_nodes(target, 1); } cbm_store_free_edges(edges, edge_count); @@ -664,6 +665,29 @@ TEST(ei_typescript_dotted_alias_import_targets_source_module) { PASS(); } +#ifndef _WIN32 +/* A `?` in a folder name is part of the path, not a bundler query: the alias + * import ~/dir?name/file names src/dir?name/file.ts, never src/dir.ts. + * Windows file names cannot hold `?`. */ +TEST(ei_typescript_alias_import_keeps_question_mark_folder) { + static const EILangFile f[] = { + {"tsconfig.json", + "{\"compilerOptions\":{\"baseUrl\":\".\",\"paths\":{\"~/*\":[\"src/*\"]}}}\n"}, + {"src/dir?name/file.ts", "export const wanted = 1;\n"}, + {"src/dir.ts", "export const decoy = 2;\n"}, + {"src/user.ts", "import { wanted } from '~/dir?name/file';\n" + "export const u = wanted;\n"}, + }; + static const EIFileEdgeExpect want[] = { + {"src/user.ts", "src/dir?name/file.ts"}, + }; + + ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want, + (int)(sizeof(want) / sizeof(want[0])), 1, "Module")); + PASS(); +} +#endif + /* The alias branch keeps a dotted basename, but an alias import of a non-JS * file still names that file: `~/styles/globals.css` and the bundler query * form `~/styles.css?url` reach the CSS modules, not src/styles/css.ts or the @@ -2642,6 +2666,9 @@ SUITE(edge_imports) { RUN_TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682); RUN_TEST(ei_typescript_dotted_alias_import_targets_source_module); RUN_TEST(ei_typescript_alias_import_non_js_file_targets_its_module); +#ifndef _WIN32 + RUN_TEST(ei_typescript_alias_import_keeps_question_mark_folder); +#endif RUN_TEST(ei_typescript_default_import); RUN_TEST(ei_typescript_namespace_import); RUN_TEST(ei_typescript_aliased_import); From 57205dcf5157e9b8d8f13bed5f5dcc736bc62f5e Mon Sep 17 00:00:00 2001 From: chidev Date: Fri, 13 Nov 2026 14:10:35 -0600 Subject: [PATCH 4/4] fix(514): do not read a spelled module extension twice on an alias import The alias resolver removes a spelled module extension. The module-name helper then read the next dotted part as another extension, so ~/tokens.css.ts reached tokens.css and ~/worker.task.mjs.ts reached worker.task.mjs when both files exist. What is left after the resolver removed a spelled extension is now the whole file stem. Refs #514 Signed-off-by: chidev --- src/pipeline/pass_pkgmap.c | 22 ++++++++++++++++++---- tests/test_edge_imports.c | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index a46dfc5b30..d8446b2e4a 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -1534,7 +1534,23 @@ static char *resolve_backslash_prefix(CBMHashTable *map, const char *module_path * #fragment on the basename is not part of the extension; a `?` in a folder * name is part of the path. Unknown suffixes such as cat.service stay intact. The shared alias * resolver is unchanged, so doc links keep the file identity they had. */ -static char *alias_module_qn(const cbm_pipeline_ctx_t *ctx, char *aliased) { +static char *alias_module_qn(const cbm_pipeline_ctx_t *ctx, const char *module_path, + char *aliased) { + static const char *const module_exts[] = {".ts", ".tsx", ".js", ".jsx", ".mjs", + ".cjs", ".mts", ".cts", ".json"}; + /* The import spelled a module extension and the alias resolver removed + * it: what is left is the whole file stem, dots included + * (~/tokens.css.ts names tokens.css.ts, never tokens.css). */ + size_t mlen = strlen(module_path); + size_t alen = strlen(aliased); + for (size_t i = 0; i < sizeof(module_exts) / sizeof(module_exts[0]); i++) { + size_t elen = strlen(module_exts[i]); + bool spelled = mlen > elen && strcmp(module_path + mlen - elen, module_exts[i]) == 0; + bool still_there = alen > elen && strcmp(aliased + alen - elen, module_exts[i]) == 0; + if (spelled && !still_there) { + return cbm_pipeline_fqn_folder(ctx->project_name, aliased); + } + } char *slash = strrchr(aliased, '/'); char *base = slash ? slash + SKIP_ONE : aliased; char *query = strchr(base, '?'); @@ -1546,8 +1562,6 @@ static char *alias_module_qn(const cbm_pipeline_ctx_t *ctx, char *aliased) { if (fragment) { *fragment = '\0'; } - static const char *const module_exts[] = {".ts", ".tsx", ".js", ".jsx", ".mjs", - ".cjs", ".mts", ".cts", ".json"}; for (size_t i = 0; ext && i < sizeof(module_exts) / sizeof(module_exts[0]); i++) { if (strcmp(ext, module_exts[i]) == 0) { return cbm_pipeline_fqn_module(ctx->project_name, aliased); @@ -1603,7 +1617,7 @@ char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *sou if (amap) { char *aliased = cbm_path_alias_resolve(amap, module_path); if (aliased) { - char *qn = alias_module_qn(ctx, aliased); + char *qn = alias_module_qn(ctx, module_path, aliased); free(aliased); return qn; } diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index ed0a4d9f7f..f9d47e2b7c 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -665,6 +665,31 @@ TEST(ei_typescript_dotted_alias_import_targets_source_module) { PASS(); } +/* An import that spells the module extension of a dotted name gets that + * file, not the shorter file beside it: ~/tokens.css.ts is tokens.css.ts + * (never tokens.css), and ~/worker.task.mjs.ts is worker.task.mjs.ts (never + * worker.task.mjs). */ +TEST(ei_typescript_alias_import_spelled_ext_on_dotted_name) { + static const EILangFile f[] = { + {"tsconfig.json", + "{\"compilerOptions\":{\"baseUrl\":\".\",\"paths\":{\"~/*\":[\"src/*\"]}}}\n"}, + {"src/tokens.css", ":root { --gap: 4px; }\n"}, + {"src/tokens.css.ts", "export const tokens = { gap: 4 };\n"}, + {"src/worker.task.mjs", "export function run() {}\n"}, + {"src/worker.task.mjs.ts", "export function typedRun(): void {}\n"}, + {"src/a.ts", "import { tokens } from '~/tokens.css.ts';\nexport const a = tokens;\n"}, + {"src/b.ts", "import { typedRun } from '~/worker.task.mjs.ts';\nexport const b = typedRun;\n"}, + }; + static const EIFileEdgeExpect want[] = { + {"src/a.ts", "src/tokens.css.ts"}, + {"src/b.ts", "src/worker.task.mjs.ts"}, + }; + + ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want, + (int)(sizeof(want) / sizeof(want[0])), 2, "Module")); + PASS(); +} + #ifndef _WIN32 /* A `?` in a folder name is part of the path, not a bundler query: the alias * import ~/dir?name/file names src/dir?name/file.ts, never src/dir.ts. @@ -2666,6 +2691,7 @@ SUITE(edge_imports) { RUN_TEST(ei_typescript_dotted_relative_import_targets_source_module_issue1682); RUN_TEST(ei_typescript_dotted_alias_import_targets_source_module); RUN_TEST(ei_typescript_alias_import_non_js_file_targets_its_module); + RUN_TEST(ei_typescript_alias_import_spelled_ext_on_dotted_name); #ifndef _WIN32 RUN_TEST(ei_typescript_alias_import_keeps_question_mark_folder); #endif