@intentius/chant-lexicon-terraform 0.60.0 → 0.62.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/composites/terraform-adopt-op.d.ts +18 -2
  2. package/dist/composites/terraform-adopt-op.d.ts.map +1 -1
  3. package/dist/describe-resources.d.ts +59 -0
  4. package/dist/describe-resources.d.ts.map +1 -1
  5. package/dist/hcl/edges.d.ts +124 -0
  6. package/dist/hcl/edges.d.ts.map +1 -0
  7. package/dist/hcl/parse.d.ts +42 -0
  8. package/dist/hcl/parse.d.ts.map +1 -1
  9. package/dist/hcl/roots.d.ts +6 -0
  10. package/dist/hcl/roots.d.ts.map +1 -1
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/integrity.json +2 -2
  14. package/dist/manifest.json +1 -1
  15. package/dist/op/activities/terraform.d.ts +44 -8
  16. package/dist/op/activities/terraform.d.ts.map +1 -1
  17. package/dist/op/adoption.d.ts +110 -35
  18. package/dist/op/adoption.d.ts.map +1 -1
  19. package/package.json +2 -2
  20. package/src/__fixtures__/ACCEPTANCE.md +135 -60
  21. package/src/__fixtures__/graph-roots/README.md +21 -0
  22. package/src/__fixtures__/graph-roots/app/main.tf +41 -0
  23. package/src/__fixtures__/graph-roots/app/modules/cdn/main.tf +7 -0
  24. package/src/__fixtures__/graph-roots/network/main.tf +16 -0
  25. package/src/__fixtures__/live-estate/README.md +65 -19
  26. package/src/__fixtures__/live-estate/adoptable.tf +13 -0
  27. package/src/__fixtures__/live-ls.json +5 -5
  28. package/src/__fixtures__/live-plan.json +67 -35
  29. package/src/composites/terraform-adopt-op.acceptance.test.ts +101 -50
  30. package/src/composites/terraform-adopt-op.test.ts +33 -11
  31. package/src/composites/terraform-adopt-op.ts +20 -2
  32. package/src/composites/terraform-apply-op.acceptance.test.ts +4 -2
  33. package/src/describe-resources.live.test.ts +76 -4
  34. package/src/describe-resources.test.ts +35 -0
  35. package/src/describe-resources.ts +110 -3
  36. package/src/hcl/edges.test.ts +207 -0
  37. package/src/hcl/edges.ts +316 -0
  38. package/src/hcl/parse.ts +53 -0
  39. package/src/hcl/roots.ts +14 -0
  40. package/src/index.ts +1 -0
  41. package/src/op/activities/choudoufu.test.ts +3 -3
  42. package/src/op/activities/terraform.ts +70 -21
  43. package/src/op/adoption.test.ts +129 -11
  44. package/src/op/adoption.ts +181 -49
