@intentius/chant-lexicon-terraform 0.124.78 → 0.124.80

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 (45) hide show
  1. package/dist/change-set.d.ts +1 -0
  2. package/dist/change-set.d.ts.map +1 -1
  3. package/dist/change-set.js +19 -0
  4. package/dist/components/capability-plugin.d.ts +75 -2
  5. package/dist/components/capability-plugin.d.ts.map +1 -1
  6. package/dist/components/capability-plugin.js +250 -142
  7. package/dist/components/compose-group.d.ts +2 -1
  8. package/dist/components/compose-group.d.ts.map +1 -1
  9. package/dist/components/compose-group.js +6 -6
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -1
  13. package/dist/integrity.json +2 -2
  14. package/dist/manifest.json +1 -1
  15. package/dist/op/activities/terraform.d.ts +33 -0
  16. package/dist/op/activities/terraform.d.ts.map +1 -1
  17. package/dist/op/activities/terraform.js +37 -5
  18. package/dist/state-reads.d.ts +20 -3
  19. package/dist/state-reads.d.ts.map +1 -1
  20. package/dist/state-reads.js +20 -4
  21. package/dist/terragrunt/capability.d.ts +129 -0
  22. package/dist/terragrunt/capability.d.ts.map +1 -0
  23. package/dist/terragrunt/capability.js +352 -0
  24. package/dist/terragrunt/index.d.ts +3 -1
  25. package/dist/terragrunt/index.d.ts.map +1 -1
  26. package/dist/terragrunt/index.js +3 -1
  27. package/dist/terragrunt/units.d.ts +9 -0
  28. package/dist/terragrunt/units.d.ts.map +1 -1
  29. package/dist/terragrunt/units.js +25 -0
  30. package/package.json +2 -2
  31. package/src/change-set.test.ts +27 -1
  32. package/src/change-set.ts +19 -0
  33. package/src/components/capability-plugin.test.ts +61 -6
  34. package/src/components/capability-plugin.ts +290 -139
  35. package/src/components/compose-group.ts +8 -7
  36. package/src/components/compose-op-wave.test.ts +134 -0
  37. package/src/components/terraform-apply-op-wave.test.ts +295 -0
  38. package/src/index.ts +5 -0
  39. package/src/op/activities/terraform.test.ts +21 -0
  40. package/src/op/activities/terraform.ts +64 -4
  41. package/src/state-reads.ts +23 -5
  42. package/src/terragrunt/capability.test.ts +348 -0
  43. package/src/terragrunt/capability.ts +437 -0
  44. package/src/terragrunt/index.ts +3 -1
  45. package/src/terragrunt/units.ts +25 -0
