@intentius/chant-lexicon-terraform 0.124.78 → 0.124.79

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.
@@ -22,14 +22,17 @@ const planTargets: Array<string[] | undefined> = [];
22
22
  const planTexts: Array<boolean | undefined> = [];
23
23
  // #4463: a plan that fails, as one with a stand-in a validation block rejects does.
24
24
  let planError: Error | undefined;
25
+ // #4842: the env each activity was handed.
26
+ const envs: Array<[string, unknown]> = [];
25
27
 
26
28
  vi.mock("../op/activities/terraform", () => ({
27
29
  // The exit constants exit-codes.ts maps choudoufu's statuses with (#4553), as the module has them.
28
30
  CHOUDOUFU_APPROVAL_EXIT_CODE: 3,
29
31
  CHOUDOUFU_WAVE_EXIT: { landed: 0, error: 1, setMoved: 3, rootFailed: 4 },
30
32
  choudoufuWaveRoot: vi.fn(async () => waveRoot),
31
- choudoufuPlanSet: vi.fn(async (args: { root: string }) => {
33
+ choudoufuPlanSet: vi.fn(async (args: { root: string; env?: unknown }) => {
32
34
  calls.push(`plan-set ${args.root}`);
35
+ envs.push(["plan-set", args.env]);
33
36
  return {
34
37
  setDigest: `sha256:set-${args.root}`,
35
38
  rootDigest: `sha256:root-${args.root}`,
@@ -46,24 +49,28 @@ vi.mock("../op/activities/terraform", () => ({
46
49
  waveApplyArgs.push(args);
47
50
  return waveApply;
48
51
  }),
49
- terraformInit: vi.fn(async (args: { root: string }) => {
52
+ terraformInit: vi.fn(async (args: { root: string; env?: unknown }) => {
50
53
  calls.push(`init ${args.root}`);
54
+ envs.push(["init", args.env]);
51
55
  return { dir: `/x/${args.root}` };
52
56
  }),
53
- terraformPlan: vi.fn(async (args: { root: string; vars?: Record<string, unknown>; targets?: string[]; text?: boolean }) => {
57
+ terraformPlan: vi.fn(async (args: { root: string; vars?: Record<string, unknown>; targets?: string[]; text?: boolean; env?: unknown }) => {
54
58
  calls.push(`plan ${args.root} ${JSON.stringify(args.vars ?? {})}`);
59
+ envs.push(["plan", args.env]);
55
60
  planTargets.push(args.targets);
56
61
  planTexts.push(args.text);
57
62
  if (planError) throw planError;
58
63
  // `dir` as the real result has it (#4545 writes the plan document beside the plan file there).
59
64
  return { planFile: "chant.tfplan", dir: `/x/${args.root}`, planDigest: `jcs1-sha256:${args.root}`, json: planJson, ...(changeSet ? { changeSet } : {}) };
60
65
  }),
61
- terraformApply: vi.fn(async (args: { root: string; planFile: string }) => {
66
+ terraformApply: vi.fn(async (args: { root: string; planFile: string; env?: unknown }) => {
62
67
  calls.push(`apply ${args.root} ${args.planFile}`);
68
+ envs.push(["apply", args.env]);
63
69
  return { planFile: args.planFile, dir: `/x/${args.root}`, ...applyResult };
64
70
  }),
65
- terraformOutputs: vi.fn(async (args: { root: string }) => {
71
+ terraformOutputs: vi.fn(async (args: { root: string; env?: unknown }) => {
66
72
  calls.push(`output ${args.root}`);
73
+ envs.push(["output", args.env]);
67
74
  return { id: `${args.root}-id` };
68
75
  }),
69
76
  }));
@@ -82,6 +89,38 @@ beforeEach(() => {
82
89
  planTargets.length = 0;
83
90
  planTexts.length = 0;
84
91
  planError = undefined;
92
+ envs.length = 0;
93
+ });
94
+
95
+ describe("terraform-apply with a run hook's env and check after init (#4842)", () => {
96
+ const env = { AWS_ROLE_ARN: "arn:aws:iam::1:role/e01" };
97
+
98
+ test("plan and run hand processEnv to init, plan, apply and output, and call afterInit between init and plan", async () => {
99
+ const order: string[] = [];
100
+ const planned = await terraformApplyCapability.plan!({ env: "test", component: "e01", processEnv: env, afterInit: async () => void order.push(`check after ${calls.length} calls`) }, { root: "e01" });
101
+ expect(calls).toEqual(["init e01", "plan e01 {}"]);
102
+ expect(order).toEqual(["check after 1 calls"]);
103
+ await terraformApplyCapability.run({ env: "test", component: "e01", processEnv: env, plans: { e01: planned.artifact } }, { root: "e01" });
104
+ expect(envs).toEqual([["init", env], ["plan", env], ["apply", env], ["output", env]]);
105
+ });
106
+
107
+ test("a check after init that throws stops the plan", async () => {
108
+ await expect(terraformApplyCapability.plan!({ env: "test", component: "e01", afterInit: async () => { throw new Error("a module outside the list"); } }, { root: "e01" })).rejects.toThrow("a module outside the list");
109
+ expect(calls).toEqual(["init e01"]);
110
+ });
111
+
112
+ test("a choudoufu root planned as a one-root set gets the env, and the check before its plan", async () => {
113
+ waveRoot = { wave: true, root: "estates/e01" };
114
+ const order: string[] = [];
115
+ await terraformApplyCapability.plan!({ env: "test", component: "e01", processEnv: env, afterInit: async () => void order.push(`check after ${calls.length} calls`) }, { root: "e01" });
116
+ expect(order).toEqual(["check after 0 calls"]);
117
+ expect(envs).toEqual([["plan-set", env]]);
118
+ });
119
+
120
+ test("without them nothing changes", async () => {
121
+ await terraformApplyCapability.plan!({ env: "test", component: "e01" }, { root: "e01" });
122
+ expect(envs).toEqual([["init", undefined], ["plan", undefined]]);
123
+ });
85
124
  });
86
125
 
87
126
  describe("terraform-apply plans with one show -json (#4479)", () => {
@@ -208,7 +247,7 @@ describe("terraform-apply on a choudoufu root in a gated wave (#3459)", () => {
208
247
 
209
248
  describe("terraform-apply", () => {
210
249
  test("the plugin contributes it", () => {
211
- expect(terraformCapabilityPlugin.capabilities().map((c) => c.kind)).toEqual(["terraform-apply", "terraform-state-unlock", "terraform-state-push", "terraform-copy", "terraform-action"]);
250
+ expect(terraformCapabilityPlugin.capabilities().map((c) => c.kind)).toEqual(["terraform-apply", "terraform-state-unlock", "terraform-state-push", "terraform-copy", "terraform-action", "terragrunt-apply"]);
212
251
  });
213
252
 
214
253
  test("plan inits and plans the root, with its vars, and names the root as the member", async () => {
@@ -486,3 +525,19 @@ describe("terraform-apply previews a root outside a group with placeholders (#44
486
525
  expect(calls.some((c) => c.startsWith("apply"))).toBe(false);
487
526
  });
488
527
  });
528
+
529
+ describe("terraform-apply on a root whose plan changes only outputs (#4849)", () => {
530
+ test("its part names the outputs, so its change set is not empty", async () => {
531
+ const { terraformChangeSetPart } = await import("../change-set");
532
+ const { composeChangeSet, isEmptyChangeSet } = await import("@intentius/chant/change-set");
533
+ const plan = {
534
+ resource_changes: [{ address: "terraform_data.keep", mode: "managed", type: "terraform_data", name: "keep", change: { actions: ["no-op"], before: { input: "a" }, after: { input: "a" }, after_unknown: {} } }],
535
+ output_changes: { url: { actions: ["update"], before: "a", after: "b", after_unknown: false } },
536
+ };
537
+ planJson = plan;
538
+ changeSet = terraformChangeSetPart({ member: "e01", plan }) as unknown as Record<string, unknown>;
539
+ const planned = await terraformApplyCapability.plan!({ env: "test", component: "e01" }, { root: "e01" });
540
+ expect(planned.changeSet?.member.outputs).toEqual(["url"]);
541
+ expect(isEmptyChangeSet(composeChangeSet([planned.changeSet!]))).toBe(false);
542
+ });
543
+ });
@@ -119,6 +119,14 @@
119
119
  * yet makes the member a hole naming it, so the root is never planned on a
120
120
  * stand-in.
121
121
  *
122
+ * A wave's run hooks in the product's process (chant#4842) reach the step
123
+ * through its context. `processEnv` goes to every command for the root (a
124
+ * role's credentials, values from a secret store), and `afterInit` is
125
+ * called after `init` and before `plan`, or before `live-plan-set`, which
126
+ * inits and plans in one command. A `root` with no `terraform.roots` entry
127
+ * plans the step's `dir` as its directory, so a product can name each root
128
+ * by its path without writing an entry for it.
129
+ *
122
130
  * Every plan also writes its `show -json` beside the plan file and names it
123
131
  * as `artifact.planJson`, the document a wave's cost hold prices
124
132
  * (`../cost-hold.ts`).
@@ -167,10 +175,11 @@ import { conePlanDigest, type PlanCone } from "../cone";
167
175
  import { fillStateReads, stateReadsProblems, waitingReadsReason, type TerraformStateRead } from "../state-reads";
168
176
  import { terraformStateOpCapabilities } from "../state-ops";
169
177
  import { terraformActionCapability } from "../action-ops";
178
+ import { terragruntApply } from "../terragrunt/capability";
170
179
 
171
180
  /** The `terraform-apply` step's input. */
172
181
  export interface TerraformApplyStepInput {
173
- /** Key into `terraform.roots`. Also the member's name in a wave's set digest. */
182
+ /** Key into `terraform.roots`. Also the member's name in a wave's set digest. With no such entry, `dir` is the root's directory (chant#4842). */
174
183
  root: string;
175
184
  /** Input variables, after the root's var files. See `TerraformPlanArgs.vars`. */
176
185
  vars?: Record<string, unknown>;
@@ -246,7 +255,10 @@ function waitingOnReads(ctx: DeployContext, input: TerraformApplyStepInput, wait
246
255
  return { member: input.root, planDigest, changeSet };
247
256
  }
248
257
 
249
- const rootArgs = (input: TerraformApplyStepInput) => ({ root: input.root, ...(input.cwd ? { cwd: input.cwd } : {}), ...(input.dir ? { dir: input.dir } : {}) });
258
+ /** A run hook's environment (chant#4842, `DeployContext.processEnv`), which every command for the root runs with. */
259
+ type RunEnv = Record<string, string> | undefined;
260
+
261
+ const rootArgs = (input: TerraformApplyStepInput, env?: RunEnv) => ({ root: input.root, ...(input.cwd ? { cwd: input.cwd } : {}), ...(input.dir ? { dir: input.dir } : {}), ...(env ? { env } : {}) });
250
262
 
251
263
  function requireRoot(input: TerraformApplyStepInput): void {
252
264
  if (typeof input.root !== "string" || input.root === "") {
@@ -254,14 +266,16 @@ function requireRoot(input: TerraformApplyStepInput): void {
254
266
  }
255
267
  }
256
268
 
257
- async function planRoot(input: TerraformApplyStepInput, targets?: string[]): Promise<PlannedRoot & { changeSet?: ChangeSetPart; json?: unknown }> {
269
+ async function planRoot(input: TerraformApplyStepInput, targets?: string[], at: { env?: RunEnv; afterInit?: () => Promise<void> } = {}): Promise<PlannedRoot & { changeSet?: ChangeSetPart; json?: unknown }> {
258
270
  requireRoot(input);
259
- await terraformInit(rootArgs(input));
271
+ await terraformInit(rootArgs(input, at.env));
272
+ // chant#4842: the run hook's check, once init fetched the modules and providers and before the plan runs their code.
273
+ await at.afterInit?.();
260
274
  // One `show -json` per plan (#4479): its document is the digest, the change
261
275
  // set, the planned outputs and the shape. Nothing here reads the
262
276
  // `-no-color` render, so the plan skips it.
263
277
  const planned = await terraformPlan({
264
- ...rootArgs(input),
278
+ ...rootArgs(input, at.env),
265
279
  ...(input.vars ? { vars: input.vars } : {}),
266
280
  ...(targets && targets.length > 0 ? { targets } : {}),
267
281
  ...lockTimeoutOf(input),
@@ -390,7 +404,8 @@ async function rootDir(input: TerraformApplyStepInput): Promise<{ dir: string; b
390
404
  const start = resolve(input.cwd ?? process.cwd());
391
405
  const { config, configPath } = await loadChantConfigUpward(start);
392
406
  const terraform = (config as { terraform?: TerraformConfig }).terraform;
393
- const declared = terraform?.roots?.[input.root]?.dir;
407
+ // chant#4842: a root with no entry and a dir of its own is that directory.
408
+ const declared = terraform?.roots?.[input.root]?.dir ?? input.dir;
394
409
  if (declared === undefined) return undefined;
395
410
  const dir = input.dir ?? declared;
396
411
  return { dir: resolve(configPath ? dirname(configPath) : start, dir), ...(terraform?.binary ? { binary: terraform.binary } : {}) };
@@ -418,9 +433,10 @@ async function previewRoot(ctx: DeployContext, input: TerraformApplyStepInput, u
418
433
  if (typeof stand === "string") return waitingRoot(ctx, input, unresolved, stand);
419
434
  let planned: Awaited<ReturnType<typeof terraformPlan>>;
420
435
  try {
421
- await terraformInit(rootArgs(input));
436
+ await terraformInit(rootArgs(input, ctx.processEnv));
437
+ await ctx.afterInit?.();
422
438
  planned = await terraformPlan({
423
- ...rootArgs(input),
439
+ ...rootArgs(input, ctx.processEnv),
424
440
  vars: stand.vars,
425
441
  planFile: PREVIEW_PLAN_FILE,
426
442
  ...(targets ? { targets } : {}),
@@ -509,7 +525,7 @@ async function runChoudoufuWave(
509
525
  planned: ChoudoufuWavePlannedRoot,
510
526
  ): Promise<TerraformApplyStepOutput> {
511
527
  const applied = await choudoufuWaveApply({
512
- ...rootArgs(input),
528
+ ...rootArgs(input, ctx.processEnv),
513
529
  planSetFile: planned.choudoufu.planSetFile,
514
530
  setDigest: planned.choudoufu.setDigest,
515
531
  ...(ctx.carried?.[input.root] !== undefined ? { carried: ctx.carried[input.root] } : {}),
@@ -520,7 +536,7 @@ async function runChoudoufuWave(
520
536
  if (terraformPlanMoved({ command: "live-wave-apply", code: applied.exitCode })) throw new TerraformPlanMovedError(input.root, message);
521
537
  throw new Error(message);
522
538
  }
523
- const outputs = await terraformOutputs(rootArgs(input));
539
+ const outputs = await terraformOutputs(rootArgs(input, ctx.processEnv));
524
540
  return { root: input.root, planDigest: planned.planDigest, fromWavePlan: true, outputs };
525
541
  }
526
542
 
@@ -563,9 +579,12 @@ export const terraformApplyCapability: Capability<TerraformApplyStepInput, Terra
563
579
  const targets = ctx.targets && ctx.targets.length > 0 ? ctx.targets : undefined;
564
580
  if (unresolved.length > 0) return previewRoot(ctx, input, unresolved, targets);
565
581
  // A targeted root plans with `plan -target`, and a cone plan with `plan -cone` (#4545), never as a choudoufu one-root set.
566
- const wave = targets || input.cone ? { wave: false } : await choudoufuWaveRoot({ ...rootArgs(input), ...(input.vars ? { vars: input.vars } : {}) });
582
+ const env = ctx.processEnv;
583
+ const wave = targets || input.cone ? { wave: false } : await choudoufuWaveRoot({ ...rootArgs(input, env), ...(input.vars ? { vars: input.vars } : {}) });
567
584
  if (wave.wave) {
568
- const set = await choudoufuPlanSet(rootArgs(input));
585
+ // chant#4842: live-plan-set inits and plans in one command, so the run hook's check comes before it.
586
+ await ctx.afterInit?.();
587
+ const set = await choudoufuPlanSet(rootArgs(input, env));
569
588
  const artifact: ChoudoufuWavePlannedRoot = {
570
589
  planDigest: set.planDigest,
571
590
  choudoufu: { planSetFile: set.planSetFile, setDigest: set.setDigest },
@@ -573,7 +592,7 @@ export const terraformApplyCapability: Capability<TerraformApplyStepInput, Terra
573
592
  };
574
593
  return { member: input.root, planDigest: set.planDigest, artifact, changeSet: set.changeSet, outputs: terraformPlannedOutputs(set.json), shape: terraformPlanShape(set.json, input.root) };
575
594
  }
576
- const { changeSet, json, ...planned } = await planRoot(input, targets);
595
+ const { changeSet, json, ...planned } = await planRoot(input, targets, { env, ...(ctx.afterInit ? { afterInit: ctx.afterInit } : {}) });
577
596
  // #4552: a choudoufu root's rows, keyed from the plan document while it is at hand.
578
597
  const lockRows = changeSet?.member.planner === "choudoufu" ? terraformLockRows(input.root, changeSet.member.scope, json) : undefined;
579
598
  return {
@@ -590,10 +609,10 @@ export const terraformApplyCapability: Capability<TerraformApplyStepInput, Terra
590
609
  },
591
610
  // The root's outputs as its state holds them now (#3183): what a dependent
592
611
  // planned before this root applies reads through `stackOutput()`.
593
- async outputs(_ctx: DeployContext, input: TerraformApplyStepInput): Promise<Record<string, unknown>> {
612
+ async outputs(ctx: DeployContext, input: TerraformApplyStepInput): Promise<Record<string, unknown>> {
594
613
  requireRoot(input);
595
- await terraformInit(rootArgs(input));
596
- return terraformOutputs(rootArgs(input));
614
+ await terraformInit(rootArgs(input, ctx.processEnv));
615
+ return terraformOutputs(rootArgs(input, ctx.processEnv));
597
616
  },
598
617
  // #4447 (ws-107): a group core forms of components that read each other plans and applies as one composed run
599
618
  // when `terraform.compose.enabled` is set (./compose-group.ts). Delegates, so the methods are read when called.
@@ -634,20 +653,20 @@ export const terraformApplyCapability: Capability<TerraformApplyStepInput, Terra
634
653
  if (isShapeArtifact(fromWave)) {
635
654
  // A root previewed with stand-ins (#4463): plan it now with the real
636
655
  // values, and apply that plan only when it is within the preview's shape.
637
- const fresh = await planRoot(await readsForRun(input), ctx.targets);
656
+ const fresh = await planRoot(await readsForRun(input), ctx.targets, { env: ctx.processEnv });
638
657
  const differences = planShapeDifferences(fromWave.shape, terraformPlanShape(fresh.json, input.root));
639
658
  if (differences.length > 0) throw new PlanShapeMovedError(input.root, differences);
640
- const applied = await terraformApply({ ...rootArgs(input), planFile: fresh.planFile, ...lockTimeoutOf(input) });
659
+ const applied = await terraformApply({ ...rootArgs(input, ctx.processEnv), planFile: fresh.planFile, ...lockTimeoutOf(input) });
641
660
  if (!applied.applied) {
642
661
  throw new Error(`terraform-apply: root "${input.root}" refused the plan it was given (${applied.refused ?? "refused"}). ${applied.refusal ?? ""}`);
643
662
  }
644
- const outputs = await terraformOutputs(rootArgs(input));
663
+ const outputs = await terraformOutputs(rootArgs(input, ctx.processEnv));
645
664
  return { root: input.root, planDigest: fresh.planDigest, fromWavePlan: false, outputs };
646
665
  }
647
666
  const planned: PlannedRoot = isPlannedRoot(fromWave)
648
667
  ? fromWave
649
- : await planRoot(await readsForRun(input), ctx.targets).then(({ planFile, planDigest }) => ({ planFile, planDigest }));
650
- const applied = await terraformApply({ ...rootArgs(input), planFile: planned.planFile, ...lockTimeoutOf(input) });
668
+ : await planRoot(await readsForRun(input), ctx.targets, { env: ctx.processEnv }).then(({ planFile, planDigest }) => ({ planFile, planDigest }));
669
+ const applied = await terraformApply({ ...rootArgs(input, ctx.processEnv), planFile: planned.planFile, ...lockTimeoutOf(input) });
651
670
  if (!applied.applied) {
652
671
  const message =
653
672
  `terraform-apply: root "${input.root}" refused the plan it was given (${applied.refused ?? "refused"}). ` +
@@ -658,7 +677,7 @@ export const terraformApplyCapability: Capability<TerraformApplyStepInput, Terra
658
677
  if (applied.refused === "approval-mismatch" && isPlannedRoot(fromWave)) throw new TerraformPlanMovedError(input.root, message);
659
678
  throw new Error(message);
660
679
  }
661
- const outputs = await terraformOutputs(rootArgs(input));
680
+ const outputs = await terraformOutputs(rootArgs(input, ctx.processEnv));
662
681
  return { root: input.root, planDigest: planned.planDigest, fromWavePlan: isPlannedRoot(fromWave), outputs };
663
682
  },
664
683
  };
@@ -678,7 +697,13 @@ export const terraformCapabilityPlugin: CapabilityPlugin = {
678
697
  return ownPackageVersion(import.meta.url);
679
698
  },
680
699
  capabilities(): Array<Capability<never, unknown>> {
681
- return [terraformApplyCapability as Capability<never, unknown>, ...terraformStateOpCapabilities.map((c) => c as unknown as Capability<never, unknown>), terraformActionCapability as unknown as Capability<never, unknown>];
700
+ return [
701
+ terraformApplyCapability as Capability<never, unknown>,
702
+ ...terraformStateOpCapabilities.map((c) => c as unknown as Capability<never, unknown>),
703
+ terraformActionCapability as unknown as Capability<never, unknown>,
704
+ // #4842: Terragrunt units as an op wave's step, found at plan time and run with one run --all.
705
+ terragruntApply as unknown as Capability<never, unknown>,
706
+ ];
682
707
  },
683
708
  families(): Record<string, readonly string[]> {
684
709
  return TERRAFORM_VERB_FAMILIES;
@@ -0,0 +1,134 @@
1
+ /**
2
+ * A composed run as an op wave (#4842): a wave marked `together` on
3
+ * `terraform-apply` plans its roots as one composed run through the group
4
+ * methods (`./compose-group.ts`, chant's `ComposedRun`) and applies it in
5
+ * one call, as terragucci's `waves.compose` does. The run is a fake
6
+ * `ComposedRunner`, so nothing here runs a binary; the composition is real,
7
+ * read from roots written to a temporary project.
8
+ */
9
+
10
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
11
+ import { tmpdir } from "node:os";
12
+ import { dirname, join } from "node:path";
13
+ import { afterAll, describe, expect, test } from "vitest";
14
+ import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
15
+ import { memoryGateLedgerPort, runOpWave, type OpWavesSpec } from "@intentius/chant/op";
16
+ import type { ChangeSetPart } from "@intentius/chant/change-set";
17
+ import type { TerraformConfig } from "../config";
18
+ import type { ComposedRunApply, ComposedRunner, ComposedRunPlan } from "../compose/estate";
19
+ import { composeGroup, type GroupRunnerOptions } from "./compose-group";
20
+ import { terraformApplyCapability } from "./capability-plugin";
21
+
22
+ const made: string[] = [];
23
+ afterAll(() => {
24
+ for (const d of made) rmSync(d, { recursive: true, force: true });
25
+ });
26
+
27
+ function project(): string {
28
+ const dir = mkdtempSync(join(tmpdir(), "chant-compose-op-wave-"));
29
+ made.push(dir);
30
+ const roots: Record<string, string> = {
31
+ "roots/a/main.tf": 'resource "terraform_data" "x" {\n input = "a"\n}\n\noutput "id" {\n value = terraform_data.x.output\n}\n',
32
+ "roots/b/main.tf": 'variable "a_id" {\n type = string\n}\n\nresource "terraform_data" "x" {\n input = var.a_id\n}\n',
33
+ };
34
+ for (const [path, text] of Object.entries(roots)) {
35
+ mkdirSync(dirname(join(dir, path)), { recursive: true });
36
+ writeFileSync(join(dir, path), text);
37
+ }
38
+ return dir;
39
+ }
40
+
41
+ const terraform = { binary: "tofu", roots: { a: { dir: "roots/a" }, b: { dir: "roots/b" } }, compose: { enabled: true } } as unknown as TerraformConfig;
42
+
43
+ const part = (root: string): ChangeSetPart => ({
44
+ member: { member: root, lexicon: "terraform", planner: "tofu", status: "planned", planDigest: `jcs1-sha256:${root}`, holes: [] },
45
+ entries: [{ member: root, address: "terraform_data.x", type: "terraform_data", action: "create" }] as never,
46
+ });
47
+
48
+ function composedPlan(digest: string): ComposedRunPlan {
49
+ const member = (root: string) => ({
50
+ member: root,
51
+ planDigest: `jcs1-sha256:${root}`,
52
+ changeSet: part(root),
53
+ outputs: { known: {}, unknown: ["id"] },
54
+ shape: { member: root, entries: [{ logicalId: "terraform_data.x", action: "create" }], unknownInputs: [] },
55
+ slice: {},
56
+ counts: { add: 1, change: 0, destroy: 0, changes: 1, destroys: 0 },
57
+ reads: [],
58
+ });
59
+ return { members: { a: member("a"), b: member("b") }, digest, planFile: "/w/composed.tfplan", text: "", texts: {}, generated: [], counts: { add: 2, change: 0, destroy: 0, changes: 2, destroys: 0 }, summary: "Plan: 2 to add, 0 to change, 0 to destroy." } as unknown as ComposedRunPlan;
60
+ }
61
+
62
+ const appliedBoth: ComposedRunApply = {
63
+ ok: true,
64
+ members: { a: { status: "applied", planned: 1, done: 1, outputs: { id: "a-1" } }, b: { status: "applied", planned: 1, done: 1, outputs: {} } },
65
+ } as unknown as ComposedRunApply;
66
+
67
+ /** `terraform-apply` with its group methods on a fake run: each plan answers the next of `digests`. */
68
+ function capability(dir: string, digests: string[]): { registry: CapabilityRegistry; calls: string[]; built: GroupRunnerOptions[] } {
69
+ const calls: string[] = [];
70
+ const built: GroupRunnerOptions[] = [];
71
+ const g = composeGroup({
72
+ project: async () => ({ projectRoot: dir, terraform }),
73
+ runner: (o): ComposedRunner => {
74
+ built.push(o);
75
+ return {
76
+ plan: async () => {
77
+ calls.push(`plan lock=${o.lock}`);
78
+ return composedPlan(digests.shift() ?? "sha256:composed-1");
79
+ },
80
+ apply: async () => {
81
+ calls.push("apply");
82
+ return appliedBoth;
83
+ },
84
+ release: async () => void calls.push("release"),
85
+ } as ComposedRunner;
86
+ },
87
+ identity: async () => undefined,
88
+ env: {},
89
+ log: () => {},
90
+ });
91
+ const cap = { ...terraformApplyCapability, planGroup: g.planGroup, runGroup: g.runGroup } as unknown as Capability<never, unknown>;
92
+ return { registry: new CapabilityRegistry().register(cap), calls, built };
93
+ }
94
+
95
+ const spec: OpWavesSpec = {
96
+ name: "tf-apply",
97
+ op: "tf-apply",
98
+ gateName: "wave-{k}",
99
+ step: { kind: "terraform-apply", root: "{target}" },
100
+ waves: [
101
+ {
102
+ name: "wave-1",
103
+ together: true,
104
+ gate: "never",
105
+ runs: [{ target: "a" }, { target: "b", dependsOn: ["a"], input: { vars: { a_id: { stackOutput: { stack: "a", name: "id" } } } } }],
106
+ },
107
+ ],
108
+ };
109
+
110
+ const wave = (cwd: string, registry: CapabilityRegistry) =>
111
+ runOpWave({ spec, specFile: "waves.json", wave: 1, cwd, show: () => ({ sha: "b".repeat(40), text: JSON.stringify(spec) }), gates: memoryGateLedgerPort(), capabilities: registry, log: () => {} });
112
+
113
+ describe("a composed run as a together op wave (#4842)", () => {
114
+ test("plans the roots as one composed run without locks, and applies that same plan under them in one call", async () => {
115
+ const dir = project();
116
+ const { registry, calls, built } = capability(dir, ["sha256:composed-1", "sha256:composed-1"]);
117
+ const result = await wave(dir, registry);
118
+ expect(result.exitCode).toBe(0);
119
+ expect(calls).toEqual(["plan lock=false", "release", "plan lock=true", "apply", "release"]);
120
+ expect(built[0]!.composition.members).toEqual(["a", "b"]);
121
+ expect(built[0]!.vars).toEqual({ a: undefined, b: { a_id: { stackOutput: { stack: "a", name: "id" } } } });
122
+ expect(result.applied).toEqual(["a", "b"]);
123
+ expect(result.decision?.members.map((m) => m.member)).toEqual(["a", "b"]);
124
+ });
125
+
126
+ test("a composed plan that moved since the wave was decided applies nothing, and the wave exits 4", async () => {
127
+ const dir = project();
128
+ const { registry, calls } = capability(dir, ["sha256:composed-1", "sha256:composed-2"]);
129
+ const result = await wave(dir, registry);
130
+ expect(result.exitCode).toBe(4);
131
+ expect(result.moved).toEqual(["a", "b"]);
132
+ expect(calls).not.toContain("apply");
133
+ });
134
+ });
@@ -397,6 +397,27 @@ describe("root resolution against terraform.roots (#2086)", () => {
397
397
  await terraformInit({ root: "app", cwd: dir });
398
398
  expect(execCalls[0].cmd.startsWith("tofu init")).toBe(true);
399
399
  });
400
+
401
+ test("a root with no entry plans the dir it is given, with the namespace's binary (#4842)", async () => {
402
+ const dir = project({ binary: "tofu", roots: { app: { dir: "./infra" } } });
403
+ mkdirSync(join(dir, "live", "net"), { recursive: true });
404
+ const result = await terraformInit({ root: "live/net", dir: "live/net", cwd: dir });
405
+ expect(result.dir).toBe(resolve(dir, "live/net"));
406
+ expect(execCalls[0].cmd).toBe("tofu init -input=false");
407
+ expect(execCalls[0].opts.cwd).toBe(resolve(dir, "live/net"));
408
+ // Without a dir, an unknown name still fails.
409
+ await expect(terraformInit({ root: "live/net", cwd: dir })).rejects.toThrow(/no root named "live\/net"/);
410
+ });
411
+
412
+ test("env is laid over the job's for every command of the root (#4842)", async () => {
413
+ const dir = project({ roots: { app: { dir: "./infra", workspace: "blue" } } });
414
+ await terraformInit({ root: "app", cwd: dir, env: { AWS_ROLE_ARN: "arn:aws:iam::1:role/app", TF_WORKSPACE: "green" } });
415
+ expect(execCalls[0].opts.env).toMatchObject({ TF_IN_AUTOMATION: "1", AWS_ROLE_ARN: "arn:aws:iam::1:role/app", TF_WORKSPACE: "green" });
416
+ await terraformApply({ root: "app", cwd: dir, planFile: "chant.tfplan", env: { AWS_ROLE_ARN: "arn:aws:iam::1:role/app" } });
417
+ expect(execCalls[1].opts.env).toMatchObject({ AWS_ROLE_ARN: "arn:aws:iam::1:role/app", TF_WORKSPACE: "blue" });
418
+ await terraformInit({ root: "app", cwd: dir });
419
+ expect(execCalls[2].opts.env?.AWS_ROLE_ARN).toBe(process.env.AWS_ROLE_ARN);
420
+ });
400
421
  });
401
422
 
402
423
  // ── terraformInit ───────────────────────────────────────────────────────────
@@ -203,8 +203,19 @@ export interface TerraformRootArgs {
203
203
  * under a release sets it to the release's recorded copy of the root; the
204
204
  * entry's workspace, var files, backend config and estate still apply, and
205
205
  * its var files and backend-config files resolve against this directory.
206
+ *
207
+ * With no `terraform.roots` entry named `root`, a `dir` names the root's
208
+ * directory itself (chant#4842): the root plans there with the namespace's
209
+ * binary and no workspace, var files or backend config of an entry.
206
210
  */
207
211
  dir?: string;
212
+ /**
213
+ * Environment variables every command for the root runs with, over the
214
+ * job's and the root's own (`TF_WORKSPACE`), such as a role's credentials
215
+ * (chant#4842). An op wave's run hook gives them per run through
216
+ * `DeployContext.processEnv`.
217
+ */
218
+ env?: Record<string, string>;
208
219
  }
209
220
 
210
221
  export interface TerraformInitArgs extends TerraformRootArgs {
@@ -608,9 +619,14 @@ export function terraformEnvironment(root?: Pick<TerraformRootConfig, "workspace
608
619
  return {
609
620
  TF_IN_AUTOMATION: "1",
610
621
  ...(root?.workspace ? { TF_WORKSPACE: root.workspace } : {}),
622
+ // chant#4842: the run's own environment, which resolveRoot ties to the root it resolved.
623
+ ...(root ? rootProcessEnv.get(root) : undefined),
611
624
  };
612
625
  }
613
626
 
627
+ /** The `env` a root was resolved with (chant#4842), by the resolved root's entry, so every command run for it gets it. */
628
+ const rootProcessEnv = new WeakMap<object, Record<string, string>>();
629
+
614
630
  /**
615
631
  * `terraform init`, with the root's `backendConfigFiles` as
616
632
  * `-backend-config=<path>` flags and then its `backendConfig` as
@@ -958,7 +974,10 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
958
974
  const projectRoot = configPath ? dirname(configPath) : start;
959
975
  const namespace = (config as { terraform?: TerraformConfig }).terraform;
960
976
  const roots = namespace?.roots ?? {};
961
- const root = roots[args.root];
977
+ // chant#4842: a root with no entry and a dir of its own plans that directory.
978
+ const declared = roots[args.root] ?? (args.dir !== undefined ? { dir: args.dir } : undefined);
979
+ const root = declared && args.env ? { ...declared } : declared;
980
+ if (root && args.env) rootProcessEnv.set(root, args.env);
962
981
  if (!root) {
963
982
  const known = Object.keys(roots).sort();
964
983
  throw new Error(