@@ -0,0 +1,316 @@
1
+ /**
2
+ * Resolve each block's references into graph edges (chant #2265).
3
+ *
4
+ * `chant graph --format ir` used to emit a complete, module-descended,
5
+ * fully-attributed node set for a Terraform estate and zero edges. Core's
6
+ * `collectEdges` walks a node's config bag for `AttrRef` objects and `Ref`
7
+ * intrinsics; a block body out of hcl2json holds `"${aws_vpc.main.id}"`, a
8
+ * string, so the walk found nothing and said so. This module supplies what
9
+ * that walk cannot see: the references, already resolved to the entity keys
10
+ * `blocksToEntities` minted, published on each entity as core's lexicon-neutral
11
+ * `EntityReference` (see `@intentius/chant/graph-ir`).
12
+ *
13
+ * ## Where the reference forms come from
14
+ *
15
+ * The `${...}` body is tokenized by hcl2json's own expression AST
16
+ * (`getReferencesInExpression`), never by a regex over the string. That is the
17
+ * same instrument `packages/core/src/terraform/parse.ts` uses for the carve-out
18
+ * advisor, and for the same reason: a quoted address used as a map key
19
+ * (`var.m["aws_s3_bucket.assets.arn"]`) and an escaped `$${...}` literal are
20
+ * not references, and no regex over the raw string can tell. `./references.ts`
21
+ * IS such a regex scan and stays one: it answers "is this declaration used
22
+ * anywhere", deliberately generously, for a report-only rule. An edge a
23
+ * renderer draws is a stricter claim and gets the stricter instrument.
24
+ *
25
+ * Each accessor the AST returns is classified by core's own `refFromAccessor`
26
+ * for the three forms it already knows (`<type>.<name>`, `module.<name>`,
27
+ * `data.<type>.<name>`), and here for the two it deliberately excludes as
28
+ * non-resources, `var.<name>` and `local.<name>`.
29
+ *
30
+ * ## Four decisions a renderer will draw
31
+ *
32
+ * **Which forms become edges.** All five: `resource`, `data`, `module`, `var`
33
+ * and `local`. The first three are uncontroversial. The last two are edges
34
+ * because this lexicon emits a NODE for every `variable` and every `locals`
35
+ * block, and on a real estate those are the majority of them (108 variables
36
+ * and 10 locals out of 247 nodes, on the estate the issue was filed against).
37
+ * Dropping their edges would leave nearly half the graph as unconnected cards,
38
+ * which is the same picture the zero-edge bug drew. "Which resources consume
39
+ * `var.region`" is also a question a reader of a root module actually asks,
40
+ * and `terraform graph` itself answers it.
41
+ *
42
+ * A `local.<name>` resolves to the `locals` BLOCK that declares that name,
43
+ * found by looking `name` up in each block's body, so two `locals` blocks in
44
+ * one scope resolve independently rather than both matching.
45
+ *
46
+ * `provider = aws.west` is NOT an edge. The reference names a `provider`
47
+ * block by type AND alias, and this lexicon's entity key for one is
48
+ * `provider.<type>` with the alias inside the body, so two aliased providers
49
+ * of one type key as `provider.aws` and `provider.aws~2` and the reference
50
+ * cannot be resolved to either without guessing. `./references.ts` still
51
+ * records it for TF020, where "is it used at all" is answerable without
52
+ * knowing which block.
53
+ *
54
+ * **`depends_on` is an edge.** It arrives from hcl2json as
55
+ * `["${aws_vpc.main}"]` under the key `depends_on`, so it resolves through the
56
+ * same path as any other reference and lands as `viaAttr: "depends_on"` with
57
+ * no `toAttr`, which is exactly what it is: an ordering edge with no attribute
58
+ * flowing along it.
59
+ *
60
+ * **`count` / `for_each` are block-to-block.** chant's entity is the block, so
61
+ * `count = 3` is one node and one edge, not three. The expansion itself is
62
+ * recorded on the entity (`props.expansion`, `./parse.ts`) rather than left to
63
+ * be inferred from a node count that never grows. A `count = length(var.x)`
64
+ * also yields a real edge to `var.x` through `viaAttr: "count"`, since the
65
+ * meta-argument is an expression like any other.
66
+ *
67
+ * **Attribute naming.** `viaAttr` is the consumer-side TOP-LEVEL attribute the
68
+ * reference sits under and `toAttr` the producer-side attribute it read, which
69
+ * is the same pair carve's `TfEdge` carries as `via` and `attrs` and the pair
70
+ * behold already renders. One edge per (consumer, producer, `viaAttr`), and
71
+ * `toAttr` is set only when that edge read exactly one producer attribute:
72
+ * `subnet_id = aws_subnet.a.id` gives `id`, while
73
+ * `tags = merge(aws_vpc.main.tags, { id = aws_vpc.main.id })` names two and
74
+ * gets none rather than an arbitrary one.
75
+ *
76
+ * ## Scope, and what is deliberately not resolved
77
+ *
78
+ * Resolution is bounded to the referring block's own module scope
79
+ * (`scopeOfKey`), which is Terraform's own namespace: a `var.region` inside
80
+ * `module.cdn` is the child module's variable and never the root's. That also
81
+ * means no edge ever crosses roots, which is correct and intended (#2265):
82
+ * separate roots read each other by NAME through a data source, never by
83
+ * reference, and a value-match pass over those is a consumer's job, not this
84
+ * one's.
85
+ */
86
+
87
+ import { isResourceDeclarable, type Declarable } from "@intentius/chant/declarable";
88
+ import type { EntityReference } from "@intentius/chant/graph-ir";
89
+ import { loadHcl2json, type Hcl2Json } from "@intentius/chant/terraform/parse";
90
+ import { refFromAccessor } from "@intentius/chant/terraform/graph";
91
+ import { LOCALS_TYPE, scopeOfKey, type BlockBody, type TerraformEntity } from "./parse";
92
+
93
+ /** An identifier as Terraform's grammar allows it. Same as `./references.ts`. */
94
+ const NAME = "[A-Za-z_][A-Za-z0-9_-]*";
95
+
96
+ /** `var.<name>` / `local.<name>` at the head of a traversal accessor. */
97
+ const SCALAR_HEAD_RE = new RegExp(`^(var|local)\\.(${NAME})(?:\\.|$)`);
98
+
99
+ /** One reference read out of a body, before it is resolved to an entity key. */
100
+ interface RawReference {
101
+ /** The address in this lexicon's own vocabulary: `aws_vpc.main`, `var.region`, `local.tags`. */
102
+ address: string;
103
+ /** Producer-side attribute, when the accessor named one. */
104
+ attr?: string;
105
+ /** Consumer-side top-level attribute the reference sits under. */
106
+ via: string;
107
+ }
108
+
109
+ /**
110
+ * Classify one AST traversal accessor. `var`/`local` are handled here because
111
+ * core's `refFromAccessor` excludes them by design (they are not carvable
112
+ * resources); everything else defers to it, quoted map keys and numeric
113
+ * indexes included.
114
+ */
115
+ export function referenceFromAccessor(accessor: string): { address: string; attr?: string } | undefined {
116
+ const scalar = SCALAR_HEAD_RE.exec(accessor);
117
+ if (scalar) return { address: `${scalar[1]}.${scalar[2]}` };
118
+ return refFromAccessor(accessor) ?? undefined;
119
+ }
120
+
121
+ /** Every `${...}` string in a body, paired with the top-level attribute it sits under. */
122
+ function expressionsInBody(body: BlockBody): Array<{ via: string; expression: string }> {
123
+ const out: Array<{ via: string; expression: string }> = [];
124
+ const visit = (value: unknown, via: string, depth: number): void => {
125
+ if (depth > 8) return;
126
+ if (typeof value === "string") {
127
+ if (value.includes("${")) out.push({ via, expression: value });
128
+ return;
129
+ }
130
+ if (Array.isArray(value)) {
131
+ for (const item of value) visit(item, via, depth + 1);
132
+ return;
133
+ }
134
+ if (typeof value === "object" && value !== null) {
135
+ for (const [key, inner] of Object.entries(value as Record<string, unknown>)) {
136
+ visit(inner, depth === 0 ? key : via, depth + 1);
137
+ }
138
+ }
139
+ };
140
+ visit(body, "", 0);
141
+ return out;
142
+ }
143
+
144
+ /** The `${...}` strings of every entity in the map, deduplicated. */
145
+ function allExpressions(entities: ReadonlyMap<string, Declarable>): string[] {
146
+ const seen = new Set<string>();
147
+ for (const entity of entities.values()) {
148
+ if (!isResourceDeclarable(entity)) continue;
149
+ const body = bodyOf(entity as TerraformEntity);
150
+ for (const { expression } of expressionsInBody(body)) seen.add(expression);
151
+ }
152
+ return [...seen];
153
+ }
154
+
155
+ function bodyOf(entity: TerraformEntity): BlockBody {
156
+ const props = entity.props as Partial<TerraformEntity["props"]>;
157
+ return (typeof props.body === "object" && props.body !== null ? props.body : {}) as BlockBody;
158
+ }
159
+
160
+ /**
161
+ * Resolve every expression once through the AST. An expression the parser
162
+ * refuses in isolation resolves to no references: fewer edges, never a phantom
163
+ * one, which is the same trade `resolveExpressionRefs` makes in core.
164
+ */
165
+ async function resolveAccessors(
166
+ parser: Hcl2Json,
167
+ expressions: readonly string[],
168
+ ): Promise<Map<string, string[]>> {
169
+ const out = new Map<string, string[]>();
170
+ for (const expression of expressions) {
171
+ try {
172
+ const found = await parser.getReferencesInExpression("expression.tf", expression);
173
+ out.set(
174
+ expression,
175
+ found.map((r) => r.value),
176
+ );
177
+ } catch {
178
+ out.set(expression, []);
179
+ }
180
+ }
181
+ return out;
182
+ }
183
+
184
+ /** Address to entity key, and local name to the `locals` block declaring it, per module scope. */
185
+ export interface ScopeIndex {
186
+ byAddress: Map<string, string>;
187
+ byLocal: Map<string, string>;
188
+ }
189
+
190
+ function buildScopeIndex(entities: ReadonlyMap<string, Declarable>): Map<string, ScopeIndex> {
191
+ const scopes = new Map<string, ScopeIndex>();
192
+ for (const [key, entity] of entities) {
193
+ if (!isResourceDeclarable(entity)) continue;
194
+ const te = entity as TerraformEntity;
195
+ const address = (te.props as Partial<TerraformEntity["props"]>).address;
196
+ if (typeof address !== "string") continue;
197
+ const scope = scopeOfKey(key);
198
+ let index = scopes.get(scope);
199
+ if (!index) {
200
+ index = { byAddress: new Map(), byLocal: new Map() };
201
+ scopes.set(scope, index);
202
+ }
203
+ // First key wins: two blocks that genuinely share an address are keyed
204
+ // `~2`, `~3` (`./parse.ts`), and a reference cannot say which it meant.
205
+ if (!index.byAddress.has(address)) index.byAddress.set(address, key);
206
+ if (te.entityType === LOCALS_TYPE) {
207
+ for (const name of Object.keys(bodyOf(te))) {
208
+ if (!index.byLocal.has(name)) index.byLocal.set(name, key);
209
+ }
210
+ }
211
+ }
212
+ return scopes;
213
+ }
214
+
215
+ /** The entity key an address names within one scope, or undefined. */
216
+ function resolveAddress(address: string, index: ScopeIndex): string | undefined {
217
+ if (address.startsWith("local.")) return index.byLocal.get(address.slice("local.".length));
218
+ return index.byAddress.get(address);
219
+ }
220
+
221
+ /** Every reference in one body, as raw addresses paired with the attribute they sit under. */
222
+ function rawReferences(body: BlockBody, accessors: ReadonlyMap<string, readonly string[]>): RawReference[] {
223
+ const out: RawReference[] = [];
224
+ for (const { via, expression } of expressionsInBody(body)) {
225
+ for (const accessor of accessors.get(expression) ?? []) {
226
+ const ref = referenceFromAccessor(accessor);
227
+ if (ref) out.push({ address: ref.address, ...(ref.attr ? { attr: ref.attr } : {}), via });
228
+ }
229
+ }
230
+ return out;
231
+ }
232
+
233
+ /**
234
+ * The references one block declares, resolved to entity keys and collapsed to
235
+ * one entry per (producer, `viaAttr`). Sorted, so the IR a build emits is
236
+ * byte-stable across runs.
237
+ */
238
+ export function referencesOfEntity(
239
+ key: string,
240
+ entity: TerraformEntity,
241
+ scopes: ReadonlyMap<string, ScopeIndex>,
242
+ accessors: ReadonlyMap<string, readonly string[]>,
243
+ ): EntityReference[] {
244
+ const index = scopes.get(scopeOfKey(key));
245
+ if (!index) return [];
246
+
247
+ const grouped = new Map<string, { to: string; viaAttr: string; attrs: Set<string> }>();
248
+ for (const raw of rawReferences(bodyOf(entity), accessors)) {
249
+ const to = resolveAddress(raw.address, index);
250
+ if (!to || to === key) continue;
251
+ const groupKey = `${to}${raw.via}`;
252
+ let group = grouped.get(groupKey);
253
+ if (!group) {
254
+ group = { to, viaAttr: raw.via, attrs: new Set() };
255
+ grouped.set(groupKey, group);
256
+ }
257
+ if (raw.attr) group.attrs.add(raw.attr);
258
+ }
259
+
260
+ const out: EntityReference[] = [];
261
+ for (const { to, viaAttr, attrs } of grouped.values()) {
262
+ out.push({
263
+ to,
264
+ ...(viaAttr ? { viaAttr } : {}),
265
+ // Exactly one, or none: an edge carrying an arbitrary pick out of two
266
+ // would be a label a reader cannot trust.
267
+ ...(attrs.size === 1 ? { toAttr: [...attrs][0] } : {}),
268
+ });
269
+ }
270
+ return out.sort((a, b) => cmp(a.to, b.to) || cmp(a.viaAttr ?? "", b.viaAttr ?? ""));
271
+ }
272
+
273
+ /** Code-point ordering, so output does not depend on the machine's locale. */
274
+ function cmp(a: string, b: string): number {
275
+ return a < b ? -1 : a > b ? 1 : 0;
276
+ }
277
+
278
+ /**
279
+ * Stamp `references` onto every terraform entity in `entities`, in place.
280
+ *
281
+ * Takes the whole build's entity map rather than one root's, because one
282
+ * shared expression cache across every root is what keeps this to one wasm
283
+ * call per DISTINCT expression instead of one per occurrence. Resolution is
284
+ * still per module scope, so pooling the parse pools no references.
285
+ *
286
+ * Best effort, like everything else on this path: a parser that cannot be
287
+ * loaded, or one that does not expose the expression AST, leaves every entity
288
+ * unreferenced rather than failing a build over a diagram.
289
+ */
290
+ export async function resolveEntityReferences(
291
+ entities: Map<string, Declarable>,
292
+ hcl2json?: Hcl2Json,
293
+ ): Promise<void> {
294
+ const expressions = allExpressions(entities);
295
+ if (expressions.length === 0) return;
296
+
297
+ let parser: Hcl2Json;
298
+ try {
299
+ parser = hcl2json ?? (await loadHcl2json());
300
+ } catch {
301
+ return;
302
+ }
303
+ if (typeof parser.getReferencesInExpression !== "function") return;
304
+
305
+ const accessors = await resolveAccessors(parser, expressions);
306
+ const scopes = buildScopeIndex(entities);
307
+
308
+ for (const [key, entity] of entities) {
309
+ if (!isResourceDeclarable(entity)) continue;
310
+ const te = entity as TerraformEntity;
311
+ const references = referencesOfEntity(key, te, scopes, accessors);
312
+ if (references.length === 0) continue;
313
+ const stamped: TerraformEntity = { ...te, references };
314
+ entities.set(key, stamped);
315
+ }
316
+ }
package/src/hcl/parse.ts CHANGED
@@ -22,6 +22,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs";
22
22
  import { join } from "node:path";
