Skip to content

Commit 949e875

Browse files
committed
feat(infra): a .tfvars assignment OVERRIDEs the variable of its module
A .tfvars file's keys were Variable nodes connected to nothing: nothing said which variable `region = "eu"` sets. An assignment to a variable the module of the file's own directory declares now OVERRIDEs {via: tfvars, line} that `variable` block, from the file's Variable (child to base, as a values or patch overlay). A .tfvars file elsewhere is applied by a -var-file the repository does not state, and links nothing. A run touching a .tfvars file re-resolves like one touching a .tf file. Measured on 17 infrastructure repositories: 124 of 124 assignments of the 19 files that set variables of their own directory, nothing else; deterministic over three runs; the incremental re-index equals a full index in 4 scenarios (a value edited, the variables file edited, a .tfvars file deleted). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent d1bf070 commit 949e875

4 files changed

Lines changed: 143 additions & 22 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ The install script placed beside the binary is **reported, not deleted** — uni
257257
- `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`
258258
- `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
259259
- `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`
260+
- `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
260261
- `EMITS`, `LISTENS_ON` (channels)
261262
- `DATA_FLOWS` with arg-to-param mapping + field access chains
262263
- `SIMILAR_TO` (MinHash + LSH near-clone detection, Jaccard scored)

‎src/pipeline/pass_terraform.c‎

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,23 @@ static bool tf_is_module_file(const char *rel) {
394394
return dot && (strcmp(dot, ".tf") == 0 || strcmp(dot, ".tofu") == 0);
395395
}
396396

397-
/* ── References (PHASE 6 P6.1, P6.2) ───────────────────────────────── */
397+
/* A variable-definitions file (.tfvars; a .tfvars.json is JSON, not read here). */
398+
static bool tf_is_tfvars(const char *rel) {
399+
const char *dot = strrchr(tf_basename(rel), '.');
400+
return dot && strcmp(dot, ".tfvars") == 0;
401+
}
402+
403+
/* The body of a config file's root node. */
404+
static TSNode tf_root_body(TSNode root) {
405+
for (uint32_t i = 0; i < ts_node_named_child_count(root); i++) {
406+
if (tf_type(ts_node_named_child(root, i), "body")) {
407+
return ts_node_named_child(root, i);
408+
}
409+
}
410+
return (TSNode){0};
411+
}
412+
413+
/* ── References (PHASE 6 P6.1 - P6.3) ─────────────────────────────── */
398414

