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
69 changes: 68 additions & 1 deletion src/pipeline/pass_pkgmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,73 @@ 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 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, 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, '?');
if (query) {
*query = '\0';
}
char *ext = strrchr(base, '.');
char *fragment = ext ? strchr(ext, '#') : NULL;
if (fragment) {
*fragment = '\0';
}
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) {
Expand All @@ -1628,7 +1695,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 = cbm_pipeline_fqn_module(ctx->project_name, aliased);
char *qn = alias_module_qn(ctx, module_path, aliased);
free(aliased);
return qn;
}
Expand Down
94 changes: 94 additions & 0 deletions tests/repro/repro_issue514.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,101 @@ TEST(repro_issue514_data_flow_surfaces_arg_expr) {
PASS();
}

/* 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;
}

/* 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/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"
"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 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, caller_qn));

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_alias_import_callers);
}
Loading
Loading