23
23
  import { loadHcl2json, type Hcl2Json } from "@intentius/chant/terraform/parse";
24
24
  import { DECLARABLE_MARKER, type Declarable } from "@intentius/chant/declarable";
25
+ import type { EntityReference } from "@intentius/chant/graph-ir";
25
26
  import type { SuppressionDirective } from "@intentius/chant/lint/suppressions";
26
27
  import type { TerraformDeleteMode } from "../config";
27
28
  import { scanSuppressions, directivesFor, type FileScan } from "./suppressions";
@@ -29,6 +30,18 @@ import { scanSuppressions, directivesFor, type FileScan } from "./suppressions";
29
30
  /** A parsed HCL block body, as `@cdktf/hcl2json` encodes it. */
30
31
  export type BlockBody = Record<string, unknown>;
31
32
 
33
+ /**
34
+ * The meta-argument that expands a block into instances, when it carries one
35
+ * (chant #2265). Recorded because chant's entity is the BLOCK, one node
36
+ * whatever `count` evaluates to, so an edge out of an expanded block is
37
+ * block-to-block and says nothing about how many instances reference how many
38
+ * others. That is the honest shape for a read of the declaration alone, since
39
+ * the instance count is a plan-time answer and often a state-time one, but it
40
+ * is a shape a reader should be told rather than left to infer from a node
41
+ * count that never grows.
42
+ */
43
+ export type TerraformExpansion = "count" | "for_each";
44
+
32
45
  /** Whether a root runs under choudoufu with a declared estate (#2103). */
