Skip to content

The agent that turns websites into APIs!

fix: capture stderr only to avoid double execution on failure

kalil03215218e8754b99
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 19dc8c5..a61a2cc 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -2165,17 +2165,19 @@ def run_script(ctx, identifier, script_args, file_name, list_scripts):
python_path = str(venv_python)
- # Execute with real-time output
+ # Execute with real-time stdout, capture stderr for import error detection
cmd = [python_path, str(script), *script_args]
- result = subprocess.run(cmd)
+ result = subprocess.run(cmd, stderr=subprocess.PIPE, text=True)
+
+ # Print stderr so the user sees it, then check for missing imports
+ if result.stderr:
+ sys.stderr.write(result.stderr)
- # On failure, check if it's a missing import and offer to install
if result.returncode != 0:
- probe = subprocess.run(cmd, capture_output=True, text=True)
- if "ModuleNotFoundError: No module named" in (probe.stderr or ""):
+ if "ModuleNotFoundError: No module named" in (result.stderr or ""):
import re as _re
- match = _re.search(r"No module named ['\"]([^'\"]+)['\"]", probe.stderr)
+ match = _re.search(r"No module named ['\"]([^'\"]+)['\"]", result.stderr)
if match:
missing = match.group(1)
console.print(f"[yellow]Missing dependency: {missing}[/yellow]")
diff --git a/tests/test_run_command.py b/tests/test_run_command.py
index c05dd82..72fbce4 100644
--- a/tests/test_run_command.py
+++ b/tests/test_run_command.py
@@ -563,12 +563,11 @@ class TestRunCommandImportRetry:
(venv / "bin" / "python").write_text("#!/bin/sh")
(venv / "bin" / "pip").write_text("#!/bin/sh")
- # 1st call: real-time run fails, 2nd: probe with captured stderr, 3rd: pip install, 4th: retry succeeds
- first_run = MagicMock(returncode=1)
- probe = MagicMock(returncode=1, stdout="", stderr="ModuleNotFoundError: No module named 'pandas'")
+ # 1st call: run with captured stderr fails, 2nd: pip install, 3rd: retry succeeds
+ first_run = MagicMock(returncode=1, stderr="ModuleNotFoundError: No module named 'pandas'")
pip_ok = MagicMock(returncode=0, stdout="", stderr="")
retry_ok = MagicMock(returncode=0)
- with patch("subprocess.run", side_effect=[first_run, probe, pip_ok, retry_ok]):
+ with patch("subprocess.run", side_effect=[first_run, pip_ok, retry_ok]):
with patch("questionary.confirm") as mock_confirm:
mock_confirm.return_value.ask.return_value = True
result = cli_runner.invoke(main, ["run", "def789ghi012"])
@@ -584,9 +583,8 @@ class TestRunCommandImportRetry:
(venv / "bin" / "python").write_text("#!/bin/sh")
(venv / "bin" / "pip").write_text("#!/bin/sh")
- first_run = MagicMock(returncode=1)
- probe = MagicMock(returncode=1, stdout="", stderr="ModuleNotFoundError: No module named 'foo'")
- with patch("subprocess.run", side_effect=[first_run, probe]):
+ first_run = MagicMock(returncode=1, stderr="ModuleNotFoundError: No module named 'foo'")
+ with patch("subprocess.run", side_effect=[first_run]):
with patch("questionary.confirm") as mock_confirm:
mock_confirm.return_value.ask.return_value = False
result = cli_runner.invoke(main, ["run", "def789ghi012"])
@@ -600,9 +598,8 @@ class TestRunCommandImportRetry:
(venv / "bin").mkdir()
(venv / "bin" / "python").write_text("#!/bin/sh")
- first_run = MagicMock(returncode=1)
- probe = MagicMock(returncode=1, stdout="", stderr="TypeError: something else broke")
- with patch("subprocess.run", side_effect=[first_run, probe]):
+ first_run = MagicMock(returncode=1, stderr="TypeError: something else broke")
+ with patch("subprocess.run", side_effect=[first_run]):
result = cli_runner.invoke(main, ["run", "def789ghi012"])
assert result.exit_code == 1