fix(bash-ast): stop deparse from changing shell semantics - #122
Merged
Conversation
Here-doc bodies are read as opaque text, `&` stays on the command it backgrounds, brace groups and subshells stay grouped, and NAME=value is an assignment only in assignment position. Comments can be kept, and parse takes a hard timeout so a pathological input fails instead of hanging. Closes constructive-io/constructive-planning#1646
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
deparse(parse(src))silently changed what a script does. Measured over the 111+ inlinerun:blocks ofconstructive-db/.github/workflows(constructive-io/constructive-planning#1646): 40 changed output, 3 failed to parse, 1 hung the parser. After this change all 114 extracted blocks round trip with an unchanged AST, an idempotent second deparse, and output accepted bybash -n.Five root causes, all in the lexer/parser/deparser boundary:
cat <<EOF\n|\nEOFspun forever.<</<<-now register aPendingHereDoc; the newline handler reads raw lines until the delimiter and hangs the body off the operator token, so the body is opaque text carried onRedirect.heredocand re-emitted after the line that opened it.&was consumed as a plain separator, sokubectl proxy &became blocking.asyncnow lives on the command node (BaseNode.async) and the deparser appends&.CompoundList, so[ -n "$x" ] || { echo …; exit 0; }madeexit 0unconditional and{ a; b; } | teepiped onlyb. NewBraceGroupnode, kept grouped through pipelines and&&/||; brace groups, subshells and compound commands can now carryredirects.NAME=valueword was anASSIGNMENT_WORD, which turnedpsql --set ON_ERROR_STOP=1into a second statement and brokelocal label="$1". The lexer now tracks command position (;,&,|,&&,||, newline,(,),{, reserved words) and only classifies an assignment there or as an operand ofexport/local/declare/readonly/typeset.<(…)and{…}were split on punctuation. Process substitutions are read whole;{/}are reserved words only when standalone, so-o jsonpath={.metadata.labels}survives.Two new options, both opt-in so existing behavior is unchanged:
keepCommentsemitsCOMMENTtokens andCommentnodes (previously comments were dropped).timeoutMsis a hard wall-clock budget checked inLexer.tokenize()and the parser's list loops — the class of defect here is a hang, and a hang must be a loud failure. The parser also throws on a no-progress iteration instead of spinning.The deparser now decides layout per list: a list is emitted multiline when any statement carries a here-doc body, is backgrounded, or is a comment, and inline (
;-joined) otherwise — which is what keeps{ echo a; exit 0; }andif …; then …; fion one line while a here-doc forces a break.Tests
__tests__/semantics.test.ts— one case per defect in the issue, asserting the AST shape and, where the script is side-effect free, thatbash -cproduces identical output for the original and the deparsed form.__tests__/corpus.test.ts— the 114 real workflow blocks, committed as__fixtures__/workflows/*.sh, each parsed under a 5s budget and checked for AST equality, deparse idempotence, andbash -nvalidity.196 tests pass;
eslint,tsc --noEmitandmakage buildare clean.Closes constructive-io/constructive-planning#1646
Link to Devin session: https://app.devin.ai/sessions/51c2a0e49ae642a9ab0bbaee1d269b78
Requested by: @pyramation