399415
/* A Terraform module is a directory: every .tf / .tofu file of it. A
400416
* reference resolves inside its module only -- var.X to `variable "X"`,
@@ -410,7 +426,12 @@ static bool tf_is_module_file(const char *rel) {
410426
* directory inside the repository, holding .tf / .tofu files) IMPORTS
411427
* {local_name: X, via: terraform, line} that directory's Folder, and
412428
* module.X.Y (also module.X[i].Y, module.X[*].Y) USAGEs {ref: output}, beside
413-
* module.X, the `output "Y"` of it. */
429+
* module.X, the `output "Y"` of it.
430+
*
431+
* A .tfvars file sets variables of the module in its own directory: each
432+
* assignment to a declared variable OVERRIDEs {via: tfvars, line} its
433+
* `variable` block (a file elsewhere is applied by a -var-file on some
434+
* command line, which is nothing the repository's Terraform states). */
414435

415436
typedef struct {
416437
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
656677
* calls and the references of each top-level block. */
657678
static void tf_mod_file(tf_mod_t *m, TSNode root) {
658679
CBMArena *a = m->arena;
659-
TSNode body = {0};
660-
for (uint32_t i = 0; i < ts_node_named_child_count(root) && ts_node_is_null(body); i++) {
661-
if (tf_type(ts_node_named_child(root, i), "body")) {
662-
body = ts_node_named_child(root, i);
663-
}
664-
}
680+
TSNode body = tf_root_body(root);
665681
for (uint32_t i = 0; !ts_node_is_null(body) && i < ts_node_named_child_count(body); i++) {
666682
TSNode b = ts_node_named_child(body, i);
667683
TSNode bb;
@@ -849,10 +865,48 @@ static CBMHashTable *tf_folder_index(cbm_pipeline_ctx_t *ctx) {
849865
return ix;
850866
}
851867

868+
/* A .tfvars file sets variables of the module in its own directory: each
869+
* top-level assignment whose name the module declares OVERRIDEs
870+
* {via: tfvars, line}, from the file's Variable of that name, the
871+
* `variable` block. */
872+
static int tf_link_tfvars(tf_mod_t *m, TSParser *parser, const char *rel) {
873+
char path[CBM_SZ_2K];
874+
snprintf(path, sizeof(path), "%s/%s", m->ctx->repo_path, rel);
875+
size_t len = 0;
876+
char *src = tf_read(path, &len);
877+
TSTree *tree = src ? ts_parser_parse_string(parser, NULL, src, (uint32_t)len) : NULL;
878+
TSNode body = tree ? tf_root_body(ts_tree_root_node(tree)) : (TSNode){0};
879+
int edges = 0;
880+
for (uint32_t i = 0; !ts_node_is_null(body) && i < ts_node_named_child_count(body); i++) {
881+
TSNode at = ts_node_named_child(body, i);
882+
if (!tf_type(at, "attribute") || ts_node_named_child_count(at) == 0) {
883+
continue;
884+
}
885+
const char *key = tf_text(m->arena, src, ts_node_named_child(at, 0));
886+
const char *vname = key ? cbm_arena_sprintf(m->arena, "variable.%s", key) : NULL;
887+
const char *vfile = vname ? cbm_ht_get(m->decls, vname) : NULL;
888+
int64_t from = vfile ? tf_def_node(m, rel, key) : 0;
889+
int64_t to = from > 0 ? tf_def_node(m, vfile, vname) : 0;
890+
if (to > 0) {
891+
char props[CBM_SZ_128];
892+
snprintf(props, sizeof(props), "{\"via\":\"tfvars\",\"line\":%d}",
893+
(int)ts_node_start_point(at).row + SKIP_ONE);
894+
cbm_gbuf_insert_edge(m->ctx->gbuf, from, to, "OVERRIDE", props);
895+
edges++;
896+
}
897+
}
898+
if (tree) {
899+
ts_tree_delete(tree);
900+
}
901+
cbm_free(CBM_MEM_CLASS_OTHER, src);
902+
return edges;
903+
}
904+
852905
/* Every module directory, `paths[0..pn)` sorted by directory: parse all,
853-
* then link each -- a reference may read an output of another directory. */
854-
static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *const *paths,
855-
int pn) {
906+
* then link each -- a reference may read an output of another directory --
907+
* and the .tfvars files `vpaths[0..vn)` to the module of their directory. */
908+
static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *const *paths, int pn,
909+
const char *const *vpaths, int vn) {
856910
CBMArena arena;
857911
cbm_arena_init_lazy(&arena, CBM_SZ_4K);
858912
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
877931
for (int k = 0; k < nm; k++) {
878932
edges += tf_mod_link(list[k], mods, folders);
879933
}
934+
for (int k = 0; mods && k < vn; k++) {
935+
const char *slash = strrchr(vpaths[k], '/');
936+
char dir[CBM_SZ_1K];
937+
snprintf(dir, sizeof(dir), "%.*s", slash ? (int)(slash - vpaths[k]) : 0, vpaths[k]);
938+
tf_mod_t *m = cbm_ht_get(mods, dir);
939+
edges += m ? tf_link_tfvars(m, parser, vpaths[k]) : 0;
940+
}
880941
for (int k = 0; k < nm; k++) {
881942
tf_mod_free(list[k]);
882943
}
@@ -891,35 +952,43 @@ static int tf_link_repo(cbm_pipeline_ctx_t *ctx, TSParser *parser, const char *c
891952
return edges;
892953
}
893954

894-
/* When one of the run's files is a .tf / .tofu file, every module directory
895-
* of the repository (the graph's File nodes) is resolved whole: on an
896-
* incremental run an unchanged file's reference may meet what a changed one
897-
* declares -- in its own directory (a variable, a locals entry) or in a
898-
* module it calls (an output). */
955+
/* When one of the run's files is a .tf / .tofu / .tfvars file, every module
956+
* directory of the repository (the graph's File nodes) is resolved whole: on
957+
* an incremental run an unchanged file's reference may meet what a changed
958+
* one declares -- in its own directory (a variable, a locals entry), in a
959+
* module it calls (an output), or a .tfvars file a variable it sets. */
899960
static int tf_link_refs(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count,
900961
TSParser *parser) {
901962
bool any = false;
902963
for (int i = 0; i < file_count && !any; i++) {
903-
any = files[i].language == CBM_LANG_HCL && tf_is_module_file(files[i].rel_path);
964+
any = (files[i].language == CBM_LANG_HCL && tf_is_module_file(files[i].rel_path)) ||
965+
tf_is_tfvars(files[i].rel_path);
904966
}
905967
const cbm_gbuf_node_t **fnodes = NULL;
906968
int fn = 0;
907969
if (!any || cbm_gbuf_find_by_label(ctx->gbuf, "File", &fnodes, &fn) != 0 || fn <= 0) {
908970
return 0;
909971
}
910972
const char **paths = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)fn * sizeof(char *));
973+
const char **vpaths = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)fn * sizeof(char *));
911974
int pn = 0;
912-
for (int i = 0; paths && i < fn; i++) {
913-
if (fnodes[i]->file_path && tf_is_module_file(fnodes[i]->file_path)) {
914-
paths[pn++] = fnodes[i]->file_path;
975+
int vn = 0;
976+
for (int i = 0; paths && vpaths && i < fn; i++) {
977+
const char *rel = fnodes[i]->file_path;
978+
if (rel && tf_is_module_file(rel)) {
979+
paths[pn++] = rel;
980+
} else if (rel && tf_is_tfvars(rel)) {
981+
vpaths[vn++] = rel;
915982
}
916983
}
917984
int edges = 0;
918985
if (pn > 0 && ts_parser_set_language(parser, cbm_ts_language(CBM_LANG_HCL))) {
919986
qsort((void *)paths, (size_t)pn, sizeof(*paths), tf_cmp_dir_path);
920-
edges = tf_link_repo(ctx, parser, paths, pn);
987+
qsort((void *)vpaths, (size_t)vn, sizeof(*vpaths), tf_cmp_dir_path);
988+
edges = tf_link_repo(ctx, parser, paths, pn, vpaths, vn);
921989
}
922990
cbm_free(CBM_MEM_CLASS_OTHER, (void *)paths);
991+
cbm_free(CBM_MEM_CLASS_OTHER, (void *)vpaths);
923992
return edges;
924993
}
925994

‎src/pipeline/pipeline_internal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,8 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr
15041504
* the generic name matcher's USAGE / CALLS; a bare block label names
15051505
* its block like a quoted one; a module call with a local source
15061506
* IMPORTS its directory's Folder (the root: the Project) and module.X.Y
1507-
* USAGEs {ref:output} the output there
1507+
* USAGEs {ref:output} the output there; a .tfvars assignment OVERRIDEs
1508+
* {via:tfvars} the variable block of its own directory
15081509
* (pass_terraform.c, extract_unified.c, extract_defs.c). An index built
15091510
* before holds none of them, so it rebuilds once on upgrade. */
15101511
enum { CBM_SEMANTIC_INDEX_VERSION = 9 };

‎tests/test_grammar_probe_f.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,6 +2974,55 @@ TEST(probe_terraform_module_calls_import_their_source) {
29742974
PASS();
29752975
}
29762976

2977+
/* Every tfvars OVERRIDE edge as "<file> <name> -> <file> <block> <line>". */
2978+
static char *gpf_tf_tfvars(const GpfFile *files, int nfiles) {
2979+
GpfProj lp;
2980+
cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
2981+
char *out = NULL;
2982+
if (store) {
2983+
sqlite3_stmt *st = NULL;
2984+
const char *sql =
2985+
"SELECT coalesce((SELECT group_concat(x, char(10)) FROM (SELECT s.file_path || ' ' || "
2986+
"s.name || ' -> ' || t.file_path || ' ' || t.name || ' ' || "
2987+
"json_extract(e.properties,'$.line') AS x FROM edges e JOIN nodes s ON "
2988+
"s.id=e.source_id JOIN nodes t ON t.id=e.target_id WHERE e.project=?1 AND "
2989+
"e.type='OVERRIDE' AND json_extract(e.properties,'$.via')='tfvars' ORDER BY x)), '')";
2990+
if (sqlite3_prepare_v2(cbm_store_get_db(store), sql, -1, &st, NULL) == SQLITE_OK) {
2991+
sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
2992+
if (sqlite3_step(st) == SQLITE_ROW) {
2993+
out = strdup((const char *)sqlite3_column_text(st, 0));
2994+
}
2995+
}
2996+
sqlite3_finalize(st);
2997+
}
2998+
gpf_cleanup(&lp, store);
2999+
return out;
3000+
}
3001+
3002+
/* A .tfvars file's assignment to a variable the module of its own directory
3003+
* declares OVERRIDEs {via: tfvars, line} that `variable` block, from the
3004+
* file's Variable of the name. An undeclared name, and a file in a directory
3005+
* without Terraform files (applied by a -var-file the repository does not
3006+
* state), make none. */
3007+
TEST(probe_terraform_tfvars_override_their_variables) {
3008+
static const GpfFile files[] = {
3009+
{"stack/variables.tf", "variable \"region\" {}\nvariable \"size\" {\n default = 1\n}\n"},
3010+
{"stack/terraform.tfvars", "region = \"eu\"\nsize = 3\nunknown = \"x\"\n"},
3011+
{"stack/prod/prod.tfvars", "region = \"us\"\n"},
3012+
{"other.tfvars", "region = \"ap\"\n"}};
3013+
int n = (int)(sizeof(files) / sizeof(files[0]));
3014+
char *got = gpf_tf_tfvars(files, n);
3015+
static const char want[] = "stack/terraform.tfvars region -> stack/variables.tf variable.region 1\n"
3016+
"stack/terraform.tfvars size -> stack/variables.tf variable.size 2";
3017+
if (!got || strcmp(got, want) != 0) {
3018+
fprintf(stderr, "terraform tfvars got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
3019+
free(got);
3020+
FAIL("terraform tfvars");
3021+
}
3022+
free(got);
3023+
PASS();
3024+
}
3025+
29773026
/* Every script HTTP_CALLS edge as "<source file> <route QN>", then a line
29783027
* "edgeless routes <n>": the Route nodes no edge touches. */
29793028
static char *gpf_script_http(const GpfFile *files, int nfiles) {
@@ -5414,6 +5463,7 @@ SUITE(grammar_probe_f) {
54145463
RUN_TEST(probe_terraform_refs_resolve_in_their_module);
54155464
RUN_TEST(probe_terraform_module_files_extract_no_generic_refs);
54165465
RUN_TEST(probe_terraform_module_calls_import_their_source);
5466+
RUN_TEST(probe_terraform_tfvars_override_their_variables);
54175467
RUN_TEST(probe_image_hub_splits_by_component);
54185468
RUN_TEST(probe_gitops_objects_deploy_their_units);
54195469
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);

0 commit comments

Comments
 (0)