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 .tfvars assignment OVERRIDEs the variable of its module

Martin Vogel949e87533724
diff --git a/README.md b/README.md
index dc95cb665..9e918142e 100644
--- a/README.md
+++ b/README.md
@@ -257,6 +257,7 @@ The install script placed beside the binary is **reported, not deleted** — uni
- `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. A module call with a local source (`module "net" { source = "./modules/net" }`) `IMPORTS` (`local_name`: the module's name) the `Folder` of that directory (the root directory: the `Project`), and `module.net.id` (also `module.net[0].id`, `module.net[*].id`) `USAGE`s (`ref: output`) the `output "id"` there, beside `module.net`
+- `OVERRIDE` with `via: tfvars` — a `.tfvars` assignment to a variable the module of its own directory declares, from the file's `Variable` to that `variable` block (the edge keeps `line`); a `.tfvars` file elsewhere is applied by a `-var-file` the repository does not state and links nothing
- `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/src/pipeline/pass_terraform.c b/src/pipeline/pass_terraform.c
index 0141eba25..0b2803e43 100644
--- a/src/pipeline/pass_terraform.c
+++ b/src/pipeline/pass_terraform.c
@@ -394,7 +394,23 @@ static bool tf_is_module_file(const char *rel) {
return dot && (strcmp(dot, ".tf") == 0 || strcmp(dot, ".tofu") == 0);
}
-/* ── References (PHASE 6 P6.1, P6.2) ───────────────────────────────── */
+/* A variable-definitions file (.tfvars; a .tfvars.json is JSON, not read here). */
+static bool tf_is_tfvars(const char *rel) {
+ const char *dot = strrchr(tf_basename(rel), '.');
+ return dot && strcmp(dot, ".tfvars") == 0;
+}
+
+/* The body of a config file's root node. */
+static TSNode tf_root_body(TSNode root) {
+ for (uint32_t i = 0; i < ts_node_named_child_count(root); i++) {
+ if (tf_type(ts_node_named_child(root, i), "body")) {
+ return ts_node_named_child(root, i);
+ }
+ }
+ return (TSNode){0};
+}
+
+/* ── References (PHASE 6 P6.1 - P6.3) ─────────────────────────────── */
/* A Terraform module is a directory: every .tf / .tofu file of it. A
* reference resolves inside its module only -- var.X to `variable "X"`,
@@ -410,7 +426,12 @@ static bool tf_is_module_file(const char *rel) {
* directory inside the repository, holding .tf / .tofu files) IMPORTS
* {local_name: X, via: terraform, line} that directory's Folder, and
* module.X.Y (also module.X[i].Y, module.X[*].Y) USAGEs {ref: output}, beside
- * module.X, the `output "Y"` of it. */
+ * module.X, the `output "Y"` of it.
+ *
+ * A .tfvars file sets variables of the module in its own directory: each
+ * assignment to a declared variable OVERRIDEs {via: tfvars, line} its
+ * `variable` block (a file elsewhere is applied by a -var-file on some
+ * command line, which is nothing the repository's Terraform states). */
typedef struct {
char *file; /* the referencing file */
@@ -656,12 +677,7 @@ static void tf_mod_call(tf_mod_t *m, const char *name, TSNode block, TSNode body
* calls 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);
- }
- }
+ TSNode body = tf_root_body(root);
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;
@@ -849,10 +865,48 @@ static CBMHashTable *tf_folder_index(cbm_pipeline_ctx_t *ctx) {
return ix;
}
+/* A .tfvars file sets variables of the module in its own directory: each
+ * top-level assignment whose name the module declares OVERRIDEs
+ * {via: tfvars, line}, from the file's Variable of that name, the
+ * `variable` block. */
+static int tf_link_tfvars(tf_mod_t *m, TSParser *parser, const char *rel) {
+ char path[CBM_SZ_2K];
+ snprintf(path, sizeof(path), "%s/%s", m->ctx->repo_path, rel);
+ size_t len = 0;
+ char *src = tf_read(path, &len);
+ TSTree *tree = src ? ts_parser_parse_string(parser, NULL, src, (uint32_t)len) : NULL;
+ TSNode body = tree ? tf_root_body(ts_tree_root_node(tree)) : (TSNode){0};
+ int edges = 0;
+ for (uint32_t i = 0; !ts_node_is_null(body) && i < ts_node_named_child_count(body); i++) {
+ TSNode at = ts_node_named_child(body, i);
+ if (!tf_type(at, "attribute") || ts_node_named_child_count(at) == 0) {
+ continue;
+ }
+ const char *key = tf_text(m->arena, src, ts_node_named_child(at, 0));
+ const char *vname = key ? cbm_arena_sprintf(m->arena, "variable.%s", key) : NULL;
+ const char *vfile = vname ? cbm_ht_get(m->decls, vname) : NULL;
+ int64_t from = vfile ? tf_def_node(m, rel, key) : 0;
+ int64_t to = from > 0 ? tf_def_node(m, vfile, vname) : 0;
+ if (to > 0) {
+ char props[CBM_SZ_128];
+ snprintf(props, sizeof(props), "{\"via\":\"tfvars\",\"line\":%d}",
+ (int)ts_node_start_point(at).row + SKIP_ONE);
+ cbm_gbuf_insert_edge(m->ctx->gbuf, from, to, "OVERRIDE", props);
+ edges++;
+ }
+ }
+ if (tree) {
+ ts_tree_delete(tree);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, src);
+ return edges;
+}
+
/* Every module directory, `paths[0..pn)` sorted by directory: parse all,
- * then link each -- a reference may read an output of another directory. */
-static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *const *paths,
- int pn) {
+ * then link each -- a reference may read an output of another directory --
+ * and the .tfvars files `vpaths[0..vn)` to the module of their directory. */
+static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *const *paths, int pn,
+ const char *const *vpaths, int vn) {
CBMArena arena;
cbm_arena_init_lazy(&arena, CBM_SZ_4K);
CBMHashTable *mods = cbm_ht_create(0);
@@ -877,6 +931,13 @@ static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *c
for (int k = 0; k < nm; k++) {
edges += tf_mod_link(list[k], mods, folders);
}
+ for (int k = 0; mods && k < vn; k++) {
+ const char *slash = strrchr(vpaths[k], '/');
+ char dir[CBM_SZ_1K];
+ snprintf(dir, sizeof(dir), "%.*s", slash ? (int)(slash - vpaths[k]) : 0, vpaths[k]);
+ tf_mod_t *m = cbm_ht_get(mods, dir);
+ edges += m ? tf_link_tfvars(m, parser, vpaths[k]) : 0;
+ }
for (int k = 0; k < nm; k++) {
tf_mod_free(list[k]);
}
@@ -891,16 +952,17 @@ static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *c
return edges;
}
-/* When one of the run's files is a .tf / .tofu file, every module directory
- * of the repository (the graph's File nodes) is resolved whole: on an
- * incremental run an unchanged file's reference may meet what a changed one
- * declares -- in its own directory (a variable, a locals entry) or in a
- * module it calls (an output). */
+/* When one of the run's files is a .tf / .tofu / .tfvars file, every module
+ * directory of the repository (the graph's File nodes) is resolved whole: on
+ * an incremental run an unchanged file's reference may meet what a changed
+ * one declares -- in its own directory (a variable, a locals entry), in a
+ * module it calls (an output), or a .tfvars file a variable it sets. */
static int tf_link_refs(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count,
TSParser *parser) {
bool any = false;
for (int i = 0; i < file_count && !any; i++) {
- any = files[i].language == CBM_LANG_HCL && tf_is_module_file(files[i].rel_path);
+ any = (files[i].language == CBM_LANG_HCL && tf_is_module_file(files[i].rel_path)) ||
+ tf_is_tfvars(files[i].rel_path);
}
const cbm_gbuf_node_t **fnodes = NULL;
int fn = 0;
@@ -908,18 +970,25 @@ static int tf_link_refs(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, i
return 0;
}
const char **paths = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)fn * sizeof(char *));
+ const char **vpaths = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)fn * sizeof(char *));
int pn = 0;
- for (int i = 0; paths && i < fn; i++) {
- if (fnodes[i]->file_path && tf_is_module_file(fnodes[i]->file_path)) {
- paths[pn++] = fnodes[i]->file_path;
+ int vn = 0;
+ for (int i = 0; paths && vpaths && i < fn; i++) {
+ const char *rel = fnodes[i]->file_path;
+ if (rel && tf_is_module_file(rel)) {
+ paths[pn++] = rel;
+ } else if (rel && tf_is_tfvars(rel)) {
+ vpaths[vn++] = rel;
}
}
int edges = 0;
if (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);
- edges = tf_link_repo(ctx, parser, paths, pn);
+ qsort((void *)vpaths, (size_t)vn, sizeof(*vpaths), tf_cmp_dir_path);
+ edges = tf_link_repo(ctx, parser, paths, pn, vpaths, vn);
}
cbm_free(CBM_MEM_CLASS_OTHER, (void *)paths);
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)vpaths);
return edges;
}
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index dffad5070..f9e139c12 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1504,7 +1504,8 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr
* the generic name matcher's USAGE / CALLS; a bare block label names
* its block like a quoted one; a module call with a local source
* IMPORTS its directory's Folder (the root: the Project) and module.X.Y
- * USAGEs {ref:output} the output there
+ * USAGEs {ref:output} the output there; a .tfvars assignment OVERRIDEs
+ * {via:tfvars} the variable block of its own directory
* (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 };
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 5e837a117..5c5572f5a 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2974,6 +2974,55 @@ TEST(probe_terraform_module_calls_import_their_source) {
PASS();
}
+/* Every tfvars OVERRIDE edge as "<file> <name> -> <file> <block> <line>". */
+static char *gpf_tf_tfvars(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 || ' ' || "
+ "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='OVERRIDE' AND json_extract(e.properties,'$.via')='tfvars' ORDER BY x)), '')";
+ 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 .tfvars file's assignment to a variable the module of its own directory
+ * declares OVERRIDEs {via: tfvars, line} that `variable` block, from the
+ * file's Variable of the name. An undeclared name, and a file in a directory
+ * without Terraform files (applied by a -var-file the repository does not
+ * state), make none. */
+TEST(probe_terraform_tfvars_override_their_variables) {
+ static const GpfFile files[] = {
+ {"stack/variables.tf", "variable \"region\" {}\nvariable \"size\" {\n default = 1\n}\n"},
+ {"stack/terraform.tfvars", "region = \"eu\"\nsize = 3\nunknown = \"x\"\n"},
+ {"stack/prod/prod.tfvars", "region = \"us\"\n"},
+ {"other.tfvars", "region = \"ap\"\n"}};
+ int n = (int)(sizeof(files) / sizeof(files[0]));
+ char *got = gpf_tf_tfvars(files, n);
+ static const char want[] = "stack/terraform.tfvars region -> stack/variables.tf variable.region 1\n"
+ "stack/terraform.tfvars size -> stack/variables.tf variable.size 2";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "terraform tfvars got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("terraform tfvars");
+ }
+ free(got);
+ 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) {
@@ -5414,6 +5463,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_terraform_refs_resolve_in_their_module);
RUN_TEST(probe_terraform_module_files_extract_no_generic_refs);
RUN_TEST(probe_terraform_module_calls_import_their_source);
+ RUN_TEST(probe_terraform_tfvars_override_their_variables);
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);