@intentius/chant-lexicon-terraform 0.124.90 → 0.124.92
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.
- package/dist/engine-runner.d.ts +4 -2
- package/dist/engine-runner.d.ts.map +1 -1
- package/dist/engine-runner.js +15 -5
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/op/command-hook.d.ts +18 -0
- package/dist/op/command-hook.d.ts.map +1 -1
- package/dist/op/command-hook.js +49 -4
- package/dist/state/exec.d.ts +4 -0
- package/dist/state/exec.d.ts.map +1 -1
- package/dist/state/exec.js +7 -3
- package/dist/terragrunt/run.d.ts.map +1 -1
- package/dist/terragrunt/run.js +1 -0
- package/package.json +2 -2
- package/src/engine-runner.test.ts +88 -2
- package/src/engine-runner.ts +15 -5
- package/src/op/command-hook.ts +52 -4
- package/src/state/exec.ts +12 -3
- package/src/terragrunt/run.ts +1 -0
package/dist/engine-runner.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type OtlpSpan, type OtlpTrace } from "@intentius/chant/otlp";
|
|
|
2
2
|
import { type TerraformCommand, type TerraformCommandHook } from "./op/command-hook.js";
|
|
3
3
|
import { type BinaryExec, type Ran } from "./state/exec.js";
|
|
4
4
|
import type { TerragruntExec } from "./terragrunt/run.js";
|
|
5
|
-
export { withTerraformCommandHook, asUpstreamCommands, type TerraformCommand, type TerraformCommandHook, type TerraformCommandResult, type TerraformCommandRun } from "./op/command-hook.js";
|
|
5
|
+
export { withTerraformCommandHook, asUpstreamCommands, composeCommandHooks, type TerraformCommand, type TerraformCommandHook, type TerraformCommandResult, type TerraformCommandRun } from "./op/command-hook.js";
|
|
6
6
|
/** A run of the binary that blocks: `-chdir=<dir>`, `TF_IN_AUTOMATION=1`, as {@link runBinary} runs it. */
|
|
7
7
|
export type BinaryExecSync = (binary: string, args: string[], dir: string, env: NodeJS.ProcessEnv) => Ran;
|
|
8
8
|
/** What {@link engineRunner} takes. */
|
|
@@ -77,7 +77,9 @@ export interface CommandTraceOptions {
|
|
|
77
77
|
* `process.command_args`) under `parent(command)`, the variables
|
|
78
78
|
* {@link commandTraceEnv} gives for it, and the span ended with
|
|
79
79
|
* `process.exit.code` once the command ends, failed unless it exited 0 (or 2
|
|
80
|
-
* for a `plan`, `-detailed-exitcode`'s changes)
|
|
80
|
+
* for a `plan`, `-detailed-exitcode`'s changes), its error the result's
|
|
81
|
+
* `error` when the command did not exit (a binary that could not be started,
|
|
82
|
+
* chant#5007), `exit <code>` otherwise. A command timed apart from
|
|
81
83
|
* its call (`at`, a Terragrunt unit's plan) spans the time it ran. Pass it as
|
|
82
84
|
* `engineTimings({ inner })`, so the engine's spans join the trace and reach
|
|
83
85
|
* the timings' receiver.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine-runner.d.ts","sourceRoot":"","sources":["../src/engine-runner.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"engine-runner.d.ts","sourceRoot":"","sources":["../src/engine-runner.ts"],"names":[],"mappings":"AAsCA,OAAO,EAAmC,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvG,OAAO,EAAoB,KAAK,gBAAgB,EAAE,KAAK,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACvG,OAAO,EAA6C,KAAK,UAAU,EAAE,KAAK,GAAG,EAAE,MAAM,cAAc,CAAC;AACpG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,KAAK,gBAAgB,EAAE,KAAK,oBAAoB,EAAE,KAAK,sBAAsB,EAAE,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAE/M,2GAA2G;AAC3G,MAAM,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,EA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,QAAQ,GAAG,SAAS,CAAC;IAC7D,0IAA0I;IAC1I,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IACxB,wGAAwG;IACxG,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,oBAAoB,CAa/E"}
|
package/dist/engine-runner.js
CHANGED
|
@@ -15,7 +15,13 @@
|
|
|
15
15
|
* - {@link commandTrace} is a `TerraformCommandHook` that opens a span per
|
|
16
16
|
* engine command under the product's span for its root, and gives the
|
|
17
17
|
* command `TRACEPARENT` and the resource attributes that name the span
|
|
18
|
-
* ({@link commandTraceEnv}). Pass it as `engineTimings({ inner })
|
|
18
|
+
* ({@link commandTraceEnv}). Pass it as `engineTimings({ inner })`, or
|
|
19
|
+
* beside a product's own hook through `composeCommandHooks` (chant#5007).
|
|
20
|
+
*
|
|
21
|
+
* Every run `exec` makes answers its stderr apart (`Ran.stderr`) and, for a
|
|
22
|
+
* binary that could not be started, the spawn error (`Ran.error`), which the
|
|
23
|
+
* hook's `after` gets as a null code with `error` and `commandTrace` puts on
|
|
24
|
+
* the span (chant#5007).
|
|
19
25
|
*
|
|
20
26
|
* A Terragrunt unit's plan reaches the same hook through
|
|
21
27
|
* `planTerragruntWave({ onCommand })` (`./terragrunt/run.ts`).
|
|
@@ -33,7 +39,7 @@ import { FORGE_TOKEN_VARS } from "@intentius/chant/ci-handover";
|
|
|
33
39
|
import { formatOtlpPairs, parseOtlpPairs } from "@intentius/chant/otlp";
|
|
34
40
|
import { hookedBinaryExec } from "./op/command-hook.js";
|
|
35
41
|
import { engineEnv, runBinary, tokenlessBinaryExec } from "./state/exec.js";
|
|
36
|
-
export { withTerraformCommandHook, asUpstreamCommands } from "./op/command-hook.js";
|
|
42
|
+
export { withTerraformCommandHook, asUpstreamCommands, composeCommandHooks } from "./op/command-hook.js";
|
|
37
43
|
/** Whether `p` is a file this process may run. */
|
|
38
44
|
function runnable(p) {
|
|
39
45
|
try {
|
|
@@ -60,7 +66,9 @@ export function engineRunner(options = {}) {
|
|
|
60
66
|
const execSync = (binary, args, dir, given) => {
|
|
61
67
|
const p = spawnSync(binary, [`-chdir=${dir}`, ...args], { encoding: "utf-8", env: { ...env(given), TF_IN_AUTOMATION: "1" }, maxBuffer: 512 * 1024 * 1024 });
|
|
62
68
|
const stdout = p.stdout ?? "";
|
|
63
|
-
|
|
69
|
+
const stderr = p.stderr ?? "";
|
|
70
|
+
const error = p.error && p.status === null ? { error: p.error.message } : {};
|
|
71
|
+
return { code: p.error && p.status === null ? 127 : (p.status ?? 1), stdout, out: `${stdout}${stderr}${p.error ? p.error.message : ""}`, stderr, ...error };
|
|
64
72
|
};
|
|
65
73
|
const terragrunt = (file, args, opts) => new Promise((done) => {
|
|
66
74
|
execFile(file, [...args], { cwd: opts.cwd, env: env({ ...process.env, ...opts.env }), maxBuffer: 256 * 1024 * 1024 }, (err, stdout, stderr) => {
|
|
@@ -111,7 +119,9 @@ export function commandTraceEnv(env, trace, span, options) {
|
|
|
111
119
|
* `process.command_args`) under `parent(command)`, the variables
|
|
112
120
|
* {@link commandTraceEnv} gives for it, and the span ended with
|
|
113
121
|
* `process.exit.code` once the command ends, failed unless it exited 0 (or 2
|
|
114
|
-
* for a `plan`, `-detailed-exitcode`'s changes)
|
|
122
|
+
* for a `plan`, `-detailed-exitcode`'s changes), its error the result's
|
|
123
|
+
* `error` when the command did not exit (a binary that could not be started,
|
|
124
|
+
* chant#5007), `exit <code>` otherwise. A command timed apart from
|
|
115
125
|
* its call (`at`, a Terragrunt unit's plan) spans the time it ran. Pass it as
|
|
116
126
|
* `engineTimings({ inner })`, so the engine's spans join the trace and reach
|
|
117
127
|
* the timings' receiver.
|
|
@@ -127,7 +137,7 @@ export function commandTrace(options) {
|
|
|
127
137
|
if (r.at)
|
|
128
138
|
span.start = BigInt(Math.round(r.at.start)) * 1000000n;
|
|
129
139
|
const ok = r.code === 0 || (c.name === "plan" && r.code === 2);
|
|
130
|
-
options.trace.end(span, { "process.exit.code": r.code ?? -1 }, ok ? undefined : `exit ${r.code}`, r.at?.end);
|
|
140
|
+
options.trace.end(span, { "process.exit.code": r.code ?? -1 }, ok ? undefined : r.code === null && r.error ? r.error : `exit ${r.code}`, r.at?.end);
|
|
131
141
|
},
|
|
132
142
|
};
|
|
133
143
|
};
|
package/dist/integrity.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"algorithm": "sha256",
|
|
3
3
|
"artifacts": {
|
|
4
|
-
"manifest.json": "
|
|
4
|
+
"manifest.json": "df2b0411d8fcf78e1fc85504962e10d3ab634ae04c2adc7c2ef4fcb0c05a8c9b",
|
|
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": "
|
|
62
|
+
"composite": "69c0f294eadcf220465b6995896cb0db3fa3a4d34be0ab9448ada0e987cc7d1f"
|
|
63
63
|
}
|
package/dist/manifest.json
CHANGED
|
@@ -47,6 +47,12 @@ export interface TerraformCommandResult {
|
|
|
47
47
|
start: number;
|
|
48
48
|
end: number;
|
|
49
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* Why the command did not exit on its own, when `code` is null (chant#5007):
|
|
52
|
+
* the spawn error's message for a binary that could not be started, the
|
|
53
|
+
* error a run threw, or why a Terragrunt unit did not run.
|
|
54
|
+
*/
|
|
55
|
+
error?: string;
|
|
50
56
|
}
|
|
51
57
|
/** What a {@link TerraformCommandHook} answers for one command (chant#4865). */
|
|
52
58
|
export interface TerraformCommandRun {
|
|
@@ -93,6 +99,18 @@ export declare function scopedCommand(scope: CommandScope): Pick<TerraformComman
|
|
|
93
99
|
* the exit code, time and output. Outside a scope, `exec` as it is.
|
|
94
100
|
*/
|
|
95
101
|
export declare function hookedBinaryExec(exec: BinaryExec): BinaryExec;
|
|
102
|
+
/**
|
|
103
|
+
* Several command hooks as one (chant#5007), such as a product's own timing
|
|
104
|
+
* hook and `commandTrace`, for a step that takes one `onCommand`. Each is
|
|
105
|
+
* called in turn and sees the command with the variables the hooks before
|
|
106
|
+
* it answered laid over its `env`, so a hook that reads the command's
|
|
107
|
+
* `OTEL_RESOURCE_ATTRIBUTES` keeps what an earlier one set; the variables
|
|
108
|
+
* they answer are laid over each other in the same order. Once the command
|
|
109
|
+
* ends every `after` is called in the same order, all of them even when
|
|
110
|
+
* one throws, and the first error is thrown after the last. An undefined
|
|
111
|
+
* hook is left out.
|
|
112
|
+
*/
|
|
113
|
+
export declare function composeCommandHooks(...hooks: Array<TerraformCommandHook | undefined>): TerraformCommandHook;
|
|
96
114
|
/** The words of a command line the builders here wrote: plain words, and `quoteArg`'s single-quoted ones. */
|
|
97
115
|
export declare function commandWords(line: string): string[];
|
|
98
116
|
//# sourceMappingURL=command-hook.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-hook.d.ts","sourceRoot":"","sources":["../../src/op/command-hook.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,UAAU,EAAO,MAAM,eAAe,CAAC;AAErD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,kGAAkG;IAClG,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM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I,CAAC,YAAY,EAAE,MAAM,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAGvJ;AAED,mJAAmJ;AACnJ,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAGxF;AAED,0EAA0E;AAC1E,wBAAgB,mBAAmB,IAAI,YAAY,GAAG,SAAS,CAE9D;AAED,yEAAyE;AACzE,wBAAgB,aAAa,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAAC,gBAAgB,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,QAAQ,CAAC,CAOrH;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAgC7D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,oBAAoB,GAAG,SAAS,CAAC,GAAG,oBAAoB,CA0B3G;AAKD,6GAA6G;AAC7G,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAqBnD"}
|
package/dist/op/command-hook.js
CHANGED
|
@@ -74,15 +74,60 @@ export function hookedBinaryExec(exec) {
|
|
|
74
74
|
ran = await exec(binary, args, dir, given.env ? { ...env, ...given.env } : env, io);
|
|
75
75
|
}
|
|
76
76
|
catch (err) {
|
|
77
|
-
|
|
77
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
78
|
+
await given.after?.({ code: null, seconds: (performance.now() - start) / 1000, stdout: "", stderr: message, error: message });
|
|
78
79
|
throw err;
|
|
79
80
|
}
|
|
80
|
-
// `out` holds stdout and stderr together, as they came; stderr
|
|
81
|
-
const stderr = ran.out.startsWith(ran.stdout) ? ran.out.slice(ran.stdout.length) : ran.out;
|
|
82
|
-
|
|
81
|
+
// `out` holds stdout and stderr together, as they came; an exec that does not keep stderr apart leaves what is left once stdout is taken out of it.
|
|
82
|
+
const stderr = ran.stderr ?? (ran.out.startsWith(ran.stdout) ? ran.out.slice(ran.stdout.length) : ran.out);
|
|
83
|
+
// A binary that could not be started did not exit (chant#5007): its code is the hook's null, with the spawn error's message.
|
|
84
|
+
const failed = ran.error !== undefined ? { code: null, error: ran.error } : { code: ran.code };
|
|
85
|
+
await given.after?.({ ...failed, seconds: (performance.now() - start) / 1000, stdout: ran.stdout, stderr });
|
|
83
86
|
return ran;
|
|
84
87
|
};
|
|
85
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Several command hooks as one (chant#5007), such as a product's own timing
|
|
91
|
+
* hook and `commandTrace`, for a step that takes one `onCommand`. Each is
|
|
92
|
+
* called in turn and sees the command with the variables the hooks before
|
|
93
|
+
* it answered laid over its `env`, so a hook that reads the command's
|
|
94
|
+
* `OTEL_RESOURCE_ATTRIBUTES` keeps what an earlier one set; the variables
|
|
95
|
+
* they answer are laid over each other in the same order. Once the command
|
|
96
|
+
* ends every `after` is called in the same order, all of them even when
|
|
97
|
+
* one throws, and the first error is thrown after the last. An undefined
|
|
98
|
+
* hook is left out.
|
|
99
|
+
*/
|
|
100
|
+
export function composeCommandHooks(...hooks) {
|
|
101
|
+
const list = hooks.filter((h) => h !== undefined);
|
|
102
|
+
return async (command) => {
|
|
103
|
+
const runs = [];
|
|
104
|
+
let env = {};
|
|
105
|
+
for (const hook of list) {
|
|
106
|
+
const run = (await hook(Object.keys(env).length > 0 ? { ...command, env: { ...command.env, ...env } } : command)) ?? {};
|
|
107
|
+
runs.push(run);
|
|
108
|
+
if (run.env)
|
|
109
|
+
env = { ...env, ...run.env };
|
|
110
|
+
}
|
|
111
|
+
if (runs.every((r) => !r.env && !r.after))
|
|
112
|
+
return;
|
|
113
|
+
return {
|
|
114
|
+
...(Object.keys(env).length > 0 ? { env } : {}),
|
|
115
|
+
after: async (result) => {
|
|
116
|
+
let first;
|
|
117
|
+
for (const run of runs) {
|
|
118
|
+
try {
|
|
119
|
+
await run.after?.(result);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
first ?? (first = { error });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (first)
|
|
126
|
+
throw first.error;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
}
|
|
86
131
|
/** A word single-quoted for a shell, as `quoteArg` writes it. */
|
|
87
132
|
const quoteWord = (w) => `'${w.replace(/'/g, `'\\''`)}'`;
|
|
88
133
|
/** The words of a command line the builders here wrote: plain words, and `quoteArg`'s single-quoted ones. */
|
package/dist/state/exec.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ export interface Ran {
|
|
|
3
3
|
stdout: string;
|
|
4
4
|
/** stdout and stderr together, as they came. */
|
|
5
5
|
out: string;
|
|
6
|
+
/** stderr alone, less the lines `io.stderr` took (chant#5007). {@link runBinary} sets it; an exec of a product's or a test's may leave it out. */
|
|
7
|
+
stderr?: string;
|
|
8
|
+
/** Why the binary did not run, when it could not be started: the spawn error's message (chant#5007). `code` is then 127. */
|
|
9
|
+
error?: string;
|
|
6
10
|
}
|
|
7
11
|
/** What a run of the binary hands back while it runs: `stderr` sees each line of stderr as it arrives, and a line it takes (true) is left out of `out`. */
|
|
8
12
|
export interface BinaryIo {
|
package/dist/state/exec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exec.d.ts","sourceRoot":"","sources":["../../src/state/exec.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,gDAAgD;IAChD,GAAG,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"exec.d.ts","sourceRoot":"","sources":["../../src/state/exec.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,GAAG;IAClB,IAAI,EAAE,MA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W,aAAa;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC;IACtB,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CACtB;AAED,wEAAwE;AACxE,eAAO,MAAM,SAAS,MAAO,MAAM,iBAAU,MAAgE,CAAC;AAE9G,yEAAyE;AACzE,wBAAsB,SAAS,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAc9I"}
|
package/dist/state/exec.js
CHANGED
|
@@ -11,6 +11,7 @@ import { FORGE_TOKEN_VARS } from "@intentius/chant/ci-handover";
|
|
|
11
11
|
export const runBinary = (binary, args, dir, env, io) => new Promise((done) => {
|
|
12
12
|
const child = spawn(binary, [`-chdir=${dir}`, ...args], { env: { ...env, TF_IN_AUTOMATION: "1" }, stdio: ["ignore", "pipe", "pipe"] });
|
|
13
13
|
let stdout = "";
|
|
14
|
+
let stderr = "";
|
|
14
15
|
let out = "";
|
|
15
16
|
let partial = "";
|
|
16
17
|
const stderrLine = (line) => {
|
|
@@ -21,8 +22,10 @@ export const runBinary = (binary, args, dir, env, io) => new Promise((done) => {
|
|
|
21
22
|
catch {
|
|
22
23
|
taken = false;
|
|
23
24
|
}
|
|
24
|
-
if (!taken)
|
|
25
|
+
if (!taken) {
|
|
25
26
|
out += `${line}\n`;
|
|
27
|
+
stderr += `${line}\n`;
|
|
28
|
+
}
|
|
26
29
|
};
|
|
27
30
|
child.stdout.on("data", (d) => {
|
|
28
31
|
stdout += d;
|
|
@@ -34,6 +37,7 @@ export const runBinary = (binary, args, dir, env, io) => new Promise((done) => {
|
|
|
34
37
|
child.stderr.on("data", (d) => {
|
|
35
38
|
if (!io?.stderr) {
|
|
36
39
|
out += d;
|
|
40
|
+
stderr += d;
|
|
37
41
|
return;
|
|
38
42
|
}
|
|
39
43
|
partial += d;
|
|
@@ -49,11 +53,11 @@ export const runBinary = (binary, args, dir, env, io) => new Promise((done) => {
|
|
|
49
53
|
};
|
|
50
54
|
child.on("error", (e) => {
|
|
51
55
|
rest();
|
|
52
|
-
done({ code: 127, stdout, out: `${out}${e.message}
|
|
56
|
+
done({ code: 127, stdout, out: `${out}${e.message}`, stderr, error: e.message });
|
|
53
57
|
});
|
|
54
58
|
child.on("close", (code) => {
|
|
55
59
|
rest();
|
|
56
|
-
done({ code: code ?? 1, stdout, out });
|
|
60
|
+
done({ code: code ?? 1, stdout, out, stderr });
|
|
57
61
|
});
|
|
58
62
|
});
|
|
59
63
|
/**
|
|
@@ -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;AACnF,OAAO,KAAK,EAAoB,oBAAoB,EAAuB,MAAM,oBAAoB,CAAC;AAEtG,OAAO,EAKL,KAAK,qBAAqB,EAC1B,KAAK,cAAc,EACpB,MAAM,SAAS,CAAC;AACjB,OAAO,EAcL,KAAK,kBAAkB,EACxB,MAAM,SAAS,CAAC;AACjB,OAAO,EAQL,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,CAAC,IAAI,EAAE,MAAM,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5C,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAwDnG;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"}
|
package/dist/terragrunt/run.js
CHANGED
|
@@ -180,6 +180,7 @@ async function unitCommandRuns(hook, input, work, outDir, waveArgs) {
|
|
|
180
180
|
seconds: timed ? (end - start) / 1000 : 0,
|
|
181
181
|
stdout: "",
|
|
182
182
|
stderr: row ? why : "not in Terragrunt's run report, so it did not run",
|
|
183
|
+
...(row ? {} : { error: "not in Terragrunt's run report, so it did not run" }),
|
|
183
184
|
...(timed ? { at: { start, end } } : {}),
|
|
184
185
|
});
|
|
185
186
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-terraform",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.92",
|
|
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.
|
|
96
|
+
"@intentius/chant": "^0.124.92",
|
|
97
97
|
"zod": "^4.3.6"
|
|
98
98
|
},
|
|
99
99
|
"description": "terraform lexicon for chant, existing Terraform root modules as declarable entities",
|
|
@@ -12,7 +12,7 @@ import { tmpdir } from "node:os";
|
|
|
12
12
|
import { dirname, join } from "node:path";
|
|
13
13
|
import { describe, expect, test } from "vitest";
|
|
14
14
|
import { OtlpTrace } from "@intentius/chant/otlp";
|
|
15
|
-
import { commandTrace, commandTraceEnv, engineRunner, withTerraformCommandHook, type TerraformCommand, type TerraformCommandResult } from "./engine-runner";
|
|
15
|
+
import { commandTrace, commandTraceEnv, composeCommandHooks, engineRunner, withTerraformCommandHook, type TerraformCommand, type TerraformCommandHook, type TerraformCommandResult } from "./engine-runner";
|
|
16
16
|
import { engineTimings } from "./engine-timings";
|
|
17
17
|
import type { BinaryExec } from "./state/exec";
|
|
18
18
|
import { planTerragruntWave, terragruntUnitWrapper, type TerragruntExec } from "./terragrunt/run";
|
|
@@ -253,9 +253,95 @@ describe("a Terragrunt unit's plan", () => {
|
|
|
253
253
|
return { code: 0, stdout: "", stderr: "" };
|
|
254
254
|
};
|
|
255
255
|
await planTerragruntWave({ dir, units: ["live/a"], workDir: "w", binary: bin, provisional: true, exec, onCommand: () => ({ after: (r) => void after.push(r) }) });
|
|
256
|
-
expect(after).toMatchObject([{ code: null, seconds: 0 }]);
|
|
256
|
+
expect(after).toMatchObject([{ code: null, seconds: 0, error: "not in Terragrunt's run report, so it did not run" }]);
|
|
257
257
|
let called = false;
|
|
258
258
|
await planTerragruntWave({ dir, units: ["live/a"], workDir: "w2", provisional: true, exec, onCommand: () => void (called = true) });
|
|
259
259
|
expect(called).toBe(false);
|
|
260
260
|
});
|
|
261
261
|
});
|
|
262
|
+
|
|
263
|
+
describe("what a product's own command trace read (chant#5007)", () => {
|
|
264
|
+
test("exec answers stderr apart, and a binary that could not be started as the spawn error", async () => {
|
|
265
|
+
const dir = tmp();
|
|
266
|
+
const bin = script(join(dir, "bin/tofu"), "echo out\necho err >&2\nexit 1");
|
|
267
|
+
const runner = engineRunner();
|
|
268
|
+
const ran = await runner.exec(bin, ["init"], dir, { ...JOB });
|
|
269
|
+
expect(ran).toMatchObject({ code: 1, stdout: "out\n", stderr: "err\n" });
|
|
270
|
+
expect(ran.error).toBeUndefined();
|
|
271
|
+
const missing = await runner.exec(join(dir, "missing"), ["init"], dir, { ...JOB });
|
|
272
|
+
expect(missing.code).toBe(127);
|
|
273
|
+
expect(missing.error).toMatch(/ENOENT/);
|
|
274
|
+
const sync = runner.execSync(bin, ["init"], dir, { ...JOB });
|
|
275
|
+
expect(sync).toMatchObject({ code: 1, stdout: "out\n", stderr: "err\n" });
|
|
276
|
+
expect(runner.execSync(join(dir, "missing"), ["init"], dir, { ...JOB }).error).toMatch(/ENOENT/);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("a stage's own run is traced once, as the product's trace was: span, attributes, variables, exit and error", async () => {
|
|
280
|
+
const dir = tmp();
|
|
281
|
+
const record = join(dir, "env");
|
|
282
|
+
const bin = script(join(dir, "bin/tofu"), `echo "$TRACEPARENT $OTEL_RESOURCE_ATTRIBUTES $OTEL_TRACES_EXPORTER" >> '${record}'\n[ "$1" = "-chdir=${dir}" ] || exit 9\n[ "$2" = plan ] && exit 2\nexit 0`);
|
|
283
|
+
const trace = new OtlpTrace();
|
|
284
|
+
const root = trace.start("root roots/a", undefined, { "terragucci.root": "roots/a" });
|
|
285
|
+
const results: TerraformCommandResult[] = [];
|
|
286
|
+
// The product's own timing half, beside chant's trace.
|
|
287
|
+
const timing: TerraformCommandHook = () => ({ after: (r) => void results.push(r) });
|
|
288
|
+
const hook = composeCommandHooks(timing, commandTrace({ trace, prefix: "terragucci", parent: () => root, env: { OTEL_RESOURCE_ATTRIBUTES: "team=infra" } }));
|
|
289
|
+
const runner = engineRunner({ tokens: ["TG_TOKEN"] });
|
|
290
|
+
await withTerraformCommandHook(hook, { root: "roots/a", method: "plan" }, async () => {
|
|
291
|
+
expect((await runner.exec(bin, ["init", "-input=false"], dir, { ...JOB })).code).toBe(0);
|
|
292
|
+
expect((await runner.exec(bin, ["plan", "-out=p"], dir, { ...JOB })).code).toBe(2);
|
|
293
|
+
expect((await runner.exec(join(dir, "missing"), ["show"], dir, { ...JOB })).code).toBe(127);
|
|
294
|
+
});
|
|
295
|
+
const spans = trace.spans.filter((s) => s !== root);
|
|
296
|
+
expect(spans.map((s) => [s.name, s.parentSpanId, s.kind, s.attributes["process.executable.name"], s.attributes["process.command_args"], s.attributes["process.exit.code"]])).toEqual([
|
|
297
|
+
[`${bin} init`, root.spanId, 3, bin, "init -input=false", 0],
|
|
298
|
+
[`${bin} plan`, root.spanId, 3, bin, "plan -out=p", 2],
|
|
299
|
+
[`${join(dir, "missing")} show`, root.spanId, 3, join(dir, "missing"), "show", -1],
|
|
300
|
+
]);
|
|
301
|
+
expect(spans[0]!.error).toBeUndefined();
|
|
302
|
+
expect(spans[1]!.error).toBeUndefined();
|
|
303
|
+
expect(spans[2]!.error).toMatch(/ENOENT/);
|
|
304
|
+
const lines = readFileSync(record, "utf-8").trim().split("\n");
|
|
305
|
+
expect(lines).toHaveLength(2);
|
|
306
|
+
expect(lines[0]).toContain(trace.traceparent(spans[0]!));
|
|
307
|
+
expect(lines[0]).toContain("team=infra");
|
|
308
|
+
expect(lines[0]).toContain(`terragucci.span_id=${spans[0]!.spanId}`);
|
|
309
|
+
expect(lines[0]).toContain("terragucci.root=roots%2Fa");
|
|
310
|
+
expect(lines[0]!.endsWith(" otlp")).toBe(true);
|
|
311
|
+
expect(results.map((r) => [r.code, r.error === undefined ? undefined : /ENOENT/.test(r.error)])).toEqual([
|
|
312
|
+
[0, undefined],
|
|
313
|
+
[2, undefined],
|
|
314
|
+
[null, true],
|
|
315
|
+
]);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test("composeCommandHooks calls each hook in turn over the variables before it, and every after even when one throws", async () => {
|
|
319
|
+
const order: string[] = [];
|
|
320
|
+
const seenEnv: Array<Record<string, string>> = [];
|
|
321
|
+
const hook = composeCommandHooks(
|
|
322
|
+
(c) => {
|
|
323
|
+
seenEnv.push({ ...c.env });
|
|
324
|
+
return { env: { A: "1", OTEL_RESOURCE_ATTRIBUTES: "first=1" }, after: () => void order.push("first") };
|
|
325
|
+
},
|
|
326
|
+
undefined,
|
|
327
|
+
(c) => {
|
|
328
|
+
seenEnv.push({ ...c.env });
|
|
329
|
+
return {
|
|
330
|
+
env: { B: "2" },
|
|
331
|
+
after: () => {
|
|
332
|
+
order.push("second");
|
|
333
|
+
throw new Error("second failed");
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
},
|
|
337
|
+
() => ({ after: () => void order.push("third") }),
|
|
338
|
+
);
|
|
339
|
+
const base: TerraformCommand = { root: "r", method: "plan", binary: "tofu", name: "plan", args: ["plan"], line: "tofu plan", cwd: "/w", env: { TF_IN_AUTOMATION: "1" } };
|
|
340
|
+
const run = await hook(base);
|
|
341
|
+
expect(seenEnv).toEqual([{ TF_IN_AUTOMATION: "1" }, { TF_IN_AUTOMATION: "1", A: "1", OTEL_RESOURCE_ATTRIBUTES: "first=1" }]);
|
|
342
|
+
expect(run?.env).toEqual({ A: "1", OTEL_RESOURCE_ATTRIBUTES: "first=1", B: "2" });
|
|
343
|
+
await expect(run!.after!({ code: 0, seconds: 1, stdout: "", stderr: "" })).rejects.toThrow("second failed");
|
|
344
|
+
expect(order).toEqual(["first", "second", "third"]);
|
|
345
|
+
expect(await composeCommandHooks(() => undefined)(base)).toBeUndefined();
|
|
346
|
+
});
|
|
347
|
+
});
|
package/src/engine-runner.ts
CHANGED
|
@@ -15,7 +15,13 @@
|
|
|
15
15
|
* - {@link commandTrace} is a `TerraformCommandHook` that opens a span per
|
|
16
16
|
* engine command under the product's span for its root, and gives the
|
|
17
17
|
* command `TRACEPARENT` and the resource attributes that name the span
|
|
18
|
-
* ({@link commandTraceEnv}). Pass it as `engineTimings({ inner })
|
|
18
|
+
* ({@link commandTraceEnv}). Pass it as `engineTimings({ inner })`, or
|
|
19
|
+
* beside a product's own hook through `composeCommandHooks` (chant#5007).
|
|
20
|
+
*
|
|
21
|
+
* Every run `exec` makes answers its stderr apart (`Ran.stderr`) and, for a
|
|
22
|
+
* binary that could not be started, the spawn error (`Ran.error`), which the
|
|
23
|
+
* hook's `after` gets as a null code with `error` and `commandTrace` puts on
|
|
24
|
+
* the span (chant#5007).
|
|
19
25
|
*
|
|
20
26
|
* A Terragrunt unit's plan reaches the same hook through
|
|
21
27
|
* `planTerragruntWave({ onCommand })` (`./terragrunt/run.ts`).
|
|
@@ -35,7 +41,7 @@ import { hookedBinaryExec, type TerraformCommand, type TerraformCommandHook } fr
|
|
|
35
41
|
import { engineEnv, runBinary, tokenlessBinaryExec, type BinaryExec, type Ran } from "./state/exec";
|
|
36
42
|
import type { TerragruntExec } from "./terragrunt/run";
|
|
37
43
|
|
|
38
|
-
export { withTerraformCommandHook, asUpstreamCommands, type TerraformCommand, type TerraformCommandHook, type TerraformCommandResult, type TerraformCommandRun } from "./op/command-hook";
|
|
44
|
+
export { withTerraformCommandHook, asUpstreamCommands, composeCommandHooks, type TerraformCommand, type TerraformCommandHook, type TerraformCommandResult, type TerraformCommandRun } from "./op/command-hook";
|
|
39
45
|
|
|
40
46
|
/** A run of the binary that blocks: `-chdir=<dir>`, `TF_IN_AUTOMATION=1`, as {@link runBinary} runs it. */
|
|
41
47
|
export type BinaryExecSync = (binary: string, args: string[], dir: string, env: NodeJS.ProcessEnv) => Ran;
|
|
@@ -102,7 +108,9 @@ export function engineRunner(options: EngineRunnerOptions = {}): EngineRunner {
|
|
|
102
108
|
const execSync: BinaryExecSync = (binary, args, dir, given) => {
|
|
103
109
|
const p = spawnSync(binary, [`-chdir=${dir}`, ...args], { encoding: "utf-8", env: { ...env(given), TF_IN_AUTOMATION: "1" }, maxBuffer: 512 * 1024 * 1024 });
|
|
104
110
|
const stdout = p.stdout ?? "";
|
|
105
|
-
|
|
111
|
+
const stderr = p.stderr ?? "";
|
|
112
|
+
const error = p.error && p.status === null ? { error: p.error.message } : {};
|
|
113
|
+
return { code: p.error && p.status === null ? 127 : (p.status ?? 1), stdout, out: `${stdout}${stderr}${p.error ? p.error.message : ""}`, stderr, ...error };
|
|
106
114
|
};
|
|
107
115
|
const terragrunt: TerragruntExec = (file, args, opts) =>
|
|
108
116
|
new Promise((done) => {
|
|
@@ -170,7 +178,9 @@ export interface CommandTraceOptions {
|
|
|
170
178
|
* `process.command_args`) under `parent(command)`, the variables
|
|
171
179
|
* {@link commandTraceEnv} gives for it, and the span ended with
|
|
172
180
|
* `process.exit.code` once the command ends, failed unless it exited 0 (or 2
|
|
173
|
-
* for a `plan`, `-detailed-exitcode`'s changes)
|
|
181
|
+
* for a `plan`, `-detailed-exitcode`'s changes), its error the result's
|
|
182
|
+
* `error` when the command did not exit (a binary that could not be started,
|
|
183
|
+
* chant#5007), `exit <code>` otherwise. A command timed apart from
|
|
174
184
|
* its call (`at`, a Terragrunt unit's plan) spans the time it ran. Pass it as
|
|
175
185
|
* `engineTimings({ inner })`, so the engine's spans join the trace and reach
|
|
176
186
|
* the timings' receiver.
|
|
@@ -184,7 +194,7 @@ export function commandTrace(options: CommandTraceOptions): TerraformCommandHook
|
|
|
184
194
|
after: (r) => {
|
|
185
195
|
if (r.at) span.start = BigInt(Math.round(r.at.start)) * 1_000_000n;
|
|
186
196
|
const ok = r.code === 0 || (c.name === "plan" && r.code === 2);
|
|
187
|
-
options.trace.end(span, { "process.exit.code": r.code ?? -1 }, ok ? undefined : `exit ${r.code}`, r.at?.end);
|
|
197
|
+
options.trace.end(span, { "process.exit.code": r.code ?? -1 }, ok ? undefined : r.code === null && r.error ? r.error : `exit ${r.code}`, r.at?.end);
|
|
188
198
|
},
|
|
189
199
|
};
|
|
190
200
|
};
|
package/src/op/command-hook.ts
CHANGED
|
@@ -55,6 +55,12 @@ export interface TerraformCommandResult {
|
|
|
55
55
|
* from the hook's call to its `after`.
|
|
56
56
|
*/
|
|
57
57
|
at?: { start: number; end: number };
|
|
58
|
+
/**
|
|
59
|
+
* Why the command did not exit on its own, when `code` is null (chant#5007):
|
|
60
|
+
* the spawn error's message for a binary that could not be started, the
|
|
61
|
+
* error a run threw, or why a Terragrunt unit did not run.
|
|
62
|
+
*/
|
|
63
|
+
error?: string;
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
/** What a {@link TerraformCommandHook} answers for one command (chant#4865). */
|
|
@@ -151,16 +157,58 @@ export function hookedBinaryExec(exec: BinaryExec): BinaryExec {
|
|
|
151
157
|
try {
|
|
152
158
|
ran = await exec(binary, args, dir, given.env ? { ...env, ...given.env } : env, io);
|
|
153
159
|
} catch (err) {
|
|
154
|
-
|
|
160
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
161
|
+
await given.after?.({ code: null, seconds: (performance.now() - start) / 1000, stdout: "", stderr: message, error: message });
|
|
155
162
|
throw err;
|
|
156
163
|
}
|
|
157
|
-
// `out` holds stdout and stderr together, as they came; stderr
|
|
158
|
-
const stderr = ran.out.startsWith(ran.stdout) ? ran.out.slice(ran.stdout.length) : ran.out;
|
|
159
|
-
|
|
164
|
+
// `out` holds stdout and stderr together, as they came; an exec that does not keep stderr apart leaves what is left once stdout is taken out of it.
|
|
165
|
+
const stderr = ran.stderr ?? (ran.out.startsWith(ran.stdout) ? ran.out.slice(ran.stdout.length) : ran.out);
|
|
166
|
+
// A binary that could not be started did not exit (chant#5007): its code is the hook's null, with the spawn error's message.
|
|
167
|
+
const failed = ran.error !== undefined ? { code: null, error: ran.error } : { code: ran.code };
|
|
168
|
+
await given.after?.({ ...failed, seconds: (performance.now() - start) / 1000, stdout: ran.stdout, stderr });
|
|
160
169
|
return ran;
|
|
161
170
|
};
|
|
162
171
|
}
|
|
163
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Several command hooks as one (chant#5007), such as a product's own timing
|
|
175
|
+
* hook and `commandTrace`, for a step that takes one `onCommand`. Each is
|
|
176
|
+
* called in turn and sees the command with the variables the hooks before
|
|
177
|
+
* it answered laid over its `env`, so a hook that reads the command's
|
|
178
|
+
* `OTEL_RESOURCE_ATTRIBUTES` keeps what an earlier one set; the variables
|
|
179
|
+
* they answer are laid over each other in the same order. Once the command
|
|
180
|
+
* ends every `after` is called in the same order, all of them even when
|
|
181
|
+
* one throws, and the first error is thrown after the last. An undefined
|
|
182
|
+
* hook is left out.
|
|
183
|
+
*/
|
|
184
|
+
export function composeCommandHooks(...hooks: Array<TerraformCommandHook | undefined>): TerraformCommandHook {
|
|
185
|
+
const list = hooks.filter((h): h is TerraformCommandHook => h !== undefined);
|
|
186
|
+
return async (command) => {
|
|
187
|
+
const runs: TerraformCommandRun[] = [];
|
|
188
|
+
let env: Record<string, string> = {};
|
|
189
|
+
for (const hook of list) {
|
|
190
|
+
const run = (await hook(Object.keys(env).length > 0 ? { ...command, env: { ...command.env, ...env } } : command)) ?? {};
|
|
191
|
+
runs.push(run);
|
|
192
|
+
if (run.env) env = { ...env, ...run.env };
|
|
193
|
+
}
|
|
194
|
+
if (runs.every((r) => !r.env && !r.after)) return;
|
|
195
|
+
return {
|
|
196
|
+
...(Object.keys(env).length > 0 ? { env } : {}),
|
|
197
|
+
after: async (result) => {
|
|
198
|
+
let first: { error: unknown } | undefined;
|
|
199
|
+
for (const run of runs) {
|
|
200
|
+
try {
|
|
201
|
+
await run.after?.(result);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
first ??= { error };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (first) throw first.error;
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
164
212
|
/** A word single-quoted for a shell, as `quoteArg` writes it. */
|
|
165
213
|
const quoteWord = (w: string): string => `'${w.replace(/'/g, `'\\''`)}'`;
|
|
166
214
|
|
package/src/state/exec.ts
CHANGED
|
@@ -13,6 +13,10 @@ export interface Ran {
|
|
|
13
13
|
stdout: string;
|
|
14
14
|
/** stdout and stderr together, as they came. */
|
|
15
15
|
out: string;
|
|
16
|
+
/** stderr alone, less the lines `io.stderr` took (chant#5007). {@link runBinary} sets it; an exec of a product's or a test's may leave it out. */
|
|
17
|
+
stderr?: string;
|
|
18
|
+
/** Why the binary did not run, when it could not be started: the spawn error's message (chant#5007). `code` is then 127. */
|
|
19
|
+
error?: string;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
/** What a run of the binary hands back while it runs: `stderr` sees each line of stderr as it arrives, and a line it takes (true) is left out of `out`. */
|
|
@@ -28,6 +32,7 @@ export const runBinary: BinaryExec = (binary, args, dir, env, io) =>
|
|
|
28
32
|
new Promise((done) => {
|
|
29
33
|
const child = spawn(binary, [`-chdir=${dir}`, ...args], { env: { ...env, TF_IN_AUTOMATION: "1" }, stdio: ["ignore", "pipe", "pipe"] });
|
|
30
34
|
let stdout = "";
|
|
35
|
+
let stderr = "";
|
|
31
36
|
let out = "";
|
|
32
37
|
let partial = "";
|
|
33
38
|
const stderrLine = (line: string): void => {
|
|
@@ -37,7 +42,10 @@ export const runBinary: BinaryExec = (binary, args, dir, env, io) =>
|
|
|
37
42
|
} catch {
|
|
38
43
|
taken = false;
|
|
39
44
|
}
|
|
40
|
-
if (!taken)
|
|
45
|
+
if (!taken) {
|
|
46
|
+
out += `${line}\n`;
|
|
47
|
+
stderr += `${line}\n`;
|
|
48
|
+
}
|
|
41
49
|
};
|
|
42
50
|
child.stdout.on("data", (d) => {
|
|
43
51
|
stdout += d;
|
|
@@ -48,6 +56,7 @@ export const runBinary: BinaryExec = (binary, args, dir, env, io) =>
|
|
|
48
56
|
child.stderr.on("data", (d) => {
|
|
49
57
|
if (!io?.stderr) {
|
|
50
58
|
out += d;
|
|
59
|
+
stderr += d;
|
|
51
60
|
return;
|
|
52
61
|
}
|
|
53
62
|
partial += d;
|
|
@@ -61,11 +70,11 @@ export const runBinary: BinaryExec = (binary, args, dir, env, io) =>
|
|
|
61
70
|
};
|
|
62
71
|
child.on("error", (e) => {
|
|
63
72
|
rest();
|
|
64
|
-
done({ code: 127, stdout, out: `${out}${e.message}
|
|
73
|
+
done({ code: 127, stdout, out: `${out}${e.message}`, stderr, error: e.message });
|
|
65
74
|
});
|
|
66
75
|
child.on("close", (code) => {
|
|
67
76
|
rest();
|
|
68
|
-
done({ code: code ?? 1, stdout, out });
|
|
77
|
+
done({ code: code ?? 1, stdout, out, stderr });
|
|
69
78
|
});
|
|
70
79
|
});
|
|
71
80
|
|
package/src/terragrunt/run.ts
CHANGED
|
@@ -290,6 +290,7 @@ async function unitCommandRuns(
|
|
|
290
290
|
seconds: timed ? (end - start) / 1000 : 0,
|
|
291
291
|
stdout: "",
|
|
292
292
|
stderr: row ? why : "not in Terragrunt's run report, so it did not run",
|
|
293
|
+
...(row ? {} : { error: "not in Terragrunt's run report, so it did not run" }),
|
|
293
294
|
...(timed ? { at: { start, end } } : {}),
|
|
294
295
|
});
|
|
295
296
|
}
|