Skip to content

Commit 246d1a0

Browse files
committed
fix(incremental): a Terraform input change declines to the full rebuild
A Terraform module's output read can live in one file (outputs.tf: module.net.vpc_id) and the module's source in another (main.tf). When only main.tf changed -- its source pointed elsewhere -- the incremental repair recomputed that file's edges alone, so the read, held by two unchanged files, stayed in the graph although a full reindex no longer has it. Measured on kube-hetzner: 18 stale output reads after moving one module's source. Terraform, OpenTofu and Terragrunt inputs reach past their own file the way Kubernetes bundles do: the files of a module directory resolve each other's references, a module call decides what another file reads, a .tfvars file sets the variables beside it. A change to a .tf, .tofu, .tfvars or .hcl file now declines the repair to the full rebuild, as a kustomization or manifest change already does. The incremental re-index now equals a full index in all 18 Terraform scenarios, the stale-read one included; a Terraform edit costs a full rebuild (fabric: 1.64 -> 1.90 s). A pipeline test moves a module's source and requires the read to go. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 949e875 commit 246d1a0

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎src/pipeline/pipeline_incremental.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,28 @@ static const char *closure_k8s_decline(cbm_store_t *store, const char *project,
25162516
return NULL;
25172517
}
25182518

2519+
/* Terraform / OpenTofu / Terragrunt inputs reach past their own file
2520+
* (pass_terraform.c): the files of a module directory resolve each other's
2521+
* references, a module call's source decides which outputs another file of
2522+
* the directory reads, a .tfvars file sets the variables beside it, and a
2523+
* Terragrunt unit includes and reads other .hcl files. The repair recomputes
2524+
* the changed files' edges only, so a delta touching any of them -- an edge
2525+
* held by two unchanged files may hang on it -- declines to the full
2526+
* rebuild. Returns the reason, or NULL. */
2527+
static const char *closure_terraform_decline(char **paths, int n) {
2528+
static const char *const exts[] = {".tf", ".tofu", ".tfvars", ".hcl"};
2529+
for (int i = 0; i < n; i++) {
2530+
const char *slash = strrchr(paths[i], '/');
2531+
const char *dot = strrchr(slash ? slash + SKIP_ONE : paths[i], '.');
2532+
for (size_t k = 0; dot && k < sizeof(exts) / sizeof(exts[0]); k++) {
2533+
if (strcmp(dot, exts[k]) == 0) {
2534+
return "terraform_input_changed";
2535+
}
2536+
}
2537+
}
2538+
return NULL;
2539+
}
2540+
25192541
/* The env join (pass_envjoin.c) reads each image's files from its
25202542
* Dockerfile's Module, stored with the ignore patterns of its context: an
25212543
* ignore file changing, or a deletion of a file an image copies (which
@@ -2737,6 +2759,10 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p
27372759
if (decline) {
27382760
goto done;
27392761
}
2762+
decline = closure_terraform_decline(changed_paths, n_changed + n_deleted);
2763+
if (decline) {
2764+
goto done;
2765+
}
27402766
decline =
27412767
closure_stem_mark_decline(project, files, file_count, changed_paths + n_changed, n_deleted);
27422768
if (decline) {

‎tests/test_pipeline.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,55 @@ TEST(pipeline_incremental_preserves_cross_file_calls) {
14901490
PASS();
14911491
}
14921492

1493+
static void write_temp_file(const char *dir, const char *name, const char *content);
1494+
1495+
/* Regression: a Terraform module's output read lives in one file
1496+
* (infra/outputs.tf: module.net.vpc_id) and the module's source in another
1497+
* (infra/main.tf). Pointing the source elsewhere edits main.tf alone; the
1498+
* repair route recomputed the changed file's edges only, so the read -- held
1499+
* by two UNCHANGED files, outputs.tf and modules/net/main.tf -- stayed in the
1500+
* graph, which a full reindex no longer has. A Terraform input change now
1501+
* declines to the full rebuild (closure_terraform_decline). */
1502+
TEST(pipeline_incremental_drops_terraform_reads_of_a_moved_module) {
1503+
if (setup_test_repo() != 0) {
1504+
FAIL("failed to create temp dir");
1505+
}
1506+
write_temp_file(g_tmpdir, "infra/main.tf", "module \"net\" {\n source = \"./modules/net\"\n}\n");
1507+
write_temp_file(g_tmpdir, "infra/outputs.tf",
1508+
"output \"net_vpc\" {\n value = module.net.vpc_id\n}\n");
1509+
write_temp_file(g_tmpdir, "infra/modules/net/main.tf",
1510+
"output \"vpc_id\" {\n value = \"x\"\n}\n");
1511+
char db_path[512];
1512+
snprintf(db_path, sizeof(db_path), "%s/test_incr_tf.db", g_tmpdir);
1513+
1514+
cbm_pipeline_t *p1 = cbm_pipeline_new(g_tmpdir, db_path, CBM_MODE_FULL);
1515+
ASSERT_NOT_NULL(p1);
1516+
ASSERT_EQ(cbm_pipeline_run(p1), 0);
1517+
cbm_store_t *s1 = cbm_store_open_path(db_path);
1518+
ASSERT_NOT_NULL(s1);
1519+
ASSERT_TRUE(cross_file_edge_exists(s1, cbm_pipeline_project_name(p1), "output.net_vpc",
1520+
"output.vpc_id", "USAGE"));
1521+
cbm_store_close(s1);
1522+
cbm_pipeline_free(p1);
1523+
1524+
/* the module's source moves to a directory that does not exist */
1525+
write_temp_file(g_tmpdir, "infra/main.tf",
1526+
"module \"net\" {\n source = \"./modules/none\"\n}\n");
1527+
1528+
cbm_pipeline_t *p2 = cbm_pipeline_new(g_tmpdir, db_path, CBM_MODE_FULL);
1529+
ASSERT_NOT_NULL(p2);
1530+
ASSERT_EQ(cbm_pipeline_run(p2), 0);
1531+
cbm_store_t *s2 = cbm_store_open_path(db_path);
1532+
ASSERT_NOT_NULL(s2);
1533+
ASSERT_FALSE(cross_file_edge_exists(s2, cbm_pipeline_project_name(p2), "output.net_vpc",
1534+
"output.vpc_id", "USAGE"));
1535+
cbm_store_close(s2);
1536+
cbm_pipeline_free(p2);
1537+
1538+
teardown_test_repo();
1539+
PASS();
1540+
}
1541+
14931542
/* TS/JS receiver-aware weak-strategy suppression (#592/#606 direction; Perl
14941543
* precedent #477). A member call x.foo() whose receiver TYPE the TS-LSP cannot
14951544
* resolve (a regex literal `re.test()`) must NOT be bound to a same-named
@@ -23143,6 +23192,7 @@ SUITE(pipeline) {
2314323192
RUN_TEST(pipeline_nix_scoped_binding_calls_resolve);
2314423193
RUN_TEST(pipeline_hcl_block_reference_resolves_to_its_block);
2314523194
RUN_TEST(pipeline_incremental_preserves_cross_file_calls);
23195+
RUN_TEST(pipeline_incremental_drops_terraform_reads_of_a_moved_module);
2314623196
RUN_TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes);
2314723197
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
2314823198
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);

0 commit comments

Comments
 (0)