Skip to content

Commit a9d7a96

Browse files
committed
feat(infra): a Terraform reference USAGEs the block of its module declaring it
Terraform references were resolved by the generic name matcher: by their last name segment, across the whole repository. `format()` CALLS a variable named format of another module, `local.x` meets a resource named local, `.id` an output id anywhere. Measured on the 7 Terraform repositories of 17 infrastructure corpora: 12,437 edges, recall 0.63 and precision 0.39 against an independent reading of the references, and not one of them left the block holding its reference. A reference -- var.X, local.X, module.X, data.T.N, T.N -- now USAGEs {ref, via: terraform, line}, from the top-level block holding it, the block declaring it in the same module: the .tf / .tofu files of its directory (a locals entry: its locals block); an entry of the block's depends_on list DEPENDS_ON the object instead. The generic matcher makes no edge for a .tf / .tofu file any more; a name only mentioned in string text is no reference, nor is a block's reference to itself. A changed file re-resolves its whole directory, so an unchanged file's reference meets the declaration a change adds and loses the one it removes. A bare block label (`variable region {}`, valid HCL) now names its block like a quoted one; such blocks used to share the keyword's name and QN. Measured: 7,771 of 7,771 file-level and 13,397 of 13,397 block-level reference pairs (edge type included) and nothing else; the 10 other corpora are unchanged; deterministic over three runs; the incremental re-index equals a full index in 7 scenarios (rename a variable, a module, a locals entry; delete the variables file; add a file; add a declaration an unchanged file references; quote a bare label); index time unchanged (within 1 % on the four largest Terraform corpora). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent e58c5ba commit a9d7a96

10 files changed

Lines changed: 541 additions & 27 deletions

File tree

‎README.md‎

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

‎internal/cbm/extract_defs.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7192,18 +7192,19 @@ static char *find_hcl_block_name(CBMArena *a, TSNode node, const char *source) {
71927192
// Append the block's quoted labels so each block gets a distinct,
71937193
// query-friendly name: resource "aws_instance" "web" -> resource.aws_instance.web
71947194
// rather than every resource collapsing to the bare keyword "resource" (#337).
7195-
// HCL stores labels as string_lit -> template_literal children.
7195+
// HCL stores quoted labels as string_lit -> template_literal children and
7196+
// bare ones (`variable region {}`, valid HCL) as identifiers after the
7197+
// keyword's own -- both name the block.
71967198
uint32_t cc = ts_node_named_child_count(node);
71977199
for (uint32_t i = 0; i < cc; i++) {
71987200
TSNode ch = ts_node_named_child(node, i);
7199-
if (strcmp(ts_node_type(ch), "string_lit") != 0) {
7200-
continue;
7201-
}
7202-
TSNode lit = cbm_find_child_by_kind(ch, "template_literal");
7203-
if (ts_node_is_null(lit)) {
7204-
continue;
7201+
char *label = NULL;
7202+
if (strcmp(ts_node_type(ch), "string_lit") == 0) {
7203+
TSNode lit = cbm_find_child_by_kind(ch, "template_literal");
7204+
label = ts_node_is_null(lit) ? NULL : cbm_node_text(a, lit, source);
7205+
} else if (strcmp(ts_node_type(ch), "identifier") == 0 && !ts_node_eq(ch, id)) {
7206+
label = cbm_node_text(a, ch, source);
72057207
}
7206-
char *label = cbm_node_text(a, lit, source);
72077208
if (!label || !label[0]) {
72087209
continue;
72097210
}

‎internal/cbm/extract_unified.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,12 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
26342634
#else
26352635
const uint32_t budget_nodes = ctx->walk_budget_nodes;
26362636
#endif
2637+
/* A Terraform / OpenTofu module file's references resolve inside its
2638+
* module directory (pass_terraform.c); a call or usage by name here would
2639+
* meet same-named blocks of any module and a builtin function's name. */
2640+
const char *ext = ctx->rel_path ? strrchr(ctx->rel_path, '.') : NULL;
2641+
bool tf_module = ctx->language == CBM_LANG_HCL && ext &&
2642+
(strcmp(ext, ".tf") == 0 || strcmp(ext, ".tofu") == 0);
26372643

26382644
for (;;) {
26392645
TSNode node = ts_tree_cursor_current_node(&cursor);
@@ -2659,8 +2665,11 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
26592665
handle_string_constants(ctx, node, &state);
26602666
handle_objectscript_type_map(ctx, node, &state);
26612667
handle_url_builders(ctx, node, &state);
2662-
CBMInvocationDescriptor invocation = handle_calls(ctx, node, spec, &state);
2663-
handle_usages(ctx, node, spec, &state);
2668+
CBMInvocationDescriptor invocation =
2669+
tf_module ? (CBMInvocationDescriptor){0} : handle_calls(ctx, node, spec, &state);
2670+
if (!tf_module) {
2671+
handle_usages(ctx, node, spec, &state);
2672+
}
26642673
handle_throws(ctx, node, spec, &state);
26652674
handle_readwrites(ctx, node, spec, &state);
26662675
handle_type_refs(ctx, node, spec, &state);

0 commit comments

Comments
 (0)