@intentius/chant-lexicon-terraform 0.124.80 → 0.124.81

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.
@@ -132,6 +132,14 @@
132
132
  * plans the step's `dir` as its directory, so a product can name each root
133
133
  * by its path without writing an entry for it.
134
134
  *
135
+ * A wave that plans and applies several roots at once (chant#4864) gives
136
+ * each run its serial section (`DeployContext.serial`). Every `init` the
137
+ * step runs, a root's own and an upstream's for its `reads`, takes its turn
138
+ * there (`../init-turn.ts`): one at a time among the runs that share a
139
+ * provider cache, else one at a time per directory. A choudoufu one-root
140
+ * set's `live-plan-set` inits as it plans, so with a shared cache the whole
141
+ * command takes the turn.
142
+ *
135
143
  * Every plan also writes its `show -json` beside the plan file and names it
136
144
  * as `artifact.planJson`, the document a wave's cost hold prices
137
145
  * (`../cost-hold.ts`).
@@ -1 +1 @@
1
- {"version":3,"file":"capability-plugin.d.ts","sourceRoot":"","sources":["../../src/components/capability-plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyIG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAiC,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAExH,OAAO,EAIL,KAAK,iBAAiB,EACvB,MAAM,2CAA2C,CAAC;AAInD,OAAO,EAAqB,KAAK,gBAAgB,EAAE,MAAM,+CAA+C,CAAC;AACzG,OAAO,KAAK,EAAmB,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,kDAAkD,CAAC;AA0BlI,OAAO,EAA0D,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAKjH,0CAA0C;AAC1C,MAAM,WAAW,uBAAuB;IACtC,iJAAiJ;IACjJ,IAAI,EAAE,MAAM,CAAC;IAC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AE,MAAM,EAAE,OAAO,EAAE,MAAM,EAGxD;CACF;AA6DD;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,GAAE,kBAAuB,GAAG,UAAU,CAAC,uBAAuB,EAAE,wBAAwB,CAAC,CA0IzI;AAED,wFAAwF;AACxF,eAAO,MAAM,wBAAwB,EAAE,UAAU,CAAC,uBAAuB,EAAE,wBAAwB,CAAkC,CAAC;AAEtI,eAAO,MAAM,uBAAuB;aAClC,KAAK,YAAG,iBAAiB;aAEzB,KAAK,YAAG,wBAAwB,EAAE,sBAAsB;aACxD,IAAI,YAAG,gBAAgB;aAEvB,MAAM,YAAG,kBAAkB;CACnB,CAAC;AAEX,eAAO,MAAM,yBAAyB,EAAE,gBAoBvC,CAAC"}
@@ -132,6 +132,14 @@
132
132
  * plans the step's `dir` as its directory, so a product can name each root
133
133
  * by its path without writing an entry for it.
134
134
  *
135
+ * A wave that plans and applies several roots at once (chant#4864) gives
136
+ * each run its serial section (`DeployContext.serial`). Every `init` the
137
+ * step runs, a root's own and an upstream's for its `reads`, takes its turn
138
+ * there (`../init-turn.ts`): one at a time among the runs that share a
139
+ * provider cache, else one at a time per directory. A choudoufu one-root
140
+ * set's `live-plan-set` inits as it plans, so with a shared cache the whole
141
+ * command takes the turn.
142
+ *
135
143
  * Every plan also writes its `show -json` beside the plan file and names it
136
144
  * as `artifact.planJson`, the document a wave's cost hold prices
137
145
  * (`../cost-hold.ts`).
@@ -161,6 +169,7 @@ import { fillStateReads, stateReadsProblems, waitingReadsReason } from "../state
161
169
  import { terraformStateOpCapabilities } from "../state-ops.js";
162
170
  import { terraformActionCapability } from "../action-ops.js";
163
171
  import { terragruntApply } from "../terragrunt/capability.js";
172
+ import { inInitTurn, pluginCacheDir } from "../init-turn.js";
164
173
  /** The lock timeout a step's plan and apply pass (#4545). */
165
174
  function lockTimeoutOf(input) {
166
175
  if (input.lockTimeout === false)
@@ -175,7 +184,7 @@ function lockTimeoutOf(input) {
175
184
  * read once, the values under the step's own `vars`. `waiting` when an
176
185
  * upstream has no such output yet. Throws when a state cannot be read.
177
186
  */
178
- async function withReads(input, resolveRoot) {
187
+ async function withReads(input, resolveRoot, serial) {
179
188
  const reads = input.reads ?? [];
180
189
  if (reads.length === 0)
181
190
  return { input };
@@ -193,7 +202,8 @@ async function withReads(input, resolveRoot) {
193
202
  ...(tools?.binary ? { binary: tools.binary } : {}),
194
203
  ...(tools?.readEnv ? { env: tools.readEnv } : {}),
195
204
  };
196
- await terraformInit(at);
205
+ // chant#4864: runs that plan at once may read the same upstream, so its init takes a turn.
206
+ await inInitTurn(serial, at.env, initDir(at), () => terraformInit(at));
197
207
  return terraformOutputs(at);
198
208
  });
199
209
  if (filled.errors.length > 0)
@@ -219,6 +229,10 @@ const rootArgs = (input, env) => ({
219
229
  ...(input.workspace ? { workspace: input.workspace } : {}),
220
230
  ...(input.binary ? { binary: input.binary } : {}),
221
231
  });
232
+ /** The directory an init runs in, as far as the step's input says, for its turn when no provider cache is shared (chant#4864). */
233
+ const initDir = (at) => join(at.cwd ?? process.cwd(), at.dir ?? at.root);
234
+ /** `terraform init` of `input`'s root in its turn (chant#4864). */
235
+ const initRoot = (input, env, serial) => inInitTurn(serial, env, initDir(rootArgs(input, env)), () => terraformInit(rootArgs(input, env)));
222
236
  function requireRoot(input) {
223
237
  if (typeof input.root !== "string" || input.root === "") {
224
238
  throw new Error('terraform-apply: "root" is required and names an entry in terraform.roots');
@@ -232,7 +246,7 @@ function requireRoot(input) {
232
246
  }
233
247
  async function planRoot(input, targets, at = {}) {
234
248
  requireRoot(input);
235
- await terraformInit(rootArgs(input, at.env));
249
+ await initRoot(input, at.env, at.serial);
236
250
  // chant#4842: the run hook's check, once init fetched the modules and providers and before the plan runs their code.
237
251
  await at.afterInit?.();
238
252
  // One `show -json` per plan (#4479): its document is the digest, the change
@@ -423,7 +437,7 @@ async function previewRoot(ctx, input, unresolved, targets) {
423
437
  return waitingRoot(ctx, input, unresolved, stand);
424
438
  let planned;
425
439
  try {
426
- await terraformInit(rootArgs(input, ctx.processEnv));
440
+ await initRoot(input, ctx.processEnv, ctx.serial);
427
441
  await ctx.afterInit?.();
428
442
  planned = await terraformPlan({
429
443
  ...rootArgs(input, ctx.processEnv),
@@ -518,8 +532,8 @@ async function runChoudoufuWave(ctx, input, planned) {
518
532
  return { root: input.root, planDigest: planned.planDigest, fromWavePlan: true, outputs };
519
533
  }
520
534
  /** The input a `run` that plans for itself plans with (#4545): its reads filled, or an error naming the upstream to apply first. */
521
- async function readsForRun(input, resolveRoot) {
522
- const read = await withReads(input, resolveRoot);
535
+ async function readsForRun(input, resolveRoot, serial) {
536
+ const read = await withReads(input, resolveRoot, serial);
523
537
  if ("waiting" in read)
524
538
  throw new Error(`terraform-apply: root "${input.root}": ${read.waiting}`);
525
539
  return read.input;
@@ -558,7 +572,7 @@ export function terraformApplyCapabilityWith(deps = {}) {
558
572
  const given = await filledInput(deps.resolveRoot, ctx, step);
559
573
  requireRoot(given);
560
574
  // #4545: a root reading an upstream's output that is not there yet is a hole naming the upstream.
561
- const read = await withReads(given, deps.resolveRoot);
575
+ const read = await withReads(given, deps.resolveRoot, ctx.serial);
562
576
  if ("waiting" in read)
563
577
  return waitingOnReads(ctx, given, read);
564
578
  const { input } = read;
@@ -572,7 +586,8 @@ export function terraformApplyCapabilityWith(deps = {}) {
572
586
  if (wave.wave) {
573
587
  // chant#4842: live-plan-set inits and plans in one command, so the run hook's check comes before it.
574
588
  await ctx.afterInit?.();
575
- const set = await choudoufuPlanSet(rootArgs(input, env));
589
+ // chant#4864: live-plan-set inits too, so with a shared provider cache the whole command takes the init's turn.
590
+ const set = await inInitTurn(pluginCacheDir(env) ? ctx.serial : undefined, env, undefined, () => choudoufuPlanSet(rootArgs(input, env)));
576
591
  const artifact = {
577
592
  planDigest: set.planDigest,
578
593
  choudoufu: { planSetFile: set.planSetFile, setDigest: set.setDigest },
@@ -580,7 +595,7 @@ export function terraformApplyCapabilityWith(deps = {}) {
580
595
  };
581
596
  return { member: input.root, planDigest: set.planDigest, artifact, changeSet: set.changeSet, outputs: terraformPlannedOutputs(set.json), shape: terraformPlanShape(set.json, input.root) };
582
597
  }
583
- const { changeSet, json, ...planned } = await planRoot(input, targets, { env, ...(ctx.afterInit ? { afterInit: ctx.afterInit } : {}) });
598
+ const { changeSet, json, ...planned } = await planRoot(input, targets, { env, ...(ctx.afterInit ? { afterInit: ctx.afterInit } : {}), ...(ctx.serial ? { serial: ctx.serial } : {}) });
584
599
  // #4552: a choudoufu root's rows, keyed from the plan document while it is at hand.
585
600
  const lockRows = changeSet?.member.planner === "choudoufu" ? terraformLockRows(input.root, changeSet.member.scope, json) : undefined;
586
601
  return {
@@ -600,7 +615,7 @@ export function terraformApplyCapabilityWith(deps = {}) {
600
615
  async outputs(ctx, step) {
601
616
  const input = await filledInput(deps.resolveRoot, ctx, step);
602
617
  requireRoot(input);
603
- await terraformInit(rootArgs(input, ctx.processEnv));
618
+ await initRoot(input, ctx.processEnv, ctx.serial);
604
619
  return terraformOutputs(rootArgs(input, ctx.processEnv));
605
620
  },
606
621
  // #4447 (ws-107): a group core forms of components that read each other plans and applies as one composed run
@@ -656,7 +671,7 @@ export function terraformApplyCapabilityWith(deps = {}) {
656
671
  if (isShapeArtifact(fromWave)) {
657
672
  // A root previewed with stand-ins (#4463): plan it now with the real
658
673
  // values, and apply that plan only when it is within the preview's shape.
659
- const fresh = await planRoot(await readsForRun(input, deps.resolveRoot), ctx.targets, { env: ctx.processEnv });
674
+ const fresh = await planRoot(await readsForRun(input, deps.resolveRoot, ctx.serial), ctx.targets, { env: ctx.processEnv, ...(ctx.serial ? { serial: ctx.serial } : {}) });
660
675
  const differences = planShapeDifferences(fromWave.shape, terraformPlanShape(fresh.json, input.root));
661
676
  if (differences.length > 0)
662
677
  throw new PlanShapeMovedError(input.root, differences);
@@ -669,7 +684,7 @@ export function terraformApplyCapabilityWith(deps = {}) {
669
684
  }
670
685
  const planned = isPlannedRoot(fromWave)
671
686
  ? fromWave
672
- : await planRoot(await readsForRun(input, deps.resolveRoot), ctx.targets, { env: ctx.processEnv }).then(({ planFile, planDigest }) => ({ planFile, planDigest }));
687
+ : await planRoot(await readsForRun(input, deps.resolveRoot, ctx.serial), ctx.targets, { env: ctx.processEnv, ...(ctx.serial ? { serial: ctx.serial } : {}) }).then(({ planFile, planDigest }) => ({ planFile, planDigest }));
673
688
  const applied = await terraformApply({ ...rootArgs(input, ctx.processEnv), planFile: planned.planFile, ...lockTimeoutOf(input) });
674
689
  if (!applied.applied) {
675
690
  const message = `terraform-apply: root "${input.root}" refused the plan it was given (${applied.refused ?? "refused"}). ` +
@@ -0,0 +1,21 @@
1
+ /**
2
+ * An init's turn (chant#4864). An op wave with `parallelism` over 1 plans
3
+ * and applies several roots at once and gives each run the wave's serial
4
+ * section (`DeployContext.serial`). Terraform's provider cache
5
+ * (`TF_PLUGIN_CACHE_DIR`) is not safe for two inits writing it at once, so
6
+ * `terraform-apply` and `terragrunt-apply` take their inits in the section,
7
+ * keyed on the cache directory: one init at a time among the runs that share
8
+ * it, their plans and applies side by side. Without a cache, `terraform-apply`
9
+ * keys an init on the directory it runs in, since two runs reading the same
10
+ * upstream would init it at once. Without a section (runs one at a time)
11
+ * nothing changes.
12
+ */
13
+ /** A section as `DeployContext.serial` gives it. */
14
+ export type InitSection = <T>(key: string, fn: () => Promise<T>) => Promise<T>;
15
+ /** The provider cache a run's inits write: its own environment's `TF_PLUGIN_CACHE_DIR`, else the job's. Undefined when neither names one. */
16
+ export declare function pluginCacheDir(env?: Record<string, string | undefined>): string | undefined;
17
+ /** The key an init takes its turn under: the provider cache, else `dir` when given. Undefined when it needs no turn. */
18
+ export declare function initTurnKey(env: Record<string, string | undefined> | undefined, dir?: string): string | undefined;
19
+ /** Run `fn`, an init, in its turn: inside `serial` under {@link initTurnKey}, or as it is when there is no section or no key. */
20
+ export declare function inInitTurn<T>(serial: InitSection | undefined, env: Record<string, string | undefined> | undefined, dir: string | undefined, fn: () => Promise<T>): Promise<T>;
21
+ //# sourceMappingURL=init-turn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init-turn.d.ts","sourceRoot":"","sources":["../src/init-turn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,oDAAoD;AACpD,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;AAE/E,6IAA6I;AAC7I,wBAAgB,cAAc,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GAAG,MAAM,GAAG,SAAS,CAG3F;AAED,wHAAwH;AACxH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GAAG,SAAS,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIjH;AAED,iIAAiI;AACjI,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,GAAG,SAAS,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GAAG,SAAS,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAG7K"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * An init's turn (chant#4864). An op wave with `parallelism` over 1 plans
3
+ * and applies several roots at once and gives each run the wave's serial
4
+ * section (`DeployContext.serial`). Terraform's provider cache
5
+ * (`TF_PLUGIN_CACHE_DIR`) is not safe for two inits writing it at once, so
6
+ * `terraform-apply` and `terragrunt-apply` take their inits in the section,
7
+ * keyed on the cache directory: one init at a time among the runs that share
8
+ * it, their plans and applies side by side. Without a cache, `terraform-apply`
9
+ * keys an init on the directory it runs in, since two runs reading the same
10
+ * upstream would init it at once. Without a section (runs one at a time)
11
+ * nothing changes.
12
+ */
13
+ import { resolve } from "node:path";
14
+ /** The provider cache a run's inits write: its own environment's `TF_PLUGIN_CACHE_DIR`, else the job's. Undefined when neither names one. */
15
+ export function pluginCacheDir(env) {
16
+ const dir = env && "TF_PLUGIN_CACHE_DIR" in env ? env.TF_PLUGIN_CACHE_DIR : process.env.TF_PLUGIN_CACHE_DIR;
17
+ return dir ? resolve(dir) : undefined;
18
+ }
19
+ /** The key an init takes its turn under: the provider cache, else `dir` when given. Undefined when it needs no turn. */
20
+ export function initTurnKey(env, dir) {
21
+ const cache = pluginCacheDir(env);
22
+ if (cache)
23
+ return `terraform-plugin-cache:${cache}`;
24
+ return dir ? `terraform-init:${resolve(dir)}` : undefined;
25
+ }
26
+ /** Run `fn`, an init, in its turn: inside `serial` under {@link initTurnKey}, or as it is when there is no section or no key. */
27
+ export function inInitTurn(serial, env, dir, fn) {
28
+ const key = serial ? initTurnKey(env, dir) : undefined;
29
+ return key ? serial(key, fn) : fn();
30
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "algorithm": "sha256",
3
3
  "artifacts": {
4
- "manifest.json": "b42ddf27a61a74080d319f35a000c6dc848fb2125af048e061145e2153bea323",
4
+ "manifest.json": "0d331c704974be87668cb47e091595c0bc1b14ac7304adb0d9015a5733a970db",
5
5
  "meta.json": "ca3d163bab055381827226140568f3bef7eaac187cebd76878e0b63e9e442356",
6
6
  "types/index.d.ts": "022927e6a7b351cc4cf52d5f973f60ccd140e5810e17b32fb8868965aa1db161",
7
7
  "rules/plan-before-apply.ts": "7c10245b2c3335253b48ee7d8b075d89f43116bde20313a5d1031b343fef1282",
@@ -59,5 +59,5 @@
59
59
  "rules/tf047.ts": "bfa86f85c4c5efff297280fc2f15104d6319fa96dfc444a31829fb7353224092",
60
60
  "skills/chant-terraform.md": "53dd11c7b289652f92fdaf2c03766abda46d8964e31fe709e8b142ced4f19c50"
61
61
  },
62
- "composite": "5331a031c96dd021a1e4a58dde2ae4d326972fb8eff323f790a7d5682b62c1ef"
62
+ "composite": "ffdfe12294d137e094ab2e741e791ac6d2ec300ed861730dab17e687a5411a91"
63
63
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "terraform",
3
- "version": "0.124.80",
3
+ "version": "0.124.81",
4
4
  "chantVersion": ">=0.1.0",
5
5
  "namespace": "Terraform"
6
6
  }
@@ -27,6 +27,11 @@
27
27
  * A run hook's `processEnv` is laid over the environment Terragrunt runs
28
28
  * with; the units of one `run --all` must agree on it. Terragrunt has no
29
29
  * init of its own before the plan, so `afterInit` is called before it plans.
30
+ * A wave that plans its units run by run several at once (chant#4864) gives
31
+ * each its serial section (`DeployContext.serial`): with a provider cache
32
+ * (`TF_PLUGIN_CACHE_DIR`) a unit's mock check and an explicit `run --all --
33
+ * init` take their turn there, one unit at a time, and its plan runs beside
34
+ * the others'.
30
35
  * A product passes its own `exec` (`terragruntApplyCapability`) to wrap
31
36
  * Terragrunt, as terragucci adds its lock timeout and its backend bootstrap.
32
37
  *
@@ -1 +1 @@
1
- {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../../src/terragrunt/capability.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAoJ,MAAM,wCAAwC,CAAC;AAG3N,OAAO,EAAkH,KAAK,cAAc,EAAE,MAAM,OAAO,CAAC;AAG5J,sGAAsG;AACtG,MAAM,WAAW,wBAAwB;IACvC,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;IACb,8EAA8E;IAC9E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,mHAAm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CAAC,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,+BAA+B,CAAA;KAAE,CAAC;CAC3E;AAsBD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAmBhG;AAED,MAAM,WAAW,gCAAgC;IAC/C,4GAA4G;IAC5G,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,GAAG,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtG,+GAA+G;IAC/G,OAAO,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,UAAU,GAAG,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;CAC9D;AAED,0JAA0J;AAC1J,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,gCAAgC,GAAG,sBAAsB,CAkB3G;AAuID,+EAA+E;AAC/E,wBAAgB,yBAAyB,CAAC,IAAI,GAAE,mBAAwB,GAAG,UAAU,CAAC,wBAAwB,EAAE,yBAAyB,CAAC,CAuFzI;AAED,kDAAkD;AAClD,eAAO,MAAM,eAAe,EAAE,UAAU,CAAC,wBAAwB,EAAE,yBAAyB,CAA+B,CAAC"}
@@ -27,6 +27,11 @@
27
27
  * A run hook's `processEnv` is laid over the environment Terragrunt runs
28
28
  * with; the units of one `run --all` must agree on it. Terragrunt has no
29
29
  * init of its own before the plan, so `afterInit` is called before it plans.
30
+ * A wave that plans its units run by run several at once (chant#4864) gives
31
+ * each its serial section (`DeployContext.serial`): with a provider cache
32
+ * (`TF_PLUGIN_CACHE_DIR`) a unit's mock check and an explicit `run --all --
33
+ * init` take their turn there, one unit at a time, and its plan runs beside
34
+ * the others'.
30
35
  * A product passes its own `exec` (`terragruntApplyCapability`) to wrap
31
36
  * Terragrunt, as terragucci adds its lock timeout and its backend bootstrap.
32
37
  *
@@ -47,6 +52,7 @@ import { basename, join } from "node:path";
47
52
  import { computePlanDigest } from "@intentius/chant/lifecycle/plan-digest";
48
53
  import { applyTerragruntWave, defaultTerragruntExec, discoverTerragruntUnits, planTerragruntWave, TerragruntMockRefusal } from "./run.js";
49
54
  import { terragruntListedWaves, terragruntWaves } from "./units.js";
55
+ import { initTurnKey } from "../init-turn.js";
50
56
  /** The binaries a unit may pin a release of, and the file that pins each. */
51
57
  const VERSION_FILES = { tofu: ".opentofu-version", terraform: ".terraform-version" };
52
58
  const RELEASE = /^\d+\.\d+\.\d+(-[0-9A-Za-z.]+)?$/;
@@ -175,10 +181,13 @@ function unitPlans(parts, units, workDir, dir) {
175
181
  throw new Error(`terragrunt-apply: Terragrunt planned no ${missing.join(", ")}`);
176
182
  return out;
177
183
  }
178
- async function planUnits(exec, input, units, env) {
184
+ async function planUnits(exec, input, units, env, serial) {
179
185
  const dir = dirOf(input);
180
186
  const workDir = workDirOf(input, units);
187
+ // chant#4864: units planned at once that share a provider cache take turns at their inits.
188
+ const cache = serial ? initTurnKey(env) : undefined;
181
189
  const planned = await planTerragruntWave({
190
+ ...(cache ? { initTurn: (fn) => serial(cache, fn) } : {}),
182
191
  dir,
183
192
  units,
184
193
  workDir,
@@ -282,7 +291,7 @@ export function terragruntApplyCapability(deps = {}) {
282
291
  if (typeof input.unit !== "string" || input.unit === "")
283
292
  throw new Error("terragrunt-apply: the step names no unit");
284
293
  await ctx.afterInit?.();
285
- const { plans } = await planUnits(exec, await unitInput(resolveUnit, input, input.unit), [input.unit], ctx.processEnv);
294
+ const { plans } = await planUnits(exec, await unitInput(resolveUnit, input, input.unit), [input.unit], ctx.processEnv, ctx.serial);
286
295
  return plans.get(input.unit);
287
296
  },
288
297
  async run(ctx, step) {
@@ -290,7 +299,7 @@ export function terragruntApplyCapability(deps = {}) {
290
299
  const given = ctx.plans?.[input.unit];
291
300
  const own = await unitInput(resolveUnit, input, input.unit);
292
301
  // A unit with no plan from the wave plans now and applies that plan.
293
- const workDir = isUnitArtifact(given) ? given.workDir : (await planUnits(exec, own, [input.unit], ctx.processEnv)).workDir;
302
+ const workDir = isUnitArtifact(given) ? given.workDir : (await planUnits(exec, own, [input.unit], ctx.processEnv, ctx.serial)).workDir;
294
303
  const applied = await applyUnits(exec, own, [input.unit], workDir, ctx.processEnv);
295
304
  const result = applied.results.find((r) => r.unit === input.unit);
296
305
  if (result?.status !== "succeeded")
@@ -67,6 +67,14 @@ export interface TerragruntWaveRunInput extends TerragruntRunOptions {
67
67
  * are never applied. Unset, a wave that would read a mock is refused.
68
68
  */
69
69
  provisional?: boolean;
70
+ /**
71
+ * The plan's turn at the provider cache (chant#4864), when units planned
72
+ * by other calls at once share it. Given, the mock check and an explicit
73
+ * `run --all -- init` of the units run inside it, and the plan after it,
74
+ * so its auto-init finds them initialised. An init that fails is left to
75
+ * the plan, which fails the unit with its report row as before.
76
+ */
77
+ initTurn?: <T>(fn: () => Promise<T>) => Promise<T>;
70
78
  }
71
79
  /**
72
80
  * Thrown when a wave's plans would read `mock_outputs` (#3416). Nothing in
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/terragrunt/run.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEnF,OAAO,EAKL,KAAK,qBAAqB,EAC1B,KAAK,cAAc,EACpB,MAAM,SAAS,CAAC;AACjB,OAAO,EAcL,KAAK,kBAAkB,EACxB,MAAM,SAAS,CAAC;AACjB,OAAO,EAML,KAAK,oBAAoB,EAC1B,MAAM,QAAQ,CAAC;AAEhB,MAAM,WAAW,UAAU;IACzB,oEAAoE;IACpE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,8GAA8G;AAC9G,MAAM,MAAM,cAAc,GAAG,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AmE;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;GASG;AACH,wBAAsB,wBAAwB,CAAC,KAAK,EAAE,wBAAwB,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAiD7G;AAED,MAAM,WAAW,kBAAkB;IACjC,qFAAqF;IACrF,KAAK,EAAE,aAAa,EAAE,CAAC;IACvB,2CAA2C;IAC3C,WAAW,EAAE,OAAO,CAAC;IACrB,OAAO,EAAE,oBAAoB,EAAE,CAAC;IAChC,8BAA8B;IAC9B,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,4CAA4C;IAC5C,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA+CnG;AAED,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,oBAAoB,EAAE,CAAC;IAChC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA6BrG"}
@@ -11,7 +11,7 @@ import { dirname, join, resolve } from "node:path";
11
11
  import { plannerForBinary } from "../change-set.js";
12
12
  import { parseTerragruntFind, terragruntFindArgs, terragruntVersionProblem, unitPathFilter, } from "./units.js";
13
13
  import { describeMockRefusal, failMockedParts, hasExpandedDependency, markProvisional, parseRenderedDependencies, parseTerragruntOutputs, PROVISIONAL_MARKER, terragruntMockReads, terragruntMockWarnings, terragruntOutputArgs, terragruntOutputUpstreams, terragruntRenderArgs, } from "./mocks.js";
14
- import { parseTerragruntReport, terragruntEnv, terragruntUnitResults, terragruntWaveArgs, terragruntWaveParts, } from "./wave.js";
14
+ import { parseTerragruntReport, terragruntEnv, terragruntInitArgs, terragruntUnitResults, terragruntWaveArgs, terragruntWaveParts, } from "./wave.js";
15
15
  export const defaultTerragruntExec = (file, args, options) => new Promise((done) => {
16
16
  execFile(file, [...args], { cwd: options.cwd, env: { ...process.env, ...options.env }, maxBuffer: 256 * 1024 * 1024 }, (err, stdout, stderr) => {
17
17
  const code = err ? (typeof err.code === "number" ? (err.code) : null) : 0;
@@ -218,11 +218,17 @@ export async function planTerragruntWave(input) {
218
218
  // A plan or report left by an earlier attempt must not stand in for a unit that did not plan this time.
219
219
  for (const stale of [outDir, jsonOutDir, reportFile, marker])
220
220
  rmSync(stale, { recursive: true, force: true });
221
- if (!input.provisional) {
222
- const reads = await checkTerragruntWaveMocks({ ...input, exec });
223
- if (reads.length > 0)
224
- throw new TerragruntMockRefusal(reads);
225
- }
221
+ const prepare = async () => {
222
+ if (!input.provisional) {
223
+ const reads = await checkTerragruntWaveMocks({ ...input, exec });
224
+ if (reads.length > 0)
225
+ throw new TerragruntMockRefusal(reads);
226
+ }
227
+ // chant#4864: the init the plan would run itself, in the turn; a failure is the plan's to report.
228
+ if (input.initTurn)
229
+ await exec(tg, terragruntInitArgs({ units: input.units, ...(input.destroy ? { destroy: true } : {}) }), { cwd: input.dir, env: terragruntEnv(input.binary) });
230
+ };
231
+ await (input.initTurn ? input.initTurn(prepare) : prepare());
226
232
  mkdirSync(work, { recursive: true });
227
233
  // Written before planning, so a plan cut short still never applies.
228
234
  if (input.provisional)
@@ -37,6 +37,15 @@ export interface TerragruntWaveArgsInput {
37
37
  }
38
38
  /** Arguments to `terragrunt` for one wave. Throws on an empty wave: with no filter, `run --all` runs every unit. */
39
39
  export declare function terragruntWaveArgs(input: TerragruntWaveArgsInput): string[];
40
+ /**
41
+ * Arguments to `terragrunt` that init a wave's units with the same filters
42
+ * as its plan (chant#4864), for an init that takes its turn at a shared
43
+ * provider cache before the plan. Throws on an empty wave, as the plan does.
44
+ */
45
+ export declare function terragruntInitArgs(input: {
46
+ units: readonly string[];
47
+ destroy?: boolean;
48
+ }): string[];
40
49
  /**
41
50
  * The environment a wave adds: `TG_TF_PATH` when the project names a binary,
42
51
  * nothing otherwise, so Terragrunt's own choice (tofu, then terraform, or a
@@ -1 +1 @@
1
- {"version":3,"file":"wave.d.ts","sourceRoot":"","sources":["../../src/terragrunt/wave.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAInF,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,OAAO,CAAC;AAErD,MAAM,WAAW,uBAAuB;IACtC,mEAAmE;IACnE,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IACzB,OAAO,EAAE,qBAAqB,CAAC;IAC/B,8DAA8D;IAC9D,MAAM,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,UAAU,EAAE,MAAM,CAAC;IACnB,yFAAyF;IACzF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,8EAA8E;IAC9E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oFAAoF;IACpF,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC3B;AAED,oHAAoH;AACpH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,GAAG,MAAM,EAAE,CA4B3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEhF;AAED,8DAA8D;AAC9D,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,kFAAkF;IAClF,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CA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oCAAoC;AACpC,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,wFAAwF;IACxF,MAAM,EAAE,WAAW,GAAG,QAAQ,CAAC;IAC/B,oEAAoE;IACpE,MAAM,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,iHAAiH;AACjH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,mBAAmB,CAAC,GAAG,oBAAoB,EAAE,CAQhI;AAED,MAAM,WAAW,wBAAwB;IACvC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IACzB,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;IACzC,yEAAyE;IACzE,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;IACnC,oCAAoC;IACpC,OAAO,EAAE,gBAAgB,CAAC;IAC1B,kFAAkF;IAClF,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;CACjD;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,GAAG,aAAa,EAAE,CAsBpF"}
@@ -49,6 +49,28 @@ export function terragruntWaveArgs(input) {
49
49
  ];
50
50
  return args;
51
51
  }
52
+ /**
53
+ * Arguments to `terragrunt` that init a wave's units with the same filters
54
+ * as its plan (chant#4864), for an init that takes its turn at a shared
55
+ * provider cache before the plan. Throws on an empty wave, as the plan does.
56
+ */
57
+ export function terragruntInitArgs(input) {
58
+ if (input.units.length === 0)
59
+ throw new Error("a Terragrunt wave needs at least one unit; with no path filter run --all would run every unit");
60
+ const units = [...new Set(input.units)].sort();
61
+ return [
62
+ "run",
63
+ "--all",
64
+ "--non-interactive",
65
+ "--no-color",
66
+ "--no-filters-file",
67
+ ...units.flatMap((u) => ["--filter", unitPathFilter(u)]),
68
+ ...(input.destroy ? ["--filter-allow-destroy"] : []),
69
+ "--",
70
+ "init",
71
+ "-input=false",
72
+ ];
73
+ }
52
74
  /**
53
75
  * The environment a wave adds: `TG_TF_PATH` when the project names a binary,
54
76
  * nothing otherwise, so Terragrunt's own choice (tofu, then terraform, or a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intentius/chant-lexicon-terraform",
3
- "version": "0.124.80",
3
+ "version": "0.124.81",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src/",
@@ -93,7 +93,7 @@
93
93
  "access": "public"
94
94
  },
95
95
  "peerDependencies": {
96
- "@intentius/chant": "^0.124.80",
96
+ "@intentius/chant": "^0.124.81",
97
97
  "zod": "^4.3.6"
98
98
  },
99
99
  "description": "terraform lexicon for chant, existing Terraform root modules as declarable entities",
@@ -132,6 +132,14 @@
132
132
  * plans the step's `dir` as its directory, so a product can name each root
133
133
  * by its path without writing an entry for it.
134
134
  *
135
+ * A wave that plans and applies several roots at once (chant#4864) gives
136
+ * each run its serial section (`DeployContext.serial`). Every `init` the
137
+ * step runs, a root's own and an upstream's for its `reads`, takes its turn
138
+ * there (`../init-turn.ts`): one at a time among the runs that share a
139
+ * provider cache, else one at a time per directory. A choudoufu one-root
140
+ * set's `live-plan-set` inits as it plans, so with a shared cache the whole
141
+ * command takes the turn.
142
+ *
135
143
  * Every plan also writes its `show -json` beside the plan file and names it
136
144
  * as `artifact.planJson`, the document a wave's cost hold prices
137
145
  * (`../cost-hold.ts`).
@@ -181,6 +189,7 @@ import { fillStateReads, stateReadsProblems, waitingReadsReason, type TerraformS
181
189
  import { terraformStateOpCapabilities } from "../state-ops";
182
190
  import { terraformActionCapability } from "../action-ops";
183
191
  import { terragruntApply } from "../terragrunt/capability";
192
+ import { inInitTurn, pluginCacheDir, type InitSection } from "../init-turn";
184
193
 
185
194
  /** The `terraform-apply` step's input. */
186
195
  export interface TerraformApplyStepInput {
@@ -278,7 +287,7 @@ function lockTimeoutOf(input: TerraformApplyStepInput): { lockTimeout?: string }
278
287
  * read once, the values under the step's own `vars`. `waiting` when an
279
288
  * upstream has no such output yet. Throws when a state cannot be read.
280
289
  */
281
- async function withReads(input: TerraformApplyStepInput, resolveRoot?: TerraformRootResolver): Promise<{ input: TerraformApplyStepInput } | { waiting: string; upstreams: string[] }> {
290
+ async function withReads(input: TerraformApplyStepInput, resolveRoot?: TerraformRootResolver, serial?: InitSection): Promise<{ input: TerraformApplyStepInput } | { waiting: string; upstreams: string[] }> {
282
291
  const reads = input.reads ?? [];
283
292
  if (reads.length === 0) return { input };
284
293
  const problems = stateReadsProblems(reads);
@@ -294,7 +303,8 @@ async function withReads(input: TerraformApplyStepInput, resolveRoot?: Terraform
294
303
  ...(tools?.binary ? { binary: tools.binary } : {}),
295
304
  ...(tools?.readEnv ? { env: tools.readEnv } : {}),
296
305
  };
297
- await terraformInit(at);
306
+ // chant#4864: runs that plan at once may read the same upstream, so its init takes a turn.
307
+ await inInitTurn(serial, at.env, initDir(at), () => terraformInit(at));
298
308
  return terraformOutputs(at);
299
309
  });
300
310
  if (filled.errors.length > 0) throw new Error(`terraform-apply: root "${input.root}": ${filled.errors.join("; ")}`);
@@ -324,6 +334,13 @@ const rootArgs = (input: TerraformApplyStepInput, env?: RunEnv) => ({
324
334
  ...(input.binary ? { binary: input.binary } : {}),
325
335
  });
326
336
 
337
+ /** The directory an init runs in, as far as the step's input says, for its turn when no provider cache is shared (chant#4864). */
338
+ const initDir = (at: { root: string; cwd?: string; dir?: string }): string => join(at.cwd ?? process.cwd(), at.dir ?? at.root);
339
+
340
+ /** `terraform init` of `input`'s root in its turn (chant#4864). */
341
+ const initRoot = (input: TerraformApplyStepInput, env: RunEnv, serial: InitSection | undefined) =>
342
+ inInitTurn(serial, env, initDir(rootArgs(input, env)), () => terraformInit(rootArgs(input, env)));
343
+
327
344
  function requireRoot(input: TerraformApplyStepInput): void {
328
345
  if (typeof input.root !== "string" || input.root === "") {
329
346
  throw new Error('terraform-apply: "root" is required and names an entry in terraform.roots');
@@ -336,9 +353,9 @@ function requireRoot(input: TerraformApplyStepInput): void {
336
353
  }
337
354
  }
338
355
 
339
- async function planRoot(input: TerraformApplyStepInput, targets?: string[], at: { env?: RunEnv; afterInit?: () => Promise<void> } = {}): Promise<PlannedRoot & { changeSet?: ChangeSetPart; json?: unknown }> {
356
+ async function planRoot(input: TerraformApplyStepInput, targets?: string[], at: { env?: RunEnv; afterInit?: () => Promise<void>; serial?: InitSection } = {}): Promise<PlannedRoot & { changeSet?: ChangeSetPart; json?: unknown }> {
340
357
  requireRoot(input);
341
- await terraformInit(rootArgs(input, at.env));
358
+ await initRoot(input, at.env, at.serial);
342
359
  // chant#4842: the run hook's check, once init fetched the modules and providers and before the plan runs their code.
343
360
  await at.afterInit?.();
344
361
  // One `show -json` per plan (#4479): its document is the digest, the change
@@ -537,7 +554,7 @@ async function previewRoot(ctx: DeployContext, input: TerraformApplyStepInput, u
537
554
  if (typeof stand === "string") return waitingRoot(ctx, input, unresolved, stand);
538
555
  let planned: Awaited<ReturnType<typeof terraformPlan>>;
539
556
  try {
540
- await terraformInit(rootArgs(input, ctx.processEnv));
557
+ await initRoot(input, ctx.processEnv, ctx.serial);
541
558
  await ctx.afterInit?.();
542
559
  planned = await terraformPlan({
543
560
  ...rootArgs(input, ctx.processEnv),
@@ -645,8 +662,8 @@ async function runChoudoufuWave(
645
662
  }
646
663
 
647
664
  /** The input a `run` that plans for itself plans with (#4545): its reads filled, or an error naming the upstream to apply first. */
648
- async function readsForRun(input: TerraformApplyStepInput, resolveRoot?: TerraformRootResolver): Promise<TerraformApplyStepInput> {
649
- const read = await withReads(input, resolveRoot);
665
+ async function readsForRun(input: TerraformApplyStepInput, resolveRoot?: TerraformRootResolver, serial?: InitSection): Promise<TerraformApplyStepInput> {
666
+ const read = await withReads(input, resolveRoot, serial);
650
667
  if ("waiting" in read) throw new Error(`terraform-apply: root "${input.root}": ${read.waiting}`);
651
668
  return read.input;
652
669
  }
@@ -685,7 +702,7 @@ export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Cap
685
702
  const given = await filledInput(deps.resolveRoot, ctx, step);
686
703
  requireRoot(given);
687
704
  // #4545: a root reading an upstream's output that is not there yet is a hole naming the upstream.
688
- const read = await withReads(given, deps.resolveRoot);
705
+ const read = await withReads(given, deps.resolveRoot, ctx.serial);
689
706
  if ("waiting" in read) return waitingOnReads(ctx, given, read);
690
707
  const { input } = read;
691
708
  const unresolved = [...(ctx.unresolvedInputs ?? [])].sort();
@@ -697,7 +714,8 @@ export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Cap
697
714
  if (wave.wave) {
698
715
  // chant#4842: live-plan-set inits and plans in one command, so the run hook's check comes before it.
699
716
  await ctx.afterInit?.();
700
- const set = await choudoufuPlanSet(rootArgs(input, env));
717
+ // chant#4864: live-plan-set inits too, so with a shared provider cache the whole command takes the init's turn.
718
+ const set = await inInitTurn(pluginCacheDir(env) ? ctx.serial : undefined, env, undefined, () => choudoufuPlanSet(rootArgs(input, env)));
701
719
  const artifact: ChoudoufuWavePlannedRoot = {
702
720
  planDigest: set.planDigest,
703
721
  choudoufu: { planSetFile: set.planSetFile, setDigest: set.setDigest },
@@ -705,7 +723,7 @@ export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Cap
705
723
  };
706
724
  return { member: input.root, planDigest: set.planDigest, artifact, changeSet: set.changeSet, outputs: terraformPlannedOutputs(set.json), shape: terraformPlanShape(set.json, input.root) };
707
725
  }
708
- const { changeSet, json, ...planned } = await planRoot(input, targets, { env, ...(ctx.afterInit ? { afterInit: ctx.afterInit } : {}) });
726
+ const { changeSet, json, ...planned } = await planRoot(input, targets, { env, ...(ctx.afterInit ? { afterInit: ctx.afterInit } : {}), ...(ctx.serial ? { serial: ctx.serial } : {}) });
709
727
  // #4552: a choudoufu root's rows, keyed from the plan document while it is at hand.
710
728
  const lockRows = changeSet?.member.planner === "choudoufu" ? terraformLockRows(input.root, changeSet.member.scope, json) : undefined;
711
729
  return {
@@ -725,7 +743,7 @@ export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Cap
725
743
  async outputs(ctx: DeployContext, step: TerraformApplyStepInput): Promise<Record<string, unknown>> {
726
744
  const input = await filledInput(deps.resolveRoot, ctx, step);
727
745
  requireRoot(input);
728
- await terraformInit(rootArgs(input, ctx.processEnv));
746
+ await initRoot(input, ctx.processEnv, ctx.serial);
729
747
  return terraformOutputs(rootArgs(input, ctx.processEnv));
730
748
  },
731
749
  // #4447 (ws-107): a group core forms of components that read each other plans and applies as one composed run
@@ -775,7 +793,7 @@ export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Cap
775
793
  if (isShapeArtifact(fromWave)) {
776
794
  // A root previewed with stand-ins (#4463): plan it now with the real
777
795
  // values, and apply that plan only when it is within the preview's shape.
778
- const fresh = await planRoot(await readsForRun(input, deps.resolveRoot), ctx.targets, { env: ctx.processEnv });
796
+ const fresh = await planRoot(await readsForRun(input, deps.resolveRoot, ctx.serial), ctx.targets, { env: ctx.processEnv, ...(ctx.serial ? { serial: ctx.serial } : {}) });
779
797
  const differences = planShapeDifferences(fromWave.shape, terraformPlanShape(fresh.json, input.root));
780
798
  if (differences.length > 0) throw new PlanShapeMovedError(input.root, differences);
781
799
  const applied = await terraformApply({ ...rootArgs(input, ctx.processEnv), planFile: fresh.planFile, ...lockTimeoutOf(input) });
@@ -787,7 +805,7 @@ export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Cap
787
805
  }
788
806
  const planned: PlannedRoot = isPlannedRoot(fromWave)
789
807
  ? fromWave
790
- : await planRoot(await readsForRun(input, deps.resolveRoot), ctx.targets, { env: ctx.processEnv }).then(({ planFile, planDigest }) => ({ planFile, planDigest }));
808
+ : await planRoot(await readsForRun(input, deps.resolveRoot, ctx.serial), ctx.targets, { env: ctx.processEnv, ...(ctx.serial ? { serial: ctx.serial } : {}) }).then(({ planFile, planDigest }) => ({ planFile, planDigest }));
791
809
  const applied = await terraformApply({ ...rootArgs(input, ctx.processEnv), planFile: planned.planFile, ...lockTimeoutOf(input) });
792
810
  if (!applied.applied) {
793
811
  const message =
@@ -0,0 +1,158 @@
1
+ /**
2
+ * `terraform-apply` roots planned and applied `parallelism` at once on one
3
+ * provider cache (chant#4864), run by core's `runOpWave` against a fake
4
+ * `terraform` on `PATH`. The fake sleeps per command (`$FAKE_SLEEP_INIT`,
5
+ * and a root's own `delay` file for plan and apply), and marks each command
6
+ * in flight with a file under `$FAKE_STATE/<command>/`, moving it to
7
+ * `$FAKE_STATE/done/` when it ends. Every command logs how many of its kind
8
+ * were in flight as it started, so the test reads the most that ever ran
9
+ * together. The roots' `show -json` names the root, so each has its own
10
+ * plan digest. Nothing reaches a cloud.
11
+ */
12
+
13
+ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+ import { afterAll, afterEach, describe, expect, test, vi } from "vitest";
17
+ import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
18
+ import { memoryGateLedgerPort, opWavePlanPath, runOpWave, type OpWavesSpec, type RunOpWaveResult } from "@intentius/chant/op";
19
+ import { terraformApplyCapability } from "./capability-plugin";
20
+
21
+ const made: string[] = [];
22
+ afterAll(() => {
23
+ for (const d of made) rmSync(d, { recursive: true, force: true });
24
+ });
25
+ afterEach(() => vi.unstubAllEnvs());
26
+
27
+ const FAKE = `#!/bin/sh
28
+ cmd="$1"
29
+ mkdir -p "$FAKE_STATE/$cmd" "$FAKE_STATE/done"
30
+ mark="$FAKE_STATE/$cmd/\${PWD##*/}.$$"
31
+ : > "$mark"
32
+ n=$(ls "$FAKE_STATE/$cmd" | wc -l | tr -d ' ')
33
+ echo "$cmd \${PWD##*/} $n" >> "$FAKE_LOG"
34
+ root="\${PWD##*/}"
35
+ case "$cmd" in
36
+ init) sleep "\${FAKE_SLEEP_INIT:-0.1}" ;;
37
+ plan)
38
+ sleep "$(cat delay 2>/dev/null || echo 0.2)"
39
+ for a in "$@"; do case "$a" in -out=*) out="\${a#-out=}" ;; esac; done
40
+ echo plan > "$out" ;;
41
+ apply) sleep "$(cat delay 2>/dev/null || echo 0.2)" ;;
42
+ show) echo "{\\"format_version\\":\\"1.2\\",\\"resource_changes\\":[{\\"address\\":\\"terraform_data.$root\\",\\"mode\\":\\"managed\\",\\"type\\":\\"terraform_data\\",\\"name\\":\\"$root\\",\\"change\\":{\\"actions\\":[\\"create\\"],\\"before\\":null,\\"after\\":{}}}]}" ;;
43
+ output) echo '{}' ;;
44
+ esac
45
+ mv "$mark" "$FAKE_STATE/done/"
46
+ [ "$cmd" = plan ] && exit 2
47
+ exit 0
48
+ `;
49
+
50
+ const ROOTS = ["a", "b", "c", "d"];
51
+
52
+ /** A project with `roots/<name>` for each root, each planning slower than the next, and the fake on PATH. */
53
+ function project(): { dir: string; log: string } {
54
+ const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-tf-par-")));
55
+ made.push(dir);
56
+ const bin = join(dir, "bin");
57
+ mkdirSync(bin);
58
+ writeFileSync(join(bin, "terraform"), FAKE);
59
+ chmodSync(join(bin, "terraform"), 0o755);
60
+ const log = join(dir, "fake.log");
61
+ writeFileSync(log, "");
62
+ mkdirSync(join(dir, "state"));
63
+ vi.stubEnv("PATH", `${bin}:${process.env.PATH ?? ""}`);
64
+ vi.stubEnv("FAKE_LOG", log);
65
+ vi.stubEnv("FAKE_STATE", join(dir, "state"));
66
+ vi.stubEnv("FAKE_SLEEP_INIT", "0.03");
67
+ for (const [i, r] of ROOTS.entries()) {
68
+ mkdirSync(join(dir, "roots", r), { recursive: true });
69
+ writeFileSync(join(dir, "roots", r, "main.tf"), "");
70
+ writeFileSync(join(dir, "roots", r, "delay"), String(0.06 + 0.03 * (ROOTS.length - i)));
71
+ }
72
+ writeFileSync(join(dir, "chant.config.json"), JSON.stringify({ lexicons: ["terraform"], terraform: { binary: "terraform" } }));
73
+ return { dir, log };
74
+ }
75
+
76
+ /** The fake's log as `{ command: [root, in flight] }`, in the order the commands started. */
77
+ function started(log: string): Record<string, Array<[string, number]>> {
78
+ const out: Record<string, Array<[string, number]>> = {};
79
+ for (const line of readFileSync(log, "utf-8").split("\n").filter(Boolean)) {
80
+ const [cmd, root, n] = line.split(" ");
81
+ (out[cmd!] ??= []).push([root!, Number(n)]);
82
+ }
83
+ return out;
84
+ }
85
+
86
+ const most = (rows: Array<[string, number]> | undefined) => Math.max(0, ...(rows ?? []).map(([, n]) => n));
87
+
88
+ function spec(parallelism?: number): OpWavesSpec {
89
+ return {
90
+ name: "tf-apply",
91
+ op: "tf-apply",
92
+ gateName: "wave-{k}",
93
+ ...(parallelism !== undefined ? { parallelism } : {}),
94
+ step: { kind: "terraform-apply", root: "{target}", dir: "{target}", lockTimeout: false },
95
+ waves: [{ name: "wave-1", gate: "never", runs: ROOTS.map((r) => ({ target: `roots/${r}` })) }],
96
+ } as OpWavesSpec;
97
+ }
98
+
99
+ async function wave(cwd: string, s: OpWavesSpec): Promise<{ result: RunOpWaveResult; files: string[] }> {
100
+ const result = await runOpWave({
101
+ spec: s,
102
+ specFile: "waves.json",
103
+ wave: 1,
104
+ cwd,
105
+ show: () => ({ sha: "b".repeat(40), text: JSON.stringify(s) }),
106
+ gates: memoryGateLedgerPort(),
107
+ capabilities: new CapabilityRegistry().register(terraformApplyCapability as unknown as Capability<never, unknown>),
108
+ log: () => {},
109
+ });
110
+ const files = ROOTS.map((r) => {
111
+ const f = join(cwd, opWavePlanPath(s, 1, `roots/${r}`));
112
+ return existsSync(f) ? readFileSync(f, "utf-8") : "";
113
+ });
114
+ return { result, files };
115
+ }
116
+
117
+ describe("terraform-apply roots at once on one provider cache (chant#4864)", () => {
118
+ test("the plans and applies overlap, and no two inits ever run at once", async () => {
119
+ const p = project();
120
+ vi.stubEnv("TF_PLUGIN_CACHE_DIR", join(p.dir, "cache"));
121
+ const { result } = await wave(p.dir, spec(4));
122
+ expect(result.exitCode).toBe(0);
123
+ expect(result.applied).toEqual(ROOTS.map((r) => `roots/${r}`));
124
+ const s = started(p.log);
125
+ expect(s.init?.length).toBe(4);
126
+ expect(most(s.init)).toBe(1);
127
+ expect(most(s.plan)).toBeGreaterThan(1);
128
+ expect(most(s.apply)).toBeGreaterThan(1);
129
+ });
130
+
131
+ test("the digest, members and plan files are the ones one root at a time makes", async () => {
132
+ const run = async (parallelism?: number) => {
133
+ const p = project();
134
+ vi.stubEnv("TF_PLUGIN_CACHE_DIR", join(p.dir, "cache"));
135
+ const got = await wave(p.dir, spec(parallelism));
136
+ return { ...got, started: started(p.log), dir: p.dir };
137
+ };
138
+ const one = await run();
139
+ const many = await run(4);
140
+ expect(most(one.started.plan)).toBe(1);
141
+ expect(most(many.started.plan)).toBeGreaterThan(1);
142
+ expect(many.result.decision?.digest).toBeDefined();
143
+ expect(many.result.decision?.digest).toBe(one.result.decision?.digest);
144
+ expect(many.result.decision?.members).toEqual(one.result.decision?.members);
145
+ // A plan file names its plan by path inside the project, so it reads the same with each project's directory taken out.
146
+ const local = (files: string[], dir: string) => files.map((f) => f.split(dir).join("."));
147
+ expect(local(many.files, many.dir)).toEqual(local(one.files, one.dir));
148
+ expect(new Set(many.result.decision?.members.map((m) => m.planDigest)).size).toBe(4);
149
+ });
150
+
151
+ test("with no cache the roots' inits run at once, each in its own directory", async () => {
152
+ const p = project();
153
+ vi.stubEnv("TF_PLUGIN_CACHE_DIR", "");
154
+ const { result } = await wave(p.dir, spec(4));
155
+ expect(result.exitCode).toBe(0);
156
+ expect(most(started(p.log).init)).toBeGreaterThan(1);
157
+ });
158
+ });
@@ -0,0 +1,36 @@
1
+ /**
2
+ * An init's turn (chant#4864). An op wave with `parallelism` over 1 plans
3
+ * and applies several roots at once and gives each run the wave's serial
4
+ * section (`DeployContext.serial`). Terraform's provider cache
5
+ * (`TF_PLUGIN_CACHE_DIR`) is not safe for two inits writing it at once, so
6
+ * `terraform-apply` and `terragrunt-apply` take their inits in the section,
7
+ * keyed on the cache directory: one init at a time among the runs that share
8
+ * it, their plans and applies side by side. Without a cache, `terraform-apply`
9
+ * keys an init on the directory it runs in, since two runs reading the same
10
+ * upstream would init it at once. Without a section (runs one at a time)
11
+ * nothing changes.
12
+ */
13
+
14
+ import { resolve } from "node:path";
15
+
16
+ /** A section as `DeployContext.serial` gives it. */
17
+ export type InitSection = <T>(key: string, fn: () => Promise<T>) => Promise<T>;
18
+
19
+ /** The provider cache a run's inits write: its own environment's `TF_PLUGIN_CACHE_DIR`, else the job's. Undefined when neither names one. */
20
+ export function pluginCacheDir(env?: Record<string, string | undefined>): string | undefined {
21
+ const dir = env && "TF_PLUGIN_CACHE_DIR" in env ? env.TF_PLUGIN_CACHE_DIR : process.env.TF_PLUGIN_CACHE_DIR;
22
+ return dir ? resolve(dir) : undefined;
23
+ }
24
+
25
+ /** The key an init takes its turn under: the provider cache, else `dir` when given. Undefined when it needs no turn. */
26
+ export function initTurnKey(env: Record<string, string | undefined> | undefined, dir?: string): string | undefined {
27
+ const cache = pluginCacheDir(env);
28
+ if (cache) return `terraform-plugin-cache:${cache}`;
29
+ return dir ? `terraform-init:${resolve(dir)}` : undefined;
30
+ }
31
+
32
+ /** Run `fn`, an init, in its turn: inside `serial` under {@link initTurnKey}, or as it is when there is no section or no key. */
33
+ export function inInitTurn<T>(serial: InitSection | undefined, env: Record<string, string | undefined> | undefined, dir: string | undefined, fn: () => Promise<T>): Promise<T> {
34
+ const key = serial ? initTurnKey(env, dir) : undefined;
35
+ return key ? serial!(key, fn) : fn();
36
+ }
@@ -346,3 +346,76 @@ describe("terragrunt-apply runs where the wave runs (chant#4854)", () => {
346
346
  expect(new Set(calls.map((c) => c.cwd))).toEqual(new Set([dir]));
347
347
  });
348
348
  });
349
+
350
+ describe("a wave's units planned at once on one provider cache (chant#4864)", () => {
351
+ /** The recorded stub with a delay per call (a plan's longest), `init` answered here, and how many inits and plans were ever in flight together. */
352
+ const slow = (calls: Call[]) => {
353
+ const inner = stub(calls);
354
+ const most = { init: 0, plan: 0 };
355
+ const now = { init: 0, plan: 0 };
356
+ const exec: TerragruntExec = async (f, args, o) => {
357
+ const cmd = args[0] === "run" ? args[args.indexOf("--") + 1] : undefined;
358
+ const kind = cmd === "init" ? "init" : cmd === "plan" ? "plan" : undefined;
359
+ if (kind) most[kind] = Math.max(most[kind], ++now[kind]);
360
+ try {
361
+ await new Promise((r) => setTimeout(r, kind === "plan" ? 120 : 20));
362
+ if (kind === "init") {
363
+ calls.push({ file: f, args: [...args], env: o.env });
364
+ return { code: 0, stdout: "", stderr: "" };
365
+ }
366
+ return await inner(f, args, o);
367
+ } finally {
368
+ if (kind) now[kind]--;
369
+ }
370
+ };
371
+ return { exec, most };
372
+ };
373
+ const parallel = (dir: string): OpWavesSpec => ({ ...spec(dir, false), parallelism: 2 });
374
+ const env = process.env.TF_PLUGIN_CACHE_DIR;
375
+ const withCache = async <T>(cache: string | undefined, fn: () => Promise<T>): Promise<T> => {
376
+ if (cache === undefined) delete process.env.TF_PLUGIN_CACHE_DIR;
377
+ else process.env.TF_PLUGIN_CACHE_DIR = cache;
378
+ try {
379
+ return await fn();
380
+ } finally {
381
+ if (env === undefined) delete process.env.TF_PLUGIN_CACHE_DIR;
382
+ else process.env.TF_PLUGIN_CACHE_DIR = env;
383
+ }
384
+ };
385
+
386
+ it("with a cache, each unit inits in its turn before its plan, and the plans run at once", async () => {
387
+ const dir = tmp();
388
+ const calls: Call[] = [];
389
+ const { exec, most } = slow(calls);
390
+ const result = await withCache(join(dir, "cache"), () => wave(parallel(dir), dir, exec));
391
+ expect(result.exitCode).toBe(0);
392
+ expect(most).toEqual({ init: 1, plan: 2 });
393
+ const ran = runAll(calls);
394
+ expect(ran.filter((r) => r[0] === "init").map((r) => r[1]).sort()).toEqual(["{./live/dev/vpc}", "{./live/prod/vpc}"]);
395
+ for (const unit of ["{./live/dev/vpc}", "{./live/prod/vpc}"]) {
396
+ const at = (cmd: string) => ran.findIndex((r) => r[0] === cmd && r[1] === unit);
397
+ expect(at("init")).toBeLessThan(at("plan"));
398
+ }
399
+ expect(calls.find((c) => c.args.includes("init"))!.args).toEqual(["run", "--all", "--non-interactive", "--no-color", "--no-filters-file", "--filter", "{./live/dev/vpc}", "--", "init", "-input=false"]);
400
+ });
401
+
402
+ it("the digest is the one a wave planned one unit at a time decides", async () => {
403
+ const decided = async (s: OpWavesSpec, dir: string) => {
404
+ const { exec } = slow([]);
405
+ return (await withCache(join(dir, "cache"), () => wave(s, dir, exec))).decision?.digest;
406
+ };
407
+ const one = tmp();
408
+ const many = tmp();
409
+ expect(await decided(parallel(many), many)).toBe(await decided(spec(one, false), one));
410
+ });
411
+
412
+ it("with no cache, the units plan at once with no init of their own", async () => {
413
+ const dir = tmp();
414
+ const calls: Call[] = [];
415
+ const { exec, most } = slow(calls);
416
+ const result = await withCache(undefined, () => wave(parallel(dir), dir, exec));
417
+ expect(result.exitCode).toBe(0);
418
+ expect(most.plan).toBe(2);
419
+ expect(runAll(calls).map((r) => r[0])).not.toContain("init");
420
+ });
421
+ });
@@ -27,6 +27,11 @@
27
27
  * A run hook's `processEnv` is laid over the environment Terragrunt runs
28
28
  * with; the units of one `run --all` must agree on it. Terragrunt has no
29
29
  * init of its own before the plan, so `afterInit` is called before it plans.
30
+ * A wave that plans its units run by run several at once (chant#4864) gives
31
+ * each its serial section (`DeployContext.serial`): with a provider cache
32
+ * (`TF_PLUGIN_CACHE_DIR`) a unit's mock check and an explicit `run --all --
33
+ * init` take their turn there, one unit at a time, and its plan runs beside
34
+ * the others'.
30
35
  * A product passes its own `exec` (`terragruntApplyCapability`) to wrap
31
36
  * Terragrunt, as terragucci adds its lock timeout and its backend bootstrap.
32
37
  *
@@ -50,6 +55,7 @@ import type { ChangeSetPart } from "@intentius/chant/change-set";
50
55
  import { computePlanDigest } from "@intentius/chant/lifecycle/plan-digest";
51
56
  import { applyTerragruntWave, defaultTerragruntExec, discoverTerragruntUnits, planTerragruntWave, TerragruntMockRefusal, type TerragruntExec } from "./run";
52
57
  import { terragruntListedWaves, terragruntWaves } from "./units";
58
+ import { initTurnKey, type InitSection } from "../init-turn";
53
59
 
54
60
  /** The step's input. Each string has `{target}`, `{op}` and `{wave}` filled per run in an op wave. */
55
61
  export interface TerragruntApplyStepInput {
@@ -261,10 +267,13 @@ function unitPlans(parts: readonly ChangeSetPart[], units: readonly string[], wo
261
267
  return out;
262
268
  }
263
269
 
264
- async function planUnits(exec: TerragruntExec, input: TerragruntApplyStepInput, units: readonly string[], env: Record<string, string> | undefined): Promise<{ workDir: string; plans: Map<string, CapabilityPlan> }> {
270
+ async function planUnits(exec: TerragruntExec, input: TerragruntApplyStepInput, units: readonly string[], env: Record<string, string> | undefined, serial?: InitSection): Promise<{ workDir: string; plans: Map<string, CapabilityPlan> }> {
265
271
  const dir = dirOf(input);
266
272
  const workDir = workDirOf(input, units);
273
+ // chant#4864: units planned at once that share a provider cache take turns at their inits.
274
+ const cache = serial ? initTurnKey(env) : undefined;
267
275
  const planned = await planTerragruntWave({
276
+ ...(cache ? { initTurn: <T>(fn: () => Promise<T>) => serial!(cache, fn) } : {}),
268
277
  dir,
269
278
  units,
270
279
  workDir,
@@ -370,7 +379,7 @@ export function terragruntApplyCapability(deps: TerragruntApplyDeps = {}): Capab
370
379
  const input = atDir(given, ctx.cwd);
371
380
  if (typeof input.unit !== "string" || input.unit === "") throw new Error("terragrunt-apply: the step names no unit");
372
381
  await ctx.afterInit?.();
373
- const { plans } = await planUnits(exec, await unitInput(resolveUnit, input, input.unit), [input.unit], ctx.processEnv);
382
+ const { plans } = await planUnits(exec, await unitInput(resolveUnit, input, input.unit), [input.unit], ctx.processEnv, ctx.serial);
374
383
  return plans.get(input.unit)!;
375
384
  },
376
385
 
@@ -379,7 +388,7 @@ export function terragruntApplyCapability(deps: TerragruntApplyDeps = {}): Capab
379
388
  const given = ctx.plans?.[input.unit];
380
389
  const own = await unitInput(resolveUnit, input, input.unit);
381
390
  // A unit with no plan from the wave plans now and applies that plan.
382
- const workDir = isUnitArtifact(given) ? given.workDir : (await planUnits(exec, own, [input.unit], ctx.processEnv)).workDir;
391
+ const workDir = isUnitArtifact(given) ? given.workDir : (await planUnits(exec, own, [input.unit], ctx.processEnv, ctx.serial)).workDir;
383
392
  const applied = await applyUnits(exec, own, [input.unit], workDir, ctx.processEnv);
384
393
  const result = applied.results.find((r) => r.unit === input.unit);
385
394
  if (result?.status !== "succeeded") throw new Error(`terragrunt-apply: ${input.unit}: ${result?.error ?? "Terragrunt did not apply it"}`);
@@ -38,6 +38,7 @@ import {
38
38
  import {
39
39
  parseTerragruntReport,
40
40
  terragruntEnv,
41
+ terragruntInitArgs,
41
42
  terragruntUnitResults,
42
43
  terragruntWaveArgs,
43
44
  terragruntWaveParts,
@@ -171,6 +172,14 @@ export interface TerragruntWaveRunInput extends TerragruntRunOptions {
171
172
  * are never applied. Unset, a wave that would read a mock is refused.
172
173
  */
173
174
  provisional?: boolean;
175
+ /**
176
+ * The plan's turn at the provider cache (chant#4864), when units planned
177
+ * by other calls at once share it. Given, the mock check and an explicit
178
+ * `run --all -- init` of the units run inside it, and the plan after it,
179
+ * so its auto-init finds them initialised. An init that fails is left to
180
+ * the plan, which fails the unit with its report row as before.
181
+ */
182
+ initTurn?: <T>(fn: () => Promise<T>) => Promise<T>;
174
183
  }
175
184
 
176
185
  /**
@@ -315,10 +324,15 @@ export async function planTerragruntWave(input: TerragruntWaveRunInput): Promise
315
324
  const marker = join(work, PROVISIONAL_MARKER);
316
325
  // A plan or report left by an earlier attempt must not stand in for a unit that did not plan this time.
317
326
  for (const stale of [outDir, jsonOutDir, reportFile, marker]) rmSync(stale, { recursive: true, force: true });
318
- if (!input.provisional) {
319
- const reads = await checkTerragruntWaveMocks({ ...input, exec });
320
- if (reads.length > 0) throw new TerragruntMockRefusal(reads);
321
- }
327
+ const prepare = async (): Promise<void> => {
328
+ if (!input.provisional) {
329
+ const reads = await checkTerragruntWaveMocks({ ...input, exec });
330
+ if (reads.length > 0) throw new TerragruntMockRefusal(reads);
331
+ }
332
+ // chant#4864: the init the plan would run itself, in the turn; a failure is the plan's to report.
333
+ if (input.initTurn) await exec(tg, terragruntInitArgs({ units: input.units, ...(input.destroy ? { destroy: true } : {}) }), { cwd: input.dir, env: terragruntEnv(input.binary) });
334
+ };
335
+ await (input.initTurn ? input.initTurn(prepare) : prepare());
322
336
  mkdirSync(work, { recursive: true });
323
337
  // Written before planning, so a plan cut short still never applies.
324
338
  if (input.provisional) writeFileSync(marker, "planned before its upstream applied; never applied\n");
@@ -72,6 +72,28 @@ export function terragruntWaveArgs(input: TerragruntWaveArgsInput): string[] {
72
72
  return args;
73
73
  }
74
74
 
75
+ /**
76
+ * Arguments to `terragrunt` that init a wave's units with the same filters
77
+ * as its plan (chant#4864), for an init that takes its turn at a shared
78
+ * provider cache before the plan. Throws on an empty wave, as the plan does.
79
+ */
80
+ export function terragruntInitArgs(input: { units: readonly string[]; destroy?: boolean }): string[] {
81
+ if (input.units.length === 0) throw new Error("a Terragrunt wave needs at least one unit; with no path filter run --all would run every unit");
82
+ const units = [...new Set(input.units)].sort();
83
+ return [
84
+ "run",
85
+ "--all",
86
+ "--non-interactive",
87
+ "--no-color",
88
+ "--no-filters-file",
89
+ ...units.flatMap((u) => ["--filter", unitPathFilter(u)]),
90
+ ...(input.destroy ? ["--filter-allow-destroy"] : []),
91
+ "--",
92
+ "init",
93
+ "-input=false",
94
+ ];
95
+ }
96
+
75
97
  /**
76
98
  * The environment a wave adds: `TG_TF_PATH` when the project names a binary,
77
99
  * nothing otherwise, so Terragrunt's own choice (tofu, then terraform, or a