Skip to content

fix(cli): name the --app target and exception type when the import fails - #864

Merged
yeongseon merged 3 commits into
mainfrom
fix/cli-import-failure-context
Oct 30, 2026
Merged

yeongseon merged 3 commits into
mainfrom
fix/cli-import-failure-context

Conversation

@yeongseon

@yeongseon yeongseon commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

azure-functions-openapi generate --app module:variable imports the module, so an exception raised by module-level code surfaced as one bare line, for example Failed to generate OpenAPI specification: 'SECRET_KEY', with no mention of the --app target. Every exception raised while importing the target is now reported with the target and the exception type.

Changes

  • handle_generate reports any exception raised by the --app import as Error: Could not import module from --app '<value>': <ExceptionType>: <message>. Only ImportError, ValueError and AttributeError were reported this way before. The exit code (1) and the empty stdout are unchanged.
  • docs/cli.md shows a minimal environment-flag guard for import-time side effects next to the existing warning, the CI invocation, and the message a still-failing import produces.

Validation

Run locally:

  • make check-all: exit 0 (1664 passed, 30 skipped). The skipped tests need the optional azure-functions-validation package, which the Hatch environment does not install.
  • The reproduction from fix(cli): include the --app module in import failure messages #860 (env -u SECRET_KEY azure-functions-openapi generate --app my_app:app) prints Error: Could not import module from --app 'my_app:app': KeyError: 'SECRET_KEY' with exit code 1 and empty stdout. On origin/main it prints only Failed to generate OpenAPI specification: 'SECRET_KEY'.
  • The documented guard works: without the flag the import fails with the new message, with the flag generation succeeds.
  • The four new tests fail when the change is reverted.

GitHub CI on 9e5e3cd: all check runs completed with no failures, including the required Analyze, bandit, ci-required, PR title and semgrep contexts.

Compatibility

The text of the error changes for the three exception types that were already reported: it now includes the exception type, for example ModuleNotFoundError: No module named 'x'. No exit code changes.

Related Issues

Closes #860

yeongseon and others added 2 commits October 30, 2026 19:34
Importing the --app module runs module-level user code, which can raise anything. Only ImportError, ValueError and AttributeError were reported with the target; any other exception fell through to a generic handler that printed just the exception text, for example a bare KeyError message for a missing environment variable. Report every exception raised by the import as: Error: Could not import module from --app <value>: <ExceptionType>: <message>. The exit code and the empty stdout are unchanged. Refs #860

Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
The --app warning recommends guarding external initialization with an environment flag but showed no example. Add a minimal guard, the CI invocation, and the error message a still-failing import produces. Refs #860

Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the CLI import-failure diagnostics change.

No open issues 🎉

What changed in this PR

Improves CLI diagnostics for --app import failures, addressing #860 by identifying the target and exception type while preserving exit code 1 and empty stdout.

Changes:

  • Broadens import-error handling to catch Exception and report contextual errors.
  • Adds regression tests for import-time KeyError and existing failure cases.
  • Documents an environment-flag guard for external initialization during spec generation.
File Description
tests/​test_cli.py Tests error context, exception types, exit codes, and empty stdout.
src/​azure_functions_openapi/​cli.py Reports the --app target and exception type for import failures.
docs/​cli.md Adds a guarded initialization example, CI command, and failure message.

Review effort: Balanced

@yeongseon
yeongseon merged commit 211f4ae into main Oct 30, 2026
29 checks passed
@yeongseon
yeongseon deleted the fix/cli-import-failure-context branch October 30, 2026 13:54
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.

fix(cli): include the --app module in import failure messages

2 participants