@intentius/chant-lexicon-terraform 0.124.79 → 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.
@@ -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`.
@@ -216,6 +216,23 @@ export interface TerraformRootArgs {
216
216
  * `DeployContext.processEnv`.
217
217
  */
218
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;
219
236
  }
220
237
 
221
238
  export interface TerraformInitArgs extends TerraformRootArgs {
@@ -621,12 +638,22 @@ export function terraformEnvironment(root?: Pick<TerraformRootConfig, "workspace
621
638
  ...(root?.workspace ? { TF_WORKSPACE: root.workspace } : {}),
622
639
  // chant#4842: the run's own environment, which resolveRoot ties to the root it resolved.
623
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)! } : {}),
624
643
  };
625
644
  }
626
645
 
627
646
  /** The `env` a root was resolved with (chant#4842), by the resolved root's entry, so every command run for it gets it. */
628
647
  const rootProcessEnv = new WeakMap<object, Record<string, string>>();
629
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
+
630
657
  /**
631
658
  * `terraform init`, with the root's `backendConfigFiles` as
632
659
  * `-backend-config=<path>` flags and then its `backendConfig` as
@@ -976,8 +1003,10 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
976
1003
  const roots = namespace?.roots ?? {};
977
1004
  // chant#4842: a root with no entry and a dir of its own plans that directory.
978
1005
  const declared = roots[args.root] ?? (args.dir !== undefined ? { dir: args.dir } : undefined);
979
- const root = declared && args.env ? { ...declared } : declared;
1006
+ const own = args.workspace !== undefined && args.workspace !== "" ? args.workspace : undefined;
1007
+ const root = declared && (args.env || own) ? { ...declared, ...(own ? { workspace: own } : {}) } : declared;
980
1008
  if (root && args.env) rootProcessEnv.set(root, args.env);
1009
+ if (root && own) rootOwnWorkspace.set(root, own);
981
1010
  if (!root) {
982
1011
  const known = Object.keys(roots).sort();
983
1012
  throw new Error(
@@ -986,7 +1015,7 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
986
1015
  (known.length > 0 ? `known roots: ${known.join(", ")}` : "the namespace declares no roots"),
987
1016
  );
988
1017
  }
989
- const binary = terraformBinary(namespace);
1018
+ const binary = args.binary !== undefined && args.binary !== "" ? args.binary : terraformBinary(namespace);
990
1019
  const dir = resolve(projectRoot, args.dir ?? root.dir);
991
1020
  await ensureChoudoufuVersion(binary, signal);
992
1021
  const estate = binary === "choudoufu" ? detectLiveEstate(dir) : undefined;
@@ -1019,8 +1048,11 @@ async function run(
1019
1048
  dir: string,
1020
1049
  env: Record<string, string>,
1021
1050
  signal?: AbortSignal,
1051
+ unset: readonly string[] = [],
1022
1052
  ): Promise<{ stdout: string; stderr: string }> {
1023
- 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 });
1024
1056
  }
1025
1057
 
1026
1058
  /**
@@ -1072,9 +1104,17 @@ export async function terraformInit(
1072
1104
  const rootPath = workspaceRelativePath(dir);
1073
1105
  const result: TerraformInitResult = { dir, ...(root.workspace ? { workspace: root.workspace } : {}), ...(rootPath !== undefined ? { rootPath } : {}) };
1074
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);
1075
1109
  let out: { stdout: string; stderr: string };
1076
1110
  try {
1077
- 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
+ }
1078
1118
  } catch (err) {
1079
1119
  throw withKnownOutputs(err, result);
1080
1120
  }
@@ -1351,6 +1391,7 @@ export async function choudoufuWaveRoot(
1351
1391
  const resolved = await resolveRoot(args, signal);
1352
1392
  if (resolved.binary !== "choudoufu") return { wave: false, reason: `the root runs ${resolved.binary}` };
1353
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" };
1354
1395
  if (args.vars && Object.values(args.vars).some((v) => v !== undefined)) {
1355
1396
  return { wave: false, reason: "the step passes -var values, which live-plan-set does not take" };
1356
1397
  }
@@ -12,6 +12,14 @@
12
12
  * as `-var`, under the step's own `vars`, which win. A read whose upstream
13
13
  * has no state, or no such output yet, waits: the root is not planned on a
14
14
  * stand-in, and the wave says which upstream to apply first.
15
+ *
16
+ * An Atmos instance is read the way terragucci's `upstreamOutputs` reads it
17
+ * (terragucci issue 1303): `dir` names its directory when it has no
18
+ * `terraform.roots` entry, and `workspace` the workspace its state is in, so
19
+ * the read inits in `default`, selects or creates the workspace with
20
+ * `TF_WORKSPACE` unset, then runs `output -json` in it. A product's
21
+ * `resolveRoot` answers the binary and the environment (its role) the read
22
+ * runs with, never the reader's.
15
23
  */
16
24
 
17
25
  /** One var a root reads from another root's state. */
@@ -22,6 +30,10 @@ export interface TerraformStateRead {
22
30
  root: string;
23
31
  /** The output, then the keys into its value. */
24
32
  output: string[];
33
+ /** The upstream's directory, from the project root, when it has no `terraform.roots` entry (terragucci issue 1303). */
34
+ dir?: string;
35
+ /** The workspace the upstream's state is in (terragucci issue 1303), selected or created after an init in `default`. */
36
+ workspace?: string;
25
37
  }
26
38
 
27
39
  /** What filling a root's reads came to. */
@@ -51,6 +63,8 @@ export function stateReadsProblems(reads: unknown): string[] {
51
63
  else vars.add(read.var);
52
64
  if (typeof read.root !== "string" || read.root === "") problems.push(`reads[${i}].root must name a terraform.roots entry`);
53
65
  if (!Array.isArray(read.output) || read.output.length === 0 || !read.output.every((k) => typeof k === "string" && k !== "")) problems.push(`reads[${i}].output must be the output's name, then any keys into its value`);
