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.

feat(infra): a Terraform reference USAGEs the block of its module declaring it

Martin Vogela9d7a9622749
diff --git a/README.md b/README.md
index a393677dc..2ba526a12 100644
--- a/README.md
+++ b/README.md
@@ -256,6 +256,7 @@ The install script placed beside the binary is **reported, not deleted** — uni
- `DEPENDS_ON` — a Helm chart to the charts it declares (the local chart for a resolvable `file://` repository, otherwise an external `Chart`; the edge keeps `repository`), and a `go.mod` / `requirements.txt` file to an external `Package`; a Dockerfile to the `Image`s it builds on (`via: from`, `copy-from`); an `Image` to the Dockerfile it is built from (`via: build`; `tool`: `skaffold`, `compose`, `docker-build` for `docker` / `podman` / `nerdctl` / `buildah` build commands in scripts, Make and CI); a compose service to the services its `depends_on` / `links` name
- `CONFIGURES` — code reading an environment variable to the shared `EnvVar` of that name (`strategy: env_access`); a Kubernetes container, compose service or Dockerfile (`ENV` of the image) setting one to that container's own `EnvVar` (`strategy: env_set`, with `source`: `value`, `configMapKeyRef`, `secretKeyRef`, `fieldRef`, ..., `compose`, `dockerfile` — values are never stored); and that container's `EnvVar` to the code reading the name inside the files its image is built from (`strategy: env_read`, with `dockerfile`) — the image's files are the sources its Dockerfile's `COPY` / `ADD` put into it, less the context's `.dockerignore`
- `OVERRIDE` with `via: patch` — a Kustomize patch to each object of its kustomization's build it changes (child → base): a patch file's documents to the object of the same kind and name; an inline patch (from the kustomization) or a JSON6902 operation file to what its `target` selects (group, version, kind, name, namespace, label selector); a component's in the build of each kustomization including it. The edge keeps `kustomization`, `patch` and `line`; a patch file is an `IMPORTS` (`local_name: patches`) of its kustomization, never a member of the build
+- `USAGE` with `via: terraform` — a Terraform / OpenTofu reference (`var.X`, `local.X`, `module.X`, `data.T.N`, `T.N`) from the top-level block holding it to the block of its module — the `.tf` / `.tofu` files of its directory — that declares it (a `locals` entry: its `locals` block); an entry of the block's `depends_on` list is a `DEPENDS_ON` instead. The edge keeps `ref` (`var`, `local`, `module`, `data`, `resource`) and `line`; a name only mentioned in string text is no reference
- `EMITS`, `LISTENS_ON` (channels)
- `DATA_FLOWS` with arg-to-param mapping + field access chains
- `SIMILAR_TO` (MinHash + LSH near-clone detection, Jaccard scored)
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index a017a5518..77e0bbca0 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -7192,18 +7192,19 @@ static char *find_hcl_block_name(CBMArena *a, TSNode node, const char *source) {
// Append the block's quoted labels so each block gets a distinct,
// query-friendly name: resource "aws_instance" "web" -> resource.aws_instance.web
// rather than every resource collapsing to the bare keyword "resource" (#337).
- // HCL stores labels as string_lit -> template_literal children.
+ // HCL stores quoted labels as string_lit -> template_literal children and
+ // bare ones (`variable region {}`, valid HCL) as identifiers after the
+ // keyword's own -- both name the block.
uint32_t cc = ts_node_named_child_count(node);
for (uint32_t i = 0; i < cc; i++) {
TSNode ch = ts_node_named_child(node, i);
- if (strcmp(ts_node_type(ch), "string_lit") != 0) {
- continue;
- }
- TSNode lit = cbm_find_child_by_kind(ch, "template_literal");
- if (ts_node_is_null(lit)) {
- continue;
+ char *label = NULL;
+ if (strcmp(ts_node_type(ch), "string_lit") == 0) {
+ TSNode lit = cbm_find_child_by_kind(ch, "template_literal");
+ label = ts_node_is_null(lit) ? NULL : cbm_node_text(a, lit, source);
+ } else if (strcmp(ts_node_type(ch), "identifier") == 0 && !ts_node_eq(ch, id)) {
+ label = cbm_node_text(a, ch, source);
}
- char *label = cbm_node_text(a, lit, source);
if (!label || !label[0]) {
continue;
}
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index f8e4a228f..96e6ac865 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -2634,6 +2634,12 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
#else
const uint32_t budget_nodes = ctx->walk_budget_nodes;
#endif
+ /* A Terraform / OpenTofu module file's references resolve inside its
+ * module directory (pass_terraform.c); a call or usage by name here would
+ * meet same-named blocks of any module and a builtin function's name. */
+ const char *ext = ctx->rel_path ? strrchr(ctx->rel_path, '.') : NULL;
+ bool tf_module = ctx->language == CBM_LANG_HCL && ext &&
+ (strcmp(ext, ".tf") == 0 || strcmp(ext, ".tofu") == 0);
for (;;) {
TSNode node = ts_tree_cursor_current_node(&cursor);
@@ -2659,8 +2665,11 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
handle_string_constants(ctx, node, &state);
handle_objectscript_type_map(ctx, node, &state);
handle_url_builders(ctx, node, &state);
- CBMInvocationDescriptor invocation = handle_calls(ctx, node, spec, &state);
- handle_usages(ctx, node, spec, &state);
+ CBMInvocationDescriptor invocation =
+ tf_module ? (CBMInvocationDescriptor){0} : handle_calls(ctx, node, spec, &state);
+ if (!tf_module) {
+ handle_usages(ctx, node, spec, &state);
+ }
handle_throws(ctx, node, spec, &state);
handle_readwrites(ctx, node, spec, &state);
handle_type_refs(ctx, node, spec, &state);
diff --git a/src/pipeline/pass_terraform.c b/src/pipeline/pass_terraform.c
index 309f6c99c..bdb1802bf 100644
--- a/src/pipeline/pass_terraform.c
+++ b/src/pipeline/pass_terraform.c
@@ -393,6 +393,364 @@ static bool tf_is_module_file(const char *rel) {
return dot && (strcmp(dot, ".tf") == 0 || strcmp(dot, ".tofu") == 0);
}
+/* ── References (PHASE 6 P6.1) ─────────────────────────────────────── */
+
+/* A Terraform module is a directory: every .tf / .tofu file of it. A
+ * reference resolves inside its module only -- var.X to `variable "X"`,
+ * local.X to the locals block declaring X, module.X to `module "X"`,
+ * data.T.N to `data "T" "N"`, T.N to `resource "T" "N"` (T.N declared there);
+ * anything else (each, count, self, path, terraform, a for-expression name, a
+ * builtin function) to nothing. Edge: USAGE {ref, via: terraform, line} from
+ * the top-level block holding the reference to the declaring block; an entry
+ * of the block's depends_on list orders it after the object: DEPENDS_ON, the
+ * same properties. */
+
+typedef struct {
+ char *file; /* the referencing file */
+ char *src; /* the holding top-level block's definition name */
+ char *key; /* what it names: variable.X, local.X, module.X, data.T.N, resource.T.N */
+ const char *ref; /* static: var, local, module, data, resource */
+ int line;
+ bool dep; /* stands in the holder's depends_on list */
+} tf_ref_t;
+
+typedef struct {
+ cbm_pipeline_ctx_t *ctx;
+ CBMArena *arena;
+ CBMHashTable *decls; /* key (as tf_ref_t.key) -> declaring file (arena) */
+ tf_ref_t *refs;
+ int ref_n;
+ int ref_cap;
+ const char *src; /* the file's text */
+ const char *file; /* its repository path (arena) */
+ bool dep; /* walking a depends_on list */
+} tf_mod_t;
+
+static const char *tf_text(CBMArena *a, const char *src, TSNode n) {
+ uint32_t b = ts_node_start_byte(n);
+ return cbm_arena_strndup(a, src + b, ts_node_end_byte(n) - b);
+}
+
+/* A block label: a string_lit's text, or an identifier (`resource aws_x y`). */
+static const char *tf_block_label(CBMArena *a, const char *src, TSNode n) {
+ if (tf_type(n, "identifier")) {
+ return tf_text(a, src, n);
+ }
+ for (uint32_t i = 0; tf_type(n, "string_lit") && i < ts_node_named_child_count(n); i++) {
+ TSNode c = ts_node_named_child(n, i);
+ if (tf_type(c, "template_literal")) {
+ return tf_text(a, src, c);
+ }
+ }
+ return NULL;
+}
+
+/* A top-level block's definition name: its keyword and labels joined by
+ * "." (variable.X, resource.T.N, locals) -- the name extract_defs gives it. */
+static const char *tf_block_name(CBMArena *a, const char *src, TSNode block, TSNode *body) {
+ const char *name = NULL;
+ *body = (TSNode){0};
+ for (uint32_t i = 0; i < ts_node_named_child_count(block); i++) {
+ TSNode c = ts_node_named_child(block, i);
+ if (tf_type(c, "body")) {
+ *body = c;
+ } else if (i == 0 && tf_type(c, "identifier")) {
+ name = tf_text(a, src, c);
+ } else if (name) {
+ const char *label = tf_block_label(a, src, c);
+ name = label ? cbm_arena_sprintf(a, "%s.%s", name, label) : name;
+ }
+ }
+ return name;
+}
+
+static void tf_ref_push(tf_mod_t *m, const char *src_block, const char *ref, const char *key,
+ int line) {
+ if (m->ref_n >= m->ref_cap) {
+ int cap = m->ref_cap ? m->ref_cap * PAIR_LEN : CBM_SZ_64;
+ tf_ref_t *grown = cbm_realloc(CBM_MEM_CLASS_OTHER, m->refs, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ m->refs = grown;
+ m->ref_cap = cap;
+ }
+ m->refs[m->ref_n++] =
+ (tf_ref_t){(char *)m->file, (char *)src_block, (char *)key, ref, line, m->dep};
+}
+
+/* The identifier of the k-th get_attr following `v` among its siblings (a
+ * traversal is variable_expr, get_attr, get_attr, ... once the grammar's
+ * hidden expression rule is flattened); NULL past the attributes. */
+static const char *tf_attr(CBMArena *a, const char *src, TSNode v, int k) {
+ TSNode s = ts_node_next_named_sibling(v);
+ for (int i = 0; !ts_node_is_null(s) && tf_type(s, "get_attr"); i++) {
+ if (i == k) {
+ for (uint32_t c = 0; c < ts_node_named_child_count(s); c++) {
+ if (tf_type(ts_node_named_child(s, c), "identifier")) {
+ return tf_text(a, src, ts_node_named_child(s, c));
+ }
+ }
+ return NULL;
+ }
+ s = ts_node_next_named_sibling(s);
+ }
+ return NULL;
+}
+
+/* One traversal `root.attr...` inside block `src_block`. */
+static void tf_traversal(tf_mod_t *m, const char *src_block, TSNode v) {
+ if (ts_node_named_child_count(v) == 0) {
+ return;
+ }
+ CBMArena *a = m->arena;
+ const char *root = tf_text(a, m->src, ts_node_named_child(v, 0));
+ const char *a1 = tf_attr(a, m->src, v, 0);
+ if (!root || !a1) {
+ return;
+ }
+ int line = (int)ts_node_start_point(v).row + SKIP_ONE;
+ if (strcmp(root, "var") == 0) {
+ tf_ref_push(m, src_block, "var", cbm_arena_sprintf(a, "variable.%s", a1), line);
+ } else if (strcmp(root, "local") == 0) {
+ tf_ref_push(m, src_block, "local", cbm_arena_sprintf(a, "local.%s", a1), line);
+ } else if (strcmp(root, "module") == 0) {
+ tf_ref_push(m, src_block, "module", cbm_arena_sprintf(a, "module.%s", a1), line);
+ } else if (strcmp(root, "data") == 0) {
+ const char *a2 = tf_attr(a, m->src, v, SKIP_ONE);
+ if (a2) {
+ tf_ref_push(m, src_block, "data", cbm_arena_sprintf(a, "data.%s.%s", a1, a2), line);
+ }
+ } else if (strchr(root, '_')) {
+ tf_ref_push(m, src_block, "resource", cbm_arena_sprintf(a, "resource.%s.%s", root, a1),
+ line);
+ }
+}
+
+/* Every traversal under `n` (iterative: deep expressions). */
+static void tf_walk_refs(tf_mod_t *m, const char *src_block, TSNode n) {
+ TSTreeCursor cur = ts_tree_cursor_new(n);
+ bool more = true;
+ while (more) {
+ TSNode c = ts_tree_cursor_current_node(&cur);
+ if (tf_type(c, "variable_expr")) {
+ tf_traversal(m, src_block, c);
+ }
+ if (ts_tree_cursor_goto_first_child(&cur)) {
+ continue;
+ }
+ while (!ts_tree_cursor_goto_next_sibling(&cur)) {
+ if (!ts_tree_cursor_goto_parent(&cur)) {
+ more = false;
+ break;
+ }
+ }
+ }
+ ts_tree_cursor_delete(&cur);
+}
+
+/* Node `n`'s text is `word`. */
+static bool tf_is_word(const tf_mod_t *m, TSNode n, const char *word) {
+ uint32_t b = ts_node_start_byte(n);
+ size_t len = strlen(word);
+ return ts_node_end_byte(n) - b == len && memcmp(m->src + b, word, len) == 0;
+}
+
+/* The references of one top-level block's body, those of its depends_on
+ * list marked as such. */
+static void tf_walk_block(tf_mod_t *m, const char *src_block, TSNode body) {
+ for (uint32_t i = 0; i < ts_node_named_child_count(body); i++) {
+ TSNode c = ts_node_named_child(body, i);
+ m->dep = tf_type(c, "attribute") && ts_node_named_child_count(c) > 0 &&
+ tf_is_word(m, ts_node_named_child(c, 0), "depends_on");
+ tf_walk_refs(m, src_block, c);
+ }
+ m->dep = false;
+}
+
+/* One file of a module: its declarations (blocks, locals entries) and the
+ * references of each top-level block. */
+static void tf_mod_file(tf_mod_t *m, TSNode root) {
+ CBMArena *a = m->arena;
+ TSNode body = {0};
+ for (uint32_t i = 0; i < ts_node_named_child_count(root) && ts_node_is_null(body); i++) {
+ if (tf_type(ts_node_named_child(root, i), "body")) {
+ body = ts_node_named_child(root, i);
+ }
+ }
+ for (uint32_t i = 0; !ts_node_is_null(body) && i < ts_node_named_child_count(body); i++) {
+ TSNode b = ts_node_named_child(body, i);
+ TSNode bb;
+ const char *name = tf_type(b, "block") ? tf_block_name(a, m->src, b, &bb) : NULL;
+ if (!name) {
+ continue;
+ }
+ cbm_ht_set(m->decls, name, (void *)m->file); /* `{}` declares too */
+ if (ts_node_is_null(bb)) {
+ continue;
+ }
+ if (strcmp(name, "locals") == 0) {
+ for (uint32_t j = 0; j < ts_node_named_child_count(bb); j++) {
+ TSNode at = ts_node_named_child(bb, j);
+ if (tf_type(at, "attribute") && ts_node_named_child_count(at) > 0) {
+ const char *key = tf_text(a, m->src, ts_node_named_child(at, 0));
+ cbm_ht_set(m->decls, cbm_arena_sprintf(a, "local.%s", key), (void *)m->file);
+ }
+ }
+ }
+ tf_walk_block(m, name, bb);
+ }
+}
+
+/* The node of definition `name` in `file`, by the QN extract_defs gave it. */
+static int64_t tf_def_node(tf_mod_t *m, const char *file, const char *name) {
+ const char *qn = cbm_fqn_compute(m->arena, m->ctx->project_name, file, name);
+ const cbm_gbuf_node_t *n = qn ? cbm_gbuf_find_by_qn(m->ctx->gbuf, qn) : NULL;
+ atomic_fetch_add_explicit(&g_tf_candidates, SKIP_ONE, memory_order_relaxed);
+ return n && n->file_path && strcmp(n->file_path, file) == 0 ? n->id : 0;
+}
+
+/* Order paths by directory, then name: a directory's files are contiguous. */
+static int tf_cmp_dir_path(const void *x, const void *y) {
+ const char *p = *(const char *const *)x;
+ const char *q = *(const char *const *)y;
+ const char *ps = strrchr(p, '/');
+ const char *qs = strrchr(q, '/');
+ size_t pl = ps ? (size_t)(ps - p) : 0;
+ size_t ql = qs ? (size_t)(qs - q) : 0;
+ size_t n = pl < ql ? pl : ql;
+ int c = strncmp(p, q, n);
+ if (c == 0 && pl != ql) {
+ c = pl < ql ? -1 : 1;
+ }
+ return c ? c : strcmp(p, q);
+}
+
+static bool tf_same_dir(const char *p, const char *q) {
+ const char *ps = strrchr(p, '/');
+ const char *qs = strrchr(q, '/');
+ size_t pl = ps ? (size_t)(ps - p) : 0;
+ size_t ql = qs ? (size_t)(qs - q) : 0;
+ return pl == ql && strncmp(p, q, pl) == 0;
+}
+
+/* Resolve the module's references against its declarations. */
+static int tf_mod_link(tf_mod_t *m) {
+ int edges = 0;
+ for (int i = 0; i < m->ref_n; i++) {
+ const tf_ref_t *r = &m->refs[i];
+ const char *tfile = cbm_ht_get(m->decls, r->key);
+ if (!tfile) {
+ continue;
+ }
+ bool local = strcmp(r->ref, "local") == 0;
+ int64_t src = tf_def_node(m, r->file, r->src);
+ int64_t tgt = tf_def_node(m, tfile, local ? "locals" : r->key);
+ if (src <= 0 || tgt <= 0 || src == tgt) {
+ continue;
+ }
+ char props[CBM_SZ_128];
+ snprintf(props, sizeof(props), "{\"ref\":\"%s\",\"via\":\"terraform\",\"line\":%d}", r->ref,
+ r->line);
+ cbm_gbuf_insert_edge(m->ctx->gbuf, src, tgt, r->dep ? "DEPENDS_ON" : "USAGE", props);
+ edges++;
+ }
+ return edges;
+}
+
+/* One module directory, its files `paths[0..n)`: parse each (read from the
+ * repository), then resolve. */
+static int tf_link_module(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *const *paths,
+ int n) {
+ CBMArena arena;
+ cbm_arena_init_lazy(&arena, CBM_SZ_4K);
+ tf_mod_t m = {.ctx = ctx, .arena = &arena, .decls = cbm_ht_create(0)};
+ for (int i = 0; m.decls && i < n; i++) {
+ char path[CBM_SZ_2K];
+ snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, paths[i]);
+ size_t len = 0;
+ char *src = tf_read(path, &len);
+ TSTree *tree = src ? ts_parser_parse_string(parser, NULL, src, (uint32_t)len) : NULL;
+ if (tree) {
+ m.src = src;
+ m.file = cbm_arena_strdup(&arena, paths[i]);
+ tf_mod_file(&m, ts_tree_root_node(tree));
+ ts_tree_delete(tree);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, src);
+ }
+ int edges = m.decls ? tf_mod_link(&m) : 0;
+ if (m.decls) {
+ cbm_ht_free(m.decls);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, m.refs);
+ cbm_arena_destroy(&arena);
+ return edges;
+}
+
+/* Every module directory one of the run's .tf / .tofu files is in, resolved
+ * whole: all its files (the graph's File nodes) -- on an incremental run an
+ * unchanged file's reference may meet what a changed one declares. */
+static int tf_link_refs(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count,
+ TSParser *parser) {
+ CBMHashTable *dirs = cbm_ht_create(0);
+ if (!dirs) {
+ return 0;
+ }
+ CBMArena keys;
+ cbm_arena_init_lazy(&keys, CBM_SZ_4K);
+ int ndirs = 0;
+ for (int i = 0; i < file_count; i++) {
+ const char *rel = files[i].rel_path;
+ if (files[i].language == CBM_LANG_HCL && tf_is_module_file(rel)) {
+ const char *slash = strrchr(rel, '/');
+ const char *dir = cbm_arena_strndup(&keys, rel, slash ? (size_t)(slash - rel) : 0);
+ if (dir && !cbm_ht_get(dirs, dir)) {
+ cbm_ht_set(dirs, dir, (void *)dir);
+ ndirs++;
+ }
+ }
+ }
+ const cbm_gbuf_node_t **fnodes = NULL;
+ int fn = 0;
+ if (ndirs == 0 || cbm_gbuf_find_by_label(ctx->gbuf, "File", &fnodes, &fn) != 0) {
+ cbm_ht_free(dirs);
+ cbm_arena_destroy(&keys);
+ return 0;
+ }
+ const char **paths =
+ cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)(fn ? fn : SKIP_ONE) * sizeof(char *));
+ int pn = 0;
+ for (int i = 0; paths && i < fn; i++) {
+ const char *rel = fnodes[i]->file_path;
+ if (!rel || !tf_is_module_file(rel)) {
+ continue;
+ }
+ const char *slash = strrchr(rel, '/');
+ char dir[CBM_SZ_1K];
+ snprintf(dir, sizeof(dir), "%.*s", slash ? (int)(slash - rel) : 0, rel);
+ if (cbm_ht_get(dirs, dir)) {
+ paths[pn++] = rel;
+ }
+ }
+ int edges = 0;
+ if (paths && pn > 0 && ts_parser_set_language(parser, cbm_ts_language(CBM_LANG_HCL))) {
+ qsort((void *)paths, (size_t)pn, sizeof(*paths), tf_cmp_dir_path);
+ for (int i = 0; i < pn;) {
+ int j = i;
+ while (j < pn && tf_same_dir(paths[i], paths[j])) {
+ j++;
+ }
+ edges += tf_link_module(ctx, parser, paths + i, j - i);
+ i = j;
+ }
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)paths);
+ cbm_ht_free(dirs);
+ cbm_arena_destroy(&keys);
+ return edges;
+}
+
void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count) {
TSParser *parser = NULL;
@@ -434,6 +792,10 @@ void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
}
cbm_free(CBM_MEM_CLASS_OTHER, src);
}
+ if (!parser) {
+ parser = ts_parser_new();
+ }
+ int refs = parser ? tf_link_refs(ctx, files, file_count, parser) : 0;
if (parser) {
ts_parser_delete(parser);
}
@@ -441,9 +803,11 @@ void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
cbm_ht_free(charts);
}
cbm_arena_destroy(&arena);
- if (edges > 0) {
+ if (edges > 0 || refs > 0) {
char buf[CBM_SZ_32];
+ char rbuf[CBM_SZ_32];
snprintf(buf, sizeof(buf), "%d", edges);
- cbm_log_info("pass.terraform.deploys", "edges", buf);
+ snprintf(rbuf, sizeof(rbuf), "%d", refs);
+ cbm_log_info("pass.terraform.edges", "deploys", buf, "refs", rbuf);
}
}
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 92e20f8fb..f6a126c47 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1498,8 +1498,13 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr
* A kustomize patch OVERRIDEs the objects of its build it changes, its
* file an IMPORTS of the kustomization; a Resource keeps the version of
* its apiVersion and its labels; a kustomization DEPLOYS the charts it inflates
- * (helmCharts) (extract_k8s.c, pass_k8s.c). An index built before
- * holds none of them, so it rebuilds once on upgrade. */
+ * (helmCharts) (extract_k8s.c, pass_k8s.c). A Terraform reference
+ * USAGEs {ref, via:terraform}, from its block, the block of its module
+ * (directory) declaring it (one in depends_on: DEPENDS_ON), instead of
+ * the generic name matcher's USAGE / CALLS; a bare block label names
+ * its block like a quoted one
+ * (pass_terraform.c, extract_unified.c, extract_defs.c). An index built
+ * before holds none of them, so it rebuilds once on upgrade. */
enum { CBM_SEMANTIC_INDEX_VERSION = 9 };
typedef struct {
diff --git a/tests/fixtures/complexity/HCL/main.tf b/tests/fixtures/complexity/HCL/main.tf
index 14b399a79..74cf28398 100644
--- a/tests/fixtures/complexity/HCL/main.tf
+++ b/tests/fixtures/complexity/HCL/main.tf
@@ -1,10 +1,16 @@
+locals {
+ ns = "${var.prefix}-apps"
+}
+
resource "helm_release" "this" {
- name = "app"
- chart = "${path.module}/charts/app"
+ name = "app"
+ namespace = local.ns
+ chart = "${path.module}/charts/app"
}
resource "helm_release" "ingress" {
name = "ingress"
repository = "https://kubernetes.github.io/ingress-nginx"
chart = "ingress-nginx"
+ depends_on = [helm_release.this]
}
diff --git a/tests/fixtures/complexity/HCL/variables.tf b/tests/fixtures/complexity/HCL/variables.tf
new file mode 100644
index 000000000..cf328746c
--- /dev/null
+++ b/tests/fixtures/complexity/HCL/variables.tf
@@ -0,0 +1,3 @@
+variable "prefix" {
+ default = "cx"
+}
diff --git a/tests/repro/repro_call_argument_matrix_a.c b/tests/repro/repro_call_argument_matrix_a.c
index 5399bddab..ce0fe18fa 100644
--- a/tests/repro/repro_call_argument_matrix_a.c
+++ b/tests/repro/repro_call_argument_matrix_a.c
@@ -823,12 +823,16 @@ SEMANTIC_CASE(scss, SCSS, "main.scss",
"@function bare($handler) {\n @return $handler;\n}\n",
"$handler", "call_expression", 2, 2);
-NATIVE_SCOPE_CASE(hcl, HCL, "main.tf", "locals {\n watched = [1]\n result = length(watched)\n}\n",
+/* A Terraform / OpenTofu module file (.tf, .tofu) leaves its references to
+ * the module resolver (pass_terraform.c) and extracts no generic calls or
+ * usages (probe_terraform_module_files_extract_no_generic_refs); the generic
+ * HCL behaviour checked here is that of every other HCL file. */
+NATIVE_SCOPE_CASE(hcl, HCL, "main.hcl", "locals {\n watched = [1]\n result = length(watched)\n}\n",
"function_call",
"HCL exposes built-in function applications but cannot declare user routines, "
"so local-expression references use exact global counts",
"length", NULL, "watched", NULL, NULL,
- "locals {\n watched = [1]\n result = watched\n}\n", "bare.tf", NULL, "watched",
+ "locals {\n watched = [1]\n result = watched\n}\n", "bare.hcl", NULL, "watched",
NULL, 1, "variable_expr", "watched", 1, 1, 1, 0);
/* This known-valid PL/pgSQL shape exercises the grammar-real `invocation` node.
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index e4e180798..e45f806bc 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2781,6 +2781,123 @@ TEST(probe_kustomize_helm_charts_deploy_their_chart) {
PASS();
}
+/* Every Terraform reference edge as
+ * "<file> <block> -> <file> <block> <type> <ref>:<line>", then a line
+ * "other tf edges <n>": USAGE / CALLS edges leaving a .tf file that the
+ * module resolver did not make. */
+static char *gpf_tf_refs(const GpfFile *files, int nfiles) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ char *out = NULL;
+ if (store) {
+ sqlite3_stmt *st = NULL;
+ const char *sql =
+ "SELECT coalesce((SELECT group_concat(x, char(10)) FROM (SELECT s.file_path || ' ' || "
+ "s.name || ' -> ' || t.file_path || ' ' || t.name || ' ' || e.type || ' ' || "
+ "json_extract(e.properties,'$.ref') || ':' || json_extract(e.properties,'$.line') AS x "
+ "FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id "
+ "WHERE e.project=?1 AND e.type IN ('USAGE','DEPENDS_ON') AND "
+ "json_extract(e.properties,'$.via')='terraform' ORDER BY x)), '') || char(10) || "
+ "'other tf edges ' || (SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id "
+ "WHERE e.project=?1 AND e.type IN ('USAGE','CALLS') AND s.file_path LIKE '%.tf' AND "
+ "coalesce(json_extract(e.properties,'$.via'),'') != 'terraform')";
+ if (sqlite3_prepare_v2(cbm_store_get_db(store), sql, -1, &st, NULL) == SQLITE_OK) {
+ sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
+ if (sqlite3_step(st) == SQLITE_ROW) {
+ out = strdup((const char *)sqlite3_column_text(st, 0));
+ }
+ }
+ sqlite3_finalize(st);
+ }
+ gpf_cleanup(&lp, store);
+ return out;
+}
+
+/* A Terraform reference (var.X, local.X, module.X, data.T.N, T.N) USAGEs,
+ * from the top-level block holding it, the block declaring it in the same
+ * directory -- the module, all its .tf files -- with the reference kind and
+ * line; an entry of the block's depends_on list is a DEPENDS_ON (beside a
+ * USAGE where the block also uses the object). A bare label (`variable prefix {}`) names its block like a quoted
+ * one. A name mentioned in string text, a `/ *` glob inside an
+ * interpolation, a reference to the block holding it, and a directory
+ * declaring nothing of the name make no edge; the generic name matcher
+ * makes none at all for a .tf file. */
+TEST(probe_terraform_refs_resolve_in_their_module) {
+ static const GpfFile files[] = {
+ {"infra/variables.tf",
+ "variable \"region\" {}\nvariable prefix {\n default = \"x\"\n}\n"
+ "variable \"unused\" {\n description = \"see var.region\"\n}\n"},
+ {"infra/main.tf",
+ "locals {\n name = \"${var.prefix}-svc\"\n full = \"${local.name}-${var.region}\"\n}\n"
+ "resource \"aws_s3_bucket\" \"logs\" {\n bucket = local.full\n}\n"
+ "resource \"aws_s3_bucket_policy\" \"logs\" {\n bucket = aws_s3_bucket.logs.id\n"
+ " policy = data.aws_iam_policy_document.p.json\n"
+ " depends_on = [aws_s3_bucket.logs]\n}\n"
+ "data \"aws_iam_policy_document\" \"p\" {\n statement {\n"
+ " resources = [\"${aws_s3_bucket.logs.arn}/*\"]\n }\n}\n"
+ "module \"net\" {\n source = \"./net\"\n region = var.region\n"
+ " depends_on = [\n aws_s3_bucket_policy.logs,\n ]\n}\n"
+ "output \"net\" {\n value = module.net.id\n}\n"},
+ {"infra/net/main.tf",
+ "variable \"region\" {}\nresource \"aws_vpc\" \"v\" {\n tags = { r = var.region }\n}\n"},
+ {"other/x.tf", "resource \"null_resource\" \"n\" {\n triggers = { r = var.region }\n}\n"}};
+ int n = (int)(sizeof(files) / sizeof(files[0]));
+ char *got = gpf_tf_refs(files, n);
+ static const char want[] =
+ "infra/main.tf data.aws_iam_policy_document.p -> infra/main.tf resource.aws_s3_bucket.logs "
+ "USAGE resource:15\n"
+ "infra/main.tf locals -> infra/variables.tf variable.prefix USAGE var:2\n"
+ "infra/main.tf locals -> infra/variables.tf variable.region USAGE var:3\n"
+ "infra/main.tf module.net -> infra/main.tf resource.aws_s3_bucket_policy.logs "
+ "DEPENDS_ON resource:22\n"
+ "infra/main.tf module.net -> infra/variables.tf variable.region USAGE var:20\n"
+ "infra/main.tf output.net -> infra/main.tf module.net USAGE module:26\n"
+ "infra/main.tf resource.aws_s3_bucket.logs -> infra/main.tf locals USAGE local:6\n"
+ "infra/main.tf resource.aws_s3_bucket_policy.logs -> infra/main.tf "
+ "data.aws_iam_policy_document.p USAGE data:10\n"
+ "infra/main.tf resource.aws_s3_bucket_policy.logs -> infra/main.tf "
+ "resource.aws_s3_bucket.logs DEPENDS_ON resource:11\n"
+ "infra/main.tf resource.aws_s3_bucket_policy.logs -> infra/main.tf "
+ "resource.aws_s3_bucket.logs USAGE resource:9\n"
+ "infra/net/main.tf resource.aws_vpc.v -> infra/net/main.tf variable.region USAGE var:3\n"
+ "other tf edges 0";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "terraform refs got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("terraform refs");
+ }
+ free(got);
+ PASS();
+}
+
+/* A Terraform / OpenTofu module file (.tf, .tofu) extracts its blocks but no
+ * generic calls or usages -- its references are the module resolver's -- while
+ * any other HCL file (Packer, Nomad, Terragrunt .hcl) keeps them. */
+TEST(probe_terraform_module_files_extract_no_generic_refs) {
+ static const char src[] = "locals {\n watched = [1]\n result = length(watched)\n}\n";
+ static const char *const names[] = {"main.tf", "main.tofu", "main.hcl"};
+ int calls[3];
+ int usages[3];
+ int defs[3];
+ for (int i = 0; i < 3; i++) {
+ CBMFileResult *r = cbm_extract_file(src, (int)strlen(src), CBM_LANG_HCL, "probe", names[i],
+ 0, NULL, NULL);
+ ASSERT_NOT_NULL(r);
+ calls[i] = r->calls.count;
+ usages[i] = r->usages.count;
+ defs[i] = r->defs.count;
+ cbm_free_result(r);
+ }
+ for (int i = 0; i < 2; i++) {
+ ASSERT_EQ(calls[i], 0);
+ ASSERT_EQ(usages[i], 0);
+ ASSERT_GT(defs[i], 0);
+ }
+ ASSERT_GT(calls[2], 0);
+ ASSERT_GT(usages[2], 0);
+ PASS();
+}
+
/* Every script HTTP_CALLS edge as "<source file> <route QN>", then a line
* "edgeless routes <n>": the Route nodes no edge touches. */
static char *gpf_script_http(const GpfFile *files, int nfiles) {
@@ -5218,6 +5335,8 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_script_http_request_calls_its_route);
RUN_TEST(probe_kustomize_patches_override_their_base);
RUN_TEST(probe_kustomize_helm_charts_deploy_their_chart);
+ RUN_TEST(probe_terraform_refs_resolve_in_their_module);
+ RUN_TEST(probe_terraform_module_files_extract_no_generic_refs);
RUN_TEST(probe_image_hub_splits_by_component);
RUN_TEST(probe_gitops_objects_deploy_their_units);
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 7099db3ca..89989037d 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -1379,15 +1379,15 @@ TEST(pipeline_nix_scoped_binding_calls_resolve) {
/* Terraform reference resolution, end to end.
*
* An HCL block names itself with its labels appended -- find_hcl_block_name
- * mints "resource.aws_instance.web" -- while its QN's last dot segment is
- * bare "web", and a reference written `aws_instance.web.id` reaches the
- * by-name index through that tail. HCL is therefore a language where the
- * definition name and the QN tail are different strings, and an index keyed on
- * either one alone drops every cross-resource reference in the file.
+ * mints "resource.aws_instance.web" -- and the module resolver
+ * (pass_terraform.c) finds the block a reference `aws_instance.web.id` names
+ * by recomputing that QN from the declaring file and the name. The reference
+ * USAGEs it from the block holding it, `resource.aws_eip.ip`; the file itself
+ * USAGEs nothing (a .tf file is no longer served by the generic by-name index).
*
- * This has to be a pipeline test: the registry is shared by every language
- * and nothing in the HCL extractor mentions the index key, so the two halves
- * can disagree with every extraction-level assertion still green.
+ * This has to be a pipeline test: the extractor mints the name and the
+ * resolver recomputes the QN, so the two halves can disagree with every
+ * extraction-level assertion still green.
*/
TEST(pipeline_hcl_block_reference_resolves_to_its_block) {
if (setup_test_repo() != 0) {
@@ -1422,7 +1422,9 @@ TEST(pipeline_hcl_block_reference_resolves_to_its_block) {
ASSERT_NOT_NULL(ts);
const char *tf_project = cbm_pipeline_project_name(tp);
- ASSERT(cross_file_edge_exists(ts, tf_project, "main", "resource.aws_instance.web", "USAGE"));
+ ASSERT(cross_file_edge_exists(ts, tf_project, "resource.aws_eip.ip",
+ "resource.aws_instance.web", "USAGE"));
+ ASSERT(!cross_file_edge_exists(ts, tf_project, "main", "resource.aws_instance.web", "USAGE"));
cbm_store_close(ts);
cbm_pipeline_free(tp);