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.

Resolve grammar-only IMPORTS edges for sibling-file imports

Martin Vogel861b9d917e9f
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index 3584ad0b3..d1a59374c 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -923,7 +923,10 @@ static void generic_import_from_text(CBMExtractCtx *ctx, TSNode node) {
if (len > 0 && text[len - SKIP_ONE] == ';') {
text[len - SKIP_ONE] = '\0';
}
- if (text[0]) {
+ /* Strip surrounding quotes (Pony `use "util"`, func `#include "utils.fc"`)
+ * so the module path is a clean filename the resolver can match. */
+ text = strip_quotes(a, text);
+ if (text && text[0]) {
CBMImport imp = {.local_name = path_last(a, text), .module_path = text};
cbm_imports_push(&ctx->result->imports, a, imp);
}
@@ -1627,9 +1630,16 @@ static void lisp_push_module(CBMExtractCtx *ctx, TSNode mod_node) {
CBMArena *a = ctx->arena;
const char *mk = ts_node_type(mod_node);
char *mod = NULL;
- if (strcmp(mk, "symbol") == 0 || strcmp(mk, "string") == 0) {
+ if (strcmp(mk, "symbol") == 0 || strcmp(mk, "string") == 0 || strcmp(mk, "sym_lit") == 0 ||
+ strcmp(mk, "str_lit") == 0 || strcmp(mk, "kwd_lit") == 0) {
mod = strip_quotes(a, cbm_node_text(a, mod_node, ctx->source));
- } else if (strcmp(mk, "list") == 0) {
+ /* Clojure/Fennel keyword module refs carry a leading ':' (e.g. `:util`,
+ * `(require :util)`); strip it so the name matches the sibling file. */
+ if (mod && mod[0] == ':') {
+ mod = cbm_arena_strdup(a, mod + 1);
+ }
+ } else if (strcmp(mk, "list") == 0 || strcmp(mk, "list_lit") == 0 ||
+ strcmp(mk, "vec_lit") == 0) {
/* (only-in racket/math pi) → take first symbol after a leading keyword,
* else join inner symbols with '/'. Simplest robust choice: the whole
* list text minus the parens. */
@@ -1656,7 +1666,10 @@ static void parse_lisp_imports(CBMExtractCtx *ctx) {
}
do {
TSNode node = ts_tree_cursor_current_node(&cursor);
- if (strcmp(ts_node_type(node), "list") != 0) {
+ const char *nt = ts_node_type(node);
+ /* Different lisp grammars name the s-expression list node differently:
+ * Scheme/Racket/CL/elisp use "list"; Clojure uses "list_lit". */
+ if (strcmp(nt, "list") != 0 && strcmp(nt, "list_lit") != 0) {
continue;
}
uint32_t nc = ts_node_named_child_count(node);
@@ -1664,7 +1677,9 @@ static void parse_lisp_imports(CBMExtractCtx *ctx) {
continue;
}
TSNode head = ts_node_named_child(node, 0);
- if (strcmp(ts_node_type(head), "symbol") != 0) {
+ const char *ht = ts_node_type(head);
+ /* Clojure tokenizes the head symbol as "sym_lit"; others as "symbol". */
+ if (strcmp(ht, "symbol") != 0 && strcmp(ht, "sym_lit") != 0) {
continue;
}
char *hn = cbm_node_text(a, head, ctx->source);
@@ -1677,7 +1692,8 @@ static void parse_lisp_imports(CBMExtractCtx *ctx) {
TSNode mod_node = ts_node_named_child(node, j);
const char *mk = ts_node_type(mod_node);
/* (require 'json) — the quoted datum is a `quote` wrapping a symbol. */
- if (strcmp(mk, "quote") == 0 && ts_node_named_child_count(mod_node) > 0) {
+ if ((strcmp(mk, "quote") == 0 || strcmp(mk, "quoting_lit") == 0) &&
+ ts_node_named_child_count(mod_node) > 0) {
lisp_push_module(ctx, ts_node_named_child(mod_node, 0));
} else {
lisp_push_module(ctx, mod_node);
@@ -2017,6 +2033,42 @@ static void parse_bitbake_imports(CBMExtractCtx *ctx) {
}
}
+// --- Meson imports: subdir('name') → descend into name/meson.build ---
+// Meson's subdir() includes a child directory's meson.build into the build.
+// The grammar models a call as `function_expression` (id + argument_list).
+// Find subdir() calls and push the directory name as the module path; the
+// pipeline resolver maps "name" → "name/meson.build".
+static void parse_meson_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ /* tree-sitter-meson models a call as `normal_command` (identifier +
+ * arguments) and string arguments as `string`. */
+ if (strcmp(k, "normal_command") == 0) {
+ uint32_t nc = ts_node_named_child_count(node);
+ if (nc >= 1) {
+ TSNode fn = ts_node_named_child(node, 0);
+ char *fname = cbm_node_text(a, fn, ctx->source);
+ if (fname && strcmp(fname, "subdir") == 0) {
+ TSNode str = node;
+ if (find_first_descendant_of(node, "string", &str)) {
+ char *path = strip_quotes(a, cbm_node_text(a, str, ctx->source));
+ if (path && path[0]) {
+ CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
// --- Kconfig imports: source "path" ---
static void parse_kconfig_imports(CBMExtractCtx *ctx) {
TSNodeStack stack;
@@ -2412,6 +2464,9 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
break;
case CBM_LANG_SCHEME:
case CBM_LANG_RACKET:
+ case CBM_LANG_EMACSLISP:
+ case CBM_LANG_FENNEL:
+ case CBM_LANG_COMMONLISP:
parse_lisp_imports(ctx);
break;
case CBM_LANG_STARLARK:
@@ -2441,6 +2496,9 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
case CBM_LANG_JUST:
parse_just_imports(ctx);
break;
+ case CBM_LANG_MESON:
+ parse_meson_imports(ctx);
+ break;
case CBM_LANG_NIX:
parse_nix_imports(ctx);
break;
diff --git a/src/discover/language.c b/src/discover/language.c
index bacb8e476..856c9f8f5 100644
--- a/src/discover/language.c
+++ b/src/discover/language.c
@@ -255,6 +255,7 @@ static const ext_entry_t EXT_TABLE[] = {
{"justfile", CBM_LANG_JUST},
{"Justfile", CBM_LANG_JUST},
{".justfile", CBM_LANG_JUST},
+ {".just", CBM_LANG_JUST}, /* `import 'common.just'` target files */
{"hyprland.conf", CBM_LANG_HYPRLANG},
{"ssh_config", CBM_LANG_SSHCONFIG},
{"sshd_config", CBM_LANG_SSHCONFIG},
@@ -263,6 +264,9 @@ static const ext_entry_t EXT_TABLE[] = {
{"WORKSPACE", CBM_LANG_STARLARK},
{"WORKSPACE.bazel", CBM_LANG_STARLARK},
+ /* BitBake include fragments — `require/include foo.inc` target files. */
+ {".inc", CBM_LANG_BITBAKE},
+
/* Vue */
{".vue", CBM_LANG_VUE},
diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c
index dbdd45a78..5dcb5953c 100644
--- a/src/pipeline/pass_pkgmap.c
+++ b/src/pipeline/pass_pkgmap.c
@@ -1261,6 +1261,73 @@ static bool import_targetable_label(const char *label) {
return false;
}
+/* Resolve a sibling-file import: a bare path/name (no leading "./") that names
+ * a file relative to the importer's directory. This covers build/markup
+ * grammars whose import string is a sibling filename or directory rather than a
+ * dotted module path:
+ * - SCSS `@use 'vars'` → sibling `_vars.scss` (partial underscore)
+ * - Just `import 'common.just'` → sibling `common.just`
+ * - BitBake `require mypackage.inc` → sibling `mypackage.inc`
+ * - Meson `subdir('lib')` → `lib/meson.build`
+ * - func `#include "utils.fc"` → sibling `utils.fc`
+ * - Pony `use "util"` → sibling `util.pony`
+ * Builds a path relative to source_rel's directory, then looks up the resulting
+ * File/Module-node QN (extension is stripped by fqn_module). Returns a borrowed
+ * node or NULL. Several filename conventions are tried in turn. */
+static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx,
+ const char *source_rel, const char *source_file_qn,
+ const char *module_path) {
+ if (!module_path || !module_path[0]) {
+ return NULL;
+ }
+ /* Directory of the importing file (empty for repo-root files). */
+ char *dir = path_dirname(source_rel ? source_rel : "");
+ if (!dir) {
+ return NULL;
+ }
+
+ /* Candidate relative paths, in priority order. */
+ char cands[4][PKGMAP_PATH_BUF];
+ int ncand = 0;
+ const char *base = module_path;
+ /* Skip a leading "./". */
+ if (base[0] == '.' && base[1] == '/') {
+ base += 2;
+ }
+ /* 1. Direct sibling: dir/<module_path>. */
+ snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, dir[0] ? "/" : "", base);
+ /* 2. SCSS partial: dir/[subdir/]_<basename>.scss (underscore-prefixed). */
+ {
+ const char *slash = strrchr(base, '/');
+ const char *bn = slash ? slash + 1 : base;
+ char *dpart = slash ? cbm_strndup(base, (size_t)(slash - base)) : strdup("");
+ if (dpart && bn[0] != '_') {
+ snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s%s_%s.scss", dir, dir[0] ? "/" : "",
+ dpart[0] ? dpart : "", dpart[0] ? "/" : "", bn);
+ }
+ free(dpart);
+ }
+ /* 3. Meson subdir: dir/<module_path>/meson.build. */
+ snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s/meson.build", dir, dir[0] ? "/" : "", base);
+
+ const cbm_gbuf_node_t *found = NULL;
+ for (int i = 0; i < ncand; i++) {
+ char *qn = cbm_pipeline_fqn_module(ctx->project_name, cands[i]);
+ if (!qn) {
+ continue;
+ }
+ const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(ctx->gbuf, qn);
+ free(qn);
+ if (n && (!source_file_qn || !n->qualified_name ||
+ strcmp(n->qualified_name, source_file_qn) != 0)) {
+ found = n;
+ break;
+ }
+ }
+ free(dir);
+ return found;
+}
+
const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx,
const char *source_rel,
const char *source_file_qn,
@@ -1278,6 +1345,17 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
return target;
}
+ /* Strategy 1b: sibling-file resolution for build/markup grammars whose
+ * import string is a sibling filename or directory (SCSS partials, Just/
+ * BitBake/func includes, Meson subdir, Pony use). */
+ {
+ const cbm_gbuf_node_t *sib =
+ resolve_sibling_file(ctx, source_rel, source_file_qn, imp->module_path);
+ if (sib) {
+ return sib;
+ }
+ }
+
/* Strategy 2: namespace map. `using App.Utils`, `import com.example.Foo`,
* `use App\Utils\Helper` name a NAMESPACE (or a member of it) that the
* path-based QN cannot express. Try the full module path and progressively