Skip to content

fix(cli): don't sys.exit at import when OptionsAI is unavailable (#6)#25

Open
bradsmithmba wants to merge 1 commit into
cloudtrainerwork:masterfrom
bradsmithmba:fix/cli-no-exit-at-import
Open

fix(cli): don't sys.exit at import when OptionsAI is unavailable (#6)#25
bradsmithmba wants to merge 1 commit into
cloudtrainerwork:masterfrom
bradsmithmba:fix/cli-no-exit-at-import

Conversation

@bradsmithmba

Copy link
Copy Markdown

Summary

src/cli/main.py called sys.exit(1) at module scope when OptionsAI failed to import:

    except ImportError as e:
        typer.echo(f"Error importing OptionsAI: {e}", err=True)
        typer.echo("Please ensure all dependencies are installed ...", err=True)
        sys.exit(1)

sys.exit raises SystemExit, so any module that merely imports src.cli.main terminates the entire process. Under pytest this aborts collection:

INTERNALERROR> File ".../tests/cli/test_commands.py", line 9, in <module>
INTERNALERROR>   from src.cli.main import app
INTERNALERROR> File ".../src/cli/main.py", line 38, in <module>
INTERNALERROR>   sys.exit(1)
INTERNALERROR> SystemExit: 1
no tests collected

Closes #6.

Fix

Capture the import error instead of exiting, and report it at command execution time inside get_options_ai() — preserving the original user-facing message and exit code 1 for real CLI usage, while making the module safe to import:

    except ImportError as e:
        OptionsAI = None
        _options_ai_import_error = e
def get_options_ai(config_path=None):
    if OptionsAI is None:
        console.print(f"[red]OptionsAI is unavailable: {_options_ai_import_error}[/red]")
        console.print("Please ensure all dependencies are installed ...")
        raise typer.Exit(1)
    ...

Testing

clean master this branch
pytest tests/cli/ collection INTERNALERROR: SystemExit, 0 collected collects normally
import src.cli.main terminates process imports cleanly

The cli command tests now collect (9 pass, 19 fail). The 19 failures occur because the app-level @app.callback() calls get_options_ai() on every invocation and OptionsAI cannot be constructed while the feature pipeline is broken — that is blocked by #1 and is not introduced by this change. They will pass once #1 lands.

🤖 Generated with Claude Code

src/cli/main.py called sys.exit(1) at module scope when OptionsAI failed
to import. Because sys.exit raises SystemExit, any module that merely
imported src.cli.main aborted the whole process — pytest collection died
with `INTERNALERROR: SystemExit: 1` and no tests ran.

Capture the import failure instead and surface it at command execution
time via get_options_ai(), preserving the original user-facing message and
exit code 1 for actual CLI use while keeping the module safe to import.

Before: `pytest tests/cli/` -> INTERNALERROR SystemExit, 0 collected.
After:  the module imports cleanly and the cli test suite collects. The
remaining command-level failures depend on OptionsAI being constructable
(blocked by cloudtrainerwork#1) and are unrelated to this change.

Closes cloudtrainerwork#6

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

src/cli/main.py calls sys.exit(1) at import time, killing pytest process

1 participant