Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(pipeline): resolve build-tool path aliases for cross-file imports (#243)

Peter Cox10a9b83e27f2
diff --git a/Makefile.cbm b/Makefile.cbm
index 0eb8d6e37..351f628b2 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -171,6 +171,7 @@ GRAPH_BUFFER_SRCS = src/graph_buffer/graph_buffer.c
# Pipeline module (new)
PIPELINE_SRCS = \
src/pipeline/fqn.c \
+ src/pipeline/path_alias.c \
src/pipeline/registry.c \
src/pipeline/pipeline.c \
src/pipeline/pipeline_incremental.c \
@@ -295,7 +296,7 @@ TEST_DISCOVER_SRCS = \
TEST_GRAPH_BUFFER_SRCS = tests/test_graph_buffer.c
-TEST_PIPELINE_SRCS = tests/test_registry.c tests/test_pipeline.c tests/test_fqn.c tests/test_configlink.c tests/test_infrascan.c tests/test_worker_pool.c tests/test_parallel.c
+TEST_PIPELINE_SRCS = tests/test_registry.c tests/test_pipeline.c tests/test_fqn.c tests/test_path_alias.c tests/test_configlink.c tests/test_infrascan.c tests/test_worker_pool.c tests/test_parallel.c
TEST_WATCHER_SRCS = tests/test_watcher.c
diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c
index ff96c2312..007c0ce85 100644
--- a/src/pipeline/pass_pkgmap.c
+++ b/src/pipeline/pass_pkgmap.c
@@ -912,6 +912,21 @@ char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *sou
return qn;
}
+ /* 1b. Try build-tool path aliases (tsconfig/jsconfig paths today;
+ * other loaders can register here later). Independent of pkgmap. */
+ if (ctx->path_aliases && source_rel) {
+ const cbm_path_alias_map_t *amap =
+ cbm_path_alias_find_for_file(ctx->path_aliases, source_rel);
+ if (amap) {
+ char *aliased = cbm_path_alias_resolve(amap, module_path);
+ if (aliased) {
+ char *qn = cbm_pipeline_fqn_module(ctx->project_name, aliased);
+ free(aliased);
+ return qn;
+ }
+ }
+ }
+
/* 2. No pkgmap → fall through immediately */
CBMHashTable *pkgmap = cbm_pipeline_get_pkgmap();
if (!pkgmap) {
diff --git a/src/pipeline/path_alias.c b/src/pipeline/path_alias.c
new file mode 100644
index 000000000..3e7ef9dc5
--- /dev/null
+++ b/src/pipeline/path_alias.c
@@ -0,0 +1,455 @@
+/*
+ * path_alias.c — Resolve build-tool path aliases.
+ *
+ * Builds a directory-scoped collection of alias maps from per-language
+ * config files (currently tsconfig.json / jsconfig.json) so the import
+ * resolver can turn "@/lib/auth"-style imports into repo-relative paths.
+ *
+ * Design notes:
+ * - Public types and functions are language-agnostic. Adding a Vite /
+ * Webpack / Python loader means writing a new load_*_file() helper
+ * and registering it in find_alias_files. The resolver, the
+ * collection, and the pipeline integration do not change.
+ * - Sorting uses qsort (n log n). The bubble-sorts that the original
+ * Layer 1b draft used were O(n^2); with up to 256 alias entries
+ * and 256 scoped maps per repo, qsort is the right ceiling.
+ * - The repo walk caps recursion depth and total file count and emits
+ * a warning when either cap fires, so silent truncation on
+ * pathological monorepos shows up in the index log.
+ */
+
+#include "pipeline/path_alias.h"
+
+#include "foundation/compat_fs.h"
+#include "foundation/constants.h"
+#include "foundation/log.h"
+#include "foundation/platform.h"
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <yyjson/yyjson.h>
+
+/* Resource ceilings. Chosen to comfortably cover real-world monorepos
+ * (Next.js Skyline, large nx workspaces) while bounding worst-case
+ * memory and walk time. Cap hits are logged. */
+enum {
+ CBM_PATH_ALIAS_MAX_ENTRIES = 256, /* per single config file */
+ CBM_PATH_ALIAS_MAX_FILES = 256, /* config files per repo walk */
+ CBM_PATH_ALIAS_MAX_FILE_BYTES = 64 * 1024,
+ CBM_PATH_ALIAS_MAX_DEPTH = 32, /* directory recursion depth */
+};
+
+/* ── Helpers ───────────────────────────────────────────────────── */
+
+/* Strip .ts/.tsx/.js/.jsx in place. Returns its argument. */
+static char *strip_resolved_ext(char *path) {
+ if (!path) {
+ return path;
+ }
+ 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';
+ }
+ return path;
+}
+
+/* If target starts with "./" and dir_prefix is non-empty, prepend dir_prefix.
+ * Returns heap-allocated repo-relative target. */
+static char *resolve_target_relative(const char *dir_prefix, const char *target) {
+ if (!target) {
+ return NULL;
+ }
+ const char *t = target;
+ if (t[0] == '.' && t[1] == '/') {
+ t += 2;
+ }
+ if (!dir_prefix || dir_prefix[0] == '\0') {
+ return strdup(t);
+ }
+ size_t dp_len = strlen(dir_prefix);
+ size_t t_len = strlen(t);
+ char *result = malloc(dp_len + 1 + t_len + 1);
+ if (!result) {
+ return NULL;
+ }
+ snprintf(result, dp_len + 1 + t_len + 1, "%s/%s", dir_prefix, t);
+ return result;
+}
+
+/* qsort comparator: alias entries by alias_prefix length, descending. */
+static int cmp_alias_entry_by_specificity(const void *a, const void *b) {
+ const cbm_path_alias_t *ea = a;
+ const cbm_path_alias_t *eb = b;
+ size_t la = strlen(ea->alias_prefix);
+ size_t lb = strlen(eb->alias_prefix);
+ if (lb > la) {
+ return 1;
+ }
+ if (lb < la) {
+ return -1;
+ }
+ return 0;
+}
+
+/* qsort comparator: scopes by dir_prefix length, descending. */
+static int cmp_scope_by_specificity(const void *a, const void *b) {
+ const cbm_path_alias_scope_t *sa = a;
+ const cbm_path_alias_scope_t *sb = b;
+ size_t la = strlen(sa->dir_prefix);
+ size_t lb = strlen(sb->dir_prefix);
+ if (lb > la) {
+ return 1;
+ }
+ if (lb < la) {
+ return -1;
+ }
+ return 0;
+}
+
+/* ── tsconfig.json / jsconfig.json loader ──────────────────────── */
+
+/* Parse compilerOptions.paths and compilerOptions.baseUrl into an alias map.
+ * dir_prefix is the directory of the config file relative to the repo root
+ * (e.g. "apps/manager", or "" for repo root). Returns NULL if the file is
+ * missing, malformed, or has neither a usable paths block nor a baseUrl. */
+static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char *dir_prefix) {
+ FILE *f = fopen(abs_path, "r");
+ if (!f) {
+ return NULL;
+ }
+ fseek(f, 0, SEEK_END);
+ long len = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if (len <= 0 || len > CBM_PATH_ALIAS_MAX_FILE_BYTES) {
+ fclose(f);
+ return NULL;
+ }
+ char *buf = malloc((size_t)len + 1);
+ if (!buf) {
+ fclose(f);
+ return NULL;
+ }
+ size_t nread = fread(buf, 1, (size_t)len, f);
+ fclose(f);
+ buf[nread] = '\0';
+
+ yyjson_read_flag flg = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
+ yyjson_doc *doc = yyjson_read(buf, nread, flg);
+ free(buf);
+ if (!doc) {
+ return NULL;
+ }
+ yyjson_val *root = yyjson_doc_get_root(doc);
+ yyjson_val *compiler_opts = yyjson_obj_get(root, "compilerOptions");
+ if (!compiler_opts) {
+ yyjson_doc_free(doc);
+ return NULL;
+ }
+ yyjson_val *base_url_val = yyjson_obj_get(compiler_opts, "baseUrl");
+ const char *base_url_str = base_url_val ? yyjson_get_str(base_url_val) : NULL;
+ yyjson_val *paths_obj = yyjson_obj_get(compiler_opts, "paths");
+ if (!paths_obj && !base_url_str) {
+ yyjson_doc_free(doc);
+ return NULL;
+ }
+
+ cbm_path_alias_map_t *map = calloc(1, sizeof(*map));
+ if (!map) {
+ yyjson_doc_free(doc);
+ return NULL;
+ }
+
+ if (base_url_str && base_url_str[0] != '\0' && strcmp(base_url_str, ".") != 0) {
+ map->base_url = resolve_target_relative(dir_prefix, base_url_str);
+ } else if (base_url_str && strcmp(base_url_str, ".") == 0 && dir_prefix &&
+ dir_prefix[0] != '\0') {
+ map->base_url = strdup(dir_prefix);
+ }
+
+ if (paths_obj && yyjson_is_obj(paths_obj)) {
+ size_t obj_size = yyjson_obj_size(paths_obj);
+ bool capped = obj_size > CBM_PATH_ALIAS_MAX_ENTRIES;
+ int capacity =
+ (int)(capped ? (size_t)CBM_PATH_ALIAS_MAX_ENTRIES : obj_size);
+ if (capacity > 0) {
+ map->entries = calloc((size_t)capacity, sizeof(cbm_path_alias_t));
+ if (!map->entries) {
+ free(map->base_url);
+ free(map);
+ yyjson_doc_free(doc);
+ return NULL;
+ }
+ yyjson_val *key;
+ yyjson_obj_iter iter = yyjson_obj_iter_with(paths_obj);
+ while ((key = yyjson_obj_iter_next(&iter)) != NULL && map->count < capacity) {
+ yyjson_val *val = yyjson_obj_iter_get_val(key);
+ const char *alias_pattern = yyjson_get_str(key);
+ if (!alias_pattern || !yyjson_is_arr(val) || yyjson_arr_size(val) == 0) {
+ continue;
+ }
+ const char *target_pattern = yyjson_get_str(yyjson_arr_get_first(val));
+ if (!target_pattern) {
+ continue;
+ }
+ cbm_path_alias_t *entry = &map->entries[map->count];
+ const char *star = strchr(alias_pattern, '*');
+ if (star) {
+ entry->has_wildcard = true;
+ entry->alias_prefix =
+ strndup(alias_pattern, (size_t)(star - alias_pattern));
+ entry->alias_suffix = strdup(star + 1);
+ } else {
+ entry->has_wildcard = false;
+ entry->alias_prefix = strdup(alias_pattern);
+ entry->alias_suffix = strdup("");
+ }
+ const char *tstar = strchr(target_pattern, '*');
+ if (tstar) {
+ char *pre = strndup(target_pattern, (size_t)(tstar - target_pattern));
+ entry->target_prefix = resolve_target_relative(dir_prefix, pre);
+ free(pre);
+ entry->target_suffix = strdup(tstar + 1);
+ } else {
+ entry->target_prefix = resolve_target_relative(dir_prefix, target_pattern);
+ entry->target_suffix = strdup("");
+ }
+ map->count++;
+ }
+ if (capped) {
+ cbm_log_warn("path_alias.entries.cap_hit", "config", abs_path, "kept",
+ /* itoa via thread-local buffer would be tidier; keep simple */
+ "256_of_more");
+ }
+ qsort(map->entries, (size_t)map->count, sizeof(cbm_path_alias_t),
+ cmp_alias_entry_by_specificity);
+ }
+ }
+
+ yyjson_doc_free(doc);
+ return map;
+}
+
+/* ── Public API ────────────────────────────────────────────────── */
+
+void cbm_path_alias_collection_free(cbm_path_alias_collection_t *coll) {
+ if (!coll) {
+ return;
+ }
+ for (int i = 0; i < coll->count; i++) {
+ free(coll->scopes[i].dir_prefix);
+ if (coll->scopes[i].map) {
+ cbm_path_alias_map_t *map = coll->scopes[i].map;
+ for (int j = 0; j < map->count; j++) {
+ free(map->entries[j].alias_prefix);
+ free(map->entries[j].alias_suffix);
+ free(map->entries[j].target_prefix);
+ free(map->entries[j].target_suffix);
+ }
+ free(map->entries);
+ free(map->base_url);
+ free(map);
+ }
+ }
+ free(coll->scopes);
+ free(coll);
+}
+
+char *cbm_path_alias_resolve(const cbm_path_alias_map_t *map, const char *module_path) {
+ if (!map || !module_path) {
+ return NULL;
+ }
+ size_t mod_len = strlen(module_path);
+
+ for (int i = 0; i < map->count; i++) {
+ const cbm_path_alias_t *e = &map->entries[i];
+ size_t prefix_len = strlen(e->alias_prefix);
+ size_t suffix_len = strlen(e->alias_suffix);
+
+ if (e->has_wildcard) {
+ if (mod_len < prefix_len + suffix_len) {
+ continue;
+ }
+ if (strncmp(module_path, e->alias_prefix, prefix_len) != 0) {
+ continue;
+ }
+ if (suffix_len > 0 &&
+ strcmp(module_path + mod_len - suffix_len, e->alias_suffix) != 0) {
+ continue;
+ }
+ size_t wild_len = mod_len - prefix_len - suffix_len;
+ const char *wild_start = module_path + prefix_len;
+ size_t tp_len = strlen(e->target_prefix);
+ size_t ts_len = strlen(e->target_suffix);
+ char *result = malloc(tp_len + wild_len + ts_len + 1);
+ if (!result) {
+ return NULL;
+ }
+ memcpy(result, e->target_prefix, tp_len);
+ memcpy(result + tp_len, wild_start, wild_len);
+ memcpy(result + tp_len + wild_len, e->target_suffix, ts_len);
+ result[tp_len + wild_len + ts_len] = '\0';
+ return strip_resolved_ext(result);
+ }
+
+ if (strcmp(module_path, e->alias_prefix) == 0) {
+ return strip_resolved_ext(strdup(e->target_prefix));
+ }
+ }
+
+ /* baseUrl fallback. Apply only to non-relative imports that look
+ * sub-path-ish (contain '/' but don't start with '.' or '@'); skips
+ * obvious package names like "react" or "lodash". */
+ if (map->base_url && module_path[0] != '.' && module_path[0] != '@' &&
+ strchr(module_path, '/') != NULL) {
+ size_t bu_len = strlen(map->base_url);
+ size_t need = bu_len + 1 + mod_len + 1;
+ char *result = malloc(need);
+ if (!result) {
+ return NULL;
+ }
+ snprintf(result, need, "%s/%s", map->base_url, module_path);
+ return strip_resolved_ext(result);
+ }
+ return NULL;
+}
+
+/* ── Repo walk ─────────────────────────────────────────────────── */
+
+typedef struct {
+ char abs[CBM_SZ_512];
+ char rel[CBM_SZ_256];
+} alias_config_hit_t;
+
+static const char *const TS_CONFIG_NAMES[] = {"tsconfig.json", "jsconfig.json"};
+enum { TS_CONFIG_NAMES_COUNT = 2 };
+
+static void find_alias_files(const char *abs_dir, const char *rel_dir, alias_config_hit_t *out,
+ int *count, int max_count, int depth) {
+ if (*count >= max_count || depth > CBM_PATH_ALIAS_MAX_DEPTH) {
+ return;
+ }
+ cbm_dir_t *d = cbm_opendir(abs_dir);
+ if (!d) {
+ return;
+ }
+
+ /* One config file per directory: prefer tsconfig.json over jsconfig.json. */
+ for (int i = 0; i < TS_CONFIG_NAMES_COUNT && *count < max_count; i++) {
+ char check[CBM_SZ_512];
+ snprintf(check, sizeof(check), "%s/%s", abs_dir, TS_CONFIG_NAMES[i]);
+ FILE *f = fopen(check, "r");
+ if (f) {
+ fclose(f);
+ snprintf(out[*count].abs, sizeof(out[*count].abs), "%s", check);
+ snprintf(out[*count].rel, sizeof(out[*count].rel), "%s", rel_dir);
+ (*count)++;
+ break;
+ }
+ }
+
+ cbm_dirent_t *ent;
+ while ((ent = cbm_readdir(d)) != NULL && *count < max_count) {
+ if (!ent->is_dir) {
+ continue;
+ }
+ const char *name = ent->name;
+ if (name[0] == '.' || strcmp(name, "node_modules") == 0 ||
+ strcmp(name, "dist") == 0 || strcmp(name, "build") == 0 ||
+ strcmp(name, ".next") == 0 || strcmp(name, "coverage") == 0 ||
+ strcmp(name, "target") == 0 /* Rust */) {
+ continue;
+ }
+ char child_abs[CBM_SZ_512];
+ char child_rel[CBM_SZ_256];
+ snprintf(child_abs, sizeof(child_abs), "%s/%s", abs_dir, name);
+ if (rel_dir[0] == '\0') {
+ snprintf(child_rel, sizeof(child_rel), "%s", name);
+ } else {
+ snprintf(child_rel, sizeof(child_rel), "%s/%s", rel_dir, name);
+ }
+ find_alias_files(child_abs, child_rel, out, count, max_count, depth + 1);
+ }
+ cbm_closedir(d);
+}
+
+cbm_path_alias_collection_t *cbm_load_path_aliases(const char *repo_path) {
+ if (!repo_path) {
+ return NULL;
+ }
+ alias_config_hit_t *hits = calloc(CBM_PATH_ALIAS_MAX_FILES, sizeof(*hits));
+ if (!hits) {
+ return NULL;
+ }
+ int count = 0;
+ find_alias_files(repo_path, "", hits, &count, CBM_PATH_ALIAS_MAX_FILES, 0);
+ if (count >= CBM_PATH_ALIAS_MAX_FILES) {
+ cbm_log_warn("path_alias.files.cap_hit", "repo", repo_path, "kept",
+ "256_of_more");
+ }
+ if (count == 0) {
+ free(hits);
+ return NULL;
+ }
+
+ cbm_path_alias_collection_t *coll = calloc(1, sizeof(*coll));
+ if (!coll) {
+ free(hits);
+ return NULL;
+ }
+ coll->scopes = calloc((size_t)count, sizeof(cbm_path_alias_scope_t));
+ if (!coll->scopes) {
+ free(coll);
+ free(hits);
+ return NULL;
+ }
+
+ for (int i = 0; i < count; i++) {
+ cbm_path_alias_map_t *map = load_tsconfig_file(hits[i].abs, hits[i].rel);
+ if (!map) {
+ continue;
+ }
+ coll->scopes[coll->count].dir_prefix = strdup(hits[i].rel);
+ coll->scopes[coll->count].map = map;
+ coll->count++;
+ }
+ free(hits);
+
+ if (coll->count == 0) {
+ free(coll->scopes);
+ free(coll);
+ return NULL;
+ }
+
+ qsort(coll->scopes, (size_t)coll->count, sizeof(cbm_path_alias_scope_t),
+ cmp_scope_by_specificity);
+ return coll;
+}
+
+const cbm_path_alias_map_t *cbm_path_alias_find_for_file(
+ const cbm_path_alias_collection_t *coll, const char *rel_path) {
+ if (!coll || !rel_path) {
+ return NULL;
+ }
+ for (int i = 0; i < coll->count; i++) {
+ const char *prefix = coll->scopes[i].dir_prefix;
+ size_t plen = strlen(prefix);
+ if (plen == 0) {
+ return coll->scopes[i].map;
+ }
+ if (strncmp(rel_path, prefix, plen) == 0 &&
+ (rel_path[plen] == '/' || rel_path[plen] == '\0')) {
+ return coll->scopes[i].map;
+ }
+ }
+ return NULL;
+}
diff --git a/src/pipeline/path_alias.h b/src/pipeline/path_alias.h
new file mode 100644
index 000000000..015b75e83
--- /dev/null
+++ b/src/pipeline/path_alias.h
@@ -0,0 +1,92 @@
+#ifndef CBM_PATH_ALIAS_H
+#define CBM_PATH_ALIAS_H
+
+/*
+ * path_alias.h — Generic build-tool path alias resolution.
+ *
+ * Resolves module paths that the indexer would otherwise leave as bare
+ * imports (e.g. "@/lib/auth" or "~/components/Button") into repo-relative
+ * paths the existing module-FQN logic can consume. The alias source is
+ * pluggable per language: the loader walks the repo, recognises supported
+ * config files, and adds each one's aliases to a single collection scoped
+ * by directory.
+ *
+ * Currently implemented sources: TypeScript / JavaScript (tsconfig.json
+ * and jsconfig.json compilerOptions.paths + baseUrl). The data model and
+ * resolver below are deliberately language-agnostic so further loaders
+ * (Vite/Webpack alias config, Python tool.* hints, ...) can register
+ * additional scopes without touching the resolver or the pipeline.
+ *
+ * Lookup is two-step:
+ * 1. cbm_path_alias_find_for_file picks the nearest ancestor scope for
+ * a given source file (longest matching dir_prefix wins).
+ * 2. cbm_path_alias_resolve runs the chosen scope's alias map against
+ * the import path, returning a heap-allocated repo-relative target
+ * (or NULL if no alias matches).
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+
+/* Single alias entry: prefix-pattern → target-pattern, optionally with a
+ * single '*' wildcard. The pattern is split at the wildcard so resolution
+ * can match the prefix/suffix and slot the wildcard portion into the
+ * target. Patterns without '*' are exact-match. */
+typedef struct {
+ char *alias_prefix; /* portion before '*' in the key (e.g. "@/") */
+ char *alias_suffix; /* portion after '*' in the key (usually "") */
+ char *target_prefix; /* portion before '*' in the value (e.g. "src/") */
+ char *target_suffix; /* portion after '*' in the value (usually "") */
+ bool has_wildcard; /* true when the alias pattern contained '*' */
+} cbm_path_alias_t;
+
+/* Alias map for a single config source (one tsconfig.json, one webpack
+ * alias block, ...). Entries are sorted by alias_prefix length descending
+ * so longer (more specific) prefixes are matched first, matching the
+ * resolution semantics of every build tool we know of. */
+typedef struct {
+ cbm_path_alias_t *entries;
+ int count;
+ char *base_url; /* compilerOptions.baseUrl or equivalent; NULL if unset */
+} cbm_path_alias_map_t;
+
+/* A directory-scoped alias map. dir_prefix is the path of the config file
+ * relative to the repo root ("" for a root-level config). Source files
+ * pick up the map of the nearest ancestor scope. */
+typedef struct {
+ char *dir_prefix;
+ cbm_path_alias_map_t *map;
+} cbm_path_alias_scope_t;
+
+/* Aggregated alias scopes for a whole repository. Scopes are sorted by
+ * dir_prefix length descending so the nearest-ancestor lookup is a single
+ * linear scan. */
+typedef struct {
+ cbm_path_alias_scope_t *scopes;
+ int count;
+} cbm_path_alias_collection_t;
+
+/* Walk repo_path for known build-config files and build a collection of
+ * scoped alias maps. Currently picks up tsconfig.json and jsconfig.json;
+ * additional loaders register here. Returns NULL when no usable configs
+ * are found (also NULL on out-of-memory). Caller frees with
+ * cbm_path_alias_collection_free. */
+cbm_path_alias_collection_t *cbm_load_path_aliases(const char *repo_path);
+
+/* Free a collection produced by cbm_load_path_aliases. NULL-safe. */
+void cbm_path_alias_collection_free(cbm_path_alias_collection_t *coll);
+
+/* Pick the nearest ancestor scope for a file path that is relative to the
+ * repo root. Returns the scope's alias map, or NULL if no scope applies.
+ * The returned pointer is owned by the collection. */
+const cbm_path_alias_map_t *cbm_path_alias_find_for_file(
+ const cbm_path_alias_collection_t *coll, const char *rel_path);
+
+/* Resolve module_path against map. Returns a heap-allocated repo-relative
+ * path (caller frees), or NULL when no alias entry matches and no
+ * baseUrl fallback applies. Common JS/TS extensions (.ts, .tsx, .js,
+ * .jsx) are stripped from the resolved path so the existing module-FQN
+ * pipeline can consume it. */
+char *cbm_path_alias_resolve(const cbm_path_alias_map_t *map, const char *module_path);
+
+#endif /* CBM_PATH_ALIAS_H */
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 1f8e1330e..789661b26 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -820,6 +820,7 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
CBM_PROF_START(t_pipeline_total);
struct timespec t0;
cbm_clock_gettime(CLOCK_MONOTONIC, &t0);
+ cbm_path_alias_collection_t *path_aliases = NULL;
/* Load user-defined extension overrides (fail-open: NULL on error) */
CBM_PROF_START(t_userconfig);
@@ -860,6 +861,10 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
p->gbuf = cbm_gbuf_new(p->project_name, p->repo_path);
p->registry = cbm_registry_new();
+ /* Phase 2b: Load build-tool path aliases (tsconfig/jsconfig today). NULL
+ * when no usable configs are found — non-TS projects pay nothing. */
+ path_aliases = cbm_load_path_aliases(p->repo_path);
+
/* Build shared context for pass functions */
cbm_pipeline_ctx_t ctx = {
.project_name = p->project_name,
@@ -868,6 +873,7 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
.registry = p->registry,
.cancelled = &p->cancelled,
.mode = (int)p->mode,
+ .path_aliases = path_aliases,
};
rc = run_extraction_phase(p, &ctx, files, file_count);
@@ -893,6 +899,7 @@ cleanup:
p->gbuf = NULL;
cbm_registry_free(p->registry);
p->registry = NULL;
+ cbm_path_alias_collection_free(path_aliases);
/* Clear and free user extension config */
cbm_set_user_lang_config(NULL);
cbm_userconfig_free(p->userconfig);
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index 0c6f49aac..51c2d1862 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -405,6 +405,9 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_log_info("incremental.registry_seed", "symbols", itoa_buf(cbm_registry_size(registry)),
"elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+ cbm_path_alias_collection_t *path_aliases =
+ cbm_load_path_aliases(cbm_pipeline_repo_path(p));
+
cbm_pipeline_ctx_t ctx = {
.project_name = project,
.repo_path = cbm_pipeline_repo_path(p),
@@ -412,6 +415,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
.registry = registry,
.cancelled = cbm_pipeline_cancelled_ptr(p),
.mode = cbm_pipeline_get_mode(p),
+ .path_aliases = path_aliases,
};
for (int i = 0; i < ci; i++) {
@@ -429,6 +433,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free(changed_files);
cbm_registry_free(registry);
+ cbm_path_alias_collection_free(path_aliases);
/* Step 7: Dump to disk */
dump_and_persist(existing, db_path, project, files, file_count, cbm_pipeline_repo_path(p));
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 316002a9a..4b655cb20 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -9,6 +9,7 @@
#define CBM_PIPELINE_INTERNAL_H
#include "pipeline/pipeline.h"
+#include "pipeline/path_alias.h"
#include "graph_buffer/graph_buffer.h"
#include "discover/discover.h"
#include "foundation/hash_table.h"
@@ -62,6 +63,11 @@ typedef struct {
* and pass_calls/usages/semantic reuse cached results instead of re-extracting.
* Indexed by file position in the files[] array. Owned by pipeline.c. */
CBMFileResult **result_cache;
+
+ /* Build-tool path aliases (tsconfig/jsconfig today; webpack/vite-style
+ * configs are an easy follow-on). NULL when no usable configs were found.
+ * Owned by pipeline.c / pipeline_incremental.c. */
+ const cbm_path_alias_collection_t *path_aliases;
} cbm_pipeline_ctx_t;
/* Get the current pipeline's package map (NULL if none). */
diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c
index d9408950d..0b1ecfdc1 100644
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@ -337,12 +337,18 @@ static cbm_resolution_t resolve_import_map(const cbm_registry_t *r, const char *
return empty_result();
}
- /* Build candidate: resolved.suffix or just resolved */
+ /* Build candidate: resolved.suffix or resolved.prefix.
+ * When the callee has a dot ("pkg.Func"), prefix="pkg" is the import key
+ * and suffix="Func" is the function name, so the target QN is
+ * resolved.Func. When the callee is bare ("requireAdmin"), prefix IS the
+ * function name and suffix is NULL, so the target QN must be
+ * resolved.requireAdmin — not just resolved, which would point at the
+ * module node and miss the function entirely. */
char candidate[CBM_SZ_512];
if (suffix && suffix[0]) {
snprintf(candidate, sizeof(candidate), "%s.%s", resolved, suffix);
} else {
- snprintf(candidate, sizeof(candidate), "%s", resolved);
+ snprintf(candidate, sizeof(candidate), "%s.%s", resolved, prefix);
}
/* Use cbm_ht_get_key to get the persistent heap-owned key string */
const char *stored_key = cbm_ht_get_key(r->exact, candidate);
diff --git a/tests/test_main.c b/tests/test_main.c
index 5a9257360..6841f7021 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -34,6 +34,7 @@ extern void suite_graph_buffer(void);
extern void suite_registry(void);
extern void suite_pipeline(void);
extern void suite_fqn(void);
+extern void suite_path_alias(void);
extern void suite_watcher(void);
extern void suite_lz4(void);
extern void suite_zstd(void);
@@ -100,6 +101,7 @@ int main(void) {
RUN_SUITE(registry);
RUN_SUITE(pipeline);
RUN_SUITE(fqn);
+ RUN_SUITE(path_alias);
/* Watcher (M10) */
RUN_SUITE(watcher);
diff --git a/tests/test_path_alias.c b/tests/test_path_alias.c
new file mode 100644
index 000000000..24e6ee842
--- /dev/null
+++ b/tests/test_path_alias.c
@@ -0,0 +1,316 @@
+/*
+ * test_path_alias.c -- Tests for build-tool path alias resolution.
+ *
+ * Covers the in-memory resolver (cbm_path_alias_resolve), the
+ * directory-scoped collection lookup (cbm_path_alias_find_for_file),
+ * and the resource-cap behaviour. Filesystem-based loading is exercised
+ * indirectly via the integration test that builds a tmp tsconfig tree.
+ */
+#include "test_framework.h"
+#include "../src/pipeline/path_alias.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+/* Build a path alias map programmatically (no file I/O), respecting the
+ * specificity ordering invariant the loader establishes via qsort. */
+static cbm_path_alias_map_t *make_map(const char *base_url, int count, ...) {
+ cbm_path_alias_map_t *map = calloc(1, sizeof(*map));
+ map->base_url = base_url ? strdup(base_url) : NULL;
+ map->entries = calloc((size_t)count, sizeof(cbm_path_alias_t));
+ map->count = count;
+
+ va_list args;
+ va_start(args, count);
+ for (int i = 0; i < count; i++) {
+ const char *alias_pattern = va_arg(args, const char *);
+ const char *target_pattern = va_arg(args, const char *);
+ const char *star = strchr(alias_pattern, '*');
+ if (star) {
+ map->entries[i].has_wildcard = true;
+ map->entries[i].alias_prefix =
+ strndup(alias_pattern, (size_t)(star - alias_pattern));
+ map->entries[i].alias_suffix = strdup(star + 1);
+ } else {
+ map->entries[i].has_wildcard = false;
+ map->entries[i].alias_prefix = strdup(alias_pattern);
+ map->entries[i].alias_suffix = strdup("");
+ }
+ const char *tstar = strchr(target_pattern, '*');
+ if (tstar) {
+ map->entries[i].target_prefix =
+ strndup(target_pattern, (size_t)(tstar - target_pattern));
+ map->entries[i].target_suffix = strdup(tstar + 1);
+ } else {
+ map->entries[i].target_prefix = strdup(target_pattern);
+ map->entries[i].target_suffix = strdup("");
+ }
+ }
+ va_end(args);
+
+ /* Mimic the loader's specificity sort. */
+ for (int i = 0; i < count - 1; i++) {
+ for (int j = i + 1; j < count; j++) {
+ size_t li = strlen(map->entries[i].alias_prefix);
+ size_t lj = strlen(map->entries[j].alias_prefix);
+ if (lj > li) {
+ cbm_path_alias_t tmp = map->entries[i];
+ map->entries[i] = map->entries[j];
+ map->entries[j] = tmp;
+ }
+ }
+ }
+ return map;
+}
+
+/* The map produced by make_map() owns all heap memory the same way the
+ * loader does, so the public free routine on the wrapping collection
+ * works equivalently — but tests build naked maps, so use this helper. */
+static void free_map(cbm_path_alias_map_t *map) {
+ if (!map) {
+ return;
+ }
+ for (int i = 0; i < map->count; i++) {
+ free(map->entries[i].alias_prefix);
+ free(map->entries[i].alias_suffix);
+ free(map->entries[i].target_prefix);
+ free(map->entries[i].target_suffix);
+ }
+ free(map->entries);
+ free(map->base_url);
+ free(map);
+}
+
+/* ── Basic wildcard alias ──────────────────────────────────────── */
+
+TEST(path_alias_at_wildcard) {
+ cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*");
+ char *r = cbm_path_alias_resolve(m, "@/lib/auth");
+ ASSERT_NOT_NULL(r);
+ ASSERT_STR_EQ(r, "src/lib/auth");
+ free(r);
+ free_map(m);
+ PASS();
+}
+
+TEST(path_alias_at_nested) {
+ cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*");
+ char *r = cbm_path_alias_resolve(m, "@/components/Button");
+ ASSERT_NOT_NULL(r);
+ ASSERT_STR_EQ(r, "src/components/Button");
+ free(r);
+ free_map(m);
+ PASS();
+}
+
+/* ── Specificity ordering: longest matching prefix wins ────────── */
+
+TEST(path_alias_specificity_longest_first) {
+ // @/lib/* must beat @/* even though @/* would also match.
+ cbm_path_alias_map_t *m =
+ make_map(NULL, 2, "@/*", "src/*", "@/lib/*", "src/shared/lib/*");
+ char *r = cbm_path_alias_resolve(m, "@/lib/auth");
+ ASSERT_NOT_NULL(r);
+ ASSERT_STR_EQ(r, "src/shared/lib/auth");
+ free(r);
+ /* Non-/lib paths still match the broader rule. */
+ char *r2 = cbm_path_alias_resolve(m, "@/components/X");
+ ASSERT_NOT_NULL(r2);
+ ASSERT_STR_EQ(r2, "src/components/X");
+ free(r2);
+ free_map(m);
+ PASS();
+}
+
+/* ── Exact match (no wildcard) ─────────────────────────────────── */
+
+TEST(path_alias_exact_match) {
+ cbm_path_alias_map_t *m = make_map(NULL, 1, "@app/config", "src/config/index");
+ char *r = cbm_path_alias_resolve(m, "@app/config");
+ ASSERT_NOT_NULL(r);
+ ASSERT_STR_EQ(r, "src/config/index");
+ free(r);
+ /* Anything else under @app/ should not match. */
+ char *miss = cbm_path_alias_resolve(m, "@app/other");
+ ASSERT_NULL(miss);
+ free_map(m);
+ PASS();
+}
+
+/* ── Extension stripping ───────────────────────────────────────── */
+
+TEST(path_alias_strips_ext) {
+ cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*.ts");
+ char *r = cbm_path_alias_resolve(m, "@/lib/auth");
+ ASSERT_NOT_NULL(r);
+ ASSERT_STR_EQ(r, "src/lib/auth");
+ free(r);
+ free_map(m);
+ PASS();
+}
+
+/* ── baseUrl fallback for non-relative, non-package imports ────── */
+
+TEST(path_alias_baseurl_fallback) {
+ cbm_path_alias_map_t *m = make_map("src", 0);
+ /* Looks like a sub-path → resolve against baseUrl. */
+ char *r = cbm_path_alias_resolve(m, "lib/auth");
+ ASSERT_NOT_NULL(r);
+ ASSERT_STR_EQ(r, "src/lib/auth");
+ free(r);
+ /* Bare package name → not a baseUrl candidate. */
+ char *miss = cbm_path_alias_resolve(m, "react");
+ ASSERT_NULL(miss);
+ free_map(m);
+ PASS();
+}
+
+/* ── NULL safety ───────────────────────────────────────────────── */
+
+TEST(path_alias_null_safety) {
+ ASSERT_NULL(cbm_path_alias_resolve(NULL, "anything"));
+ cbm_path_alias_map_t *m = make_map(NULL, 1, "@/*", "src/*");
+ ASSERT_NULL(cbm_path_alias_resolve(m, NULL));
+ free_map(m);
+ /* Free of NULL collection is a no-op, not a crash. */
+ cbm_path_alias_collection_free(NULL);
+ PASS();
+}
+
+/* ── Collection lookup: nearest-ancestor scope wins ───────────── */
+
+TEST(path_alias_find_for_file_nearest_ancestor) {
+ /* Build a synthetic collection by hand: two scopes, "" (root) and
+ * "apps/manager". A file inside apps/manager/src/... must pick the
+ * deeper scope; a file under packages/utils must fall back to root. */
+ cbm_path_alias_collection_t *coll = calloc(1, sizeof(*coll));
+ coll->scopes = calloc(2, sizeof(cbm_path_alias_scope_t));
+ coll->count = 2;
+
+ /* Order matters: most specific first (loader does this via qsort). */
+ coll->scopes[0].dir_prefix = strdup("apps/manager");
+ coll->scopes[0].map = make_map(NULL, 1, "@/*", "src/*");
+
+ coll->scopes[1].dir_prefix = strdup("");
+ coll->scopes[1].map = make_map(NULL, 1, "@root/*", "shared/*");
+
+ const cbm_path_alias_map_t *m1 =
+ cbm_path_alias_find_for_file(coll, "apps/manager/src/lib/auth.ts");
+ ASSERT_NOT_NULL(m1);
+ ASSERT_EQ(m1->count, 1);
+ ASSERT_STR_EQ(m1->entries[0].alias_prefix, "@/");
+
+ const cbm_path_alias_map_t *m2 =
+ cbm_path_alias_find_for_file(coll, "packages/utils/index.ts");
+ ASSERT_NOT_NULL(m2);
+ ASSERT_EQ(m2->count, 1);
+ ASSERT_STR_EQ(m2->entries[0].alias_prefix, "@root/");
+
+ cbm_path_alias_collection_free(coll);
+ PASS();
+}
+
+/* ── End-to-end via the loader: real tsconfig in a tmp dir ─────── */
+
+static int write_file(const char *path, const char *content) {
+ FILE *f = fopen(path, "w");
+ if (!f) {
+ return -1;
+ }
+ size_t len = strlen(content);
+ int rc = fwrite(content, 1, len, f) == len ? 0 : -1;
+ fclose(f);
+ return rc;
+}
+
+TEST(path_alias_loader_monorepo) {
+ char tmpl[] = "/tmp/cbm_palias_XXXXXX";
+ char *root = mkdtemp(tmpl);
+ ASSERT_NOT_NULL(root);
+
+ char sub[512];
+ snprintf(sub, sizeof(sub), "%s/apps", root);
+ mkdir(sub, 0755);
+ snprintf(sub, sizeof(sub), "%s/apps/manager", root);
+ mkdir(sub, 0755);
+
+ char path[512];
+ snprintf(path, sizeof(path), "%s/tsconfig.json", root);
+ ASSERT_EQ(write_file(path,
+ "{\n \"compilerOptions\": {\n \"paths\": {\n"
+ " \"@root/*\": [\"shared/*\"]\n }\n }\n}\n"),
+ 0);
+ snprintf(path, sizeof(path), "%s/apps/manager/tsconfig.json", root);
+ ASSERT_EQ(write_file(path,
+ "{\n // monorepo subpackage\n \"compilerOptions\": {\n"
+ " \"paths\": {\n \"@/*\": [\"./src/*\"]\n }\n },\n}\n"),
+ 0);
+
+ cbm_path_alias_collection_t *coll = cbm_load_path_aliases(root);
+ ASSERT_NOT_NULL(coll);
+ ASSERT_EQ(coll->count, 2);
+
+ /* sub-package file picks up its own tsconfig. */
+ const cbm_path_alias_map_t *m =
+ cbm_path_alias_find_for_file(coll, "apps/manager/src/feature/x.ts");
+ ASSERT_NOT_NULL(m);
+ char *r = cbm_path_alias_resolve(m, "@/lib/auth");
+ ASSERT_NOT_NULL(r);
+ /* Target paths in the sub-tsconfig are dir_prefix-relative. */
+ ASSERT_STR_EQ(r, "apps/manager/src/lib/auth");
+ free(r);
+
+ /* Root file falls back to the root tsconfig's aliases. */
+ const cbm_path_alias_map_t *m2 = cbm_path_alias_find_for_file(coll, "scripts/build.ts");
+ ASSERT_NOT_NULL(m2);
+ char *r2 = cbm_path_alias_resolve(m2, "@root/utils");
+ ASSERT_NOT_NULL(r2);
+ ASSERT_STR_EQ(r2, "shared/utils");
+ free(r2);
+
+ cbm_path_alias_collection_free(coll);
+
+ /* Cleanup tmp tree. */
+ snprintf(path, sizeof(path), "%s/apps/manager/tsconfig.json", root);
+ unlink(path);
+ snprintf(path, sizeof(path), "%s/tsconfig.json", root);
+ unlink(path);
+ snprintf(path, sizeof(path), "%s/apps/manager", root);
+ rmdir(path);
+ snprintf(path, sizeof(path), "%s/apps", root);
+ rmdir(path);
+ rmdir(root);
+ PASS();
+}
+
+/* ── Loader returns NULL when no configs found ─────────────────── */
+
+TEST(path_alias_loader_no_configs) {
+ char tmpl[] = "/tmp/cbm_palias_empty_XXXXXX";
+ char *root = mkdtemp(tmpl);
+ ASSERT_NOT_NULL(root);
+
+ cbm_path_alias_collection_t *coll = cbm_load_path_aliases(root);
+ ASSERT_NULL(coll);
+
+ rmdir(root);
+ PASS();
+}
+
+void suite_path_alias(void);
+void suite_path_alias(void) {
+ RUN_TEST(path_alias_at_wildcard);
+ RUN_TEST(path_alias_at_nested);
+ RUN_TEST(path_alias_specificity_longest_first);
+ RUN_TEST(path_alias_exact_match);
+ RUN_TEST(path_alias_strips_ext);
+ RUN_TEST(path_alias_baseurl_fallback);
+ RUN_TEST(path_alias_null_safety);
+ RUN_TEST(path_alias_find_for_file_nearest_ancestor);
+ RUN_TEST(path_alias_loader_monorepo);
+ RUN_TEST(path_alias_loader_no_configs);
+}
diff --git a/tests/test_registry.c b/tests/test_registry.c
index 4cfcff5b4..7783a71b2 100644
--- a/tests/test_registry.c
+++ b/tests/test_registry.c
@@ -283,6 +283,28 @@ TEST(resolve_import_map) {
PASS();
}
+/* Bare function call (no dot) routed through import_map. The candidate QN
+ * must be module_qn.callee, not module_qn — otherwise lookups fall through
+ * to name-based resolution and pick a same-named function from a different
+ * file. Regression for the @/lib/auth-style import case. */
+TEST(resolve_import_map_bare_function) {
+ cbm_registry_t *r = cbm_registry_new();
+ cbm_registry_add(r, "requireAdmin", "proj.lib.authorization.requireAdmin", "Function");
+ /* Same name in another module — without the fix this is what gets picked. */
+ cbm_registry_add(r, "requireAdmin", "proj.lib.users.requireAdmin", "Function");
+
+ const char *keys[] = {"requireAdmin"};
+ const char *vals[] = {"proj.lib.authorization"};
+
+ cbm_resolution_t res =
+ cbm_registry_resolve(r, "requireAdmin", "proj.actions.settings", keys, vals, 1);
+ ASSERT_STR_EQ(res.qualified_name, "proj.lib.authorization.requireAdmin");
+ ASSERT_STR_EQ(res.strategy, "import_map");
+
+ cbm_registry_free(r);
+ PASS();
+}
+
TEST(resolve_unique_name) {
cbm_registry_t *r = cbm_registry_new();
cbm_registry_add(r, "UniqueFunc", "proj.deep.path.UniqueFunc", "Function");
@@ -610,6 +632,7 @@ SUITE(registry) {
/* Resolution */
RUN_TEST(resolve_same_module);
RUN_TEST(resolve_import_map);
+ RUN_TEST(resolve_import_map_bare_function);
RUN_TEST(resolve_unique_name);
RUN_TEST(resolve_unresolved);
RUN_TEST(resolve_many_nodes);