Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions aider/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

def run_cmd(command, verbose=False, error_print=None, cwd=None):
try:
if sys.stdin.isatty() and hasattr(pexpect, "spawn") and platform.system() != "Windows":
if (
sys.stdin.isatty()
and hasattr(pexpect, "spawn")
and platform.system() != "Windows"
):
return run_cmd_pexpect(command, verbose, cwd)

return run_cmd_subprocess(command, verbose, cwd)
Expand Down Expand Up @@ -47,11 +51,8 @@ def run_cmd_subprocess(command, verbose=False, cwd=None, encoding=sys.stdout.enc
shell = os.environ.get("SHELL", "/bin/sh")
parent_process = None

# Determine the appropriate shell
if platform.system() == "Windows":
parent_process = get_windows_parent_process_name()
if parent_process == "powershell.exe":
command = f"powershell -Command {command}"

if verbose:
print("Running command:", command)
Expand All @@ -73,6 +74,7 @@ def run_cmd_subprocess(command, verbose=False, cwd=None, encoding=sys.stdout.enc
)

output = []
assert process.stdout is not None
while True:
chunk = process.stdout.read(1)
if not chunk:
Expand Down Expand Up @@ -113,7 +115,9 @@ def output_callback(b):
# Use the shell from SHELL environment variable
if verbose:
print("Running pexpect.spawn with shell:", shell)
child = pexpect.spawn(shell, args=["-i", "-c", command], encoding="utf-8", cwd=cwd)
child = pexpect.spawn(
shell, args=["-i", "-c", command], encoding="utf-8", cwd=cwd
)
else:
# Fall back to spawning the command directly
if verbose:
Expand Down
14 changes: 13 additions & 1 deletion tests/basic/test_run_cmd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest # noqa: F401

from aider.run_cmd import run_cmd
from aider.run_cmd import run_cmd, run_cmd_subprocess


def test_run_cmd_echo():
Expand All @@ -9,3 +9,15 @@ def test_run_cmd_echo():

assert exit_code == 0
assert output.strip() == "Hello, World!"


def test_run_cmd_subprocess_preserves_commas_in_powershell(monkeypatch):
monkeypatch.setattr("aider.run_cmd.platform.system", lambda: "Windows")
monkeypatch.setattr(
"aider.run_cmd.get_windows_parent_process_name", lambda: "powershell.exe"
)

exit_code, output = run_cmd_subprocess("echo Hello, World!")

assert exit_code == 0
assert output.strip() == "Hello, World!"