From f6a8cec60524ef20e5e5cefe2e4768e10f781a1f Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:42:10 -0800 Subject: [PATCH] fix(guard): a backgrounded command is work, a single-quoted $( is not `sleep 5 && gh pr checks 1&npm test` ran the tests in the background yet passed as a check; a bare & outside quotes now counts as work (&& and 2>&1 are fine). Substitution is now looked for outside single quotes only, so a jq filter like '$(.name)' stays a wait while $( in double quotes still counts. --- src/guard/hooks.ts | 7 +++++-- tests/guard-loop.test.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/guard/hooks.ts b/src/guard/hooks.ts index ab27ef46..30e30986 100644 --- a/src/guard/hooks.ts +++ b/src/guard/hooks.ts @@ -114,8 +114,11 @@ function isWaitingCheck(command: string): boolean { const first = lines[0] ?? '' // `sleep 5 &&` or a check ending in && or | is unfinished, not a check. if ((m[1] && m[1] !== ';' && !first) || /^(?:&&|\|\|?|;)|(?:&&|\|\|?)$/.test(first)) return false - // Command and process substitution run work wherever they sit. - if (/\$\(|`|[<>]\(/.test(first) || WRITE_REDIRECT.test(first.replace(/'[^']*'|"[^"]*"/g, "''"))) return false + // Command and process substitution run work anywhere outside single quotes; + // a bare & backgrounds a command, and a write redirect writes a file. + const unquoted = first.replace(/'[^']*'|"[^"]*"/g, "''") + const expanding = first.replace(/"[^"]*"|'[^']*'/g, q => (q.startsWith("'") ? "''" : q)) + if (/\$\(|`|[<>]\(/.test(expanding) || /(?])&(?![&>])/.test(unquoted) || WRITE_REDIRECT.test(unquoted)) return false const rest = first.replace(ENV_PREFIX, '$1') if (/(?:^|[\s;&|])env(?=\s|$)/.test(rest) || hasLooseAssignment(rest)) return false return !rest || isReadShapedBashCommand(rest) || GH_READ.test(rest) || BARE_ECHO.test(rest) diff --git a/tests/guard-loop.test.ts b/tests/guard-loop.test.ts index 0e7af47d..d8a217f9 100644 --- a/tests/guard-loop.test.ts +++ b/tests/guard-loop.test.ts @@ -209,10 +209,10 @@ describe('identical-call loop', () => { expect(outs.every(o => o === '')).toBe(true) }) - it('keeps the exemption for a quoted argument that spans lines or holds a pipe', async () => { + it('keeps the exemption for a quoted argument that spans lines or holds shell characters', async () => { const env = await setup() const outs: string[] = [] - for (const command of ["sleep 5 && gh pr checks 1909 --jq '.[]\n| .name'", "sleep 5 && gh pr checks 1909 --jq '.[] | .name'"]) { + for (const command of ["sleep 5 && gh pr checks 1909 --jq '.[]\n| .name'", "sleep 5 && gh pr checks 1909 --jq '.[] | .name'", "sleep 5 && gh pr checks 1 --jq '$(.name)'", "sleep 5 && gh pr checks 1 --jq 'a & b'"]) { for (let i = 0; i < 12; i++) outs.push(await call(env, 'Bash', { command }, { fails: true, output: 'pending' })) } expect(outs.every(o => o === '')).toBe(true) @@ -237,7 +237,7 @@ describe('identical-call loop', () => { }) it('still counts a sleep followed by env or a second line that does work', async () => { - for (const command of ['sleep 5 && env CI=1 npm run build', 'sleep 5 && cat log\nnpm run build', 'sleep 5 && cat log && env npm run build', 'sleep 5 && env X="$(npm test)" gh pr checks 1909', 'sleep 5 && env X=`npm test` gh pr checks 1909', 'sleep 5 && env X="${Y:-a}" gh pr checks 1909', 'sleep 5 && X=x;npm gh pr checks 1909', 'sleep 5 && env X=x;npm gh pr checks 1909', 'sleep 5 && X=a>b cat log', 'sleep 5 && gh pr checks 1909 > report.txt', 'sleep 5 && gh pr checks 1909 >> report.txt', 'sleep 5 && cat log 2>err.txt', 'sleep 5 && gh pr checks 1909 <(npm test)', 'sleep 5 && cat <(npm test)']) { + for (const command of ['sleep 5 && env CI=1 npm run build', 'sleep 5 && cat log\nnpm run build', 'sleep 5 && cat log && env npm run build', 'sleep 5 && env X="$(npm test)" gh pr checks 1909', 'sleep 5 && env X=`npm test` gh pr checks 1909', 'sleep 5 && env X="${Y:-a}" gh pr checks 1909', 'sleep 5 && X=x;npm gh pr checks 1909', 'sleep 5 && env X=x;npm gh pr checks 1909', 'sleep 5 && X=a>b cat log', 'sleep 5 && gh pr checks 1909 > report.txt', 'sleep 5 && gh pr checks 1909 >> report.txt', 'sleep 5 && cat log 2>err.txt', 'sleep 5 && gh pr checks 1909 <(npm test)', 'sleep 5 && cat <(npm test)', 'sleep 5 && gh pr checks 1&npm test', 'sleep 5 && cat log & npm test', 'sleep 5 && gh pr checks 1 --jq "$(npm test)"', `sleep 5 && gh pr checks 1 --jq "it's $(npm test)" 'x'`]) { const env = await setup() const outs: string[] = [] for (let i = 0; i < 12; i++) outs.push(await call(env, 'Bash', { command }, { output: 'Build OK' }))