33
46
  export type TerraformRootMode = "live" | "state";
34
47
 
@@ -99,7 +112,37 @@ export interface TerraformEntity extends Declarable {
99
112
  * quotes still exist. Empty when a caller built the entity by hand.
100
113
  */
101
114
  readonly source: string;
115
+ /**
116
+ * `count` or `for_each` when the block carries one (chant #2265). See
117
+ * {@link TerraformExpansion} for why one node still stands for the whole
118
+ * expansion.
119
+ */
120
+ readonly expansion?: TerraformExpansion;
102
121
  };
122
+ /**
123
+ * The deployable unit this entity belongs to: the root name, which is what
124
+ * one `terraform apply` runs against (chant #2266).
125
+ *
126
+ * Duplicates `props.root`, deliberately. `props` is what this lexicon's own
127
+ * checks and serializer read; `stack` is the lexicon-neutral field core's
128
+ * `buildGraphIr` reads to key `groups.byStack`, so a five-root project draws
129
+ * as five boundary boxes instead of one bucket named `terraform`. Core has
130
+ * no business reaching into a lexicon's `props` to find out, and this
131
+ * lexicon has no business knowing how the grouping is built, so the fact is
132
+ * said once in each vocabulary. A child module's blocks carry the calling
133
+ * ROOT's name, not the module's: the module is not separately applied.
134
+ */
135
+ readonly stack: string;
136
+ /**
137
+ * Every reference in this block, resolved to the entity keys it points at
138
+ * (chant #2265), in core's lexicon-neutral {@link EntityReference} shape.
139
+ *
140
+ * Absent until `./edges.ts` resolves it, which needs the whole root's entity
141
+ * set and so cannot happen inside the per-file parse below. An entity that
142
+ * never went through that pass carries none, which reads as "no references
143
+ * were resolved", never as "this block references nothing".
144
+ */
145
+ readonly references?: readonly EntityReference[];
103
146
  /**
104
147
  * `# chant-ignore`/`chant-ignore-file`/`chant-ignore-block` directives that
105
148
  * apply to this entity (chant #2111): the ones anchored to its own block,
@@ -126,6 +169,13 @@ export const LIVE_TYPE = "Terraform::Live";
126
169
  /** The sidecar filename choudoufu reads an estate declaration from when no in-block `live { }` is used. */