66
+ if (read.dir !== undefined && (typeof read.dir !== "string" || read.dir === "")) problems.push(`reads[${i}].dir must name the upstream's directory`);
67
+ if (read.workspace !== undefined && (typeof read.workspace !== "string" || read.workspace.trim() === "")) problems.push(`reads[${i}].workspace must name a workspace`);
54
68
  }
55
69
  return problems;
56
70
  }
@@ -71,11 +85,11 @@ export function readOutputValue(outputs: Record<string, unknown>, output: readon
71
85
  * Fill `reads` from each upstream's outputs, read once per upstream through
72
86
  * `outputsOf`, which throws when the state cannot be read.
73
87
  */
74
- export async function fillStateReads(reads: readonly TerraformStateRead[], outputsOf: (root: string) => Promise<Record<string, unknown>>): Promise<FilledStateReads> {
88
+ export async function fillStateReads(reads: readonly TerraformStateRead[], outputsOf: (root: string, read: TerraformStateRead) => Promise<Record<string, unknown>>): Promise<FilledStateReads> {
75
89
  const result: FilledStateReads = { vars: {}, waiting: [], errors: [] };
76
90
  const cache = new Map<string, Promise<Record<string, unknown>>>();
77
91
  for (const read of reads) {
78
- if (!cache.has(read.root)) cache.set(read.root, outputsOf(read.root));
92
+ if (!cache.has(read.root)) cache.set(read.root, outputsOf(read.root, read));
79
93
  let outputs: Record<string, unknown>;
80
94
  try {
81
95
  outputs = await cache.get(read.root)!;
@@ -103,14 +117,18 @@ export function waitingReadsReason(waiting: FilledStateReads["waiting"]): string
103
117
  * The reads an Atmos instance's edges file lists (`{ reads: [{ var,
104
118
  * upstream, output }] }`, as terragucci's `atmos write` leaves beside each
105
119
  * instance), with each upstream mapped to its `terraform.roots` entry by
106
- * `rootOf` (by default the upstream's own path).
120
+ * `rootOf` (by default the upstream's own path). `at` answers, per
121
+ * upstream, its directory and the workspace its state is in (terragucci
122
+ * issue 1303), which the read inits and selects.
107
123
  */
108
- export function atmosStateReads(edges: unknown, rootOf: (upstream: string) => string = (u) => u): TerraformStateRead[] {
124
+ export function atmosStateReads(edges: unknown, rootOf: (upstream: string) => string = (u) => u, at?: (upstream: string) => { dir?: string; workspace?: string }): TerraformStateRead[] {
109
125
  const reads = (edges as { reads?: unknown } | null)?.reads;
110
126
  if (!Array.isArray(reads)) return [];
111
127
  return reads.flatMap((r) => {
112
128
  const read = r as { var?: unknown; upstream?: unknown; output?: unknown } | null;
113
129
  if (!read || typeof read.var !== "string" || typeof read.upstream !== "string" || !Array.isArray(read.output) || !read.output.every((k) => typeof k === "string")) return [];
114
- return [{ var: read.var, root: rootOf(read.upstream), output: read.output as string[] }];
130
+ // terragucci issue 1303: where the upstream instance is and which workspace holds its state, when the caller knows.
131
+ const where = at?.(read.upstream);
132
+ return [{ var: read.var, root: rootOf(read.upstream), output: read.output as string[], ...(where?.dir ? { dir: where.dir } : {}), ...(where?.workspace ? { workspace: where.workspace } : {}) }];
115
133
  });
116
134
  }
@@ -325,3 +325,24 @@ describe("a unit whose plan changes only outputs (#4849)", () => {
325
325
  expect(changing.digest).toBe((await decisionOf("all")).digest);
326
326
  });
327
327
  });
328
+
329
+ describe("terragrunt-apply runs where the wave runs (chant#4854)", () => {
330
+ it("with no dir in the step, a wave run in process for another directory finds, plans and applies there", async () => {
331
+ const dir = tmp();
332
+ const calls: Array<Call & { cwd: string }> = [];
333
+ const inner = stub(calls);
334
+ const exec: TerragruntExec = (f, args, o) => {
335
+ const at = calls.length;
336
+ return inner(f, args, o).then((r) => {
337
+ calls[at]!.cwd = o.cwd;
338
+ return r;
339
+ });
340
+ };
341
+ const s = spec(dir);
342
+ const { dir: _dir, ...step } = s.step as Record<string, unknown>;
343
+ const result = await wave({ ...s, step: step as OpWavesSpec["step"] }, dir, exec);
344
+ expect(result.exitCode).toBe(0);
345
+ expect(calls.length).toBeGreaterThan(0);
346
+ expect(new Set(calls.map((c) => c.cwd))).toEqual(new Set([dir]));
347
+ });
348
+ });
@@ -217,6 +217,9 @@ const DEFAULT_WORK_DIR = ".chant/terragrunt-apply";
217
217
 
218
218
  const dirOf = (input: TerragruntApplyStepInput): string => input.dir ?? process.cwd();
219
219
 
220
+ /** The step's input with the wave's directory as its `dir` when it names none (chant#4854). */
221
+ const atDir = <T extends TerragruntApplyStepInput | undefined>(input: T, cwd: string | undefined): T => (input && input.dir === undefined && cwd !== undefined ? { ...input, dir: cwd } : input);
222
+
220
223
  /** The work directory of a `run --all` over `units`: one per set of units, so a wave and a unit of it never share one. */
221
224
  function workDirOf(input: TerragruntApplyStepInput, units: readonly string[]): string {
222
225
  const key = createHash("sha256").update(JSON.stringify([...units].sort())).digest("hex").slice(0, 16);
@@ -232,7 +235,7 @@ function withEnv(exec: TerragruntExec, env: Record<string, string> | undefined):
232
235
  /** The settings every unit of one `run --all` shares; throws when members disagree. */
233
236
  function shared(members: readonly GroupMember<TerragruntApplyStepInput>[]): { input: TerragruntApplyStepInput; units: string[]; env?: Record<string, string> } {
234
237
  if (members.length === 0) throw new Error("terragrunt-apply: a wave needs at least one unit");
235
- const inputs = members.map((m) => m.input ?? (m.step as unknown as TerragruntApplyStepInput));
238
+ const inputs = members.map((m) => atDir(m.input ?? (m.step as unknown as TerragruntApplyStepInput), m.ctx.cwd));
236
239
  for (const i of inputs) if (typeof i.unit !== "string" || i.unit === "") throw new Error("terragrunt-apply: each run names its unit");
237
240
  const first = inputs[0]!;
238
241
  const key = (i: TerragruntApplyStepInput) => JSON.stringify([i.dir, i.terragrunt, i.binary, i.parallelism, i.workDir, i.destroy === true]);
@@ -350,7 +353,8 @@ export function terragruntApplyCapability(deps: TerragruntApplyDeps = {}): Capab
350
353
  // A unit plans and applies as a whole, and Terragrunt takes each unit's own backend lock.
351
354
  defaultLockGrain: () => "target",
352
355
 
353
- async discoverRuns(ctx: RunDiscoveryContext, input: TerragruntApplyStepInput): Promise<DiscoveredRuns> {
356
+ async discoverRuns(ctx: RunDiscoveryContext, given: TerragruntApplyStepInput): Promise<DiscoveredRuns> {
357
+ const input = atDir(given, ctx.cwd);
354
358
  const found = await discoverTerragruntUnits({
355
359
  dir: dirOf(input),
356
360
  exec,
@@ -362,14 +366,16 @@ export function terragruntApplyCapability(deps: TerragruntApplyDeps = {}): Capab
362
366
  return { targets: waves[ctx.wave.number - 1] ?? [], waves: waves.length };
363
367
  },
364
368
 
365
- async plan(ctx: DeployContext, input: TerragruntApplyStepInput): Promise<CapabilityPlan> {
369
+ async plan(ctx: DeployContext, given: TerragruntApplyStepInput): Promise<CapabilityPlan> {
370
+ const input = atDir(given, ctx.cwd);
366
371
  if (typeof input.unit !== "string" || input.unit === "") throw new Error("terragrunt-apply: the step names no unit");
367
372
  await ctx.afterInit?.();
368
373
  const { plans } = await planUnits(exec, await unitInput(resolveUnit, input, input.unit), [input.unit], ctx.processEnv);
369
374
  return plans.get(input.unit)!;
370
375
  },
371
376
 
372
- async run(ctx: DeployContext, input: TerragruntApplyStepInput): Promise<TerragruntApplyStepOutput> {
377
+ async run(ctx: DeployContext, step: TerragruntApplyStepInput): Promise<TerragruntApplyStepOutput> {
378
+ const input = atDir(step, ctx.cwd);
373
379
  const given = ctx.plans?.[input.unit];
374
380
  const own = await unitInput(resolveUnit, input, input.unit);
375
381
  // A unit with no plan from the wave plans now and applies that plan.