@@ -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
+ });
@@ -0,0 +1,295 @@
1
+ /**
2
+ * `terraform-apply` as an op wave's step, run by core's `runOpWave` against a
3
+ * fake `terraform` on `PATH` (chant#4854, terragucci issue 1303). The fake
4
+ * logs each command with its directory and `TF_WORKSPACE`, keeps the
5
+ * workspaces a root's backend lists in `.ws` beside the root, refuses an
6
+ * init in a workspace that is not listed (as a real backend does) and a
7
+ * `workspace select` while `TF_WORKSPACE` is set, and answers `output -json`
8
+ * from `outputs-<workspace>.json`. Nothing reaches a cloud.
9
+ */
10
+
11
+ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import { afterAll, afterEach, describe, expect, test, vi } from "vitest";
15
+ import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
16
+ import { memoryGateLedgerPort, opWavePlanPath, runOpWave, type OpWaveRunHooks, type OpWavesSpec, type RunOpWaveResult } from "@intentius/chant/op";
17
+ import { terraformApplyCapability, terraformApplyCapabilityWith, type TerraformRootResolver } from "./capability-plugin";
18
+ import { atmosStateReads } from "../state-reads";
19
+
20
+ const made: string[] = [];
21
+ afterAll(() => {
22
+ for (const d of made) rmSync(d, { recursive: true, force: true });
23
+ });
24
+ afterEach(() => vi.unstubAllEnvs());
25
+
26
+ /** A fake terraform. `$FAKE_LOG` gets one line per command: `<name> <dir> ws=<TF_WORKSPACE or -> <args>`. */
27
+ const fake = (name: string) => `#!/bin/sh
28
+ ws="\${TF_WORKSPACE-}"
29
+ echo "${name} $PWD ws=\${TF_WORKSPACE--} $*" >> "$FAKE_LOG"
30
+ [ -n "\${FAKE_ENV_LOG-}" ] && echo "\${PWD##*/} $1 role=\${ROLE--}" >> "$FAKE_ENV_LOG"
31
+ case "$1" in
32
+ init)
33
+ if [ -n "$ws" ] && [ "$ws" != "default" ] && ! grep -qx "$ws" .ws 2>/dev/null; then
34
+ echo "Error: Currently selected workspace \\"$ws\\" does not exist" >&2; exit 1
35
+ fi
36
+ exit 0 ;;
37
+ workspace)
38
+ if [ -n "\${TF_WORKSPACE+x}" ]; then echo "Error: The selected workspace is currently overridden using the TF_WORKSPACE environment variable." >&2; exit 1; fi
39
+ for a in "$@"; do w="$a"; done
40
+ grep -qx "$w" .ws 2>/dev/null || echo "$w" >> .ws
41
+ exit 0 ;;
42
+ plan)
43
+ for a in "$@"; do case "$a" in -out=*) out="\${a#-out=}" ;; esac; done
44
+ echo plan > "$out"
45
+ exit 2 ;;
46
+ show)
47
+ echo '{"format_version":"1.2","resource_changes":[{"address":"terraform_data.x","mode":"managed","type":"terraform_data","name":"x","change":{"actions":["create"],"before":null,"after":{}}}]}'
48
+ exit 0 ;;
49
+ apply) exit 0 ;;
50
+ output)
51
+ f="outputs-\${ws:-default}.json"
52
+ if [ -f "$f" ]; then cat "$f"; else echo '{}'; fi
53
+ exit 0 ;;
54
+ esac
55
+ echo "fake ${name}: unexpected $*" >&2
56
+ exit 1
57
+ `;
58
+
59
+ /** A project with roots at `roots/<name>`, a `chant.config.json` naming `binary`, and a fake `terraform` on PATH. */
60
+ function project(roots: string[], config: Record<string, unknown> = { binary: "terraform" }): { dir: string; log: string; bin: string } {
61
+ const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-tf-op-wave-")));
62
+ made.push(dir);
63
+ const bin = join(dir, "bin");
64
+ mkdirSync(bin);
65
+ for (const name of ["terraform", "tofu"]) {
66
+ writeFileSync(join(bin, name), fake(name));
67
+ chmodSync(join(bin, name), 0o755);
68
+ }
69
+ const log = join(dir, "fake.log");
70
+ writeFileSync(log, "");
71
+ vi.stubEnv("PATH", `${bin}:${process.env.PATH ?? ""}`);
72
+ vi.stubEnv("FAKE_LOG", log);
73
+ for (const r of roots) {
74
+ mkdirSync(join(dir, "roots", r), { recursive: true });
75
+ writeFileSync(join(dir, "roots", r, "main.tf"), "");
76
+ }
77
+ writeFileSync(join(dir, "chant.config.json"), JSON.stringify({ lexicons: ["terraform"], terraform: config }));
78
+ return { dir, log, bin };
79
+ }
80
+
81
+ /** The fake's log, with the project's directory as `.`. */
82
+ const lines = (p: { dir: string; log: string }): string[] =>
83
+ readFileSync(p.log, "utf-8")
84
+ .split("\n")
85
+ .filter(Boolean)
86
+ .map((l) => l.split(p.dir).join("."));
87
+
88
+ /** A log line without the plan file's name. */
89
+ const plain = (l: string): string => l.replace(/ -out=\S+| \S+\.tfplan$/g, "");
90
+
91
+ const registry = (cap: Capability<never, unknown> = terraformApplyCapability as unknown as Capability<never, unknown>) => new CapabilityRegistry().register(cap);
92
+
93
+ function spec(runs: OpWavesSpec["waves"][number]["runs"], step: Record<string, unknown> = {}): OpWavesSpec {
94
+ return {
95
+ name: "tf-apply",
96
+ op: "tf-apply",
97
+ gateName: "wave-{k}",
98
+ step: { kind: "terraform-apply", root: "{target}", dir: "{target}", lockTimeout: false, ...step },
99
+ waves: [{ name: "wave-1", gate: "never", runs }],
100
+ } as OpWavesSpec;
101
+ }
102
+
103
+ function wave(cwd: string, s: OpWavesSpec, o: { runHooks?: OpWaveRunHooks; capabilities?: CapabilityRegistry } = {}): Promise<RunOpWaveResult> {
104
+ return runOpWave({
105
+ spec: s,
106
+ specFile: "waves.json",
107
+ wave: 1,
108
+ cwd,
109
+ show: () => ({ sha: "b".repeat(40), text: JSON.stringify(s) }),
110
+ gates: memoryGateLedgerPort(),
111
+ capabilities: o.capabilities ?? registry(),
112
+ ...(o.runHooks ? { runHooks: o.runHooks } : {}),
113
+ log: () => {},
114
+ });
115
+ }
116
+
117
+ describe("a root that names its workspace (chant#4854)", () => {
118
+ test("inits in default, selects or creates the workspace with TF_WORKSPACE unset, then plans, applies and reads outputs in it", async () => {
119
+ const p = project(["app"]);
120
+ const result = await wave(p.dir, spec([{ target: "roots/app", input: { workspace: "ue1-prod" } }], { cwd: p.dir }));
121
+ expect(result.exitCode).toBe(0);
122
+ expect(result.applied).toEqual(["roots/app"]);
123
+ expect(lines(p).map(plain)).toEqual([
124
+ "terraform ./roots/app ws=default init -input=false",
125
+ "terraform ./roots/app ws=- workspace select -or-create=true ue1-prod",
126
+ "terraform ./roots/app ws=ue1-prod plan -input=false -detailed-exitcode",
127
+ "terraform ./roots/app ws=ue1-prod show -json",
128
+ "terraform ./roots/app ws=ue1-prod apply -input=false",
129
+ "terraform ./roots/app ws=ue1-prod output -json",
130
+ ]);
131
+ expect(readFileSync(join(p.dir, "roots/app/.ws"), "utf-8")).toBe("ue1-prod\n");
132
+ });
133
+
134
+ test("the step's workspace wins over a TF_WORKSPACE the run hook's env sets", async () => {
135
+ const p = project(["app"]);
136
+ const runHooks: OpWaveRunHooks = { name: "product", beforePlan: () => ({ env: { TF_WORKSPACE: "ue1-prod", ROLE: "app" } }) };
137
+ const result = await wave(p.dir, spec([{ target: "roots/app", input: { workspace: "ue1-prod" } }], { cwd: p.dir }), { runHooks });
138
+ expect(result.exitCode).toBe(0);
139
+ const log = lines(p);
140
+ expect(log[0]).toBe("terraform ./roots/app ws=default init -input=false");
141
+ expect(log[1]).toBe("terraform ./roots/app ws=- workspace select -or-create=true ue1-prod");
142
+ expect(log.filter((l) => / (plan|apply|output) /.test(l)).every((l) => l.includes(" ws=ue1-prod "))).toBe(true);
143
+ });
144
+
145
+ test("a workspace the backend lists already is selected, not created again", async () => {
146
+ const p = project(["app"]);
147
+ writeFileSync(join(p.dir, "roots/app/.ws"), "ue1-prod\n");
148
+ const result = await wave(p.dir, spec([{ target: "roots/app", input: { workspace: "ue1-prod" } }], { cwd: p.dir }));
149
+ expect(result.exitCode).toBe(0);
150
+ expect(readFileSync(join(p.dir, "roots/app/.ws"), "utf-8")).toBe("ue1-prod\n");
151
+ });
152
+
153
+ test("without the input, a run hook's TF_WORKSPACE of a new workspace fails init, as before", async () => {
154
+ const p = project(["app"]);
155
+ const runHooks: OpWaveRunHooks = { name: "product", beforePlan: () => ({ env: { TF_WORKSPACE: "ue1-prod" } }) };
156
+ await expect(wave(p.dir, spec([{ target: "roots/app" }], { cwd: p.dir }), { runHooks })).rejects.toThrow(/Currently selected workspace "ue1-prod" does not exist/);
157
+ expect(lines(p)).toEqual(["terraform ./roots/app ws=ue1-prod init -input=false"]);
158
+ expect(existsSync(join(p.dir, "roots/app/.ws"))).toBe(false);
159
+ });
160
+
161
+ test("a root that names no workspace runs as before: no select, and no TF_WORKSPACE", async () => {
162
+ const p = project(["app"]);
163
+ const result = await wave(p.dir, spec([{ target: "roots/app" }], { cwd: p.dir }));
164
+ expect(result.exitCode).toBe(0);
165
+ expect(lines(p).some((l) => l.includes(" workspace "))).toBe(false);
166
+ expect(lines(p).every((l) => l.includes(" ws=- "))).toBe(true);
167
+ });
168
+ });
169
+
170
+ describe("where the step looks for chant.config (chant#4854)", () => {
171
+ test("with no cwd in the step, a wave run in process for another directory reads that directory's config", async () => {
172
+ const p = project(["app"], { binary: "tofu" });
173
+ // The process runs elsewhere: this checkout's own chant.config is not the project's.
174
+ expect(process.cwd()).not.toBe(p.dir);
175
+ const result = await wave(p.dir, spec([{ target: "roots/app" }]));
176
+ expect(result.exitCode).toBe(0);
177
+ expect(result.applied).toEqual(["roots/app"]);
178
+ // The project's binary, in the project's directory.
179
+ expect(lines(p).map((l) => l.split(" ").slice(0, 3).join(" "))).toEqual(["tofu ./roots/app ws=-", "tofu ./roots/app ws=-", "tofu ./roots/app ws=-", "tofu ./roots/app ws=-", "tofu ./roots/app ws=-"]);
180
+ });
181
+
182
+ test("a cwd the step names still wins", async () => {
183
+ const p = project(["app"], { binary: "tofu" });
184
+ const other = project(["app"], { binary: "terraform" });
185
+ const result = await wave(p.dir, spec([{ target: "roots/app" }], { cwd: other.dir }));
186
+ expect(result.exitCode).toBe(0);
187
+ // Both fakes log to the last project's file; each line names the directory and binary it ran.
188
+ const ran = lines(other);
189
+ expect(ran.length).toBeGreaterThan(0);
190
+ expect(ran.every((l) => l.startsWith("terraform ./roots/app "))).toBe(true);
191
+ });
192
+ });
193
+
194
+ describe("a root that runs another binary than chant.config names (terragucci issue 1303)", () => {
195
+ test("a product's resolveRoot gives each root its own binary, by path, for its plan, apply and outputs", async () => {
196
+ const p = project(["a", "b"], { binary: "terraform" });
197
+ const asked: string[] = [];
198
+ const resolveRoot: TerraformRootResolver = async (root, input) => {
199
+ asked.push(`${root} ${input.cwd === p.dir ? "at the wave's directory" : String(input.cwd)}`);
200
+ return root === "roots/b" ? { binary: join(p.bin, "tofu") } : {};
201
+ };
202
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot }) as unknown as Capability<never, unknown>);
203
+ const result = await wave(p.dir, spec([{ target: "roots/a" }, { target: "roots/b" }]), { capabilities });
204
+ expect(result.exitCode).toBe(0);
205
+ expect(result.applied).toEqual(["roots/a", "roots/b"]);
206
+ const ran = lines(p).map((l) => l.split(" ").slice(0, 3).join(" ") + " " + l.split(" ")[3]);
207
+ expect(ran).toEqual([
208
+ "terraform ./roots/a ws=- init",
209
+ "terraform ./roots/a ws=- plan",
210
+ "terraform ./roots/a ws=- show",
211
+ "tofu ./roots/b ws=- init",
212
+ "tofu ./roots/b ws=- plan",
213
+ "tofu ./roots/b ws=- show",
214
+ "terraform ./roots/a ws=- apply",
215
+ "terraform ./roots/a ws=- output",
216
+ "tofu ./roots/b ws=- apply",
217
+ "tofu ./roots/b ws=- output",
218
+ ]);
219
+ // Asked once per plan and once per apply, with the step's input as the wave filled it.
220
+ expect(asked).toEqual(["roots/a at the wave's directory", "roots/b at the wave's directory", "roots/a at the wave's directory", "roots/b at the wave's directory"]);
221
+ });
222
+
223
+ test("the change set names the planner each root ran", async () => {
224
+ const p = project(["b"], { binary: "terraform" });
225
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot: () => ({ binary: join(p.bin, "tofu") }) }) as unknown as Capability<never, unknown>);
226
+ const s = spec([{ target: "roots/b" }]);
227
+ const result = await wave(p.dir, s, { capabilities });
228
+ expect(result.exitCode).toBe(0);
229
+ const file = JSON.parse(readFileSync(join(p.dir, opWavePlanPath(s, 1, "roots/b")), "utf-8")) as { changeSet?: { member: { planner: string } } };
230
+ expect(file.changeSet?.member.planner).toBe("tofu");
231
+ });
232
+
233
+ test("a step's own binary runs without a resolver, and a resolver that answers nothing leaves the config's", async () => {
234
+ const p = project(["a"], { binary: "terraform" });
235
+ expect((await wave(p.dir, spec([{ target: "roots/a" }], { binary: "tofu" }))).exitCode).toBe(0);
236
+ expect(lines(p).every((l) => l.startsWith("tofu "))).toBe(true);
237
+ const q = project(["a"], { binary: "terraform" });
238
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot: () => ({}) }) as unknown as Capability<never, unknown>);
239
+ expect((await wave(q.dir, spec([{ target: "roots/a" }]), { capabilities })).exitCode).toBe(0);
240
+ expect(lines(q).every((l) => l.startsWith("terraform "))).toBe(true);
241
+ });
242
+
243
+ test("a resolver that throws fails the root's plan", async () => {
244
+ const p = project(["a"], { binary: "terraform" });
245
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot: () => { throw new Error("tofu 1.9.1 is not installed"); } }) as unknown as Capability<never, unknown>);
246
+ await expect(wave(p.dir, spec([{ target: "roots/a" }]), { capabilities })).rejects.toThrow(/tofu 1.9.1 is not installed/);
247
+ expect(lines(p)).toEqual([]);
248
+ });
249
+ });
250
+
251
+ describe("a root that reads an Atmos instance's outputs (terragucci issue 1303)", () => {
252
+ /** `roots/net`, an instance in workspace ue1-net with a vpc_id, and `roots/app`, which reads it. */
253
+ function instances(netOutputs: string | undefined) {
254
+ const p = project(["net", "app"], { binary: "terraform" });
255
+ writeFileSync(join(p.dir, "roots/net/.ws"), "ue1-net\n");
256
+ if (netOutputs !== undefined) writeFileSync(join(p.dir, "roots/net/outputs-ue1-net.json"), netOutputs);
257
+ const envLog = join(p.dir, "env.log");
258
+ writeFileSync(envLog, "");
259
+ vi.stubEnv("FAKE_ENV_LOG", envLog);
260
+ // As terragucci's edges file lists the read, with where the upstream is and its workspace.
261
+ const reads = atmosStateReads({ reads: [{ var: "vpc_id", upstream: "roots/net", output: ["vpc", "id"], function: "!terraform.state net vpc.id" }] }, (u) => u, (u) => ({ dir: u, workspace: "ue1-net" }));
262
+ const resolveRoot: TerraformRootResolver = (root) => (root === "roots/net" ? { binary: join(p.bin, "tofu"), readEnv: { ROLE: "net-reader" } } : {});
263
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot }) as unknown as Capability<never, unknown>);
264
+ const runHooks: OpWaveRunHooks = { name: "product", beforePlan: () => ({ env: { ROLE: "app" } }) };
265
+ const s = spec([{ target: "roots/app", input: { workspace: "ue1-app", reads } }]);
266
+ return { p, envLog, capabilities, runHooks, s };
267
+ }
268
+
269
+ test("reads the upstream in its own directory and workspace, with its binary and role, and plans on the value", async () => {
270
+ const { p, envLog, capabilities, runHooks, s } = instances('{"vpc":{"value":{"id":"vpc-1"}}}');
271
+ const result = await wave(p.dir, s, { capabilities, runHooks });
272
+ expect(result.exitCode).toBe(0);
273
+ const log = lines(p).map(plain);
274
+ expect(log.slice(0, 3)).toEqual([
275
+ "tofu ./roots/net ws=default init -input=false",
276
+ "tofu ./roots/net ws=- workspace select -or-create=true ue1-net",
277
+ "tofu ./roots/net ws=ue1-net output -json",
278
+ ]);
279
+ expect(log.find((l) => l.includes(" plan "))).toBe("terraform ./roots/app ws=ue1-app plan -input=false -detailed-exitcode -var=vpc_id=vpc-1");
280
+ const roles = readFileSync(envLog, "utf-8").split("\n").filter(Boolean);
281
+ expect(roles.filter((l) => l.startsWith("net "))).toEqual(["net init role=net-reader", "net workspace role=net-reader", "net output role=net-reader"]);
282
+ expect(roles.filter((l) => l.startsWith("app ")).every((l) => l.endsWith("role=app"))).toBe(true);
283
+ });
284
+
285
+ test("an upstream with no state in its workspace yet keeps the root from planning, naming what to apply first", async () => {
286
+ const { p, capabilities, runHooks, s } = instances(undefined);
287
+ await expect(wave(p.dir, s, { capabilities, runHooks })).rejects.toThrow("not planned: it reads vpc_id from roots/net, which has no state yet; apply roots/net first");
288
+ expect(lines(p).some((l) => l.includes("./roots/app "))).toBe(false);
289
+ });
290
+
291
+ test("a read that names a directory or workspace that is not one is refused", async () => {
292
+ const p = project(["app"], { binary: "terraform" });
293
+ await expect(wave(p.dir, spec([{ target: "roots/app", input: { reads: [{ var: "x", root: "roots/net", output: ["x"], workspace: " " }] } }]))).rejects.toThrow("reads[0].workspace must name a workspace");
294
+ });
295
+ });
package/src/index.ts CHANGED
@@ -8,6 +8,11 @@ export { terraformPlugin } from "./plugin";
8
8
  export {
9
9
  terraformCapabilityPlugin,
10
10
  terraformApplyCapability,
11
+ // terragucci issue 1303: a product's resolver of what each root runs.
12
+ terraformApplyCapabilityWith,
13
+ type TerraformApplyDeps,
14
+ type TerraformRootResolver,
15
+ type TerraformRootTools,
11
16
  TerraformPlanMovedError,
12
17
  TERRAFORM_VERB_FAMILIES,
13
18
  // #4455: a choudoufu member's outputs, read for core's `member-outputs`.
@@ -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,36 @@ 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>;
219
+ /**
220
+ * The workspace the root runs in, in place of its entry's (chant#4854),
221
+ * created when the backend does not list it yet. `terraformInit` inits in
222
+ * `default`, which every backend has, then runs `workspace select
223
+ * -or-create=true <workspace>` with `TF_WORKSPACE` unset, since init
224
+ * refuses a selected workspace the backend does not list. Every other
225
+ * command runs with `TF_WORKSPACE` set to it, over `env`. An entry's own
226
+ * `workspace` keeps its old behaviour: `TF_WORKSPACE` on every command,
227
+ * init included, and nothing created.
228
+ */
229
+ workspace?: string;
230
+ /**
231
+ * The binary to run, a name on `PATH` or a path, in place of
232
+ * `terraform.binary` (terragucci issue 1303): a root that pins its own
233
+ * release. Absent: `terraform.binary`.
234
+ */
235
+ binary?: string;
208
236
  }
