diff --git a/.size-limit.json b/.size-limit.json index 84e5efbf88..985d3e3adc 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -19,7 +19,7 @@ "README.md", "LICENSE" ], - "limit": "129.46 kB", + "limit": "129.54 kB", "brotli": false, "gzip": false }, @@ -33,7 +33,7 @@ "build/globals.js", "build/deno.js" ], - "limit": "881.85 kB", + "limit": "881.93 kB", "brotli": false, "gzip": false }, @@ -66,7 +66,7 @@ "README.md", "LICENSE" ], - "limit": "944.60 kB", + "limit": "944.68 kB", "brotli": false, "gzip": false } diff --git a/build/core.cjs b/build/core.cjs index cd9f1f0706..c4271432c4 100644 --- a/build/core.cjs +++ b/build/core.cjs @@ -583,7 +583,8 @@ var _ProcessPromise = class _ProcessPromise extends Promise { }, on: { start: () => { - $2.log({ kind: "cmd", cmd: $2.cmd, cwd, verbose: self.isVerbose(), id }); + const log2 = () => $2.log({ kind: "cmd", cmd: $2.cmd, cwd, verbose: self.isVerbose(), id }); + self.sync ? log2() : queueMicrotask(log2); self.timeout($2.timeout, $2.timeoutSignal); }, stdout: (data) => { diff --git a/src/core.ts b/src/core.ts index 39c320149f..c837186add 100644 --- a/src/core.ts +++ b/src/core.ts @@ -360,7 +360,11 @@ export class ProcessPromise extends Promise { }, on: { start: () => { - $.log({ kind: 'cmd', cmd: $.cmd, cwd, verbose: self.isVerbose(), id }) + // A process starts eagerly, so chained configurators (`quiet()`, `verbose()`) + // are applied after this hook. Defer the entry to pick them up. + // Sync mode gets no such chance and needs the cmd logged before its output. + const log = () => $.log({ kind: 'cmd', cmd: $.cmd, cwd, verbose: self.isVerbose(), id }) + self.sync ? log() : queueMicrotask(log) self.timeout($.timeout, $.timeoutSignal) }, stdout: (data) => { diff --git a/test/core.test.js b/test/core.test.js index b80a2eda91..13fdc557c8 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -517,6 +517,52 @@ describe('core', () => { id, }) }) + + it('applies chained configurators to the `cmd` entry', async () => { + // https://github.com/google/zx/issues/931 + const entries = [] + const log = (entry) => entries.push([entry.kind, entry.verbose]) + const capture = async (p) => { + entries.length = 0 + await p + return entries.slice() + } + + assert.deepEqual( + await capture($({ log, verbose: true })`echo foo`.quiet()), + [ + ['cmd', false], + ['stdout', false], + ['end', false], + ] + ) + assert.deepEqual( + await capture($({ log, verbose: true })`echo foo`.verbose(false)), + [ + ['cmd', false], + ['stdout', false], + ['end', false], + ] + ) + assert.deepEqual( + await capture( + $({ log, verbose: true, quiet: true })`echo foo`.quiet(false) + ), + [ + ['cmd', true], + ['stdout', true], + ['end', true], + ] + ) + }) + + it('keeps the `cmd` entry ahead of the output in sync mode', () => { + const entries = [] + const log = (entry) => entries.push(entry.kind) + $.sync({ log, verbose: true })`echo foo` + + assert.deepEqual(entries, ['cmd', 'stdout', 'end']) + }) }) describe('ProcessPromise', () => {