feat: respect user's shell environment in subprocess execution

This commit is contained in:
Paul Gauthier (aider) 2024-09-10 06:40:07 -07:00
parent b8ce472cb6
commit e3b7b80280

View file

@ -26,12 +26,27 @@ def run_cmd_subprocess(command, verbose=False):
if verbose: if verbose:
print("Using run_cmd_subprocess:", command) print("Using run_cmd_subprocess:", command)
try: try:
# Determine the appropriate shell
if platform.system() == "Windows":
# Use PowerShell if it's the parent process
if "powershell" in os.environ.get("PSModulePath", "").lower():
shell = "powershell"
command = f"powershell -Command {command}"
else:
shell = "cmd"
else:
shell = os.environ.get("SHELL", "/bin/sh")
if verbose:
print(f"Using shell: {shell}")
process = subprocess.Popen( process = subprocess.Popen(
command, command,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
text=True, text=True,
shell=True, shell=True,
executable=shell,
encoding=sys.stdout.encoding, encoding=sys.stdout.encoding,
errors="replace", errors="replace",
bufsize=1, bufsize=1,