127
170
  export const LIVE_SIDECAR_FILENAME = "estate.chdf.hcl";
128
171
 
172
+ /** The expansion meta-argument a block body carries, if any. `count` wins if both are present (Terraform rejects that combination anyway). */
173
+ function expansionOf(body: BlockBody): TerraformExpansion | undefined {
174
+ if ("count" in body) return "count";
175
+ if ("for_each" in body) return "for_each";
176
+ return undefined;
177
+ }
178
+
129
179
  /**
130
180
  * Build one entity. Written out rather than run through `createResource`
131
181
  * from `@intentius/chant/runtime`: that factory is for generated resource
@@ -145,11 +195,13 @@ export function terraformEntity(
145
195
  line?: number,
146
196
  suppressions?: readonly SuppressionDirective[],
147
197
  ): TerraformEntity {
198
+ const expansion = expansionOf(body);
148
199
  return {
149
200
  [DECLARABLE_MARKER]: true,
150
201
  lexicon: "terraform",
151
202
  entityType,
152
203
  kind: "resource",
204
+ stack: root,
153
205
  props: {
154
206
  address,
155
207
  body,
@@ -157,6 +209,7 @@ export function terraformEntity(
157
209
  root,
158
210
  source,
159
211
  line,
212
+ ...(expansion ? { expansion } : {}),
160
213
  ...(extra?.mode !== undefined ? { mode: extra.mode } : {}),
161
214
  ...(extra?.estate !== undefined ? { estate: extra.estate } : {}),
162
215
  ...(extra?.workspace !== undefined ? { workspace: extra.workspace } : {}),
package/src/hcl/roots.ts CHANGED
@@ -18,6 +18,12 @@
18
18
  * local source is followed into its directory and parsed as a child scope of
19
19
  * the root (#2112). `./descend.ts` owns that walk and every refusal in it; the
20
20
  * refusals surface here as this render's warnings.
21
+ *
22
+ * One pass runs after every root has parsed: `./edges.ts` resolves each
23
+ * block's `${...}` references into the entity keys they name, so the graph IR
24
+ * has edges to draw (#2265). It runs here rather than inside the per-file
25
+ * parse because an edge's target may be a block in a file, or a child module,
26
+ * that has not been read yet.
21
27
  */
