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.
This commit is contained in:
Resham Joshi 2026-11-06 19:42:10 -08:00
parent 21dcca2e5c
commit f6a8cec605
2 changed files with 8 additions and 5 deletions

View file

@ -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)

View file

@ -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' }))