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 file sets an ancestor module's variables, inferred

Martin Vogelcdfdb8f8776b
diff --git a/README.md b/README.md
index 27db9217d..5623f63fe 100644
--- a/README.md
+++ b/README.md
@@ -257,7 +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
+- `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 whose own directory declares none of its names sets the nearest ancestor module's that does, the edge marked `inferred` (such a file is applied by a `-var-file` the repository does not state)
- Terragrunt (`via: terragrunt`) — a unit (`terragrunt.hcl`), evaluated in its own context: an `include` `IMPORTS` (`local_name`) the file its `path` names; a `read_terragrunt_config` `IMPORTS` (`read`: the file holding the call) the file it names, from the block holding the call or, for a call in an included file, from the unit's `include` block that brings it in; a `dependency` `DEPENDS_ON` the other unit; a local `terraform` `source` `IMPORTS` its `Folder`. Paths may use `find_in_parent_folders`, `dirname` and `get_terragrunt_dir`; anything else is unknown and links nothing
- Ansible (`via: ansible`) — a playbook or task file (a YAML file whose top level is a sequence) `IMPORTS` (`local_name`: the directive) what its directives name: `import_playbook` a playbook, `include_tasks` / `import_tasks` / `include` a task file (from its directory, else its role's `tasks/`), `roles:` items and `include_role` / `import_role` a role's `Folder` (`roles/<name>` at or above its directory). A templated target links nothing
- `EMITS`, `LISTENS_ON` (channels)
diff --git a/src/pipeline/pass_terraform.c b/src/pipeline/pass_terraform.c
index 860bc298d..0eb47257c 100644
--- a/src/pipeline/pass_terraform.c
+++ b/src/pipeline/pass_terraform.c
@@ -432,10 +432,11 @@ static TSNode tf_root_body(TSNode root) {
* module.X.Y (also module.X[i].Y, module.X[*].Y) USAGEs {ref: output}, beside
* 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). */
+ * A .tfvars file sets variables of the module in its own directory -- or,
+ * when that declares none of its names, of the nearest ancestor directory's
+ * module that does (applied by a -var-file the repository does not state:
+ * the edge says `inferred`): each assignment to a declared variable
+ * OVERRIDEs {via: tfvars, line} its `variable` block. */
typedef struct {
char *file; /* the referencing file */
@@ -876,32 +877,70 @@ 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) {
+/* The module of the first of `d` and its ancestors (d is cut in place)
+ * declaring one of `vnames[0..n)`; NULL when none does. */
+static tf_mod_t *tf_tfvars_module(CBMHashTable *mods, char *d, const char *const *vnames, int n) {
+ for (;;) {
+ tf_mod_t *m = cbm_ht_get(mods, d);
+ for (int k = 0; m && k < n; k++) {
+ if (vnames[k] && cbm_ht_get(m->decls, vnames[k])) {
+ return m;
+ }
+ }
+ if (!d[0]) {
+ return NULL;
+ }
+ char *s = strrchr(d, '/');
+ if (s) {
+ *s = '\0';
+ } else {
+ d[0] = '\0';
+ }
+ }
+}
+
+/* A .tfvars file sets variables of a module: the one in its own directory
+ * when that declares one of its names, else the nearest ancestor directory's
+ * that does (a file there is applied by a -var-file the repository does not
+ * state: the edge says `inferred`). 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(CBMArena *arena, TSParser *parser, CBMHashTable *mods,
+ const char *repo_path, const char *rel) {
char path[CBM_SZ_2K];
- snprintf(path, sizeof(path), "%s/%s", m->ctx->repo_path, rel);
+ snprintf(path, sizeof(path), "%s/%s", 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++) {
+ uint32_t nc = ts_node_is_null(body) ? 0 : ts_node_named_child_count(body);
+ const char **vnames = nc ? cbm_arena_alloc(arena, (size_t)nc * sizeof(*vnames)) : NULL;
+ TSNode *attrs = nc ? cbm_arena_alloc(arena, (size_t)nc * sizeof(*attrs)) : NULL;
+ int n = 0;
+ for (uint32_t i = 0; vnames && attrs && i < nc; i++) {
TSNode at = ts_node_named_child(body, i);
- if (!tf_type(at, "attribute") || ts_node_named_child_count(at) == 0) {
- continue;
+ if (tf_type(at, "attribute") && ts_node_named_child_count(at) > 0) {
+ const char *key = tf_text(arena, src, ts_node_named_child(at, 0));
+ vnames[n] = key ? cbm_arena_sprintf(arena, "variable.%s", key) : NULL;
+ attrs[n++] = at;
}
- 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;
+ }
+ const char *slash = strrchr(rel, '/');
+ char d[CBM_SZ_1K];
+ snprintf(d, sizeof(d), "%.*s", slash ? (int)(slash - rel) : 0, rel);
+ size_t own = strlen(d);
+ tf_mod_t *m = n > 0 ? tf_tfvars_module(mods, d, vnames, n) : NULL;
+ int edges = 0;
+ for (int k = 0; m && k < n; k++) {
+ const char *vfile = vnames[k] ? cbm_ht_get(m->decls, vnames[k]) : NULL;
+ const char *key = vfile ? vnames[k] + sizeof("variable.") - SKIP_ONE : NULL;
+ int64_t from = key ? tf_def_node(m, rel, key) : 0;
+ int64_t to = from > 0 ? tf_def_node(m, vfile, vnames[k]) : 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);
+ snprintf(props, sizeof(props), "{\"via\":\"tfvars\",%s\"line\":%d}",
+ strlen(d) == own ? "" : "\"inferred\":true,",
+ (int)ts_node_start_point(attrs[k]).row + SKIP_ONE);
cbm_gbuf_insert_edge(m->ctx->gbuf, from, to, "OVERRIDE", props);
edges++;
}
@@ -915,7 +954,7 @@ static int tf_link_tfvars(tf_mod_t *m, TSParser *parser, const char *rel) {
/* Every module directory, `paths[0..pn)` sorted by directory: parse all,
* 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. */
+ * and the .tfvars files `vpaths[0..vn)` to the module whose variables they set. */
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;
@@ -943,11 +982,7 @@ static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *c
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;
+ edges += tf_link_tfvars(&arena, parser, mods, ctx->repo_path, vpaths[k]);
}
for (int k = 0; k < nm; k++) {
tf_mod_free(list[k]);
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index e996f8e6f..0e6904044 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1517,7 +1517,8 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr
* 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; a .tfvars assignment OVERRIDEs
- * {via:tfvars} the variable block of its own directory; a Terragrunt
+ * {via:tfvars} the variable block of its own directory (or, inferred,
+ * of the nearest ancestor module declaring its names); a Terragrunt
* unit's include / read_terragrunt_config IMPORTS {via:terragrunt} the
* file it names, a dependency DEPENDS_ON the other unit
* (pass_terraform.c, extract_unified.c, extract_defs.c); an Ansible
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 7f226faeb..7879a0ac9 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2974,7 +2974,8 @@ TEST(probe_terraform_module_calls_import_their_source) {
PASS();
}
-/* Every tfvars OVERRIDE edge as "<file> <name> -> <file> <block> <line>". */
+/* Every tfvars OVERRIDE edge as "<file> <name> -> <file> <block> <line>",
+ * " inferred" appended when the edge says so. */
static char *gpf_tf_tfvars(const GpfFile *files, int nfiles) {
GpfProj lp;
cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
@@ -2984,9 +2985,11 @@ static char *gpf_tf_tfvars(const GpfFile *files, int nfiles) {
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)), '')";
+ "json_extract(e.properties,'$.line') || CASE WHEN "
+ "json_extract(e.properties,'$.inferred') THEN ' inferred' ELSE '' END 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) {
@@ -3001,19 +3004,26 @@ static char *gpf_tf_tfvars(const GpfFile *files, int nfiles) {
/* 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. */
+ * file's Variable of the name. A file whose directory declares none of its
+ * names -- no Terraform there, or a module that does not declare them --
+ * sets the nearest ancestor module's that does (applied by a -var-file the
+ * repository does not state: `inferred`). An undeclared name, and a file
+ * with no declaring module above it, 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"},
+ {"stack/sub/main.tf", "resource \"null_resource\" \"x\" {}\n"},
+ {"stack/sub/sub.tfvars", "size = 9\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";
+ static const char want[] =
+ "stack/prod/prod.tfvars region -> stack/variables.tf variable.region 1 inferred\n"
+ "stack/sub/sub.tfvars size -> stack/variables.tf variable.size 1 inferred\n"
+ "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);