Skip to content

Commit 4b03d9d

Browse files
committed
fix: run windows cmd wrappers via shell
1 parent d3c1986 commit 4b03d9d

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/promptfoo/cli.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import NoReturn, Optional
1313

1414
_WRAPPER_ENV = "PROMPTFOO_PY_WRAPPER"
15+
_WINDOWS_SHELL_EXTENSIONS = (".bat", ".cmd")
1516

1617

1718
def check_node_installed() -> bool:
@@ -73,6 +74,19 @@ def _find_external_promptfoo() -> Optional[str]:
7374
return promptfoo_path
7475

7576

77+
def _requires_shell(executable: str) -> bool:
78+
if os.name != "nt":
79+
return False
80+
_, ext = os.path.splitext(executable)
81+
return ext.lower() in _WINDOWS_SHELL_EXTENSIONS
82+
83+
84+
def _run_command(cmd: list[str], env: Optional[dict[str, str]] = None) -> subprocess.CompletedProcess:
85+
if _requires_shell(cmd[0]):
86+
return subprocess.run(subprocess.list2cmdline(cmd), shell=True, env=env)
87+
return subprocess.run(cmd, env=env)
88+
89+
7690
def main() -> NoReturn:
7791
"""
7892
Main entry point for the promptfoo CLI wrapper.
@@ -90,15 +104,17 @@ def main() -> NoReturn:
90104
cmd = [promptfoo_path] + sys.argv[1:]
91105
env = os.environ.copy()
92106
env[_WRAPPER_ENV] = "1"
93-
result = subprocess.run(cmd, env=env)
94-
elif shutil.which("npx"):
95-
cmd = ["npx", "-y", "promptfoo@latest"] + sys.argv[1:]
96-
result = subprocess.run(cmd)
107+
result = _run_command(cmd, env=env)
97108
else:
98-
print("ERROR: Neither promptfoo nor npx is available.", file=sys.stderr)
99-
print("Please install promptfoo: npm install -g promptfoo", file=sys.stderr)
100-
print("Or ensure Node.js is properly installed.", file=sys.stderr)
101-
sys.exit(1)
109+
npx_path = shutil.which("npx")
110+
if npx_path:
111+
cmd = [npx_path, "-y", "promptfoo@latest"] + sys.argv[1:]
112+
result = _run_command(cmd)
113+
else:
114+
print("ERROR: Neither promptfoo nor npx is available.", file=sys.stderr)
115+
print("Please install promptfoo: npm install -g promptfoo", file=sys.stderr)
116+
print("Or ensure Node.js is properly installed.", file=sys.stderr)
117+
sys.exit(1)
102118

103119
sys.exit(result.returncode)
104120

0 commit comments

Comments
 (0)