209
237
 
210
238
  export interface TerraformInitArgs extends TerraformRootArgs {
@@ -608,9 +636,24 @@ export function terraformEnvironment(root?: Pick<TerraformRootConfig, "workspace
608
636
  return {
609
637
  TF_IN_AUTOMATION: "1",
610
638
  ...(root?.workspace ? { TF_WORKSPACE: root.workspace } : {}),
639
+ // chant#4842: the run's own environment, which resolveRoot ties to the root it resolved.
640
+ ...(root ? rootProcessEnv.get(root) : undefined),
641
+ // chant#4854: a workspace the step names wins over the run's environment.
642
+ ...(root && rootOwnWorkspace.has(root) ? { TF_WORKSPACE: rootOwnWorkspace.get(root)! } : {}),
611
643
  };
612
644
  }
613
645
 
646
+ /** The `env` a root was resolved with (chant#4842), by the resolved root's entry, so every command run for it gets it. */
647
+ const rootProcessEnv = new WeakMap<object, Record<string, string>>();
648
+
649
+ /** The workspace a root was resolved with (chant#4854, `TerraformRootArgs.workspace`), which init selects or creates. */
650
+ const rootOwnWorkspace = new WeakMap<object, string>();
651
+
652
+ /** `<binary> workspace select -or-create=true <workspace>` (chant#4854): select the workspace, creating it when the backend does not list it. */
653
+ export function terraformWorkspaceSelectCommand(opts: { binary: string; workspace: string }): string {
654
+ return `${opts.binary} workspace select -or-create=true ${quoteArg(opts.workspace)}`;
655
+ }
656
+
614
657
  /**
615
658
  * `terraform init`, with the root's `backendConfigFiles` as
616
659
  * `-backend-config=<path>` flags and then its `backendConfig` as
@@ -958,7 +1001,12 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
958
1001
  const projectRoot = configPath ? dirname(configPath) : start;
959
1002
  const namespace = (config as { terraform?: TerraformConfig }).terraform;
960
1003
  const roots = namespace?.roots ?? {};
961
- const root = roots[args.root];
1004
+ // chant#4842: a root with no entry and a dir of its own plans that directory.
1005
+ const declared = roots[args.root] ?? (args.dir !== undefined ? { dir: args.dir } : undefined);
1006
+ const own = args.workspace !== undefined && args.workspace !== "" ? args.workspace : undefined;
1007
+ const root = declared && (args.env || own) ? { ...declared, ...(own ? { workspace: own } : {}) } : declared;
1008
+ if (root && args.env) rootProcessEnv.set(root, args.env);
1009
+ if (root && own) rootOwnWorkspace.set(root, own);
962
1010
  if (!root) {
963
1011
  const known = Object.keys(roots).sort();
964
1012
  throw new Error(
@@ -967,7 +1015,7 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
967
1015
  (known.length > 0 ? `known roots: ${known.join(", ")}` : "the namespace declares no roots"),
968
1016
  );
969
1017
  }
970
- const binary = terraformBinary(namespace);
1018
+ const binary = args.binary !== undefined && args.binary !== "" ? args.binary : terraformBinary(namespace);
971
1019
  const dir = resolve(projectRoot, args.dir ?? root.dir);
972
1020
  await ensureChoudoufuVersion(binary, signal);
973
1021
  const estate = binary === "choudoufu" ? detectLiveEstate(dir) : undefined;
@@ -1000,8 +1048,11 @@ async function run(
1000
1048
  dir: string,
1001
1049
  env: Record<string, string>,
1002
1050
  signal?: AbortSignal,
1051
+ unset: readonly string[] = [],
1003
1052
  ): Promise<{ stdout: string; stderr: string }> {
1004
- return execAsync(cmd, { cwd: dir, env: { ...process.env, ...env }, signal, maxBuffer: MAX_BUFFER });
1053
+ const merged: NodeJS.ProcessEnv = { ...process.env, ...env };
1054
+ for (const name of unset) delete merged[name];
1055
+ return execAsync(cmd, { cwd: dir, env: merged, signal, maxBuffer: MAX_BUFFER });
1005
1056
  }
1006
1057
 
1007
1058
  /**
@@ -1053,9 +1104,17 @@ export async function terraformInit(
1053
1104
  const rootPath = workspaceRelativePath(dir);
1054
1105
  const result: TerraformInitResult = { dir, ...(root.workspace ? { workspace: root.workspace } : {}), ...(rootPath !== undefined ? { rootPath } : {}) };
1055
1106
 
1107
+ // chant#4854: a workspace the step names is created after an init in default, which every backend lists.
1108
+ const own = rootOwnWorkspace.get(root);
1056
1109
  let out: { stdout: string; stderr: string };
1057
1110
  try {
1058
- out = await run(cmd, dir, terraformEnvironment(root), signal);
1111
+ out = await run(cmd, dir, own ? { ...terraformEnvironment(root), TF_WORKSPACE: "default" } : terraformEnvironment(root), signal);
1112
+ if (own) {
1113
+ report(out.stdout, out.stderr);
1114
+ // TF_WORKSPACE would override the select, so the binary runs it with none.
1115
+ const { TF_WORKSPACE: _selected, ...selectEnv } = terraformEnvironment(root);
1116
+ out = await run(terraformWorkspaceSelectCommand({ binary, workspace: own }), dir, selectEnv, signal, ["TF_WORKSPACE"]);
1117
+ }
1059
1118
  } catch (err) {
1060
1119
  throw withKnownOutputs(err, result);
1061
1120
  }
@@ -1332,6 +1391,7 @@ export async function choudoufuWaveRoot(
1332
1391
  const resolved = await resolveRoot(args, signal);
1333
1392
  if (resolved.binary !== "choudoufu") return { wave: false, reason: `the root runs ${resolved.binary}` };
1334
1393
  if (!resolved.live) return { wave: false, reason: "the root declares no live estate" };
1394
+ if (args.workspace) return { wave: false, reason: "the step names the root's workspace, which live-plan-set does not select or create" };
1335
1395
  if (args.vars && Object.values(args.vars).some((v) => v !== undefined)) {
1336
1396
  return { wave: false, reason: "the step passes -var values, which live-plan-set does not take" };
1337
1397
  }