Skip to content

Commit 3ff32ef

Browse files
committed
fix: update try_parse_word_only_commands_sequence() to return commands in order
1 parent 8595237 commit 3ff32ef

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

codex-rs/core/src/bash.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ pub fn try_parse_word_only_commands_sequence(tree: &Tree, src: &str) -> Option<V
7373
}
7474
}
7575

76+
// Walk uses a stack (LIFO), so re-sort by position to restore source order.
77+
command_nodes.sort_by_key(|node| node.start_byte());
78+
7679
let mut commands = Vec::new();
7780
for node in command_nodes {
7881
if let Some(words) = parse_plain_command_from_node(node, src) {
@@ -150,10 +153,10 @@ mod tests {
150153
let src = "ls && pwd; echo 'hi there' | wc -l";
151154
let cmds = parse_seq(src).unwrap();
152155
let expected: Vec<Vec<String>> = vec![
153-
vec!["wc".to_string(), "-l".to_string()],
154-
vec!["echo".to_string(), "hi there".to_string()],
155-
vec!["pwd".to_string()],
156156
vec!["ls".to_string()],
157+
vec!["pwd".to_string()],
158+
vec!["echo".to_string(), "hi there".to_string()],
159+
vec!["wc".to_string(), "-l".to_string()],
157160
];
158161
assert_eq!(cmds, expected);
159162
}

codex-rs/core/src/parse_command.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,8 @@ fn parse_bash_lc_commands(original: &[String]) -> Option<Vec<ParsedCommand>> {
11561156
// bias toward the primary command when pipelines are present.
11571157
// First, drop obvious small formatting helpers (e.g., wc/awk/etc).
11581158
let had_multiple_commands = all_commands.len() > 1;
1159-
// The bash AST walker yields commands in right-to-left order for
1160-
// connector/pipeline sequences. Reverse to reflect actual execution order.
1161-
let mut filtered_commands = drop_small_formatting_commands(all_commands);
1162-
filtered_commands.reverse();
1159+
// Commands arrive in source order; drop formatting helpers while preserving it.
1160+
let filtered_commands = drop_small_formatting_commands(all_commands);
11631161
if filtered_commands.is_empty() {
11641162
return Some(vec![ParsedCommand::Unknown {
11651163
cmd: script.clone(),

0 commit comments

Comments
 (0)