22
28
 
23
29
  import { existsSync } from "node:fs";
@@ -27,6 +33,7 @@ import type { Hcl2Json } from "@intentius/chant/terraform/parse";
27
33
  import type { TerraformRootConfig } from "../config";
28
34
  import { LIVE_TYPE, parseTerraformRootDir } from "./parse";
29
35
  import { descendModules, resolveCallModuleType, type CallModuleType } from "./descend";
36
+ import { resolveEntityReferences } from "./edges";
30
37
 
31
38
  export interface TerraformRootsResult {
32
39
  entities: Map<string, Declarable>;
@@ -117,5 +124,12 @@ export async function renderTerraformRoots(
117
124
  }
118
125
  }
119
126
 
127
+ // Every root and every descended child module is parsed by now, which is
128
+ // what reference resolution needs: an edge points at an entity key, and the
129
+ // keys only all exist once the last descent has run (#2265). One pass over
130
+ // the whole map, one expression cache, resolution still bounded to each
131
+ // block's own module scope.
132
+ await resolveEntityReferences(entities, opts.hcl2json);
133
+
120
134
  return { entities, warnings };
121
135
  }
package/src/index.ts CHANGED
@@ -66,6 +66,7 @@ export {
66
66
  renderAdoptionLedger,
67
67
  type AdoptionCandidate,
68
68
  type AdoptionLedger,
69
+ type AdoptionMatch,
69
70
  } from "./op/adoption";
70
71
 
71
72
  // The Init/Plan/Gate/Apply composite (#2086).
