Skip to content

Commit 29d9576

Browse files
committed
fix(shell): preserve placeholders in let redirection paths
1 parent bc172c0 commit 29d9576

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎apps/sim/lib/execution/code-placeholders/compiler.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,8 @@ describe('code placeholder compiler', () => {
10431043
'let values[{{KEY}}]=1',
10441044
"builtin let 'x={{KEY}}'",
10451045
'let "x=$(printf %s "{{KEY}}")"',
1046+
'let >/dev/null "x={{KEY}}"',
1047+
'echo "$(( $(let "x=1" > /dev/{{KEY}}; printf 1) ))"',
10461048
'values[ 1 + {{KEY}} ]=x',
10471049
'values[$(printf %s "{{KEY}}")]=x',
10481050
'values=([{{KEY}}]=x)',
@@ -1147,6 +1149,18 @@ describe('code placeholder compiler', () => {
11471149
expect(executeShell(compiled.code, compiled.bindings)).toBe('two words\ntwo words\n')
11481150
})
11491151

1152+
it.each(['> /dev/{{KEY}}', '> "/dev/{{KEY}}"', '> "$(printf /dev/%s "{{KEY}}")"'])(
1153+
'keeps let redirection targets out of arithmetic context: %s',
1154+
async (redirect) => {
1155+
const compiled = await compileCodePlaceholders({
1156+
code: `let "x=1" ${redirect}; printf "%s\\n" "$x"`,
1157+
language: CodeLanguage.Shell,
1158+
environmentVariables: { KEY: 'null' },
1159+
})
1160+
expect(executeShell(compiled.code, compiled.bindings)).toBe('1\n')
1161+
}
1162+
)
1163+
11501164
it.each([
11511165
['unquoted', "cat <<EOF >/dev/null\nToday's report\nEOF"],
11521166
['quoted', "cat <<'EOF' >/dev/null\nToday's report\nEOF"],

‎apps/sim/lib/execution/code-placeholders/shell.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ function shellArithmeticCommandStarts(code: string, index: number): boolean {
9191
}
9292

9393
function getShellArithmeticFrame(frame: ShellScanFrame | undefined): ShellScanFrame | undefined {
94-
return frame?.kind === 'arithmetic'
95-
? frame
96-
: (frame?.commandArithmetic ?? frame?.arithmeticParent)
94+
if (frame?.kind === 'arithmetic') return frame
95+
if (!frame?.redirectionTarget && frame?.commandArithmetic) return frame.commandArithmetic
96+
return frame?.arithmeticParent
9797
}
9898

9999
function pushShellFrame(frames: ShellScanFrame[], frame: ShellScanFrame): void {

0 commit comments

Comments
 (0)