diff --git a/src/flask/app.py b/src/flask/app.py index 236a47a8..22dd3953 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -916,8 +916,8 @@ class Flask(Scaffold): if os.environ.get("FLASK_RUN_FROM_CLI") == "true": if not is_running_from_reloader(): click.secho( - " * Ignoring a call to 'app.run()', the server is" - " already being run with the 'flask run' command.\n" + " * Ignoring a call to 'app.run()' that would block" + " the current 'flask' CLI command.\n" " Only call 'app.run()' in an 'if __name__ ==" ' "__main__"\' guard.', fg="red", diff --git a/src/flask/cli.py b/src/flask/cli.py index 321794b6..af29b2c1 100644 --- a/src/flask/cli.py +++ b/src/flask/cli.py @@ -715,6 +715,11 @@ class FlaskGroup(AppGroup): parent: click.Context | None = None, **extra: t.Any, ) -> click.Context: + # Set a flag to tell app.run to become a no-op. If app.run was + # not in a __name__ == __main__ guard, it would start the server + # when importing, blocking whatever command is being called. + os.environ["FLASK_RUN_FROM_CLI"] = "true" + # Attempt to load .env and .flask env files. The --env-file # option can cause another file to be loaded. if get_load_dotenv(self.load_dotenv): diff --git a/tests/test_cli.py b/tests/test_cli.py index 7d83d865..fdd2212b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -13,7 +13,6 @@ import pytest from _pytest.monkeypatch import notset from click.testing import CliRunner -from flask import _app_ctx_stack from flask import Blueprint from flask import current_app from flask import Flask @@ -322,13 +321,11 @@ def test_app_cli_has_app_context(app, runner): app = click.get_current_context().obj.load_app() # the loaded app should be the same as current_app same_app = current_app._get_current_object() is app - # only one app context should be pushed - stack_size = len(_app_ctx_stack._local.stack) - return same_app, stack_size, value + return same_app, value cli = FlaskGroup(create_app=lambda: app) result = runner.invoke(cli, ["check", "x"], standalone_mode=False) - assert result.return_value == (True, 1, True) + assert result.return_value == (True, True) def test_with_appcontext(runner):