@@ -277,10 +277,10 @@ describe("isOlderVersion / parseChoudoufuVersion (#2103)", () => {
277
277
  // ── The version check, wired into every activity via resolveRoot ───────────
278
278
 
279
279
  describe("choudoufu version check (#2103)", () => {
280
- test("refuses a binary older than the floor, which the -json document moved to v0.14.0", async () => {
280
+ test("refuses a binary older than the floor, which the adoptable section moved to v0.15.0", async () => {
281
281
  const dir = liveProject();
282
- replies.push({ match: "version", reply: { stdout: "choudoufu v0.13.0 (based on OpenTofu v1.13.0)\n", stderr: "" } });
283
- await expect(choudoufuLiveCheck({ root: "estate", cwd: dir })).rejects.toThrow(/older than.*v0\.14\.0/);
282
+ replies.push({ match: "version", reply: { stdout: "choudoufu v0.14.0 (based on OpenTofu v1.13.0)\n", stderr: "" } });
283
+ await expect(choudoufuLiveCheck({ root: "estate", cwd: dir })).rejects.toThrow(/older than.*v0\.15\.0/);
284
284
  });
285
285
 
286
286
  test("passes at exactly the minimum version", async () => {
@@ -81,11 +81,20 @@ export const DEFAULT_LIVE_PLAN_DOCUMENT_FILE = "chant.live-plan.json";
81
81
  * estate ([choudoufu #894](https://github.com/INTENTIUS/choudoufu/issues/894),
82
82
  * PR 915), which is the shape every chant live root has: before it,
83
83
  * {@link choudoufuLivePlan} could not produce the document for a real root at
84
- * all. {@link terraformApply} depends on the exit-3 refusal and
85
- * `describeResources()` and both document-reading Ops depend on the document,
86
- * so the floor is the release that shipped the later of the two.
84
+ * all. v0.15.0 brought the document's `adoptable` section and the `swept`
85
+ * list beside it
86
+ * ([choudoufu #962](https://github.com/INTENTIUS/choudoufu/issues/962), PR
87
+ * 963), which is the only place a content match reaches a machine: before it,
88
+ * a declared resource whose identity the provider assigns could never be
89
+ * adopted through the document at all, and `TerraformAdoptOp`'s own
90
+ * acceptance suite had never passed on any binary (#2241).
91
+ *
92
+ * {@link terraformApply} depends on the exit-3 refusal, `describeResources()`
93
+ * and both document-reading Ops depend on the document, and the adopt path
94
+ * depends on the `adoptable` section, so the floor is the release that
95
+ * shipped the last of the three.
87
96
  */
88
- export const MIN_CHOUDOUFU_VERSION = "0.14.0";
97
+ export const MIN_CHOUDOUFU_VERSION = "0.15.0";
89
98
 
90
99
  /**
91
100
  * choudoufu's own refusal summary for `-out` on the `live-plan -estate`
@@ -185,6 +194,12 @@ export interface ChoudoufuLivePlanArgs extends TerraformRootArgs {
185
194
  * cost is that the second read asks the estate-wide sweep the account-bounded
186
195
  * question ("which live resources carry no ownership marker at all"), so it
187
196
  * is slower than the ordinary render, not faster.
197
+ *
198
+ * Since #2241 the flag also puts `TOFU_LIVE_COLLECT_UNCLAIMED=1` on the
199
+ * `-json` run, so the document carries the same sweep's answer as its
200
+ * `adoptable` and `swept` sections rather than only the render carrying it.
201
+ * That is what an adoption reads: the render's rows are prose, and the
202
+ * document's are addresses, marker values and a tagging command.
188
203
  */
189
204
  adoptionOnly?: boolean;
190
205
  }
@@ -276,11 +291,22 @@ export interface TerraformApplyResult {
276
291
  refusal?: string;
277
292
  }
278
293
 
279
- /** Counts projected out of `live-plan -json`'s `unowned` section. */
294
+ /** Counts projected out of `live-plan -json`'s `unowned` and `adoptable` sections. */
280
295
  export interface LivePlanUnownedCounts {
281
- /** Live resources at a declared identity carrying no ownership marker for this estate. */
296
+ /**
297
+ * `unowned[]`'s length: live resources sitting at an identity this
298
+ * configuration declares, carrying no ownership marker for this estate.
299
+ */
282
300
  unowned: number;
283
- /** Of those, how many an exact content match makes adoptable. */
301
+ /**
302
+ * Every live resource this run could claim with a tag write, from both
303
+ * sections: the `unowned[]` rows choudoufu offered marker values for, plus
304
+ * the `adoptable[]` rows the estate-wide sweep content-matched to a
305
+ * declaration that carries no identity of its own (choudoufu #962). Those
306
+ * two sections are disjoint, so this is a sum and never a double count.
307
+ * `adoptable[]` is populated only on an adoption run (#2241), so an
308
+ * observation read's document contributes nothing to it.
309
+ */
284
310
  adoptable: number;
285
311
  }
286
312
 
@@ -306,7 +332,10 @@ export interface LivePlanUnownedCounts {
306
332
  export interface ChoudoufuLivePlanResult extends LivePlanUnownedCounts, PlanChangeCounts {
307
333
  /** `true` when `-detailed-exitcode` reported exit 2: the live plan proposes changes. */
308
334
  drift: boolean;
309
- /** GitHub issue #788's JSON document (`bound`, `omissions`, `unowned`), captured whole. */
335
+ /**
336
+ * GitHub issue #788's JSON document (`bound`, `omissions`, `unowned`, and
337
+ * since choudoufu v0.15.0 `adoptable` and `swept`), captured whole.
338
+ */
310
339
  json: unknown;
311
340
  /**
312
341
  * The human-readable plan, from a second `live-plan` run without `-json`
@@ -334,6 +363,13 @@ export interface ChoudoufuLivePlanResult extends LivePlanUnownedCounts, PlanChan
334
363
  contested: AdoptionCandidate[];
335
364
  /** How many declared addresses are contested — the count `contested` above spells out. */
336
365
  ambiguous: number;
366
+ /**
367
+ * `swept[]`: the resource types the estate-wide sweep listed in full on this
368
+ * run. Empty on any run that did not ask for the sweep, which is every run
369
+ * without {@link ChoudoufuLivePlanArgs.adoptionOnly}, so an empty
370
+ * `adoptions` beside an empty `swept` means the question was never put.
371
+ */
372
+ swept: string[];
337
373
  /** Absolute path of the root module directory. */
338
374
  dir: string;
339
375
  /** Where the JSON document was written, relative to `dir`; {@link DEFAULT_LIVE_PLAN_DOCUMENT_FILE} unless overridden. */
@@ -549,14 +585,14 @@ export function choudoufuLiveCheckCommand(opts: { binary: string }): string {
549
585
  * (`adopt_tofu_estate`/`adopt_tofu_address` present).
550
586
  */
551
587
  export function countLivePlanUnowned(document: unknown): LivePlanUnownedCounts {
552
- const unowned = (document as { unowned?: unknown } | null | undefined)?.unowned;
553
- if (!Array.isArray(unowned)) return { unowned: 0, adoptable: 0 };
554
- let adoptable = 0;
555
- for (const entry of unowned) {
588
+ const doc = (document ?? {}) as { unowned?: unknown; adoptable?: unknown };
589
+ const unowned = Array.isArray(doc.unowned) ? doc.unowned : [];
590
+ const claimable = (entry: unknown): boolean => {
556
591
  const e = entry as { adopt_tofu_estate?: unknown; adopt_tofu_address?: unknown } | null | undefined;
557
- if (e?.adopt_tofu_estate || e?.adopt_tofu_address) adoptable++;
558
- }
559
- return { unowned: unowned.length, adoptable };
592
+ return Boolean(e?.adopt_tofu_estate || e?.adopt_tofu_address);
593
+ };
594
+ const matched = Array.isArray(doc.adoptable) ? doc.adoptable.filter(claimable).length : 0;
595
+ return { unowned: unowned.length, adoptable: unowned.filter(claimable).length + matched };
560
596
  }
561
597
 
562
598
  /**
@@ -665,8 +701,8 @@ async function ensureChoudoufuVersion(binary: string, signal?: AbortSignal): Pro
665
701
  if (isOlderVersion(version, MIN_CHOUDOUFU_VERSION)) {
666
702
  throw new Error(
667
703
  `choudoufu ${version} is older than the minimum supported version v${MIN_CHOUDOUFU_VERSION} ` +
668
- "(needed for live-plan -json, live-ls, live-check -json, and the approval artifact `plan -out` / " +
669
- "`apply <planfile>` pair). Upgrade choudoufu.",
704
+ "(needed for live-plan -json, live-ls, live-check -json, the approval artifact `plan -out` / " +
705
+ "`apply <planfile>` pair, and the document's adoptable section). Upgrade choudoufu.",
670
706
  );
671
707
  }
672
708
  })();
@@ -1011,8 +1047,17 @@ export async function choudoufuLivePlan(
1011
1047
  let drift: boolean;
1012
1048
  let jsonStdout: string;
1013
1049
  const planCmd = choudoufuLivePlanCommand({ binary, ...estateFlag, json: true });
1050
+ // The document's `adoptable` and `swept` sections are populated only on a
1051
+ // run that asked the estate-wide sweep the account-bounded question, which
1052
+ // `-adoption-only` implies and a `-json` run does not
1053
+ // (`internal/command/live_collect_unclaimed.go`). `-adoption-only` cannot
1054
+ // ride the `-json` invocation, so the env var is how a document-producing
1055
+ // run asks for the same thing. Set only under `adoptionOnly`: the sweep is
1056
+ // an account-wide list per admitted type, and an observation read has no use
1057
+ // for it.
1058
+ const planEnv = args.adoptionOnly ? { ...env, TOFU_LIVE_COLLECT_UNCLAIMED: "1" } : env;
1014
1059
  try {
1015
- const { stdout, stderr } = await run(planCmd, dir, env, signal);
1060
+ const { stdout, stderr } = await run(planCmd, dir, planEnv, signal);
1016
1061
  report(stdout, stderr);
1017
1062
  drift = false;
1018
1063
  jsonStdout = stdout;
@@ -1069,9 +1114,12 @@ export async function choudoufuLivePlan(
1069
1114
 
1070
1115
  writeFileSync(join(dir, documentPath), document);
1071
1116
 
1072
- // The adoption commands only exist in the `-adoption-only` render, so a
1073
- // candidate off an ordinary run carries the two marker values and no
1074
- // command — which is the whole ownership contract either way.
1117
+ // An `adoptable[]` row carries its own tagging command in the document
1118
+ // (choudoufu #962). An `unowned[]` row does not, and the render is the only
1119
+ // place one is printed for it, so the parse still runs on an adoption run
1120
+ // and the map still feeds that half. A candidate off an ordinary run carries
1121
+ // the two marker values and no command, which is the whole ownership
1122
+ // contract either way.
1075
1123
  const ledger = readAdoptionLedger(json, args.adoptionOnly ? parseAdoptionCommands(text) : undefined);
1076
1124
  const ledgerText = renderAdoptionLedger(ledger, estate);
1077
1125
 
@@ -1084,6 +1132,7 @@ export async function choudoufuLivePlan(
1084
1132
  adoptions: ledger.adoptions,
1085
1133
  contested: ledger.contested,
1086
1134
  ambiguous: ledger.ambiguous,
1135
+ swept: ledger.swept,
1087
1136
  dir,
1088
1137
  documentPath,
1089
1138
  estate,